CodeGenRegisters.cpp 92 KB

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