CodeGenRegisters.cpp 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453
  1. //===- CodeGenRegisters.cpp - Register and RegisterClass Info -------------===//
  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 defines structures to encapsulate information gleaned from the
  10. // target register and register class definitions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenRegisters.h"
  14. #include "CodeGenTarget.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/BitVector.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/IntEqClasses.h"
  19. #include "llvm/ADT/SetVector.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/SmallSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/StringExtras.h"
  25. #include "llvm/ADT/StringRef.h"
  26. #include "llvm/ADT/Twine.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/MathExtras.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include "llvm/TableGen/Error.h"
  31. #include "llvm/TableGen/Record.h"
  32. #include <algorithm>
  33. #include <cassert>
  34. #include <cstdint>
  35. #include <iterator>
  36. #include <map>
  37. #include <queue>
  38. #include <set>
  39. #include <string>
  40. #include <tuple>
  41. #include <utility>
  42. #include <vector>
  43. using namespace llvm;
  44. #define DEBUG_TYPE "regalloc-emitter"
  45. //===----------------------------------------------------------------------===//
  46. // CodeGenSubRegIndex
  47. //===----------------------------------------------------------------------===//
  48. CodeGenSubRegIndex::CodeGenSubRegIndex(Record *R, unsigned Enum)
  49. : TheDef(R), EnumValue(Enum), AllSuperRegsCovered(true), Artificial(true) {
  50. Name = std::string(R->getName());
  51. if (R->getValue("Namespace"))
  52. Namespace = std::string(R->getValueAsString("Namespace"));
  53. Size = R->getValueAsInt("Size");
  54. Offset = R->getValueAsInt("Offset");
  55. }
  56. CodeGenSubRegIndex::CodeGenSubRegIndex(StringRef N, StringRef Nspace,
  57. unsigned Enum)
  58. : TheDef(nullptr), Name(std::string(N)), Namespace(std::string(Nspace)),
  59. Size(-1), Offset(-1), EnumValue(Enum), AllSuperRegsCovered(true),
  60. Artificial(true) {}
  61. std::string CodeGenSubRegIndex::getQualifiedName() const {
  62. std::string N = getNamespace();
  63. if (!N.empty())
  64. N += "::";
  65. N += getName();
  66. return N;
  67. }
  68. void CodeGenSubRegIndex::updateComponents(CodeGenRegBank &RegBank) {
  69. if (!TheDef)
  70. return;
  71. std::vector<Record*> Comps = TheDef->getValueAsListOfDefs("ComposedOf");
  72. if (!Comps.empty()) {
  73. if (Comps.size() != 2)
  74. PrintFatalError(TheDef->getLoc(),
  75. "ComposedOf must have exactly two entries");
  76. CodeGenSubRegIndex *A = RegBank.getSubRegIdx(Comps[0]);
  77. CodeGenSubRegIndex *B = RegBank.getSubRegIdx(Comps[1]);
  78. CodeGenSubRegIndex *X = A->addComposite(B, this);
  79. if (X)
  80. PrintFatalError(TheDef->getLoc(), "Ambiguous ComposedOf entries");
  81. }
  82. std::vector<Record*> Parts =
  83. TheDef->getValueAsListOfDefs("CoveringSubRegIndices");
  84. if (!Parts.empty()) {
  85. if (Parts.size() < 2)
  86. PrintFatalError(TheDef->getLoc(),
  87. "CoveredBySubRegs must have two or more entries");
  88. SmallVector<CodeGenSubRegIndex*, 8> IdxParts;
  89. for (Record *Part : Parts)
  90. IdxParts.push_back(RegBank.getSubRegIdx(Part));
  91. setConcatenationOf(IdxParts);
  92. }
  93. }
  94. LaneBitmask CodeGenSubRegIndex::computeLaneMask() const {
  95. // Already computed?
  96. if (LaneMask.any())
  97. return LaneMask;
  98. // Recursion guard, shouldn't be required.
  99. LaneMask = LaneBitmask::getAll();
  100. // The lane mask is simply the union of all sub-indices.
  101. LaneBitmask M;
  102. for (const auto &C : Composed)
  103. M |= C.second->computeLaneMask();
  104. assert(M.any() && "Missing lane mask, sub-register cycle?");
  105. LaneMask = M;
  106. return LaneMask;
  107. }
  108. void CodeGenSubRegIndex::setConcatenationOf(
  109. ArrayRef<CodeGenSubRegIndex*> Parts) {
  110. if (ConcatenationOf.empty())
  111. ConcatenationOf.assign(Parts.begin(), Parts.end());
  112. else
  113. assert(std::equal(Parts.begin(), Parts.end(),
  114. ConcatenationOf.begin()) && "parts consistent");
  115. }
  116. void CodeGenSubRegIndex::computeConcatTransitiveClosure() {
  117. for (SmallVectorImpl<CodeGenSubRegIndex*>::iterator
  118. I = ConcatenationOf.begin(); I != ConcatenationOf.end(); /*empty*/) {
  119. CodeGenSubRegIndex *SubIdx = *I;
  120. SubIdx->computeConcatTransitiveClosure();
  121. #ifndef NDEBUG
  122. for (CodeGenSubRegIndex *SRI : SubIdx->ConcatenationOf)
  123. assert(SRI->ConcatenationOf.empty() && "No transitive closure?");
  124. #endif
  125. if (SubIdx->ConcatenationOf.empty()) {
  126. ++I;
  127. } else {
  128. I = ConcatenationOf.erase(I);
  129. I = ConcatenationOf.insert(I, SubIdx->ConcatenationOf.begin(),
  130. SubIdx->ConcatenationOf.end());
  131. I += SubIdx->ConcatenationOf.size();
  132. }
  133. }
  134. }
  135. //===----------------------------------------------------------------------===//
  136. // CodeGenRegister
  137. //===----------------------------------------------------------------------===//
  138. CodeGenRegister::CodeGenRegister(Record *R, unsigned Enum)
  139. : TheDef(R), EnumValue(Enum),
  140. CostPerUse(R->getValueAsListOfInts("CostPerUse")),
  141. CoveredBySubRegs(R->getValueAsBit("CoveredBySubRegs")),
  142. HasDisjunctSubRegs(false), SubRegsComplete(false),
  143. SuperRegsComplete(false), TopoSig(~0u) {
  144. Artificial = R->getValueAsBit("isArtificial");
  145. }
  146. void CodeGenRegister::buildObjectGraph(CodeGenRegBank &RegBank) {
  147. std::vector<Record*> SRIs = TheDef->getValueAsListOfDefs("SubRegIndices");
  148. std::vector<Record*> SRs = TheDef->getValueAsListOfDefs("SubRegs");
  149. if (SRIs.size() != SRs.size())
  150. PrintFatalError(TheDef->getLoc(),
  151. "SubRegs and SubRegIndices must have the same size");
  152. for (unsigned i = 0, e = SRIs.size(); i != e; ++i) {
  153. ExplicitSubRegIndices.push_back(RegBank.getSubRegIdx(SRIs[i]));
  154. ExplicitSubRegs.push_back(RegBank.getReg(SRs[i]));
  155. }
  156. // Also compute leading super-registers. Each register has a list of
  157. // covered-by-subregs super-registers where it appears as the first explicit
  158. // sub-register.
  159. //
  160. // This is used by computeSecondarySubRegs() to find candidates.
  161. if (CoveredBySubRegs && !ExplicitSubRegs.empty())
  162. ExplicitSubRegs.front()->LeadingSuperRegs.push_back(this);
  163. // Add ad hoc alias links. This is a symmetric relationship between two
  164. // registers, so build a symmetric graph by adding links in both ends.
  165. std::vector<Record*> Aliases = TheDef->getValueAsListOfDefs("Aliases");
  166. for (Record *Alias : Aliases) {
  167. CodeGenRegister *Reg = RegBank.getReg(Alias);
  168. ExplicitAliases.push_back(Reg);
  169. Reg->ExplicitAliases.push_back(this);
  170. }
  171. }
  172. StringRef CodeGenRegister::getName() const {
  173. assert(TheDef && "no def");
  174. return TheDef->getName();
  175. }
  176. namespace {
  177. // Iterate over all register units in a set of registers.
  178. class RegUnitIterator {
  179. CodeGenRegister::Vec::const_iterator RegI, RegE;
  180. CodeGenRegister::RegUnitList::iterator UnitI, UnitE;
  181. public:
  182. RegUnitIterator(const CodeGenRegister::Vec &Regs):
  183. RegI(Regs.begin()), RegE(Regs.end()) {
  184. if (RegI != RegE) {
  185. UnitI = (*RegI)->getRegUnits().begin();
  186. UnitE = (*RegI)->getRegUnits().end();
  187. advance();
  188. }
  189. }
  190. bool isValid() const { return UnitI != UnitE; }
  191. unsigned operator* () const { assert(isValid()); return *UnitI; }
  192. const CodeGenRegister *getReg() const { assert(isValid()); return *RegI; }
  193. /// Preincrement. Move to the next unit.
  194. void operator++() {
  195. assert(isValid() && "Cannot advance beyond the last operand");
  196. ++UnitI;
  197. advance();
  198. }
  199. protected:
  200. void advance() {
  201. while (UnitI == UnitE) {
  202. if (++RegI == RegE)
  203. break;
  204. UnitI = (*RegI)->getRegUnits().begin();
  205. UnitE = (*RegI)->getRegUnits().end();
  206. }
  207. }
  208. };
  209. } // end anonymous namespace
  210. // Return true of this unit appears in RegUnits.
  211. static bool hasRegUnit(CodeGenRegister::RegUnitList &RegUnits, unsigned Unit) {
  212. return RegUnits.test(Unit);
  213. }
  214. // Inherit register units from subregisters.
  215. // Return true if the RegUnits changed.
  216. bool CodeGenRegister::inheritRegUnits(CodeGenRegBank &RegBank) {
  217. bool changed = false;
  218. for (const auto &SubReg : SubRegs) {
  219. CodeGenRegister *SR = SubReg.second;
  220. // Merge the subregister's units into this register's RegUnits.
  221. changed |= (RegUnits |= SR->RegUnits);
  222. }
  223. return changed;
  224. }
  225. const CodeGenRegister::SubRegMap &
  226. CodeGenRegister::computeSubRegs(CodeGenRegBank &RegBank) {
  227. // Only compute this map once.
  228. if (SubRegsComplete)
  229. return SubRegs;
  230. SubRegsComplete = true;
  231. HasDisjunctSubRegs = ExplicitSubRegs.size() > 1;
  232. // First insert the explicit subregs and make sure they are fully indexed.
  233. for (unsigned i = 0, e = ExplicitSubRegs.size(); i != e; ++i) {
  234. CodeGenRegister *SR = ExplicitSubRegs[i];
  235. CodeGenSubRegIndex *Idx = ExplicitSubRegIndices[i];
  236. if (!SR->Artificial)
  237. Idx->Artificial = false;
  238. if (!SubRegs.insert(std::make_pair(Idx, SR)).second)
  239. PrintFatalError(TheDef->getLoc(), "SubRegIndex " + Idx->getName() +
  240. " appears twice in Register " + getName());
  241. // Map explicit sub-registers first, so the names take precedence.
  242. // The inherited sub-registers are mapped below.
  243. SubReg2Idx.insert(std::make_pair(SR, Idx));
  244. }
  245. // Keep track of inherited subregs and how they can be reached.
  246. SmallPtrSet<CodeGenRegister*, 8> Orphans;
  247. // Clone inherited subregs and place duplicate entries in Orphans.
  248. // Here the order is important - earlier subregs take precedence.
  249. for (CodeGenRegister *ESR : ExplicitSubRegs) {
  250. const SubRegMap &Map = ESR->computeSubRegs(RegBank);
  251. HasDisjunctSubRegs |= ESR->HasDisjunctSubRegs;
  252. for (const auto &SR : Map) {
  253. if (!SubRegs.insert(SR).second)
  254. Orphans.insert(SR.second);
  255. }
  256. }
  257. // Expand any composed subreg indices.
  258. // If dsub_2 has ComposedOf = [qsub_1, dsub_0], and this register has a
  259. // qsub_1 subreg, add a dsub_2 subreg. Keep growing Indices and process
  260. // expanded subreg indices recursively.
  261. SmallVector<CodeGenSubRegIndex*, 8> Indices = ExplicitSubRegIndices;
  262. for (unsigned i = 0; i != Indices.size(); ++i) {
  263. CodeGenSubRegIndex *Idx = Indices[i];
  264. const CodeGenSubRegIndex::CompMap &Comps = Idx->getComposites();
  265. CodeGenRegister *SR = SubRegs[Idx];
  266. const SubRegMap &Map = SR->computeSubRegs(RegBank);
  267. // Look at the possible compositions of Idx.
  268. // They may not all be supported by SR.
  269. for (auto Comp : Comps) {
  270. SubRegMap::const_iterator SRI = Map.find(Comp.first);
  271. if (SRI == Map.end())
  272. continue; // Idx + I->first doesn't exist in SR.
  273. // Add I->second as a name for the subreg SRI->second, assuming it is
  274. // orphaned, and the name isn't already used for something else.
  275. if (SubRegs.count(Comp.second) || !Orphans.erase(SRI->second))
  276. continue;
  277. // We found a new name for the orphaned sub-register.
  278. SubRegs.insert(std::make_pair(Comp.second, SRI->second));
  279. Indices.push_back(Comp.second);
  280. }
  281. }
  282. // Now Orphans contains the inherited subregisters without a direct index.
  283. // Create inferred indexes for all missing entries.
  284. // Work backwards in the Indices vector in order to compose subregs bottom-up.
  285. // Consider this subreg sequence:
  286. //
  287. // qsub_1 -> dsub_0 -> ssub_0
  288. //
  289. // The qsub_1 -> dsub_0 composition becomes dsub_2, so the ssub_0 register
  290. // can be reached in two different ways:
  291. //
  292. // qsub_1 -> ssub_0
  293. // dsub_2 -> ssub_0
  294. //
  295. // We pick the latter composition because another register may have [dsub_0,
  296. // dsub_1, dsub_2] subregs without necessarily having a qsub_1 subreg. The
  297. // dsub_2 -> ssub_0 composition can be shared.
  298. while (!Indices.empty() && !Orphans.empty()) {
  299. CodeGenSubRegIndex *Idx = Indices.pop_back_val();
  300. CodeGenRegister *SR = SubRegs[Idx];
  301. const SubRegMap &Map = SR->computeSubRegs(RegBank);
  302. for (const auto &SubReg : Map)
  303. if (Orphans.erase(SubReg.second))
  304. SubRegs[RegBank.getCompositeSubRegIndex(Idx, SubReg.first)] = SubReg.second;
  305. }
  306. // Compute the inverse SubReg -> Idx map.
  307. for (const auto &SubReg : SubRegs) {
  308. if (SubReg.second == this) {
  309. ArrayRef<SMLoc> Loc;
  310. if (TheDef)
  311. Loc = TheDef->getLoc();
  312. PrintFatalError(Loc, "Register " + getName() +
  313. " has itself as a sub-register");
  314. }
  315. // Compute AllSuperRegsCovered.
  316. if (!CoveredBySubRegs)
  317. SubReg.first->AllSuperRegsCovered = false;
  318. // Ensure that every sub-register has a unique name.
  319. DenseMap<const CodeGenRegister*, CodeGenSubRegIndex*>::iterator Ins =
  320. SubReg2Idx.insert(std::make_pair(SubReg.second, SubReg.first)).first;
  321. if (Ins->second == SubReg.first)
  322. continue;
  323. // Trouble: Two different names for SubReg.second.
  324. ArrayRef<SMLoc> Loc;
  325. if (TheDef)
  326. Loc = TheDef->getLoc();
  327. PrintFatalError(Loc, "Sub-register can't have two names: " +
  328. SubReg.second->getName() + " available as " +
  329. SubReg.first->getName() + " and " + Ins->second->getName());
  330. }
  331. // Derive possible names for sub-register concatenations from any explicit
  332. // sub-registers. By doing this before computeSecondarySubRegs(), we ensure
  333. // that getConcatSubRegIndex() won't invent any concatenated indices that the
  334. // user already specified.
  335. for (unsigned i = 0, e = ExplicitSubRegs.size(); i != e; ++i) {
  336. CodeGenRegister *SR = ExplicitSubRegs[i];
  337. if (!SR->CoveredBySubRegs || SR->ExplicitSubRegs.size() <= 1 ||
  338. SR->Artificial)
  339. continue;
  340. // SR is composed of multiple sub-regs. Find their names in this register.
  341. SmallVector<CodeGenSubRegIndex*, 8> Parts;
  342. for (unsigned j = 0, e = SR->ExplicitSubRegs.size(); j != e; ++j) {
  343. CodeGenSubRegIndex &I = *SR->ExplicitSubRegIndices[j];
  344. if (!I.Artificial)
  345. Parts.push_back(getSubRegIndex(SR->ExplicitSubRegs[j]));
  346. }
  347. // Offer this as an existing spelling for the concatenation of Parts.
  348. CodeGenSubRegIndex &Idx = *ExplicitSubRegIndices[i];
  349. Idx.setConcatenationOf(Parts);
  350. }
  351. // Initialize RegUnitList. Because getSubRegs is called recursively, this
  352. // processes the register hierarchy in postorder.
  353. //
  354. // Inherit all sub-register units. It is good enough to look at the explicit
  355. // sub-registers, the other registers won't contribute any more units.
  356. for (unsigned i = 0, e = ExplicitSubRegs.size(); i != e; ++i) {
  357. CodeGenRegister *SR = ExplicitSubRegs[i];
  358. RegUnits |= SR->RegUnits;
  359. }
  360. // Absent any ad hoc aliasing, we create one register unit per leaf register.
  361. // These units correspond to the maximal cliques in the register overlap
  362. // graph which is optimal.
  363. //
  364. // When there is ad hoc aliasing, we simply create one unit per edge in the
  365. // undirected ad hoc aliasing graph. Technically, we could do better by
  366. // identifying maximal cliques in the ad hoc graph, but cliques larger than 2
  367. // are extremely rare anyway (I've never seen one), so we don't bother with
  368. // the added complexity.
  369. for (unsigned i = 0, e = ExplicitAliases.size(); i != e; ++i) {
  370. CodeGenRegister *AR = ExplicitAliases[i];
  371. // Only visit each edge once.
  372. if (AR->SubRegsComplete)
  373. continue;
  374. // Create a RegUnit representing this alias edge, and add it to both
  375. // registers.
  376. unsigned Unit = RegBank.newRegUnit(this, AR);
  377. RegUnits.set(Unit);
  378. AR->RegUnits.set(Unit);
  379. }
  380. // Finally, create units for leaf registers without ad hoc aliases. Note that
  381. // a leaf register with ad hoc aliases doesn't get its own unit - it isn't
  382. // necessary. This means the aliasing leaf registers can share a single unit.
  383. if (RegUnits.empty())
  384. RegUnits.set(RegBank.newRegUnit(this));
  385. // We have now computed the native register units. More may be adopted later
  386. // for balancing purposes.
  387. NativeRegUnits = RegUnits;
  388. return SubRegs;
  389. }
  390. // In a register that is covered by its sub-registers, try to find redundant
  391. // sub-registers. For example:
  392. //
  393. // QQ0 = {Q0, Q1}
  394. // Q0 = {D0, D1}
  395. // Q1 = {D2, D3}
  396. //
  397. // We can infer that D1_D2 is also a sub-register, even if it wasn't named in
  398. // the register definition.
  399. //
  400. // The explicitly specified registers form a tree. This function discovers
  401. // sub-register relationships that would force a DAG.
  402. //
  403. void CodeGenRegister::computeSecondarySubRegs(CodeGenRegBank &RegBank) {
  404. SmallVector<SubRegMap::value_type, 8> NewSubRegs;
  405. std::queue<std::pair<CodeGenSubRegIndex*,CodeGenRegister*>> SubRegQueue;
  406. for (std::pair<CodeGenSubRegIndex*,CodeGenRegister*> P : SubRegs)
  407. SubRegQueue.push(P);
  408. // Look at the leading super-registers of each sub-register. Those are the
  409. // candidates for new sub-registers, assuming they are fully contained in
  410. // this register.
  411. while (!SubRegQueue.empty()) {
  412. CodeGenSubRegIndex *SubRegIdx;
  413. const CodeGenRegister *SubReg;
  414. std::tie(SubRegIdx, SubReg) = SubRegQueue.front();
  415. SubRegQueue.pop();
  416. const CodeGenRegister::SuperRegList &Leads = SubReg->LeadingSuperRegs;
  417. for (unsigned i = 0, e = Leads.size(); i != e; ++i) {
  418. CodeGenRegister *Cand = const_cast<CodeGenRegister*>(Leads[i]);
  419. // Already got this sub-register?
  420. if (Cand == this || getSubRegIndex(Cand))
  421. continue;
  422. // Check if each component of Cand is already a sub-register.
  423. assert(!Cand->ExplicitSubRegs.empty() &&
  424. "Super-register has no sub-registers");
  425. if (Cand->ExplicitSubRegs.size() == 1)
  426. continue;
  427. SmallVector<CodeGenSubRegIndex*, 8> Parts;
  428. // We know that the first component is (SubRegIdx,SubReg). However we
  429. // may still need to split it into smaller subregister parts.
  430. assert(Cand->ExplicitSubRegs[0] == SubReg && "LeadingSuperRegs correct");
  431. assert(getSubRegIndex(SubReg) == SubRegIdx && "LeadingSuperRegs correct");
  432. for (CodeGenRegister *SubReg : Cand->ExplicitSubRegs) {
  433. if (CodeGenSubRegIndex *SubRegIdx = getSubRegIndex(SubReg)) {
  434. if (SubRegIdx->ConcatenationOf.empty())
  435. Parts.push_back(SubRegIdx);
  436. else
  437. append_range(Parts, SubRegIdx->ConcatenationOf);
  438. } else {
  439. // Sub-register doesn't exist.
  440. Parts.clear();
  441. break;
  442. }
  443. }
  444. // There is nothing to do if some Cand sub-register is not part of this
  445. // register.
  446. if (Parts.empty())
  447. continue;
  448. // Each part of Cand is a sub-register of this. Make the full Cand also
  449. // a sub-register with a concatenated sub-register index.
  450. CodeGenSubRegIndex *Concat = RegBank.getConcatSubRegIndex(Parts);
  451. std::pair<CodeGenSubRegIndex*,CodeGenRegister*> NewSubReg =
  452. std::make_pair(Concat, Cand);
  453. if (!SubRegs.insert(NewSubReg).second)
  454. continue;
  455. // We inserted a new subregister.
  456. NewSubRegs.push_back(NewSubReg);
  457. SubRegQueue.push(NewSubReg);
  458. SubReg2Idx.insert(std::make_pair(Cand, Concat));
  459. }
  460. }
  461. // Create sub-register index composition maps for the synthesized indices.
  462. for (unsigned i = 0, e = NewSubRegs.size(); i != e; ++i) {
  463. CodeGenSubRegIndex *NewIdx = NewSubRegs[i].first;
  464. CodeGenRegister *NewSubReg = NewSubRegs[i].second;
  465. for (auto SubReg : NewSubReg->SubRegs) {
  466. CodeGenSubRegIndex *SubIdx = getSubRegIndex(SubReg.second);
  467. if (!SubIdx)
  468. PrintFatalError(TheDef->getLoc(), "No SubRegIndex for " +
  469. SubReg.second->getName() +
  470. " in " + getName());
  471. NewIdx->addComposite(SubReg.first, SubIdx);
  472. }
  473. }
  474. }
  475. void CodeGenRegister::computeSuperRegs(CodeGenRegBank &RegBank) {
  476. // Only visit each register once.
  477. if (SuperRegsComplete)
  478. return;
  479. SuperRegsComplete = true;
  480. // Make sure all sub-registers have been visited first, so the super-reg
  481. // lists will be topologically ordered.
  482. for (auto SubReg : SubRegs)
  483. SubReg.second->computeSuperRegs(RegBank);
  484. // Now add this as a super-register on all sub-registers.
  485. // Also compute the TopoSigId in post-order.
  486. TopoSigId Id;
  487. for (auto SubReg : SubRegs) {
  488. // Topological signature computed from SubIdx, TopoId(SubReg).
  489. // Loops and idempotent indices have TopoSig = ~0u.
  490. Id.push_back(SubReg.first->EnumValue);
  491. Id.push_back(SubReg.second->TopoSig);
  492. // Don't add duplicate entries.
  493. if (!SubReg.second->SuperRegs.empty() &&
  494. SubReg.second->SuperRegs.back() == this)
  495. continue;
  496. SubReg.second->SuperRegs.push_back(this);
  497. }
  498. TopoSig = RegBank.getTopoSig(Id);
  499. }
  500. void
  501. CodeGenRegister::addSubRegsPreOrder(SetVector<const CodeGenRegister*> &OSet,
  502. CodeGenRegBank &RegBank) const {
  503. assert(SubRegsComplete && "Must precompute sub-registers");
  504. for (unsigned i = 0, e = ExplicitSubRegs.size(); i != e; ++i) {
  505. CodeGenRegister *SR = ExplicitSubRegs[i];
  506. if (OSet.insert(SR))
  507. SR->addSubRegsPreOrder(OSet, RegBank);
  508. }
  509. // Add any secondary sub-registers that weren't part of the explicit tree.
  510. for (auto SubReg : SubRegs)
  511. OSet.insert(SubReg.second);
  512. }
  513. // Get the sum of this register's unit weights.
  514. unsigned CodeGenRegister::getWeight(const CodeGenRegBank &RegBank) const {
  515. unsigned Weight = 0;
  516. for (unsigned RegUnit : RegUnits) {
  517. Weight += RegBank.getRegUnit(RegUnit).Weight;
  518. }
  519. return Weight;
  520. }
  521. //===----------------------------------------------------------------------===//
  522. // RegisterTuples
  523. //===----------------------------------------------------------------------===//
  524. // A RegisterTuples def is used to generate pseudo-registers from lists of
  525. // sub-registers. We provide a SetTheory expander class that returns the new
  526. // registers.
  527. namespace {
  528. struct TupleExpander : SetTheory::Expander {
  529. // Reference to SynthDefs in the containing CodeGenRegBank, to keep track of
  530. // the synthesized definitions for their lifetime.
  531. std::vector<std::unique_ptr<Record>> &SynthDefs;
  532. TupleExpander(std::vector<std::unique_ptr<Record>> &SynthDefs)
  533. : SynthDefs(SynthDefs) {}
  534. void expand(SetTheory &ST, Record *Def, SetTheory::RecSet &Elts) override {
  535. std::vector<Record*> Indices = Def->getValueAsListOfDefs("SubRegIndices");
  536. unsigned Dim = Indices.size();
  537. ListInit *SubRegs = Def->getValueAsListInit("SubRegs");
  538. if (Dim != SubRegs->size())
  539. PrintFatalError(Def->getLoc(), "SubRegIndices and SubRegs size mismatch");
  540. if (Dim < 2)
  541. PrintFatalError(Def->getLoc(),
  542. "Tuples must have at least 2 sub-registers");
  543. // Evaluate the sub-register lists to be zipped.
  544. unsigned Length = ~0u;
  545. SmallVector<SetTheory::RecSet, 4> Lists(Dim);
  546. for (unsigned i = 0; i != Dim; ++i) {
  547. ST.evaluate(SubRegs->getElement(i), Lists[i], Def->getLoc());
  548. Length = std::min(Length, unsigned(Lists[i].size()));
  549. }
  550. if (Length == 0)
  551. return;
  552. // Precompute some types.
  553. Record *RegisterCl = Def->getRecords().getClass("Register");
  554. RecTy *RegisterRecTy = RecordRecTy::get(RegisterCl);
  555. std::vector<StringRef> RegNames =
  556. Def->getValueAsListOfStrings("RegAsmNames");
  557. // Zip them up.
  558. for (unsigned n = 0; n != Length; ++n) {
  559. std::string Name;
  560. Record *Proto = Lists[0][n];
  561. std::vector<Init*> Tuple;
  562. for (unsigned i = 0; i != Dim; ++i) {
  563. Record *Reg = Lists[i][n];
  564. if (i) Name += '_';
  565. Name += Reg->getName();
  566. Tuple.push_back(DefInit::get(Reg));
  567. }
  568. // Take the cost list of the first register in the tuple.
  569. ListInit *CostList = Proto->getValueAsListInit("CostPerUse");
  570. SmallVector<Init *, 2> CostPerUse;
  571. CostPerUse.insert(CostPerUse.end(), CostList->begin(), CostList->end());
  572. StringInit *AsmName = StringInit::get("");
  573. if (!RegNames.empty()) {
  574. if (RegNames.size() <= n)
  575. PrintFatalError(Def->getLoc(),
  576. "Register tuple definition missing name for '" +
  577. Name + "'.");
  578. AsmName = StringInit::get(RegNames[n]);
  579. }
  580. // Create a new Record representing the synthesized register. This record
  581. // is only for consumption by CodeGenRegister, it is not added to the
  582. // RecordKeeper.
  583. SynthDefs.emplace_back(
  584. std::make_unique<Record>(Name, Def->getLoc(), Def->getRecords()));
  585. Record *NewReg = SynthDefs.back().get();
  586. Elts.insert(NewReg);
  587. // Copy Proto super-classes.
  588. ArrayRef<std::pair<Record *, SMRange>> Supers = Proto->getSuperClasses();
  589. for (const auto &SuperPair : Supers)
  590. NewReg->addSuperClass(SuperPair.first, SuperPair.second);
  591. // Copy Proto fields.
  592. for (unsigned i = 0, e = Proto->getValues().size(); i != e; ++i) {
  593. RecordVal RV = Proto->getValues()[i];
  594. // Skip existing fields, like NAME.
  595. if (NewReg->getValue(RV.getNameInit()))
  596. continue;
  597. StringRef Field = RV.getName();
  598. // Replace the sub-register list with Tuple.
  599. if (Field == "SubRegs")
  600. RV.setValue(ListInit::get(Tuple, RegisterRecTy));
  601. if (Field == "AsmName")
  602. RV.setValue(AsmName);
  603. // CostPerUse is aggregated from all Tuple members.
  604. if (Field == "CostPerUse")
  605. RV.setValue(ListInit::get(CostPerUse, CostList->getElementType()));
  606. // Composite registers are always covered by sub-registers.
  607. if (Field == "CoveredBySubRegs")
  608. RV.setValue(BitInit::get(true));
  609. // Copy fields from the RegisterTuples def.
  610. if (Field == "SubRegIndices" ||
  611. Field == "CompositeIndices") {
  612. NewReg->addValue(*Def->getValue(Field));
  613. continue;
  614. }
  615. // Some fields get their default uninitialized value.
  616. if (Field == "DwarfNumbers" ||
  617. Field == "DwarfAlias" ||
  618. Field == "Aliases") {
  619. if (const RecordVal *DefRV = RegisterCl->getValue(Field))
  620. NewReg->addValue(*DefRV);
  621. continue;
  622. }
  623. // Everything else is copied from Proto.
  624. NewReg->addValue(RV);
  625. }
  626. }
  627. }
  628. };
  629. } // end anonymous namespace
  630. //===----------------------------------------------------------------------===//
  631. // CodeGenRegisterClass
  632. //===----------------------------------------------------------------------===//
  633. static void sortAndUniqueRegisters(CodeGenRegister::Vec &M) {
  634. llvm::sort(M, deref<std::less<>>());
  635. M.erase(std::unique(M.begin(), M.end(), deref<std::equal_to<>>()), M.end());
  636. }
  637. CodeGenRegisterClass::CodeGenRegisterClass(CodeGenRegBank &RegBank, Record *R)
  638. : TheDef(R), Name(std::string(R->getName())),
  639. TopoSigs(RegBank.getNumTopoSigs()), EnumValue(-1), TSFlags(0) {
  640. GeneratePressureSet = R->getValueAsBit("GeneratePressureSet");
  641. std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
  642. if (TypeList.empty())
  643. PrintFatalError(R->getLoc(), "RegTypes list must not be empty!");
  644. for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
  645. Record *Type = TypeList[i];
  646. if (!Type->isSubClassOf("ValueType"))
  647. PrintFatalError(R->getLoc(),
  648. "RegTypes list member '" + Type->getName() +
  649. "' does not derive from the ValueType class!");
  650. VTs.push_back(getValueTypeByHwMode(Type, RegBank.getHwModes()));
  651. }
  652. // Allocation order 0 is the full set. AltOrders provides others.
  653. const SetTheory::RecVec *Elements = RegBank.getSets().expand(R);
  654. ListInit *AltOrders = R->getValueAsListInit("AltOrders");
  655. Orders.resize(1 + AltOrders->size());
  656. // Default allocation order always contains all registers.
  657. Artificial = true;
  658. for (unsigned i = 0, e = Elements->size(); i != e; ++i) {
  659. Orders[0].push_back((*Elements)[i]);
  660. const CodeGenRegister *Reg = RegBank.getReg((*Elements)[i]);
  661. Members.push_back(Reg);
  662. Artificial &= Reg->Artificial;
  663. TopoSigs.set(Reg->getTopoSig());
  664. }
  665. sortAndUniqueRegisters(Members);
  666. // Alternative allocation orders may be subsets.
  667. SetTheory::RecSet Order;
  668. for (unsigned i = 0, e = AltOrders->size(); i != e; ++i) {
  669. RegBank.getSets().evaluate(AltOrders->getElement(i), Order, R->getLoc());
  670. Orders[1 + i].append(Order.begin(), Order.end());
  671. // Verify that all altorder members are regclass members.
  672. while (!Order.empty()) {
  673. CodeGenRegister *Reg = RegBank.getReg(Order.back());
  674. Order.pop_back();
  675. if (!contains(Reg))
  676. PrintFatalError(R->getLoc(), " AltOrder register " + Reg->getName() +
  677. " is not a class member");
  678. }
  679. }
  680. Namespace = R->getValueAsString("Namespace");
  681. if (const RecordVal *RV = R->getValue("RegInfos"))
  682. if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue()))
  683. RSI = RegSizeInfoByHwMode(DI->getDef(), RegBank.getHwModes());
  684. unsigned Size = R->getValueAsInt("Size");
  685. assert((RSI.hasDefault() || Size != 0 || VTs[0].isSimple()) &&
  686. "Impossible to determine register size");
  687. if (!RSI.hasDefault()) {
  688. RegSizeInfo RI;
  689. RI.RegSize = RI.SpillSize = Size ? Size
  690. : VTs[0].getSimple().getSizeInBits();
  691. RI.SpillAlignment = R->getValueAsInt("Alignment");
  692. RSI.insertRegSizeForMode(DefaultMode, RI);
  693. }
  694. CopyCost = R->getValueAsInt("CopyCost");
  695. Allocatable = R->getValueAsBit("isAllocatable");
  696. AltOrderSelect = R->getValueAsString("AltOrderSelect");
  697. int AllocationPriority = R->getValueAsInt("AllocationPriority");
  698. if (AllocationPriority < 0 || AllocationPriority > 63)
  699. PrintFatalError(R->getLoc(), "AllocationPriority out of range [0,63]");
  700. this->AllocationPriority = AllocationPriority;
  701. BitsInit *TSF = R->getValueAsBitsInit("TSFlags");
  702. for (unsigned I = 0, E = TSF->getNumBits(); I != E; ++I) {
  703. BitInit *Bit = cast<BitInit>(TSF->getBit(I));
  704. TSFlags |= uint8_t(Bit->getValue()) << I;
  705. }
  706. }
  707. // Create an inferred register class that was missing from the .td files.
  708. // Most properties will be inherited from the closest super-class after the
  709. // class structure has been computed.
  710. CodeGenRegisterClass::CodeGenRegisterClass(CodeGenRegBank &RegBank,
  711. StringRef Name, Key Props)
  712. : Members(*Props.Members), TheDef(nullptr), Name(std::string(Name)),
  713. TopoSigs(RegBank.getNumTopoSigs()), EnumValue(-1), RSI(Props.RSI),
  714. CopyCost(0), Allocatable(true), AllocationPriority(0), TSFlags(0) {
  715. Artificial = true;
  716. GeneratePressureSet = false;
  717. for (const auto R : Members) {
  718. TopoSigs.set(R->getTopoSig());
  719. Artificial &= R->Artificial;
  720. }
  721. }
  722. // Compute inherited propertied for a synthesized register class.
  723. void CodeGenRegisterClass::inheritProperties(CodeGenRegBank &RegBank) {
  724. assert(!getDef() && "Only synthesized classes can inherit properties");
  725. assert(!SuperClasses.empty() && "Synthesized class without super class");
  726. // The last super-class is the smallest one.
  727. CodeGenRegisterClass &Super = *SuperClasses.back();
  728. // Most properties are copied directly.
  729. // Exceptions are members, size, and alignment
  730. Namespace = Super.Namespace;
  731. VTs = Super.VTs;
  732. CopyCost = Super.CopyCost;
  733. // Check for allocatable superclasses.
  734. Allocatable = any_of(SuperClasses, [&](const CodeGenRegisterClass *S) {
  735. return S->Allocatable;
  736. });
  737. AltOrderSelect = Super.AltOrderSelect;
  738. AllocationPriority = Super.AllocationPriority;
  739. TSFlags = Super.TSFlags;
  740. GeneratePressureSet |= Super.GeneratePressureSet;
  741. // Copy all allocation orders, filter out foreign registers from the larger
  742. // super-class.
  743. Orders.resize(Super.Orders.size());
  744. for (unsigned i = 0, ie = Super.Orders.size(); i != ie; ++i)
  745. for (unsigned j = 0, je = Super.Orders[i].size(); j != je; ++j)
  746. if (contains(RegBank.getReg(Super.Orders[i][j])))
  747. Orders[i].push_back(Super.Orders[i][j]);
  748. }
  749. bool CodeGenRegisterClass::contains(const CodeGenRegister *Reg) const {
  750. return std::binary_search(Members.begin(), Members.end(), Reg,
  751. deref<std::less<>>());
  752. }
  753. unsigned CodeGenRegisterClass::getWeight(const CodeGenRegBank& RegBank) const {
  754. if (TheDef && !TheDef->isValueUnset("Weight"))
  755. return TheDef->getValueAsInt("Weight");
  756. if (Members.empty() || Artificial)
  757. return 0;
  758. return (*Members.begin())->getWeight(RegBank);
  759. }
  760. namespace llvm {
  761. raw_ostream &operator<<(raw_ostream &OS, const CodeGenRegisterClass::Key &K) {
  762. OS << "{ " << K.RSI;
  763. for (const auto R : *K.Members)
  764. OS << ", " << R->getName();
  765. return OS << " }";
  766. }
  767. } // end namespace llvm
  768. // This is a simple lexicographical order that can be used to search for sets.
  769. // It is not the same as the topological order provided by TopoOrderRC.
  770. bool CodeGenRegisterClass::Key::
  771. operator<(const CodeGenRegisterClass::Key &B) const {
  772. assert(Members && B.Members);
  773. return std::tie(*Members, RSI) < std::tie(*B.Members, B.RSI);
  774. }
  775. // Returns true if RC is a strict subclass.
  776. // RC is a sub-class of this class if it is a valid replacement for any
  777. // instruction operand where a register of this classis required. It must
  778. // satisfy these conditions:
  779. //
  780. // 1. All RC registers are also in this.
  781. // 2. The RC spill size must not be smaller than our spill size.
  782. // 3. RC spill alignment must be compatible with ours.
  783. //
  784. static bool testSubClass(const CodeGenRegisterClass *A,
  785. const CodeGenRegisterClass *B) {
  786. return A->RSI.isSubClassOf(B->RSI) &&
  787. std::includes(A->getMembers().begin(), A->getMembers().end(),
  788. B->getMembers().begin(), B->getMembers().end(),
  789. deref<std::less<>>());
  790. }
  791. /// Sorting predicate for register classes. This provides a topological
  792. /// ordering that arranges all register classes before their sub-classes.
  793. ///
  794. /// Register classes with the same registers, spill size, and alignment form a
  795. /// clique. They will be ordered alphabetically.
  796. ///
  797. static bool TopoOrderRC(const CodeGenRegisterClass &PA,
  798. const CodeGenRegisterClass &PB) {
  799. auto *A = &PA;
  800. auto *B = &PB;
  801. if (A == B)
  802. return false;
  803. if (A->RSI < B->RSI)
  804. return true;
  805. if (A->RSI != B->RSI)
  806. return false;
  807. // Order by descending set size. Note that the classes' allocation order may
  808. // not have been computed yet. The Members set is always vaild.
  809. if (A->getMembers().size() > B->getMembers().size())
  810. return true;
  811. if (A->getMembers().size() < B->getMembers().size())
  812. return false;
  813. // Finally order by name as a tie breaker.
  814. return StringRef(A->getName()) < B->getName();
  815. }
  816. std::string CodeGenRegisterClass::getQualifiedName() const {
  817. if (Namespace.empty())
  818. return getName();
  819. else
  820. return (Namespace + "::" + getName()).str();
  821. }
  822. // Compute sub-classes of all register classes.
  823. // Assume the classes are ordered topologically.
  824. void CodeGenRegisterClass::computeSubClasses(CodeGenRegBank &RegBank) {
  825. auto &RegClasses = RegBank.getRegClasses();
  826. // Visit backwards so sub-classes are seen first.
  827. for (auto I = RegClasses.rbegin(), E = RegClasses.rend(); I != E; ++I) {
  828. CodeGenRegisterClass &RC = *I;
  829. RC.SubClasses.resize(RegClasses.size());
  830. RC.SubClasses.set(RC.EnumValue);
  831. if (RC.Artificial)
  832. continue;
  833. // Normally, all subclasses have IDs >= rci, unless RC is part of a clique.
  834. for (auto I2 = I.base(), E2 = RegClasses.end(); I2 != E2; ++I2) {
  835. CodeGenRegisterClass &SubRC = *I2;
  836. if (RC.SubClasses.test(SubRC.EnumValue))
  837. continue;
  838. if (!testSubClass(&RC, &SubRC))
  839. continue;
  840. // SubRC is a sub-class. Grap all its sub-classes so we won't have to
  841. // check them again.
  842. RC.SubClasses |= SubRC.SubClasses;
  843. }
  844. // Sweep up missed clique members. They will be immediately preceding RC.
  845. for (auto I2 = std::next(I); I2 != E && testSubClass(&RC, &*I2); ++I2)
  846. RC.SubClasses.set(I2->EnumValue);
  847. }
  848. // Compute the SuperClasses lists from the SubClasses vectors.
  849. for (auto &RC : RegClasses) {
  850. const BitVector &SC = RC.getSubClasses();
  851. auto I = RegClasses.begin();
  852. for (int s = 0, next_s = SC.find_first(); next_s != -1;
  853. next_s = SC.find_next(s)) {
  854. std::advance(I, next_s - s);
  855. s = next_s;
  856. if (&*I == &RC)
  857. continue;
  858. I->SuperClasses.push_back(&RC);
  859. }
  860. }
  861. // With the class hierarchy in place, let synthesized register classes inherit
  862. // properties from their closest super-class. The iteration order here can
  863. // propagate properties down multiple levels.
  864. for (auto &RC : RegClasses)
  865. if (!RC.getDef())
  866. RC.inheritProperties(RegBank);
  867. }
  868. Optional<std::pair<CodeGenRegisterClass *, CodeGenRegisterClass *>>
  869. CodeGenRegisterClass::getMatchingSubClassWithSubRegs(
  870. CodeGenRegBank &RegBank, const CodeGenSubRegIndex *SubIdx) const {
  871. auto SizeOrder = [this](const CodeGenRegisterClass *A,
  872. const CodeGenRegisterClass *B) {
  873. // If there are multiple, identical register classes, prefer the original
  874. // register class.
  875. if (A == B)
  876. return false;
  877. if (A->getMembers().size() == B->getMembers().size())
  878. return A == this;
  879. return A->getMembers().size() > B->getMembers().size();
  880. };
  881. auto &RegClasses = RegBank.getRegClasses();
  882. // Find all the subclasses of this one that fully support the sub-register
  883. // index and order them by size. BiggestSuperRC should always be first.
  884. CodeGenRegisterClass *BiggestSuperRegRC = getSubClassWithSubReg(SubIdx);
  885. if (!BiggestSuperRegRC)
  886. return None;
  887. BitVector SuperRegRCsBV = BiggestSuperRegRC->getSubClasses();
  888. std::vector<CodeGenRegisterClass *> SuperRegRCs;
  889. for (auto &RC : RegClasses)
  890. if (SuperRegRCsBV[RC.EnumValue])
  891. SuperRegRCs.emplace_back(&RC);
  892. llvm::stable_sort(SuperRegRCs, SizeOrder);
  893. assert(SuperRegRCs.front() == BiggestSuperRegRC &&
  894. "Biggest class wasn't first");
  895. // Find all the subreg classes and order them by size too.
  896. std::vector<std::pair<CodeGenRegisterClass *, BitVector>> SuperRegClasses;
  897. for (auto &RC: RegClasses) {
  898. BitVector SuperRegClassesBV(RegClasses.size());
  899. RC.getSuperRegClasses(SubIdx, SuperRegClassesBV);
  900. if (SuperRegClassesBV.any())
  901. SuperRegClasses.push_back(std::make_pair(&RC, SuperRegClassesBV));
  902. }
  903. llvm::sort(SuperRegClasses,
  904. [&](const std::pair<CodeGenRegisterClass *, BitVector> &A,
  905. const std::pair<CodeGenRegisterClass *, BitVector> &B) {
  906. return SizeOrder(A.first, B.first);
  907. });
  908. // Find the biggest subclass and subreg class such that R:subidx is in the
  909. // subreg class for all R in subclass.
  910. //
  911. // For example:
  912. // All registers in X86's GR64 have a sub_32bit subregister but no class
  913. // exists that contains all the 32-bit subregisters because GR64 contains RIP
  914. // but GR32 does not contain EIP. Instead, we constrain SuperRegRC to
  915. // GR32_with_sub_8bit (which is identical to GR32_with_sub_32bit) and then,
  916. // having excluded RIP, we are able to find a SubRegRC (GR32).
  917. CodeGenRegisterClass *ChosenSuperRegClass = nullptr;
  918. CodeGenRegisterClass *SubRegRC = nullptr;
  919. for (auto *SuperRegRC : SuperRegRCs) {
  920. for (const auto &SuperRegClassPair : SuperRegClasses) {
  921. const BitVector &SuperRegClassBV = SuperRegClassPair.second;
  922. if (SuperRegClassBV[SuperRegRC->EnumValue]) {
  923. SubRegRC = SuperRegClassPair.first;
  924. ChosenSuperRegClass = SuperRegRC;
  925. // If SubRegRC is bigger than SuperRegRC then there are members of
  926. // SubRegRC that don't have super registers via SubIdx. Keep looking to
  927. // find a better fit and fall back on this one if there isn't one.
  928. //
  929. // This is intended to prevent X86 from making odd choices such as
  930. // picking LOW32_ADDR_ACCESS_RBP instead of GR32 in the example above.
  931. // LOW32_ADDR_ACCESS_RBP is a valid choice but contains registers that
  932. // aren't subregisters of SuperRegRC whereas GR32 has a direct 1:1
  933. // mapping.
  934. if (SuperRegRC->getMembers().size() >= SubRegRC->getMembers().size())
  935. return std::make_pair(ChosenSuperRegClass, SubRegRC);
  936. }
  937. }
  938. // If we found a fit but it wasn't quite ideal because SubRegRC had excess
  939. // registers, then we're done.
  940. if (ChosenSuperRegClass)
  941. return std::make_pair(ChosenSuperRegClass, SubRegRC);
  942. }
  943. return None;
  944. }
  945. void CodeGenRegisterClass::getSuperRegClasses(const CodeGenSubRegIndex *SubIdx,
  946. BitVector &Out) const {
  947. auto FindI = SuperRegClasses.find(SubIdx);
  948. if (FindI == SuperRegClasses.end())
  949. return;
  950. for (CodeGenRegisterClass *RC : FindI->second)
  951. Out.set(RC->EnumValue);
  952. }
  953. // Populate a unique sorted list of units from a register set.
  954. void CodeGenRegisterClass::buildRegUnitSet(const CodeGenRegBank &RegBank,
  955. std::vector<unsigned> &RegUnits) const {
  956. std::vector<unsigned> TmpUnits;
  957. for (RegUnitIterator UnitI(Members); UnitI.isValid(); ++UnitI) {
  958. const RegUnit &RU = RegBank.getRegUnit(*UnitI);
  959. if (!RU.Artificial)
  960. TmpUnits.push_back(*UnitI);
  961. }
  962. llvm::sort(TmpUnits);
  963. std::unique_copy(TmpUnits.begin(), TmpUnits.end(),
  964. std::back_inserter(RegUnits));
  965. }
  966. //===----------------------------------------------------------------------===//
  967. // CodeGenRegBank
  968. //===----------------------------------------------------------------------===//
  969. CodeGenRegBank::CodeGenRegBank(RecordKeeper &Records,
  970. const CodeGenHwModes &Modes) : CGH(Modes) {
  971. // Configure register Sets to understand register classes and tuples.
  972. Sets.addFieldExpander("RegisterClass", "MemberList");
  973. Sets.addFieldExpander("CalleeSavedRegs", "SaveList");
  974. Sets.addExpander("RegisterTuples",
  975. std::make_unique<TupleExpander>(SynthDefs));
  976. // Read in the user-defined (named) sub-register indices.
  977. // More indices will be synthesized later.
  978. std::vector<Record*> SRIs = Records.getAllDerivedDefinitions("SubRegIndex");
  979. llvm::sort(SRIs, LessRecord());
  980. for (unsigned i = 0, e = SRIs.size(); i != e; ++i)
  981. getSubRegIdx(SRIs[i]);
  982. // Build composite maps from ComposedOf fields.
  983. for (auto &Idx : SubRegIndices)
  984. Idx.updateComponents(*this);
  985. // Read in the register definitions.
  986. std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
  987. llvm::sort(Regs, LessRecordRegister());
  988. // Assign the enumeration values.
  989. for (unsigned i = 0, e = Regs.size(); i != e; ++i)
  990. getReg(Regs[i]);
  991. // Expand tuples and number the new registers.
  992. std::vector<Record*> Tups =
  993. Records.getAllDerivedDefinitions("RegisterTuples");
  994. for (Record *R : Tups) {
  995. std::vector<Record *> TupRegs = *Sets.expand(R);
  996. llvm::sort(TupRegs, LessRecordRegister());
  997. for (Record *RC : TupRegs)
  998. getReg(RC);
  999. }
  1000. // Now all the registers are known. Build the object graph of explicit
  1001. // register-register references.
  1002. for (auto &Reg : Registers)
  1003. Reg.buildObjectGraph(*this);
  1004. // Compute register name map.
  1005. for (auto &Reg : Registers)
  1006. // FIXME: This could just be RegistersByName[name] = register, except that
  1007. // causes some failures in MIPS - perhaps they have duplicate register name
  1008. // entries? (or maybe there's a reason for it - I don't know much about this
  1009. // code, just drive-by refactoring)
  1010. RegistersByName.insert(
  1011. std::make_pair(Reg.TheDef->getValueAsString("AsmName"), &Reg));
  1012. // Precompute all sub-register maps.
  1013. // This will create Composite entries for all inferred sub-register indices.
  1014. for (auto &Reg : Registers)
  1015. Reg.computeSubRegs(*this);
  1016. // Compute transitive closure of subregister index ConcatenationOf vectors
  1017. // and initialize ConcatIdx map.
  1018. for (CodeGenSubRegIndex &SRI : SubRegIndices) {
  1019. SRI.computeConcatTransitiveClosure();
  1020. if (!SRI.ConcatenationOf.empty())
  1021. ConcatIdx.insert(std::make_pair(
  1022. SmallVector<CodeGenSubRegIndex*,8>(SRI.ConcatenationOf.begin(),
  1023. SRI.ConcatenationOf.end()), &SRI));
  1024. }
  1025. // Infer even more sub-registers by combining leading super-registers.
  1026. for (auto &Reg : Registers)
  1027. if (Reg.CoveredBySubRegs)
  1028. Reg.computeSecondarySubRegs(*this);
  1029. // After the sub-register graph is complete, compute the topologically
  1030. // ordered SuperRegs list.
  1031. for (auto &Reg : Registers)
  1032. Reg.computeSuperRegs(*this);
  1033. // For each pair of Reg:SR, if both are non-artificial, mark the
  1034. // corresponding sub-register index as non-artificial.
  1035. for (auto &Reg : Registers) {
  1036. if (Reg.Artificial)
  1037. continue;
  1038. for (auto P : Reg.getSubRegs()) {
  1039. const CodeGenRegister *SR = P.second;
  1040. if (!SR->Artificial)
  1041. P.first->Artificial = false;
  1042. }
  1043. }
  1044. // Native register units are associated with a leaf register. They've all been
  1045. // discovered now.
  1046. NumNativeRegUnits = RegUnits.size();
  1047. // Read in register class definitions.
  1048. std::vector<Record*> RCs = Records.getAllDerivedDefinitions("RegisterClass");
  1049. if (RCs.empty())
  1050. PrintFatalError("No 'RegisterClass' subclasses defined!");
  1051. // Allocate user-defined register classes.
  1052. for (auto *R : RCs) {
  1053. RegClasses.emplace_back(*this, R);
  1054. CodeGenRegisterClass &RC = RegClasses.back();
  1055. if (!RC.Artificial)
  1056. addToMaps(&RC);
  1057. }
  1058. // Infer missing classes to create a full algebra.
  1059. computeInferredRegisterClasses();
  1060. // Order register classes topologically and assign enum values.
  1061. RegClasses.sort(TopoOrderRC);
  1062. unsigned i = 0;
  1063. for (auto &RC : RegClasses)
  1064. RC.EnumValue = i++;
  1065. CodeGenRegisterClass::computeSubClasses(*this);
  1066. }
  1067. // Create a synthetic CodeGenSubRegIndex without a corresponding Record.
  1068. CodeGenSubRegIndex*
  1069. CodeGenRegBank::createSubRegIndex(StringRef Name, StringRef Namespace) {
  1070. SubRegIndices.emplace_back(Name, Namespace, SubRegIndices.size() + 1);
  1071. return &SubRegIndices.back();
  1072. }
  1073. CodeGenSubRegIndex *CodeGenRegBank::getSubRegIdx(Record *Def) {
  1074. CodeGenSubRegIndex *&Idx = Def2SubRegIdx[Def];
  1075. if (Idx)
  1076. return Idx;
  1077. SubRegIndices.emplace_back(Def, SubRegIndices.size() + 1);
  1078. Idx = &SubRegIndices.back();
  1079. return Idx;
  1080. }
  1081. const CodeGenSubRegIndex *
  1082. CodeGenRegBank::findSubRegIdx(const Record* Def) const {
  1083. return Def2SubRegIdx.lookup(Def);
  1084. }
  1085. CodeGenRegister *CodeGenRegBank::getReg(Record *Def) {
  1086. CodeGenRegister *&Reg = Def2Reg[Def];
  1087. if (Reg)
  1088. return Reg;
  1089. Registers.emplace_back(Def, Registers.size() + 1);
  1090. Reg = &Registers.back();
  1091. return Reg;
  1092. }
  1093. void CodeGenRegBank::addToMaps(CodeGenRegisterClass *RC) {
  1094. if (Record *Def = RC->getDef())
  1095. Def2RC.insert(std::make_pair(Def, RC));
  1096. // Duplicate classes are rejected by insert().
  1097. // That's OK, we only care about the properties handled by CGRC::Key.
  1098. CodeGenRegisterClass::Key K(*RC);
  1099. Key2RC.insert(std::make_pair(K, RC));
  1100. }
  1101. // Create a synthetic sub-class if it is missing.
  1102. CodeGenRegisterClass*
  1103. CodeGenRegBank::getOrCreateSubClass(const CodeGenRegisterClass *RC,
  1104. const CodeGenRegister::Vec *Members,
  1105. StringRef Name) {
  1106. // Synthetic sub-class has the same size and alignment as RC.
  1107. CodeGenRegisterClass::Key K(Members, RC->RSI);
  1108. RCKeyMap::const_iterator FoundI = Key2RC.find(K);
  1109. if (FoundI != Key2RC.end())
  1110. return FoundI->second;
  1111. // Sub-class doesn't exist, create a new one.
  1112. RegClasses.emplace_back(*this, Name, K);
  1113. addToMaps(&RegClasses.back());
  1114. return &RegClasses.back();
  1115. }
  1116. CodeGenRegisterClass *CodeGenRegBank::getRegClass(const Record *Def) const {
  1117. if (CodeGenRegisterClass *RC = Def2RC.lookup(Def))
  1118. return RC;
  1119. PrintFatalError(Def->getLoc(), "Not a known RegisterClass!");
  1120. }
  1121. CodeGenSubRegIndex*
  1122. CodeGenRegBank::getCompositeSubRegIndex(CodeGenSubRegIndex *A,
  1123. CodeGenSubRegIndex *B) {
  1124. // Look for an existing entry.
  1125. CodeGenSubRegIndex *Comp = A->compose(B);
  1126. if (Comp)
  1127. return Comp;
  1128. // None exists, synthesize one.
  1129. std::string Name = A->getName() + "_then_" + B->getName();
  1130. Comp = createSubRegIndex(Name, A->getNamespace());
  1131. A->addComposite(B, Comp);
  1132. return Comp;
  1133. }
  1134. CodeGenSubRegIndex *CodeGenRegBank::
  1135. getConcatSubRegIndex(const SmallVector<CodeGenSubRegIndex *, 8> &Parts) {
  1136. assert(Parts.size() > 1 && "Need two parts to concatenate");
  1137. #ifndef NDEBUG
  1138. for (CodeGenSubRegIndex *Idx : Parts) {
  1139. assert(Idx->ConcatenationOf.empty() && "No transitive closure?");
  1140. }
  1141. #endif
  1142. // Look for an existing entry.
  1143. CodeGenSubRegIndex *&Idx = ConcatIdx[Parts];
  1144. if (Idx)
  1145. return Idx;
  1146. // None exists, synthesize one.
  1147. std::string Name = Parts.front()->getName();
  1148. // Determine whether all parts are contiguous.
  1149. bool isContinuous = true;
  1150. unsigned Size = Parts.front()->Size;
  1151. unsigned LastOffset = Parts.front()->Offset;
  1152. unsigned LastSize = Parts.front()->Size;
  1153. for (unsigned i = 1, e = Parts.size(); i != e; ++i) {
  1154. Name += '_';
  1155. Name += Parts[i]->getName();
  1156. Size += Parts[i]->Size;
  1157. if (Parts[i]->Offset != (LastOffset + LastSize))
  1158. isContinuous = false;
  1159. LastOffset = Parts[i]->Offset;
  1160. LastSize = Parts[i]->Size;
  1161. }
  1162. Idx = createSubRegIndex(Name, Parts.front()->getNamespace());
  1163. Idx->Size = Size;
  1164. Idx->Offset = isContinuous ? Parts.front()->Offset : -1;
  1165. Idx->ConcatenationOf.assign(Parts.begin(), Parts.end());
  1166. return Idx;
  1167. }
  1168. void CodeGenRegBank::computeComposites() {
  1169. using RegMap = std::map<const CodeGenRegister*, const CodeGenRegister*>;
  1170. // Subreg -> { Reg->Reg }, where the right-hand side is the mapping from
  1171. // register to (sub)register associated with the action of the left-hand
  1172. // side subregister.
  1173. std::map<const CodeGenSubRegIndex*, RegMap> SubRegAction;
  1174. for (const CodeGenRegister &R : Registers) {
  1175. const CodeGenRegister::SubRegMap &SM = R.getSubRegs();
  1176. for (std::pair<const CodeGenSubRegIndex*, const CodeGenRegister*> P : SM)
  1177. SubRegAction[P.first].insert({&R, P.second});
  1178. }
  1179. // Calculate the composition of two subregisters as compositions of their
  1180. // associated actions.
  1181. auto compose = [&SubRegAction] (const CodeGenSubRegIndex *Sub1,
  1182. const CodeGenSubRegIndex *Sub2) {
  1183. RegMap C;
  1184. const RegMap &Img1 = SubRegAction.at(Sub1);
  1185. const RegMap &Img2 = SubRegAction.at(Sub2);
  1186. for (std::pair<const CodeGenRegister*, const CodeGenRegister*> P : Img1) {
  1187. auto F = Img2.find(P.second);
  1188. if (F != Img2.end())
  1189. C.insert({P.first, F->second});
  1190. }
  1191. return C;
  1192. };
  1193. // Check if the two maps agree on the intersection of their domains.
  1194. auto agree = [] (const RegMap &Map1, const RegMap &Map2) {
  1195. // Technically speaking, an empty map agrees with any other map, but
  1196. // this could flag false positives. We're interested in non-vacuous
  1197. // agreements.
  1198. if (Map1.empty() || Map2.empty())
  1199. return false;
  1200. for (std::pair<const CodeGenRegister*, const CodeGenRegister*> P : Map1) {
  1201. auto F = Map2.find(P.first);
  1202. if (F == Map2.end() || P.second != F->second)
  1203. return false;
  1204. }
  1205. return true;
  1206. };
  1207. using CompositePair = std::pair<const CodeGenSubRegIndex*,
  1208. const CodeGenSubRegIndex*>;
  1209. SmallSet<CompositePair,4> UserDefined;
  1210. for (const CodeGenSubRegIndex &Idx : SubRegIndices)
  1211. for (auto P : Idx.getComposites())
  1212. UserDefined.insert(std::make_pair(&Idx, P.first));
  1213. // Keep track of TopoSigs visited. We only need to visit each TopoSig once,
  1214. // and many registers will share TopoSigs on regular architectures.
  1215. BitVector TopoSigs(getNumTopoSigs());
  1216. for (const auto &Reg1 : Registers) {
  1217. // Skip identical subreg structures already processed.
  1218. if (TopoSigs.test(Reg1.getTopoSig()))
  1219. continue;
  1220. TopoSigs.set(Reg1.getTopoSig());
  1221. const CodeGenRegister::SubRegMap &SRM1 = Reg1.getSubRegs();
  1222. for (auto I1 : SRM1) {
  1223. CodeGenSubRegIndex *Idx1 = I1.first;
  1224. CodeGenRegister *Reg2 = I1.second;
  1225. // Ignore identity compositions.
  1226. if (&Reg1 == Reg2)
  1227. continue;
  1228. const CodeGenRegister::SubRegMap &SRM2 = Reg2->getSubRegs();
  1229. // Try composing Idx1 with another SubRegIndex.
  1230. for (auto I2 : SRM2) {
  1231. CodeGenSubRegIndex *Idx2 = I2.first;
  1232. CodeGenRegister *Reg3 = I2.second;
  1233. // Ignore identity compositions.
  1234. if (Reg2 == Reg3)
  1235. continue;
  1236. // OK Reg1:IdxPair == Reg3. Find the index with Reg:Idx == Reg3.
  1237. CodeGenSubRegIndex *Idx3 = Reg1.getSubRegIndex(Reg3);
  1238. assert(Idx3 && "Sub-register doesn't have an index");
  1239. // Conflicting composition? Emit a warning but allow it.
  1240. if (CodeGenSubRegIndex *Prev = Idx1->addComposite(Idx2, Idx3)) {
  1241. // If the composition was not user-defined, always emit a warning.
  1242. if (!UserDefined.count({Idx1, Idx2}) ||
  1243. agree(compose(Idx1, Idx2), SubRegAction.at(Idx3)))
  1244. PrintWarning(Twine("SubRegIndex ") + Idx1->getQualifiedName() +
  1245. " and " + Idx2->getQualifiedName() +
  1246. " compose ambiguously as " + Prev->getQualifiedName() +
  1247. " or " + Idx3->getQualifiedName());
  1248. }
  1249. }
  1250. }
  1251. }
  1252. }
  1253. // Compute lane masks. This is similar to register units, but at the
  1254. // sub-register index level. Each bit in the lane mask is like a register unit
  1255. // class, and two lane masks will have a bit in common if two sub-register
  1256. // indices overlap in some register.
  1257. //
  1258. // Conservatively share a lane mask bit if two sub-register indices overlap in
  1259. // some registers, but not in others. That shouldn't happen a lot.
  1260. void CodeGenRegBank::computeSubRegLaneMasks() {
  1261. // First assign individual bits to all the leaf indices.
  1262. unsigned Bit = 0;
  1263. // Determine mask of lanes that cover their registers.
  1264. CoveringLanes = LaneBitmask::getAll();
  1265. for (auto &Idx : SubRegIndices) {
  1266. if (Idx.getComposites().empty()) {
  1267. if (Bit > LaneBitmask::BitWidth) {
  1268. PrintFatalError(
  1269. Twine("Ran out of lanemask bits to represent subregister ")
  1270. + Idx.getName());
  1271. }
  1272. Idx.LaneMask = LaneBitmask::getLane(Bit);
  1273. ++Bit;
  1274. } else {
  1275. Idx.LaneMask = LaneBitmask::getNone();
  1276. }
  1277. }
  1278. // Compute transformation sequences for composeSubRegIndexLaneMask. The idea
  1279. // here is that for each possible target subregister we look at the leafs
  1280. // in the subregister graph that compose for this target and create
  1281. // transformation sequences for the lanemasks. Each step in the sequence
  1282. // consists of a bitmask and a bitrotate operation. As the rotation amounts
  1283. // are usually the same for many subregisters we can easily combine the steps
  1284. // by combining the masks.
  1285. for (const auto &Idx : SubRegIndices) {
  1286. const auto &Composites = Idx.getComposites();
  1287. auto &LaneTransforms = Idx.CompositionLaneMaskTransform;
  1288. if (Composites.empty()) {
  1289. // Moving from a class with no subregisters we just had a single lane:
  1290. // The subregister must be a leaf subregister and only occupies 1 bit.
  1291. // Move the bit from the class without subregisters into that position.
  1292. unsigned DstBit = Idx.LaneMask.getHighestLane();
  1293. assert(Idx.LaneMask == LaneBitmask::getLane(DstBit) &&
  1294. "Must be a leaf subregister");
  1295. MaskRolPair MaskRol = { LaneBitmask::getLane(0), (uint8_t)DstBit };
  1296. LaneTransforms.push_back(MaskRol);
  1297. } else {
  1298. // Go through all leaf subregisters and find the ones that compose with
  1299. // Idx. These make out all possible valid bits in the lane mask we want to
  1300. // transform. Looking only at the leafs ensure that only a single bit in
  1301. // the mask is set.
  1302. unsigned NextBit = 0;
  1303. for (auto &Idx2 : SubRegIndices) {
  1304. // Skip non-leaf subregisters.
  1305. if (!Idx2.getComposites().empty())
  1306. continue;
  1307. // Replicate the behaviour from the lane mask generation loop above.
  1308. unsigned SrcBit = NextBit;
  1309. LaneBitmask SrcMask = LaneBitmask::getLane(SrcBit);
  1310. if (NextBit < LaneBitmask::BitWidth-1)
  1311. ++NextBit;
  1312. assert(Idx2.LaneMask == SrcMask);
  1313. // Get the composed subregister if there is any.
  1314. auto C = Composites.find(&Idx2);
  1315. if (C == Composites.end())
  1316. continue;
  1317. const CodeGenSubRegIndex *Composite = C->second;
  1318. // The Composed subreg should be a leaf subreg too
  1319. assert(Composite->getComposites().empty());
  1320. // Create Mask+Rotate operation and merge with existing ops if possible.
  1321. unsigned DstBit = Composite->LaneMask.getHighestLane();
  1322. int Shift = DstBit - SrcBit;
  1323. uint8_t RotateLeft = Shift >= 0 ? (uint8_t)Shift
  1324. : LaneBitmask::BitWidth + Shift;
  1325. for (auto &I : LaneTransforms) {
  1326. if (I.RotateLeft == RotateLeft) {
  1327. I.Mask |= SrcMask;
  1328. SrcMask = LaneBitmask::getNone();
  1329. }
  1330. }
  1331. if (SrcMask.any()) {
  1332. MaskRolPair MaskRol = { SrcMask, RotateLeft };
  1333. LaneTransforms.push_back(MaskRol);
  1334. }
  1335. }
  1336. }
  1337. // Optimize if the transformation consists of one step only: Set mask to
  1338. // 0xffffffff (including some irrelevant invalid bits) so that it should
  1339. // merge with more entries later while compressing the table.
  1340. if (LaneTransforms.size() == 1)
  1341. LaneTransforms[0].Mask = LaneBitmask::getAll();
  1342. // Further compression optimization: For invalid compositions resulting
  1343. // in a sequence with 0 entries we can just pick any other. Choose
  1344. // Mask 0xffffffff with Rotation 0.
  1345. if (LaneTransforms.size() == 0) {
  1346. MaskRolPair P = { LaneBitmask::getAll(), 0 };
  1347. LaneTransforms.push_back(P);
  1348. }
  1349. }
  1350. // FIXME: What if ad-hoc aliasing introduces overlaps that aren't represented
  1351. // by the sub-register graph? This doesn't occur in any known targets.
  1352. // Inherit lanes from composites.
  1353. for (const auto &Idx : SubRegIndices) {
  1354. LaneBitmask Mask = Idx.computeLaneMask();
  1355. // If some super-registers without CoveredBySubRegs use this index, we can
  1356. // no longer assume that the lanes are covering their registers.
  1357. if (!Idx.AllSuperRegsCovered)
  1358. CoveringLanes &= ~Mask;
  1359. }
  1360. // Compute lane mask combinations for register classes.
  1361. for (auto &RegClass : RegClasses) {
  1362. LaneBitmask LaneMask;
  1363. for (const auto &SubRegIndex : SubRegIndices) {
  1364. if (RegClass.getSubClassWithSubReg(&SubRegIndex) == nullptr)
  1365. continue;
  1366. LaneMask |= SubRegIndex.LaneMask;
  1367. }
  1368. // For classes without any subregisters set LaneMask to 1 instead of 0.
  1369. // This makes it easier for client code to handle classes uniformly.
  1370. if (LaneMask.none())
  1371. LaneMask = LaneBitmask::getLane(0);
  1372. RegClass.LaneMask = LaneMask;
  1373. }
  1374. }
  1375. namespace {
  1376. // UberRegSet is a helper class for computeRegUnitWeights. Each UberRegSet is
  1377. // the transitive closure of the union of overlapping register
  1378. // classes. Together, the UberRegSets form a partition of the registers. If we
  1379. // consider overlapping register classes to be connected, then each UberRegSet
  1380. // is a set of connected components.
  1381. //
  1382. // An UberRegSet will likely be a horizontal slice of register names of
  1383. // the same width. Nontrivial subregisters should then be in a separate
  1384. // UberRegSet. But this property isn't required for valid computation of
  1385. // register unit weights.
  1386. //
  1387. // A Weight field caches the max per-register unit weight in each UberRegSet.
  1388. //
  1389. // A set of SingularDeterminants flags single units of some register in this set
  1390. // for which the unit weight equals the set weight. These units should not have
  1391. // their weight increased.
  1392. struct UberRegSet {
  1393. CodeGenRegister::Vec Regs;
  1394. unsigned Weight = 0;
  1395. CodeGenRegister::RegUnitList SingularDeterminants;
  1396. UberRegSet() = default;
  1397. };
  1398. } // end anonymous namespace
  1399. // Partition registers into UberRegSets, where each set is the transitive
  1400. // closure of the union of overlapping register classes.
  1401. //
  1402. // UberRegSets[0] is a special non-allocatable set.
  1403. static void computeUberSets(std::vector<UberRegSet> &UberSets,
  1404. std::vector<UberRegSet*> &RegSets,
  1405. CodeGenRegBank &RegBank) {
  1406. const auto &Registers = RegBank.getRegisters();
  1407. // The Register EnumValue is one greater than its index into Registers.
  1408. assert(Registers.size() == Registers.back().EnumValue &&
  1409. "register enum value mismatch");
  1410. // For simplicitly make the SetID the same as EnumValue.
  1411. IntEqClasses UberSetIDs(Registers.size()+1);
  1412. std::set<unsigned> AllocatableRegs;
  1413. for (auto &RegClass : RegBank.getRegClasses()) {
  1414. if (!RegClass.Allocatable)
  1415. continue;
  1416. const CodeGenRegister::Vec &Regs = RegClass.getMembers();
  1417. if (Regs.empty())
  1418. continue;
  1419. unsigned USetID = UberSetIDs.findLeader((*Regs.begin())->EnumValue);
  1420. assert(USetID && "register number 0 is invalid");
  1421. AllocatableRegs.insert((*Regs.begin())->EnumValue);
  1422. for (const CodeGenRegister *CGR : llvm::drop_begin(Regs)) {
  1423. AllocatableRegs.insert(CGR->EnumValue);
  1424. UberSetIDs.join(USetID, CGR->EnumValue);
  1425. }
  1426. }
  1427. // Combine non-allocatable regs.
  1428. for (const auto &Reg : Registers) {
  1429. unsigned RegNum = Reg.EnumValue;
  1430. if (AllocatableRegs.count(RegNum))
  1431. continue;
  1432. UberSetIDs.join(0, RegNum);
  1433. }
  1434. UberSetIDs.compress();
  1435. // Make the first UberSet a special unallocatable set.
  1436. unsigned ZeroID = UberSetIDs[0];
  1437. // Insert Registers into the UberSets formed by union-find.
  1438. // Do not resize after this.
  1439. UberSets.resize(UberSetIDs.getNumClasses());
  1440. unsigned i = 0;
  1441. for (const CodeGenRegister &Reg : Registers) {
  1442. unsigned USetID = UberSetIDs[Reg.EnumValue];
  1443. if (!USetID)
  1444. USetID = ZeroID;
  1445. else if (USetID == ZeroID)
  1446. USetID = 0;
  1447. UberRegSet *USet = &UberSets[USetID];
  1448. USet->Regs.push_back(&Reg);
  1449. sortAndUniqueRegisters(USet->Regs);
  1450. RegSets[i++] = USet;
  1451. }
  1452. }
  1453. // Recompute each UberSet weight after changing unit weights.
  1454. static void computeUberWeights(std::vector<UberRegSet> &UberSets,
  1455. CodeGenRegBank &RegBank) {
  1456. // Skip the first unallocatable set.
  1457. for (std::vector<UberRegSet>::iterator I = std::next(UberSets.begin()),
  1458. E = UberSets.end(); I != E; ++I) {
  1459. // Initialize all unit weights in this set, and remember the max units/reg.
  1460. const CodeGenRegister *Reg = nullptr;
  1461. unsigned MaxWeight = 0, Weight = 0;
  1462. for (RegUnitIterator UnitI(I->Regs); UnitI.isValid(); ++UnitI) {
  1463. if (Reg != UnitI.getReg()) {
  1464. if (Weight > MaxWeight)
  1465. MaxWeight = Weight;
  1466. Reg = UnitI.getReg();
  1467. Weight = 0;
  1468. }
  1469. if (!RegBank.getRegUnit(*UnitI).Artificial) {
  1470. unsigned UWeight = RegBank.getRegUnit(*UnitI).Weight;
  1471. if (!UWeight) {
  1472. UWeight = 1;
  1473. RegBank.increaseRegUnitWeight(*UnitI, UWeight);
  1474. }
  1475. Weight += UWeight;
  1476. }
  1477. }
  1478. if (Weight > MaxWeight)
  1479. MaxWeight = Weight;
  1480. if (I->Weight != MaxWeight) {
  1481. LLVM_DEBUG(dbgs() << "UberSet " << I - UberSets.begin() << " Weight "
  1482. << MaxWeight;
  1483. for (auto &Unit
  1484. : I->Regs) dbgs()
  1485. << " " << Unit->getName();
  1486. dbgs() << "\n");
  1487. // Update the set weight.
  1488. I->Weight = MaxWeight;
  1489. }
  1490. // Find singular determinants.
  1491. for (const auto R : I->Regs) {
  1492. if (R->getRegUnits().count() == 1 && R->getWeight(RegBank) == I->Weight) {
  1493. I->SingularDeterminants |= R->getRegUnits();
  1494. }
  1495. }
  1496. }
  1497. }
  1498. // normalizeWeight is a computeRegUnitWeights helper that adjusts the weight of
  1499. // a register and its subregisters so that they have the same weight as their
  1500. // UberSet. Self-recursion processes the subregister tree in postorder so
  1501. // subregisters are normalized first.
  1502. //
  1503. // Side effects:
  1504. // - creates new adopted register units
  1505. // - causes superregisters to inherit adopted units
  1506. // - increases the weight of "singular" units
  1507. // - induces recomputation of UberWeights.
  1508. static bool normalizeWeight(CodeGenRegister *Reg,
  1509. std::vector<UberRegSet> &UberSets,
  1510. std::vector<UberRegSet*> &RegSets,
  1511. BitVector &NormalRegs,
  1512. CodeGenRegister::RegUnitList &NormalUnits,
  1513. CodeGenRegBank &RegBank) {
  1514. NormalRegs.resize(std::max(Reg->EnumValue + 1, NormalRegs.size()));
  1515. if (NormalRegs.test(Reg->EnumValue))
  1516. return false;
  1517. NormalRegs.set(Reg->EnumValue);
  1518. bool Changed = false;
  1519. const CodeGenRegister::SubRegMap &SRM = Reg->getSubRegs();
  1520. for (auto SRI : SRM) {
  1521. if (SRI.second == Reg)
  1522. continue; // self-cycles happen
  1523. Changed |= normalizeWeight(SRI.second, UberSets, RegSets, NormalRegs,
  1524. NormalUnits, RegBank);
  1525. }
  1526. // Postorder register normalization.
  1527. // Inherit register units newly adopted by subregisters.
  1528. if (Reg->inheritRegUnits(RegBank))
  1529. computeUberWeights(UberSets, RegBank);
  1530. // Check if this register is too skinny for its UberRegSet.
  1531. UberRegSet *UberSet = RegSets[RegBank.getRegIndex(Reg)];
  1532. unsigned RegWeight = Reg->getWeight(RegBank);
  1533. if (UberSet->Weight > RegWeight) {
  1534. // A register unit's weight can be adjusted only if it is the singular unit
  1535. // for this register, has not been used to normalize a subregister's set,
  1536. // and has not already been used to singularly determine this UberRegSet.
  1537. unsigned AdjustUnit = *Reg->getRegUnits().begin();
  1538. if (Reg->getRegUnits().count() != 1
  1539. || hasRegUnit(NormalUnits, AdjustUnit)
  1540. || hasRegUnit(UberSet->SingularDeterminants, AdjustUnit)) {
  1541. // We don't have an adjustable unit, so adopt a new one.
  1542. AdjustUnit = RegBank.newRegUnit(UberSet->Weight - RegWeight);
  1543. Reg->adoptRegUnit(AdjustUnit);
  1544. // Adopting a unit does not immediately require recomputing set weights.
  1545. }
  1546. else {
  1547. // Adjust the existing single unit.
  1548. if (!RegBank.getRegUnit(AdjustUnit).Artificial)
  1549. RegBank.increaseRegUnitWeight(AdjustUnit, UberSet->Weight - RegWeight);
  1550. // The unit may be shared among sets and registers within this set.
  1551. computeUberWeights(UberSets, RegBank);
  1552. }
  1553. Changed = true;
  1554. }
  1555. // Mark these units normalized so superregisters can't change their weights.
  1556. NormalUnits |= Reg->getRegUnits();
  1557. return Changed;
  1558. }
  1559. // Compute a weight for each register unit created during getSubRegs.
  1560. //
  1561. // The goal is that two registers in the same class will have the same weight,
  1562. // where each register's weight is defined as sum of its units' weights.
  1563. void CodeGenRegBank::computeRegUnitWeights() {
  1564. std::vector<UberRegSet> UberSets;
  1565. std::vector<UberRegSet*> RegSets(Registers.size());
  1566. computeUberSets(UberSets, RegSets, *this);
  1567. // UberSets and RegSets are now immutable.
  1568. computeUberWeights(UberSets, *this);
  1569. // Iterate over each Register, normalizing the unit weights until reaching
  1570. // a fix point.
  1571. unsigned NumIters = 0;
  1572. for (bool Changed = true; Changed; ++NumIters) {
  1573. assert(NumIters <= NumNativeRegUnits && "Runaway register unit weights");
  1574. Changed = false;
  1575. for (auto &Reg : Registers) {
  1576. CodeGenRegister::RegUnitList NormalUnits;
  1577. BitVector NormalRegs;
  1578. Changed |= normalizeWeight(&Reg, UberSets, RegSets, NormalRegs,
  1579. NormalUnits, *this);
  1580. }
  1581. }
  1582. }
  1583. // Find a set in UniqueSets with the same elements as Set.
  1584. // Return an iterator into UniqueSets.
  1585. static std::vector<RegUnitSet>::const_iterator
  1586. findRegUnitSet(const std::vector<RegUnitSet> &UniqueSets,
  1587. const RegUnitSet &Set) {
  1588. std::vector<RegUnitSet>::const_iterator
  1589. I = UniqueSets.begin(), E = UniqueSets.end();
  1590. for(;I != E; ++I) {
  1591. if (I->Units == Set.Units)
  1592. break;
  1593. }
  1594. return I;
  1595. }
  1596. // Return true if the RUSubSet is a subset of RUSuperSet.
  1597. static bool isRegUnitSubSet(const std::vector<unsigned> &RUSubSet,
  1598. const std::vector<unsigned> &RUSuperSet) {
  1599. return std::includes(RUSuperSet.begin(), RUSuperSet.end(),
  1600. RUSubSet.begin(), RUSubSet.end());
  1601. }
  1602. /// Iteratively prune unit sets. Prune subsets that are close to the superset,
  1603. /// but with one or two registers removed. We occasionally have registers like
  1604. /// APSR and PC thrown in with the general registers. We also see many
  1605. /// special-purpose register subsets, such as tail-call and Thumb
  1606. /// encodings. Generating all possible overlapping sets is combinatorial and
  1607. /// overkill for modeling pressure. Ideally we could fix this statically in
  1608. /// tablegen by (1) having the target define register classes that only include
  1609. /// the allocatable registers and marking other classes as non-allocatable and
  1610. /// (2) having a way to mark special purpose classes as "don't-care" classes for
  1611. /// the purpose of pressure. However, we make an attempt to handle targets that
  1612. /// are not nicely defined by merging nearly identical register unit sets
  1613. /// statically. This generates smaller tables. Then, dynamically, we adjust the
  1614. /// set limit by filtering the reserved registers.
  1615. ///
  1616. /// Merge sets only if the units have the same weight. For example, on ARM,
  1617. /// Q-tuples with ssub index 0 include all S regs but also include D16+. We
  1618. /// should not expand the S set to include D regs.
  1619. void CodeGenRegBank::pruneUnitSets() {
  1620. assert(RegClassUnitSets.empty() && "this invalidates RegClassUnitSets");
  1621. // Form an equivalence class of UnitSets with no significant difference.
  1622. std::vector<unsigned> SuperSetIDs;
  1623. for (unsigned SubIdx = 0, EndIdx = RegUnitSets.size();
  1624. SubIdx != EndIdx; ++SubIdx) {
  1625. const RegUnitSet &SubSet = RegUnitSets[SubIdx];
  1626. unsigned SuperIdx = 0;
  1627. for (; SuperIdx != EndIdx; ++SuperIdx) {
  1628. if (SuperIdx == SubIdx)
  1629. continue;
  1630. unsigned UnitWeight = RegUnits[SubSet.Units[0]].Weight;
  1631. const RegUnitSet &SuperSet = RegUnitSets[SuperIdx];
  1632. if (isRegUnitSubSet(SubSet.Units, SuperSet.Units)
  1633. && (SubSet.Units.size() + 3 > SuperSet.Units.size())
  1634. && UnitWeight == RegUnits[SuperSet.Units[0]].Weight
  1635. && UnitWeight == RegUnits[SuperSet.Units.back()].Weight) {
  1636. LLVM_DEBUG(dbgs() << "UnitSet " << SubIdx << " subsumed by " << SuperIdx
  1637. << "\n");
  1638. // We can pick any of the set names for the merged set. Go for the
  1639. // shortest one to avoid picking the name of one of the classes that are
  1640. // artificially created by tablegen. So "FPR128_lo" instead of
  1641. // "QQQQ_with_qsub3_in_FPR128_lo".
  1642. if (RegUnitSets[SubIdx].Name.size() < RegUnitSets[SuperIdx].Name.size())
  1643. RegUnitSets[SuperIdx].Name = RegUnitSets[SubIdx].Name;
  1644. break;
  1645. }
  1646. }
  1647. if (SuperIdx == EndIdx)
  1648. SuperSetIDs.push_back(SubIdx);
  1649. }
  1650. // Populate PrunedUnitSets with each equivalence class's superset.
  1651. std::vector<RegUnitSet> PrunedUnitSets(SuperSetIDs.size());
  1652. for (unsigned i = 0, e = SuperSetIDs.size(); i != e; ++i) {
  1653. unsigned SuperIdx = SuperSetIDs[i];
  1654. PrunedUnitSets[i].Name = RegUnitSets[SuperIdx].Name;
  1655. PrunedUnitSets[i].Units.swap(RegUnitSets[SuperIdx].Units);
  1656. }
  1657. RegUnitSets.swap(PrunedUnitSets);
  1658. }
  1659. // Create a RegUnitSet for each RegClass that contains all units in the class
  1660. // including adopted units that are necessary to model register pressure. Then
  1661. // iteratively compute RegUnitSets such that the union of any two overlapping
  1662. // RegUnitSets is repreresented.
  1663. //
  1664. // RegisterInfoEmitter will map each RegClass to its RegUnitClass and any
  1665. // RegUnitSet that is a superset of that RegUnitClass.
  1666. void CodeGenRegBank::computeRegUnitSets() {
  1667. assert(RegUnitSets.empty() && "dirty RegUnitSets");
  1668. // Compute a unique RegUnitSet for each RegClass.
  1669. auto &RegClasses = getRegClasses();
  1670. for (auto &RC : RegClasses) {
  1671. if (!RC.Allocatable || RC.Artificial || !RC.GeneratePressureSet)
  1672. continue;
  1673. // Speculatively grow the RegUnitSets to hold the new set.
  1674. RegUnitSets.resize(RegUnitSets.size() + 1);
  1675. RegUnitSets.back().Name = RC.getName();
  1676. // Compute a sorted list of units in this class.
  1677. RC.buildRegUnitSet(*this, RegUnitSets.back().Units);
  1678. // Find an existing RegUnitSet.
  1679. std::vector<RegUnitSet>::const_iterator SetI =
  1680. findRegUnitSet(RegUnitSets, RegUnitSets.back());
  1681. if (SetI != std::prev(RegUnitSets.end()))
  1682. RegUnitSets.pop_back();
  1683. }
  1684. if (RegUnitSets.empty())
  1685. PrintFatalError("RegUnitSets cannot be empty!");
  1686. LLVM_DEBUG(dbgs() << "\nBefore pruning:\n"; for (unsigned USIdx = 0,
  1687. USEnd = RegUnitSets.size();
  1688. USIdx < USEnd; ++USIdx) {
  1689. dbgs() << "UnitSet " << USIdx << " " << RegUnitSets[USIdx].Name << ":";
  1690. for (auto &U : RegUnitSets[USIdx].Units)
  1691. printRegUnitName(U);
  1692. dbgs() << "\n";
  1693. });
  1694. // Iteratively prune unit sets.
  1695. pruneUnitSets();
  1696. LLVM_DEBUG(dbgs() << "\nBefore union:\n"; for (unsigned USIdx = 0,
  1697. USEnd = RegUnitSets.size();
  1698. USIdx < USEnd; ++USIdx) {
  1699. dbgs() << "UnitSet " << USIdx << " " << RegUnitSets[USIdx].Name << ":";
  1700. for (auto &U : RegUnitSets[USIdx].Units)
  1701. printRegUnitName(U);
  1702. dbgs() << "\n";
  1703. } dbgs() << "\nUnion sets:\n");
  1704. // Iterate over all unit sets, including new ones added by this loop.
  1705. unsigned NumRegUnitSubSets = RegUnitSets.size();
  1706. for (unsigned Idx = 0, EndIdx = RegUnitSets.size(); Idx != EndIdx; ++Idx) {
  1707. // In theory, this is combinatorial. In practice, it needs to be bounded
  1708. // by a small number of sets for regpressure to be efficient.
  1709. // If the assert is hit, we need to implement pruning.
  1710. assert(Idx < (2*NumRegUnitSubSets) && "runaway unit set inference");
  1711. // Compare new sets with all original classes.
  1712. for (unsigned SearchIdx = (Idx >= NumRegUnitSubSets) ? 0 : Idx+1;
  1713. SearchIdx != EndIdx; ++SearchIdx) {
  1714. std::set<unsigned> Intersection;
  1715. std::set_intersection(RegUnitSets[Idx].Units.begin(),
  1716. RegUnitSets[Idx].Units.end(),
  1717. RegUnitSets[SearchIdx].Units.begin(),
  1718. RegUnitSets[SearchIdx].Units.end(),
  1719. std::inserter(Intersection, Intersection.begin()));
  1720. if (Intersection.empty())
  1721. continue;
  1722. // Speculatively grow the RegUnitSets to hold the new set.
  1723. RegUnitSets.resize(RegUnitSets.size() + 1);
  1724. RegUnitSets.back().Name =
  1725. RegUnitSets[Idx].Name + "_with_" + RegUnitSets[SearchIdx].Name;
  1726. std::set_union(RegUnitSets[Idx].Units.begin(),
  1727. RegUnitSets[Idx].Units.end(),
  1728. RegUnitSets[SearchIdx].Units.begin(),
  1729. RegUnitSets[SearchIdx].Units.end(),
  1730. std::inserter(RegUnitSets.back().Units,
  1731. RegUnitSets.back().Units.begin()));
  1732. // Find an existing RegUnitSet, or add the union to the unique sets.
  1733. std::vector<RegUnitSet>::const_iterator SetI =
  1734. findRegUnitSet(RegUnitSets, RegUnitSets.back());
  1735. if (SetI != std::prev(RegUnitSets.end()))
  1736. RegUnitSets.pop_back();
  1737. else {
  1738. LLVM_DEBUG(dbgs() << "UnitSet " << RegUnitSets.size() - 1 << " "
  1739. << RegUnitSets.back().Name << ":";
  1740. for (auto &U
  1741. : RegUnitSets.back().Units) printRegUnitName(U);
  1742. dbgs() << "\n";);
  1743. }
  1744. }
  1745. }
  1746. // Iteratively prune unit sets after inferring supersets.
  1747. pruneUnitSets();
  1748. LLVM_DEBUG(
  1749. dbgs() << "\n"; for (unsigned USIdx = 0, USEnd = RegUnitSets.size();
  1750. USIdx < USEnd; ++USIdx) {
  1751. dbgs() << "UnitSet " << USIdx << " " << RegUnitSets[USIdx].Name << ":";
  1752. for (auto &U : RegUnitSets[USIdx].Units)
  1753. printRegUnitName(U);
  1754. dbgs() << "\n";
  1755. });
  1756. // For each register class, list the UnitSets that are supersets.
  1757. RegClassUnitSets.resize(RegClasses.size());
  1758. int RCIdx = -1;
  1759. for (auto &RC : RegClasses) {
  1760. ++RCIdx;
  1761. if (!RC.Allocatable)
  1762. continue;
  1763. // Recompute the sorted list of units in this class.
  1764. std::vector<unsigned> RCRegUnits;
  1765. RC.buildRegUnitSet(*this, RCRegUnits);
  1766. // Don't increase pressure for unallocatable regclasses.
  1767. if (RCRegUnits.empty())
  1768. continue;
  1769. LLVM_DEBUG(dbgs() << "RC " << RC.getName() << " Units:\n";
  1770. for (auto U
  1771. : RCRegUnits) printRegUnitName(U);
  1772. dbgs() << "\n UnitSetIDs:");
  1773. // Find all supersets.
  1774. for (unsigned USIdx = 0, USEnd = RegUnitSets.size();
  1775. USIdx != USEnd; ++USIdx) {
  1776. if (isRegUnitSubSet(RCRegUnits, RegUnitSets[USIdx].Units)) {
  1777. LLVM_DEBUG(dbgs() << " " << USIdx);
  1778. RegClassUnitSets[RCIdx].push_back(USIdx);
  1779. }
  1780. }
  1781. LLVM_DEBUG(dbgs() << "\n");
  1782. assert((!RegClassUnitSets[RCIdx].empty() || !RC.GeneratePressureSet) &&
  1783. "missing unit set for regclass");
  1784. }
  1785. // For each register unit, ensure that we have the list of UnitSets that
  1786. // contain the unit. Normally, this matches an existing list of UnitSets for a
  1787. // register class. If not, we create a new entry in RegClassUnitSets as a
  1788. // "fake" register class.
  1789. for (unsigned UnitIdx = 0, UnitEnd = NumNativeRegUnits;
  1790. UnitIdx < UnitEnd; ++UnitIdx) {
  1791. std::vector<unsigned> RUSets;
  1792. for (unsigned i = 0, e = RegUnitSets.size(); i != e; ++i) {
  1793. RegUnitSet &RUSet = RegUnitSets[i];
  1794. if (!is_contained(RUSet.Units, UnitIdx))
  1795. continue;
  1796. RUSets.push_back(i);
  1797. }
  1798. unsigned RCUnitSetsIdx = 0;
  1799. for (unsigned e = RegClassUnitSets.size();
  1800. RCUnitSetsIdx != e; ++RCUnitSetsIdx) {
  1801. if (RegClassUnitSets[RCUnitSetsIdx] == RUSets) {
  1802. break;
  1803. }
  1804. }
  1805. RegUnits[UnitIdx].RegClassUnitSetsIdx = RCUnitSetsIdx;
  1806. if (RCUnitSetsIdx == RegClassUnitSets.size()) {
  1807. // Create a new list of UnitSets as a "fake" register class.
  1808. RegClassUnitSets.resize(RCUnitSetsIdx + 1);
  1809. RegClassUnitSets[RCUnitSetsIdx].swap(RUSets);
  1810. }
  1811. }
  1812. }
  1813. void CodeGenRegBank::computeRegUnitLaneMasks() {
  1814. for (auto &Register : Registers) {
  1815. // Create an initial lane mask for all register units.
  1816. const auto &RegUnits = Register.getRegUnits();
  1817. CodeGenRegister::RegUnitLaneMaskList
  1818. RegUnitLaneMasks(RegUnits.count(), LaneBitmask::getNone());
  1819. // Iterate through SubRegisters.
  1820. typedef CodeGenRegister::SubRegMap SubRegMap;
  1821. const SubRegMap &SubRegs = Register.getSubRegs();
  1822. for (auto S : SubRegs) {
  1823. CodeGenRegister *SubReg = S.second;
  1824. // Ignore non-leaf subregisters, their lane masks are fully covered by
  1825. // the leaf subregisters anyway.
  1826. if (!SubReg->getSubRegs().empty())
  1827. continue;
  1828. CodeGenSubRegIndex *SubRegIndex = S.first;
  1829. const CodeGenRegister *SubRegister = S.second;
  1830. LaneBitmask LaneMask = SubRegIndex->LaneMask;
  1831. // Distribute LaneMask to Register Units touched.
  1832. for (unsigned SUI : SubRegister->getRegUnits()) {
  1833. bool Found = false;
  1834. unsigned u = 0;
  1835. for (unsigned RU : RegUnits) {
  1836. if (SUI == RU) {
  1837. RegUnitLaneMasks[u] |= LaneMask;
  1838. assert(!Found);
  1839. Found = true;
  1840. }
  1841. ++u;
  1842. }
  1843. (void)Found;
  1844. assert(Found);
  1845. }
  1846. }
  1847. Register.setRegUnitLaneMasks(RegUnitLaneMasks);
  1848. }
  1849. }
  1850. void CodeGenRegBank::computeDerivedInfo() {
  1851. computeComposites();
  1852. computeSubRegLaneMasks();
  1853. // Compute a weight for each register unit created during getSubRegs.
  1854. // This may create adopted register units (with unit # >= NumNativeRegUnits).
  1855. computeRegUnitWeights();
  1856. // Compute a unique set of RegUnitSets. One for each RegClass and inferred
  1857. // supersets for the union of overlapping sets.
  1858. computeRegUnitSets();
  1859. computeRegUnitLaneMasks();
  1860. // Compute register class HasDisjunctSubRegs/CoveredBySubRegs flag.
  1861. for (CodeGenRegisterClass &RC : RegClasses) {
  1862. RC.HasDisjunctSubRegs = false;
  1863. RC.CoveredBySubRegs = true;
  1864. for (const CodeGenRegister *Reg : RC.getMembers()) {
  1865. RC.HasDisjunctSubRegs |= Reg->HasDisjunctSubRegs;
  1866. RC.CoveredBySubRegs &= Reg->CoveredBySubRegs;
  1867. }
  1868. }
  1869. // Get the weight of each set.
  1870. for (unsigned Idx = 0, EndIdx = RegUnitSets.size(); Idx != EndIdx; ++Idx)
  1871. RegUnitSets[Idx].Weight = getRegUnitSetWeight(RegUnitSets[Idx].Units);
  1872. // Find the order of each set.
  1873. RegUnitSetOrder.reserve(RegUnitSets.size());
  1874. for (unsigned Idx = 0, EndIdx = RegUnitSets.size(); Idx != EndIdx; ++Idx)
  1875. RegUnitSetOrder.push_back(Idx);
  1876. llvm::stable_sort(RegUnitSetOrder, [this](unsigned ID1, unsigned ID2) {
  1877. return getRegPressureSet(ID1).Units.size() <
  1878. getRegPressureSet(ID2).Units.size();
  1879. });
  1880. for (unsigned Idx = 0, EndIdx = RegUnitSets.size(); Idx != EndIdx; ++Idx) {
  1881. RegUnitSets[RegUnitSetOrder[Idx]].Order = Idx;
  1882. }
  1883. }
  1884. //
  1885. // Synthesize missing register class intersections.
  1886. //
  1887. // Make sure that sub-classes of RC exists such that getCommonSubClass(RC, X)
  1888. // returns a maximal register class for all X.
  1889. //
  1890. void CodeGenRegBank::inferCommonSubClass(CodeGenRegisterClass *RC) {
  1891. assert(!RegClasses.empty());
  1892. // Stash the iterator to the last element so that this loop doesn't visit
  1893. // elements added by the getOrCreateSubClass call within it.
  1894. for (auto I = RegClasses.begin(), E = std::prev(RegClasses.end());
  1895. I != std::next(E); ++I) {
  1896. CodeGenRegisterClass *RC1 = RC;
  1897. CodeGenRegisterClass *RC2 = &*I;
  1898. if (RC1 == RC2)
  1899. continue;
  1900. // Compute the set intersection of RC1 and RC2.
  1901. const CodeGenRegister::Vec &Memb1 = RC1->getMembers();
  1902. const CodeGenRegister::Vec &Memb2 = RC2->getMembers();
  1903. CodeGenRegister::Vec Intersection;
  1904. std::set_intersection(Memb1.begin(), Memb1.end(), Memb2.begin(),
  1905. Memb2.end(),
  1906. std::inserter(Intersection, Intersection.begin()),
  1907. deref<std::less<>>());
  1908. // Skip disjoint class pairs.
  1909. if (Intersection.empty())
  1910. continue;
  1911. // If RC1 and RC2 have different spill sizes or alignments, use the
  1912. // stricter one for sub-classing. If they are equal, prefer RC1.
  1913. if (RC2->RSI.hasStricterSpillThan(RC1->RSI))
  1914. std::swap(RC1, RC2);
  1915. getOrCreateSubClass(RC1, &Intersection,
  1916. RC1->getName() + "_and_" + RC2->getName());
  1917. }
  1918. }
  1919. //
  1920. // Synthesize missing sub-classes for getSubClassWithSubReg().
  1921. //
  1922. // Make sure that the set of registers in RC with a given SubIdx sub-register
  1923. // form a register class. Update RC->SubClassWithSubReg.
  1924. //
  1925. void CodeGenRegBank::inferSubClassWithSubReg(CodeGenRegisterClass *RC) {
  1926. // Map SubRegIndex to set of registers in RC supporting that SubRegIndex.
  1927. typedef std::map<const CodeGenSubRegIndex *, CodeGenRegister::Vec,
  1928. deref<std::less<>>>
  1929. SubReg2SetMap;
  1930. // Compute the set of registers supporting each SubRegIndex.
  1931. SubReg2SetMap SRSets;
  1932. for (const auto R : RC->getMembers()) {
  1933. if (R->Artificial)
  1934. continue;
  1935. const CodeGenRegister::SubRegMap &SRM = R->getSubRegs();
  1936. for (auto I : SRM) {
  1937. if (!I.first->Artificial)
  1938. SRSets[I.first].push_back(R);
  1939. }
  1940. }
  1941. for (auto I : SRSets)
  1942. sortAndUniqueRegisters(I.second);
  1943. // Find matching classes for all SRSets entries. Iterate in SubRegIndex
  1944. // numerical order to visit synthetic indices last.
  1945. for (const auto &SubIdx : SubRegIndices) {
  1946. if (SubIdx.Artificial)
  1947. continue;
  1948. SubReg2SetMap::const_iterator I = SRSets.find(&SubIdx);
  1949. // Unsupported SubRegIndex. Skip it.
  1950. if (I == SRSets.end())
  1951. continue;
  1952. // In most cases, all RC registers support the SubRegIndex.
  1953. if (I->second.size() == RC->getMembers().size()) {
  1954. RC->setSubClassWithSubReg(&SubIdx, RC);
  1955. continue;
  1956. }
  1957. // This is a real subset. See if we have a matching class.
  1958. CodeGenRegisterClass *SubRC =
  1959. getOrCreateSubClass(RC, &I->second,
  1960. RC->getName() + "_with_" + I->first->getName());
  1961. RC->setSubClassWithSubReg(&SubIdx, SubRC);
  1962. }
  1963. }
  1964. //
  1965. // Synthesize missing sub-classes of RC for getMatchingSuperRegClass().
  1966. //
  1967. // Create sub-classes of RC such that getMatchingSuperRegClass(RC, SubIdx, X)
  1968. // has a maximal result for any SubIdx and any X >= FirstSubRegRC.
  1969. //
  1970. void CodeGenRegBank::inferMatchingSuperRegClass(CodeGenRegisterClass *RC,
  1971. std::list<CodeGenRegisterClass>::iterator FirstSubRegRC) {
  1972. SmallVector<std::pair<const CodeGenRegister*,
  1973. const CodeGenRegister*>, 16> SSPairs;
  1974. BitVector TopoSigs(getNumTopoSigs());
  1975. // Iterate in SubRegIndex numerical order to visit synthetic indices last.
  1976. for (auto &SubIdx : SubRegIndices) {
  1977. // Skip indexes that aren't fully supported by RC's registers. This was
  1978. // computed by inferSubClassWithSubReg() above which should have been
  1979. // called first.
  1980. if (RC->getSubClassWithSubReg(&SubIdx) != RC)
  1981. continue;
  1982. // Build list of (Super, Sub) pairs for this SubIdx.
  1983. SSPairs.clear();
  1984. TopoSigs.reset();
  1985. for (const auto Super : RC->getMembers()) {
  1986. const CodeGenRegister *Sub = Super->getSubRegs().find(&SubIdx)->second;
  1987. assert(Sub && "Missing sub-register");
  1988. SSPairs.push_back(std::make_pair(Super, Sub));
  1989. TopoSigs.set(Sub->getTopoSig());
  1990. }
  1991. // Iterate over sub-register class candidates. Ignore classes created by
  1992. // this loop. They will never be useful.
  1993. // Store an iterator to the last element (not end) so that this loop doesn't
  1994. // visit newly inserted elements.
  1995. assert(!RegClasses.empty());
  1996. for (auto I = FirstSubRegRC, E = std::prev(RegClasses.end());
  1997. I != std::next(E); ++I) {
  1998. CodeGenRegisterClass &SubRC = *I;
  1999. if (SubRC.Artificial)
  2000. continue;
  2001. // Topological shortcut: SubRC members have the wrong shape.
  2002. if (!TopoSigs.anyCommon(SubRC.getTopoSigs()))
  2003. continue;
  2004. // Compute the subset of RC that maps into SubRC.
  2005. CodeGenRegister::Vec SubSetVec;
  2006. for (unsigned i = 0, e = SSPairs.size(); i != e; ++i)
  2007. if (SubRC.contains(SSPairs[i].second))
  2008. SubSetVec.push_back(SSPairs[i].first);
  2009. if (SubSetVec.empty())
  2010. continue;
  2011. // RC injects completely into SubRC.
  2012. sortAndUniqueRegisters(SubSetVec);
  2013. if (SubSetVec.size() == SSPairs.size()) {
  2014. SubRC.addSuperRegClass(&SubIdx, RC);
  2015. continue;
  2016. }
  2017. // Only a subset of RC maps into SubRC. Make sure it is represented by a
  2018. // class.
  2019. getOrCreateSubClass(RC, &SubSetVec, RC->getName() + "_with_" +
  2020. SubIdx.getName() + "_in_" +
  2021. SubRC.getName());
  2022. }
  2023. }
  2024. }
  2025. //
  2026. // Infer missing register classes.
  2027. //
  2028. void CodeGenRegBank::computeInferredRegisterClasses() {
  2029. assert(!RegClasses.empty());
  2030. // When this function is called, the register classes have not been sorted
  2031. // and assigned EnumValues yet. That means getSubClasses(),
  2032. // getSuperClasses(), and hasSubClass() functions are defunct.
  2033. // Use one-before-the-end so it doesn't move forward when new elements are
  2034. // added.
  2035. auto FirstNewRC = std::prev(RegClasses.end());
  2036. // Visit all register classes, including the ones being added by the loop.
  2037. // Watch out for iterator invalidation here.
  2038. for (auto I = RegClasses.begin(), E = RegClasses.end(); I != E; ++I) {
  2039. CodeGenRegisterClass *RC = &*I;
  2040. if (RC->Artificial)
  2041. continue;
  2042. // Synthesize answers for getSubClassWithSubReg().
  2043. inferSubClassWithSubReg(RC);
  2044. // Synthesize answers for getCommonSubClass().
  2045. inferCommonSubClass(RC);
  2046. // Synthesize answers for getMatchingSuperRegClass().
  2047. inferMatchingSuperRegClass(RC);
  2048. // New register classes are created while this loop is running, and we need
  2049. // to visit all of them. I particular, inferMatchingSuperRegClass needs
  2050. // to match old super-register classes with sub-register classes created
  2051. // after inferMatchingSuperRegClass was called. At this point,
  2052. // inferMatchingSuperRegClass has checked SuperRC = [0..rci] with SubRC =
  2053. // [0..FirstNewRC). We need to cover SubRC = [FirstNewRC..rci].
  2054. if (I == FirstNewRC) {
  2055. auto NextNewRC = std::prev(RegClasses.end());
  2056. for (auto I2 = RegClasses.begin(), E2 = std::next(FirstNewRC); I2 != E2;
  2057. ++I2)
  2058. inferMatchingSuperRegClass(&*I2, E2);
  2059. FirstNewRC = NextNewRC;
  2060. }
  2061. }
  2062. }
  2063. /// getRegisterClassForRegister - Find the register class that contains the
  2064. /// specified physical register. If the register is not in a register class,
  2065. /// return null. If the register is in multiple classes, and the classes have a
  2066. /// superset-subset relationship and the same set of types, return the
  2067. /// superclass. Otherwise return null.
  2068. const CodeGenRegisterClass*
  2069. CodeGenRegBank::getRegClassForRegister(Record *R) {
  2070. const CodeGenRegister *Reg = getReg(R);
  2071. const CodeGenRegisterClass *FoundRC = nullptr;
  2072. for (const auto &RC : getRegClasses()) {
  2073. if (!RC.contains(Reg))
  2074. continue;
  2075. // If this is the first class that contains the register,
  2076. // make a note of it and go on to the next class.
  2077. if (!FoundRC) {
  2078. FoundRC = &RC;
  2079. continue;
  2080. }
  2081. // If a register's classes have different types, return null.
  2082. if (RC.getValueTypes() != FoundRC->getValueTypes())
  2083. return nullptr;
  2084. // Check to see if the previously found class that contains
  2085. // the register is a subclass of the current class. If so,
  2086. // prefer the superclass.
  2087. if (RC.hasSubClass(FoundRC)) {
  2088. FoundRC = &RC;
  2089. continue;
  2090. }
  2091. // Check to see if the previously found class that contains
  2092. // the register is a superclass of the current class. If so,
  2093. // prefer the superclass.
  2094. if (FoundRC->hasSubClass(&RC))
  2095. continue;
  2096. // Multiple classes, and neither is a superclass of the other.
  2097. // Return null.
  2098. return nullptr;
  2099. }
  2100. return FoundRC;
  2101. }
  2102. const CodeGenRegisterClass *
  2103. CodeGenRegBank::getMinimalPhysRegClass(Record *RegRecord,
  2104. ValueTypeByHwMode *VT) {
  2105. const CodeGenRegister *Reg = getReg(RegRecord);
  2106. const CodeGenRegisterClass *BestRC = nullptr;
  2107. for (const auto &RC : getRegClasses()) {
  2108. if ((!VT || RC.hasType(*VT)) &&
  2109. RC.contains(Reg) && (!BestRC || BestRC->hasSubClass(&RC)))
  2110. BestRC = &RC;
  2111. }
  2112. assert(BestRC && "Couldn't find the register class");
  2113. return BestRC;
  2114. }
  2115. BitVector CodeGenRegBank::computeCoveredRegisters(ArrayRef<Record*> Regs) {
  2116. SetVector<const CodeGenRegister*> Set;
  2117. // First add Regs with all sub-registers.
  2118. for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
  2119. CodeGenRegister *Reg = getReg(Regs[i]);
  2120. if (Set.insert(Reg))
  2121. // Reg is new, add all sub-registers.
  2122. // The pre-ordering is not important here.
  2123. Reg->addSubRegsPreOrder(Set, *this);
  2124. }
  2125. // Second, find all super-registers that are completely covered by the set.
  2126. for (unsigned i = 0; i != Set.size(); ++i) {
  2127. const CodeGenRegister::SuperRegList &SR = Set[i]->getSuperRegs();
  2128. for (unsigned j = 0, e = SR.size(); j != e; ++j) {
  2129. const CodeGenRegister *Super = SR[j];
  2130. if (!Super->CoveredBySubRegs || Set.count(Super))
  2131. continue;
  2132. // This new super-register is covered by its sub-registers.
  2133. bool AllSubsInSet = true;
  2134. const CodeGenRegister::SubRegMap &SRM = Super->getSubRegs();
  2135. for (auto I : SRM)
  2136. if (!Set.count(I.second)) {
  2137. AllSubsInSet = false;
  2138. break;
  2139. }
  2140. // All sub-registers in Set, add Super as well.
  2141. // We will visit Super later to recheck its super-registers.
  2142. if (AllSubsInSet)
  2143. Set.insert(Super);
  2144. }
  2145. }
  2146. // Convert to BitVector.
  2147. BitVector BV(Registers.size() + 1);
  2148. for (unsigned i = 0, e = Set.size(); i != e; ++i)
  2149. BV.set(Set[i]->EnumValue);
  2150. return BV;
  2151. }
  2152. void CodeGenRegBank::printRegUnitName(unsigned Unit) const {
  2153. if (Unit < NumNativeRegUnits)
  2154. dbgs() << ' ' << RegUnits[Unit].Roots[0]->getName();
  2155. else
  2156. dbgs() << " #" << Unit;
  2157. }