RegAllocGreedy.cpp 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266
  1. //===- RegAllocGreedy.cpp - greedy register allocator ---------------------===//
  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 the RAGreedy function pass for register allocation in
  10. // optimized builds.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "AllocationOrder.h"
  14. #include "InterferenceCache.h"
  15. #include "LiveDebugVariables.h"
  16. #include "RegAllocBase.h"
  17. #include "SpillPlacement.h"
  18. #include "Spiller.h"
  19. #include "SplitKit.h"
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/BitVector.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/IndexedMap.h"
  24. #include "llvm/ADT/MapVector.h"
  25. #include "llvm/ADT/SetVector.h"
  26. #include "llvm/ADT/SmallPtrSet.h"
  27. #include "llvm/ADT/SmallSet.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. #include "llvm/ADT/Statistic.h"
  30. #include "llvm/ADT/StringRef.h"
  31. #include "llvm/Analysis/AliasAnalysis.h"
  32. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  33. #include "llvm/CodeGen/CalcSpillWeights.h"
  34. #include "llvm/CodeGen/EdgeBundles.h"
  35. #include "llvm/CodeGen/LiveInterval.h"
  36. #include "llvm/CodeGen/LiveIntervalUnion.h"
  37. #include "llvm/CodeGen/LiveIntervals.h"
  38. #include "llvm/CodeGen/LiveRangeEdit.h"
  39. #include "llvm/CodeGen/LiveRegMatrix.h"
  40. #include "llvm/CodeGen/LiveStacks.h"
  41. #include "llvm/CodeGen/MachineBasicBlock.h"
  42. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  43. #include "llvm/CodeGen/MachineDominators.h"
  44. #include "llvm/CodeGen/MachineFrameInfo.h"
  45. #include "llvm/CodeGen/MachineFunction.h"
  46. #include "llvm/CodeGen/MachineFunctionPass.h"
  47. #include "llvm/CodeGen/MachineInstr.h"
  48. #include "llvm/CodeGen/MachineLoopInfo.h"
  49. #include "llvm/CodeGen/MachineOperand.h"
  50. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  51. #include "llvm/CodeGen/MachineRegisterInfo.h"
  52. #include "llvm/CodeGen/RegAllocRegistry.h"
  53. #include "llvm/CodeGen/RegisterClassInfo.h"
  54. #include "llvm/CodeGen/SlotIndexes.h"
  55. #include "llvm/CodeGen/TargetInstrInfo.h"
  56. #include "llvm/CodeGen/TargetRegisterInfo.h"
  57. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  58. #include "llvm/CodeGen/VirtRegMap.h"
  59. #include "llvm/IR/Function.h"
  60. #include "llvm/IR/LLVMContext.h"
  61. #include "llvm/MC/MCRegisterInfo.h"
  62. #include "llvm/Pass.h"
  63. #include "llvm/Support/BlockFrequency.h"
  64. #include "llvm/Support/BranchProbability.h"
  65. #include "llvm/Support/CommandLine.h"
  66. #include "llvm/Support/Debug.h"
  67. #include "llvm/Support/MathExtras.h"
  68. #include "llvm/Support/Timer.h"
  69. #include "llvm/Support/raw_ostream.h"
  70. #include "llvm/Target/TargetMachine.h"
  71. #include <algorithm>
  72. #include <cassert>
  73. #include <cstdint>
  74. #include <memory>
  75. #include <queue>
  76. #include <tuple>
  77. #include <utility>
  78. using namespace llvm;
  79. #define DEBUG_TYPE "regalloc"
  80. STATISTIC(NumGlobalSplits, "Number of split global live ranges");
  81. STATISTIC(NumLocalSplits, "Number of split local live ranges");
  82. STATISTIC(NumEvicted, "Number of interferences evicted");
  83. static cl::opt<SplitEditor::ComplementSpillMode> SplitSpillMode(
  84. "split-spill-mode", cl::Hidden,
  85. cl::desc("Spill mode for splitting live ranges"),
  86. cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"),
  87. clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"),
  88. clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed")),
  89. cl::init(SplitEditor::SM_Speed));
  90. static cl::opt<unsigned>
  91. LastChanceRecoloringMaxDepth("lcr-max-depth", cl::Hidden,
  92. cl::desc("Last chance recoloring max depth"),
  93. cl::init(5));
  94. static cl::opt<unsigned> LastChanceRecoloringMaxInterference(
  95. "lcr-max-interf", cl::Hidden,
  96. cl::desc("Last chance recoloring maximum number of considered"
  97. " interference at a time"),
  98. cl::init(8));
  99. static cl::opt<bool> ExhaustiveSearch(
  100. "exhaustive-register-search", cl::NotHidden,
  101. cl::desc("Exhaustive Search for registers bypassing the depth "
  102. "and interference cutoffs of last chance recoloring"),
  103. cl::Hidden);
  104. static cl::opt<bool> EnableLocalReassignment(
  105. "enable-local-reassign", cl::Hidden,
  106. cl::desc("Local reassignment can yield better allocation decisions, but "
  107. "may be compile time intensive"),
  108. cl::init(false));
  109. static cl::opt<bool> EnableDeferredSpilling(
  110. "enable-deferred-spilling", cl::Hidden,
  111. cl::desc("Instead of spilling a variable right away, defer the actual "
  112. "code insertion to the end of the allocation. That way the "
  113. "allocator might still find a suitable coloring for this "
  114. "variable because of other evicted variables."),
  115. cl::init(false));
  116. static cl::opt<unsigned>
  117. HugeSizeForSplit("huge-size-for-split", cl::Hidden,
  118. cl::desc("A threshold of live range size which may cause "
  119. "high compile time cost in global splitting."),
  120. cl::init(5000));
  121. // FIXME: Find a good default for this flag and remove the flag.
  122. static cl::opt<unsigned>
  123. CSRFirstTimeCost("regalloc-csr-first-time-cost",
  124. cl::desc("Cost for first time use of callee-saved register."),
  125. cl::init(0), cl::Hidden);
  126. static cl::opt<bool> ConsiderLocalIntervalCost(
  127. "consider-local-interval-cost", cl::Hidden,
  128. cl::desc("Consider the cost of local intervals created by a split "
  129. "candidate when choosing the best split candidate."),
  130. cl::init(false));
  131. static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
  132. createGreedyRegisterAllocator);
  133. namespace {
  134. class RAGreedy : public MachineFunctionPass,
  135. public RegAllocBase,
  136. private LiveRangeEdit::Delegate {
  137. // Convenient shortcuts.
  138. using PQueue = std::priority_queue<std::pair<unsigned, unsigned>>;
  139. using SmallLISet = SmallPtrSet<LiveInterval *, 4>;
  140. using SmallVirtRegSet = SmallSet<unsigned, 16>;
  141. // context
  142. MachineFunction *MF;
  143. // Shortcuts to some useful interface.
  144. const TargetInstrInfo *TII;
  145. const TargetRegisterInfo *TRI;
  146. RegisterClassInfo RCI;
  147. // analyses
  148. SlotIndexes *Indexes;
  149. MachineBlockFrequencyInfo *MBFI;
  150. MachineDominatorTree *DomTree;
  151. MachineLoopInfo *Loops;
  152. MachineOptimizationRemarkEmitter *ORE;
  153. EdgeBundles *Bundles;
  154. SpillPlacement *SpillPlacer;
  155. LiveDebugVariables *DebugVars;
  156. AliasAnalysis *AA;
  157. // state
  158. std::unique_ptr<Spiller> SpillerInstance;
  159. PQueue Queue;
  160. unsigned NextCascade;
  161. // Live ranges pass through a number of stages as we try to allocate them.
  162. // Some of the stages may also create new live ranges:
  163. //
  164. // - Region splitting.
  165. // - Per-block splitting.
  166. // - Local splitting.
  167. // - Spilling.
  168. //
  169. // Ranges produced by one of the stages skip the previous stages when they are
  170. // dequeued. This improves performance because we can skip interference checks
  171. // that are unlikely to give any results. It also guarantees that the live
  172. // range splitting algorithm terminates, something that is otherwise hard to
  173. // ensure.
  174. enum LiveRangeStage {
  175. /// Newly created live range that has never been queued.
  176. RS_New,
  177. /// Only attempt assignment and eviction. Then requeue as RS_Split.
  178. RS_Assign,
  179. /// Attempt live range splitting if assignment is impossible.
  180. RS_Split,
  181. /// Attempt more aggressive live range splitting that is guaranteed to make
  182. /// progress. This is used for split products that may not be making
  183. /// progress.
  184. RS_Split2,
  185. /// Live range will be spilled. No more splitting will be attempted.
  186. RS_Spill,
  187. /// Live range is in memory. Because of other evictions, it might get moved
  188. /// in a register in the end.
  189. RS_Memory,
  190. /// There is nothing more we can do to this live range. Abort compilation
  191. /// if it can't be assigned.
  192. RS_Done
  193. };
  194. // Enum CutOffStage to keep a track whether the register allocation failed
  195. // because of the cutoffs encountered in last chance recoloring.
  196. // Note: This is used as bitmask. New value should be next power of 2.
  197. enum CutOffStage {
  198. // No cutoffs encountered
  199. CO_None = 0,
  200. // lcr-max-depth cutoff encountered
  201. CO_Depth = 1,
  202. // lcr-max-interf cutoff encountered
  203. CO_Interf = 2
  204. };
  205. uint8_t CutOffInfo;
  206. #ifndef NDEBUG
  207. static const char *const StageName[];
  208. #endif
  209. // RegInfo - Keep additional information about each live range.
  210. struct RegInfo {
  211. LiveRangeStage Stage = RS_New;
  212. // Cascade - Eviction loop prevention. See canEvictInterference().
  213. unsigned Cascade = 0;
  214. RegInfo() = default;
  215. };
  216. IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo;
  217. LiveRangeStage getStage(const LiveInterval &VirtReg) const {
  218. return ExtraRegInfo[VirtReg.reg].Stage;
  219. }
  220. void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) {
  221. ExtraRegInfo.resize(MRI->getNumVirtRegs());
  222. ExtraRegInfo[VirtReg.reg].Stage = Stage;
  223. }
  224. template<typename Iterator>
  225. void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) {
  226. ExtraRegInfo.resize(MRI->getNumVirtRegs());
  227. for (;Begin != End; ++Begin) {
  228. unsigned Reg = *Begin;
  229. if (ExtraRegInfo[Reg].Stage == RS_New)
  230. ExtraRegInfo[Reg].Stage = NewStage;
  231. }
  232. }
  233. /// Cost of evicting interference.
  234. struct EvictionCost {
  235. unsigned BrokenHints = 0; ///< Total number of broken hints.
  236. float MaxWeight = 0; ///< Maximum spill weight evicted.
  237. EvictionCost() = default;
  238. bool isMax() const { return BrokenHints == ~0u; }
  239. void setMax() { BrokenHints = ~0u; }
  240. void setBrokenHints(unsigned NHints) { BrokenHints = NHints; }
  241. bool operator<(const EvictionCost &O) const {
  242. return std::tie(BrokenHints, MaxWeight) <
  243. std::tie(O.BrokenHints, O.MaxWeight);
  244. }
  245. };
  246. /// EvictionTrack - Keeps track of past evictions in order to optimize region
  247. /// split decision.
  248. class EvictionTrack {
  249. public:
  250. using EvictorInfo =
  251. std::pair<unsigned /* evictor */, unsigned /* physreg */>;
  252. using EvicteeInfo = llvm::DenseMap<unsigned /* evictee */, EvictorInfo>;
  253. private:
  254. /// Each Vreg that has been evicted in the last stage of selectOrSplit will
  255. /// be mapped to the evictor Vreg and the PhysReg it was evicted from.
  256. EvicteeInfo Evictees;
  257. public:
  258. /// Clear all eviction information.
  259. void clear() { Evictees.clear(); }
  260. /// Clear eviction information for the given evictee Vreg.
  261. /// E.g. when Vreg get's a new allocation, the old eviction info is no
  262. /// longer relevant.
  263. /// \param Evictee The evictee Vreg for whom we want to clear collected
  264. /// eviction info.
  265. void clearEvicteeInfo(unsigned Evictee) { Evictees.erase(Evictee); }
  266. /// Track new eviction.
  267. /// The Evictor vreg has evicted the Evictee vreg from Physreg.
  268. /// \param PhysReg The physical register Evictee was evicted from.
  269. /// \param Evictor The evictor Vreg that evicted Evictee.
  270. /// \param Evictee The evictee Vreg.
  271. void addEviction(unsigned PhysReg, unsigned Evictor, unsigned Evictee) {
  272. Evictees[Evictee].first = Evictor;
  273. Evictees[Evictee].second = PhysReg;
  274. }
  275. /// Return the Evictor Vreg which evicted Evictee Vreg from PhysReg.
  276. /// \param Evictee The evictee vreg.
  277. /// \return The Evictor vreg which evicted Evictee vreg from PhysReg. 0 if
  278. /// nobody has evicted Evictee from PhysReg.
  279. EvictorInfo getEvictor(unsigned Evictee) {
  280. if (Evictees.count(Evictee)) {
  281. return Evictees[Evictee];
  282. }
  283. return EvictorInfo(0, 0);
  284. }
  285. };
  286. // Keeps track of past evictions in order to optimize region split decision.
  287. EvictionTrack LastEvicted;
  288. // splitting state.
  289. std::unique_ptr<SplitAnalysis> SA;
  290. std::unique_ptr<SplitEditor> SE;
  291. /// Cached per-block interference maps
  292. InterferenceCache IntfCache;
  293. /// All basic blocks where the current register has uses.
  294. SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints;
  295. /// Global live range splitting candidate info.
  296. struct GlobalSplitCandidate {
  297. // Register intended for assignment, or 0.
  298. unsigned PhysReg;
  299. // SplitKit interval index for this candidate.
  300. unsigned IntvIdx;
  301. // Interference for PhysReg.
  302. InterferenceCache::Cursor Intf;
  303. // Bundles where this candidate should be live.
  304. BitVector LiveBundles;
  305. SmallVector<unsigned, 8> ActiveBlocks;
  306. void reset(InterferenceCache &Cache, unsigned Reg) {
  307. PhysReg = Reg;
  308. IntvIdx = 0;
  309. Intf.setPhysReg(Cache, Reg);
  310. LiveBundles.clear();
  311. ActiveBlocks.clear();
  312. }
  313. // Set B[i] = C for every live bundle where B[i] was NoCand.
  314. unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) {
  315. unsigned Count = 0;
  316. for (unsigned i : LiveBundles.set_bits())
  317. if (B[i] == NoCand) {
  318. B[i] = C;
  319. Count++;
  320. }
  321. return Count;
  322. }
  323. };
  324. /// Candidate info for each PhysReg in AllocationOrder.
  325. /// This vector never shrinks, but grows to the size of the largest register
  326. /// class.
  327. SmallVector<GlobalSplitCandidate, 32> GlobalCand;
  328. enum : unsigned { NoCand = ~0u };
  329. /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to
  330. /// NoCand which indicates the stack interval.
  331. SmallVector<unsigned, 32> BundleCand;
  332. /// Callee-save register cost, calculated once per machine function.
  333. BlockFrequency CSRCost;
  334. /// Run or not the local reassignment heuristic. This information is
  335. /// obtained from the TargetSubtargetInfo.
  336. bool EnableLocalReassign;
  337. /// Enable or not the consideration of the cost of local intervals created
  338. /// by a split candidate when choosing the best split candidate.
  339. bool EnableAdvancedRASplitCost;
  340. /// Set of broken hints that may be reconciled later because of eviction.
  341. SmallSetVector<LiveInterval *, 8> SetOfBrokenHints;
  342. public:
  343. RAGreedy();
  344. /// Return the pass name.
  345. StringRef getPassName() const override { return "Greedy Register Allocator"; }
  346. /// RAGreedy analysis usage.
  347. void getAnalysisUsage(AnalysisUsage &AU) const override;
  348. void releaseMemory() override;
  349. Spiller &spiller() override { return *SpillerInstance; }
  350. void enqueue(LiveInterval *LI) override;
  351. LiveInterval *dequeue() override;
  352. unsigned selectOrSplit(LiveInterval&, SmallVectorImpl<unsigned>&) override;
  353. void aboutToRemoveInterval(LiveInterval &) override;
  354. /// Perform register allocation.
  355. bool runOnMachineFunction(MachineFunction &mf) override;
  356. MachineFunctionProperties getRequiredProperties() const override {
  357. return MachineFunctionProperties().set(
  358. MachineFunctionProperties::Property::NoPHIs);
  359. }
  360. static char ID;
  361. private:
  362. unsigned selectOrSplitImpl(LiveInterval &, SmallVectorImpl<unsigned> &,
  363. SmallVirtRegSet &, unsigned = 0);
  364. bool LRE_CanEraseVirtReg(unsigned) override;
  365. void LRE_WillShrinkVirtReg(unsigned) override;
  366. void LRE_DidCloneVirtReg(unsigned, unsigned) override;
  367. void enqueue(PQueue &CurQueue, LiveInterval *LI);
  368. LiveInterval *dequeue(PQueue &CurQueue);
  369. BlockFrequency calcSpillCost();
  370. bool addSplitConstraints(InterferenceCache::Cursor, BlockFrequency&);
  371. bool addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>);
  372. bool growRegion(GlobalSplitCandidate &Cand);
  373. bool splitCanCauseEvictionChain(unsigned Evictee, GlobalSplitCandidate &Cand,
  374. unsigned BBNumber,
  375. const AllocationOrder &Order);
  376. bool splitCanCauseLocalSpill(unsigned VirtRegToSplit,
  377. GlobalSplitCandidate &Cand, unsigned BBNumber,
  378. const AllocationOrder &Order);
  379. BlockFrequency calcGlobalSplitCost(GlobalSplitCandidate &,
  380. const AllocationOrder &Order,
  381. bool *CanCauseEvictionChain);
  382. bool calcCompactRegion(GlobalSplitCandidate&);
  383. void splitAroundRegion(LiveRangeEdit&, ArrayRef<unsigned>);
  384. void calcGapWeights(unsigned, SmallVectorImpl<float>&);
  385. unsigned canReassign(LiveInterval &VirtReg, unsigned PrevReg);
  386. bool shouldEvict(LiveInterval &A, bool, LiveInterval &B, bool);
  387. bool canEvictInterference(LiveInterval&, unsigned, bool, EvictionCost&,
  388. const SmallVirtRegSet&);
  389. bool canEvictInterferenceInRange(LiveInterval &VirtReg, unsigned PhysReg,
  390. SlotIndex Start, SlotIndex End,
  391. EvictionCost &MaxCost);
  392. unsigned getCheapestEvicteeWeight(const AllocationOrder &Order,
  393. LiveInterval &VirtReg, SlotIndex Start,
  394. SlotIndex End, float *BestEvictWeight);
  395. void evictInterference(LiveInterval&, unsigned,
  396. SmallVectorImpl<unsigned>&);
  397. bool mayRecolorAllInterferences(unsigned PhysReg, LiveInterval &VirtReg,
  398. SmallLISet &RecoloringCandidates,
  399. const SmallVirtRegSet &FixedRegisters);
  400. unsigned tryAssign(LiveInterval&, AllocationOrder&,
  401. SmallVectorImpl<unsigned>&,
  402. const SmallVirtRegSet&);
  403. unsigned tryEvict(LiveInterval&, AllocationOrder&,
  404. SmallVectorImpl<unsigned>&, unsigned,
  405. const SmallVirtRegSet&);
  406. unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
  407. SmallVectorImpl<unsigned>&);
  408. unsigned isSplitBenefitWorthCost(LiveInterval &VirtReg);
  409. /// Calculate cost of region splitting.
  410. unsigned calculateRegionSplitCost(LiveInterval &VirtReg,
  411. AllocationOrder &Order,
  412. BlockFrequency &BestCost,
  413. unsigned &NumCands, bool IgnoreCSR,
  414. bool *CanCauseEvictionChain = nullptr);
  415. /// Perform region splitting.
  416. unsigned doRegionSplit(LiveInterval &VirtReg, unsigned BestCand,
  417. bool HasCompact,
  418. SmallVectorImpl<unsigned> &NewVRegs);
  419. /// Check other options before using a callee-saved register for the first
  420. /// time.
  421. unsigned tryAssignCSRFirstTime(LiveInterval &VirtReg, AllocationOrder &Order,
  422. unsigned PhysReg, unsigned &CostPerUseLimit,
  423. SmallVectorImpl<unsigned> &NewVRegs);
  424. void initializeCSRCost();
  425. unsigned tryBlockSplit(LiveInterval&, AllocationOrder&,
  426. SmallVectorImpl<unsigned>&);
  427. unsigned tryInstructionSplit(LiveInterval&, AllocationOrder&,
  428. SmallVectorImpl<unsigned>&);
  429. unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
  430. SmallVectorImpl<unsigned>&);
  431. unsigned trySplit(LiveInterval&, AllocationOrder&,
  432. SmallVectorImpl<unsigned>&,
  433. const SmallVirtRegSet&);
  434. unsigned tryLastChanceRecoloring(LiveInterval &, AllocationOrder &,
  435. SmallVectorImpl<unsigned> &,
  436. SmallVirtRegSet &, unsigned);
  437. bool tryRecoloringCandidates(PQueue &, SmallVectorImpl<unsigned> &,
  438. SmallVirtRegSet &, unsigned);
  439. void tryHintRecoloring(LiveInterval &);
  440. void tryHintsRecoloring();
  441. /// Model the information carried by one end of a copy.
  442. struct HintInfo {
  443. /// The frequency of the copy.
  444. BlockFrequency Freq;
  445. /// The virtual register or physical register.
  446. unsigned Reg;
  447. /// Its currently assigned register.
  448. /// In case of a physical register Reg == PhysReg.
  449. unsigned PhysReg;
  450. HintInfo(BlockFrequency Freq, unsigned Reg, unsigned PhysReg)
  451. : Freq(Freq), Reg(Reg), PhysReg(PhysReg) {}
  452. };
  453. using HintsInfo = SmallVector<HintInfo, 4>;
  454. BlockFrequency getBrokenHintFreq(const HintsInfo &, unsigned);
  455. void collectHintInfo(unsigned, HintsInfo &);
  456. bool isUnusedCalleeSavedReg(unsigned PhysReg) const;
  457. /// Compute and report the number of spills and reloads for a loop.
  458. void reportNumberOfSplillsReloads(MachineLoop *L, unsigned &Reloads,
  459. unsigned &FoldedReloads, unsigned &Spills,
  460. unsigned &FoldedSpills);
  461. /// Report the number of spills and reloads for each loop.
  462. void reportNumberOfSplillsReloads() {
  463. for (MachineLoop *L : *Loops) {
  464. unsigned Reloads, FoldedReloads, Spills, FoldedSpills;
  465. reportNumberOfSplillsReloads(L, Reloads, FoldedReloads, Spills,
  466. FoldedSpills);
  467. }
  468. }
  469. };
  470. } // end anonymous namespace
  471. char RAGreedy::ID = 0;
  472. char &llvm::RAGreedyID = RAGreedy::ID;
  473. INITIALIZE_PASS_BEGIN(RAGreedy, "greedy",
  474. "Greedy Register Allocator", false, false)
  475. INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
  476. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  477. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  478. INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
  479. INITIALIZE_PASS_DEPENDENCY(MachineScheduler)
  480. INITIALIZE_PASS_DEPENDENCY(LiveStacks)
  481. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  482. INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
  483. INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
  484. INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
  485. INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
  486. INITIALIZE_PASS_DEPENDENCY(SpillPlacement)
  487. INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
  488. INITIALIZE_PASS_END(RAGreedy, "greedy",
  489. "Greedy Register Allocator", false, false)
  490. #ifndef NDEBUG
  491. const char *const RAGreedy::StageName[] = {
  492. "RS_New",
  493. "RS_Assign",
  494. "RS_Split",
  495. "RS_Split2",
  496. "RS_Spill",
  497. "RS_Memory",
  498. "RS_Done"
  499. };
  500. #endif
  501. // Hysteresis to use when comparing floats.
  502. // This helps stabilize decisions based on float comparisons.
  503. const float Hysteresis = (2007 / 2048.0f); // 0.97998046875
  504. FunctionPass* llvm::createGreedyRegisterAllocator() {
  505. return new RAGreedy();
  506. }
  507. RAGreedy::RAGreedy(): MachineFunctionPass(ID) {
  508. }
  509. void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
  510. AU.setPreservesCFG();
  511. AU.addRequired<MachineBlockFrequencyInfo>();
  512. AU.addPreserved<MachineBlockFrequencyInfo>();
  513. AU.addRequired<AAResultsWrapperPass>();
  514. AU.addPreserved<AAResultsWrapperPass>();
  515. AU.addRequired<LiveIntervals>();
  516. AU.addPreserved<LiveIntervals>();
  517. AU.addRequired<SlotIndexes>();
  518. AU.addPreserved<SlotIndexes>();
  519. AU.addRequired<LiveDebugVariables>();
  520. AU.addPreserved<LiveDebugVariables>();
  521. AU.addRequired<LiveStacks>();
  522. AU.addPreserved<LiveStacks>();
  523. AU.addRequired<MachineDominatorTree>();
  524. AU.addPreserved<MachineDominatorTree>();
  525. AU.addRequired<MachineLoopInfo>();
  526. AU.addPreserved<MachineLoopInfo>();
  527. AU.addRequired<VirtRegMap>();
  528. AU.addPreserved<VirtRegMap>();
  529. AU.addRequired<LiveRegMatrix>();
  530. AU.addPreserved<LiveRegMatrix>();
  531. AU.addRequired<EdgeBundles>();
  532. AU.addRequired<SpillPlacement>();
  533. AU.addRequired<MachineOptimizationRemarkEmitterPass>();
  534. MachineFunctionPass::getAnalysisUsage(AU);
  535. }
  536. //===----------------------------------------------------------------------===//
  537. // LiveRangeEdit delegate methods
  538. //===----------------------------------------------------------------------===//
  539. bool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) {
  540. LiveInterval &LI = LIS->getInterval(VirtReg);
  541. if (VRM->hasPhys(VirtReg)) {
  542. Matrix->unassign(LI);
  543. aboutToRemoveInterval(LI);
  544. return true;
  545. }
  546. // Unassigned virtreg is probably in the priority queue.
  547. // RegAllocBase will erase it after dequeueing.
  548. // Nonetheless, clear the live-range so that the debug
  549. // dump will show the right state for that VirtReg.
  550. LI.clear();
  551. return false;
  552. }
  553. void RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) {
  554. if (!VRM->hasPhys(VirtReg))
  555. return;
  556. // Register is assigned, put it back on the queue for reassignment.
  557. LiveInterval &LI = LIS->getInterval(VirtReg);
  558. Matrix->unassign(LI);
  559. enqueue(&LI);
  560. }
  561. void RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) {
  562. // Cloning a register we haven't even heard about yet? Just ignore it.
  563. if (!ExtraRegInfo.inBounds(Old))
  564. return;
  565. // LRE may clone a virtual register because dead code elimination causes it to
  566. // be split into connected components. The new components are much smaller
  567. // than the original, so they should get a new chance at being assigned.
  568. // same stage as the parent.
  569. ExtraRegInfo[Old].Stage = RS_Assign;
  570. ExtraRegInfo.grow(New);
  571. ExtraRegInfo[New] = ExtraRegInfo[Old];
  572. }
  573. void RAGreedy::releaseMemory() {
  574. SpillerInstance.reset();
  575. ExtraRegInfo.clear();
  576. GlobalCand.clear();
  577. }
  578. void RAGreedy::enqueue(LiveInterval *LI) { enqueue(Queue, LI); }
  579. void RAGreedy::enqueue(PQueue &CurQueue, LiveInterval *LI) {
  580. // Prioritize live ranges by size, assigning larger ranges first.
  581. // The queue holds (size, reg) pairs.
  582. const unsigned Size = LI->getSize();
  583. const unsigned Reg = LI->reg;
  584. assert(Register::isVirtualRegister(Reg) &&
  585. "Can only enqueue virtual registers");
  586. unsigned Prio;
  587. ExtraRegInfo.grow(Reg);
  588. if (ExtraRegInfo[Reg].Stage == RS_New)
  589. ExtraRegInfo[Reg].Stage = RS_Assign;
  590. if (ExtraRegInfo[Reg].Stage == RS_Split) {
  591. // Unsplit ranges that couldn't be allocated immediately are deferred until
  592. // everything else has been allocated.
  593. Prio = Size;
  594. } else if (ExtraRegInfo[Reg].Stage == RS_Memory) {
  595. // Memory operand should be considered last.
  596. // Change the priority such that Memory operand are assigned in
  597. // the reverse order that they came in.
  598. // TODO: Make this a member variable and probably do something about hints.
  599. static unsigned MemOp = 0;
  600. Prio = MemOp++;
  601. } else {
  602. // Giant live ranges fall back to the global assignment heuristic, which
  603. // prevents excessive spilling in pathological cases.
  604. bool ReverseLocal = TRI->reverseLocalAssignment();
  605. const TargetRegisterClass &RC = *MRI->getRegClass(Reg);
  606. bool ForceGlobal = !ReverseLocal &&
  607. (Size / SlotIndex::InstrDist) > (2 * RC.getNumRegs());
  608. if (ExtraRegInfo[Reg].Stage == RS_Assign && !ForceGlobal && !LI->empty() &&
  609. LIS->intervalIsInOneMBB(*LI)) {
  610. // Allocate original local ranges in linear instruction order. Since they
  611. // are singly defined, this produces optimal coloring in the absence of
  612. // global interference and other constraints.
  613. if (!ReverseLocal)
  614. Prio = LI->beginIndex().getInstrDistance(Indexes->getLastIndex());
  615. else {
  616. // Allocating bottom up may allow many short LRGs to be assigned first
  617. // to one of the cheap registers. This could be much faster for very
  618. // large blocks on targets with many physical registers.
  619. Prio = Indexes->getZeroIndex().getInstrDistance(LI->endIndex());
  620. }
  621. Prio |= RC.AllocationPriority << 24;
  622. } else {
  623. // Allocate global and split ranges in long->short order. Long ranges that
  624. // don't fit should be spilled (or split) ASAP so they don't create
  625. // interference. Mark a bit to prioritize global above local ranges.
  626. Prio = (1u << 29) + Size;
  627. }
  628. // Mark a higher bit to prioritize global and local above RS_Split.
  629. Prio |= (1u << 31);
  630. // Boost ranges that have a physical register hint.
  631. if (VRM->hasKnownPreference(Reg))
  632. Prio |= (1u << 30);
  633. }
  634. // The virtual register number is a tie breaker for same-sized ranges.
  635. // Give lower vreg numbers higher priority to assign them first.
  636. CurQueue.push(std::make_pair(Prio, ~Reg));
  637. }
  638. LiveInterval *RAGreedy::dequeue() { return dequeue(Queue); }
  639. LiveInterval *RAGreedy::dequeue(PQueue &CurQueue) {
  640. if (CurQueue.empty())
  641. return nullptr;
  642. LiveInterval *LI = &LIS->getInterval(~CurQueue.top().second);
  643. CurQueue.pop();
  644. return LI;
  645. }
  646. //===----------------------------------------------------------------------===//
  647. // Direct Assignment
  648. //===----------------------------------------------------------------------===//
  649. /// tryAssign - Try to assign VirtReg to an available register.
  650. unsigned RAGreedy::tryAssign(LiveInterval &VirtReg,
  651. AllocationOrder &Order,
  652. SmallVectorImpl<unsigned> &NewVRegs,
  653. const SmallVirtRegSet &FixedRegisters) {
  654. Order.rewind();
  655. unsigned PhysReg;
  656. while ((PhysReg = Order.next()))
  657. if (!Matrix->checkInterference(VirtReg, PhysReg))
  658. break;
  659. if (!PhysReg || Order.isHint())
  660. return PhysReg;
  661. // PhysReg is available, but there may be a better choice.
  662. // If we missed a simple hint, try to cheaply evict interference from the
  663. // preferred register.
  664. if (unsigned Hint = MRI->getSimpleHint(VirtReg.reg))
  665. if (Order.isHint(Hint)) {
  666. LLVM_DEBUG(dbgs() << "missed hint " << printReg(Hint, TRI) << '\n');
  667. EvictionCost MaxCost;
  668. MaxCost.setBrokenHints(1);
  669. if (canEvictInterference(VirtReg, Hint, true, MaxCost, FixedRegisters)) {
  670. evictInterference(VirtReg, Hint, NewVRegs);
  671. return Hint;
  672. }
  673. // Record the missed hint, we may be able to recover
  674. // at the end if the surrounding allocation changed.
  675. SetOfBrokenHints.insert(&VirtReg);
  676. }
  677. // Try to evict interference from a cheaper alternative.
  678. unsigned Cost = TRI->getCostPerUse(PhysReg);
  679. // Most registers have 0 additional cost.
  680. if (!Cost)
  681. return PhysReg;
  682. LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is available at cost "
  683. << Cost << '\n');
  684. unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost, FixedRegisters);
  685. return CheapReg ? CheapReg : PhysReg;
  686. }
  687. //===----------------------------------------------------------------------===//
  688. // Interference eviction
  689. //===----------------------------------------------------------------------===//
  690. unsigned RAGreedy::canReassign(LiveInterval &VirtReg, unsigned PrevReg) {
  691. AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix);
  692. unsigned PhysReg;
  693. while ((PhysReg = Order.next())) {
  694. if (PhysReg == PrevReg)
  695. continue;
  696. MCRegUnitIterator Units(PhysReg, TRI);
  697. for (; Units.isValid(); ++Units) {
  698. // Instantiate a "subquery", not to be confused with the Queries array.
  699. LiveIntervalUnion::Query subQ(VirtReg, Matrix->getLiveUnions()[*Units]);
  700. if (subQ.checkInterference())
  701. break;
  702. }
  703. // If no units have interference, break out with the current PhysReg.
  704. if (!Units.isValid())
  705. break;
  706. }
  707. if (PhysReg)
  708. LLVM_DEBUG(dbgs() << "can reassign: " << VirtReg << " from "
  709. << printReg(PrevReg, TRI) << " to "
  710. << printReg(PhysReg, TRI) << '\n');
  711. return PhysReg;
  712. }
  713. /// shouldEvict - determine if A should evict the assigned live range B. The
  714. /// eviction policy defined by this function together with the allocation order
  715. /// defined by enqueue() decides which registers ultimately end up being split
  716. /// and spilled.
  717. ///
  718. /// Cascade numbers are used to prevent infinite loops if this function is a
  719. /// cyclic relation.
  720. ///
  721. /// @param A The live range to be assigned.
  722. /// @param IsHint True when A is about to be assigned to its preferred
  723. /// register.
  724. /// @param B The live range to be evicted.
  725. /// @param BreaksHint True when B is already assigned to its preferred register.
  726. bool RAGreedy::shouldEvict(LiveInterval &A, bool IsHint,
  727. LiveInterval &B, bool BreaksHint) {
  728. bool CanSplit = getStage(B) < RS_Spill;
  729. // Be fairly aggressive about following hints as long as the evictee can be
  730. // split.
  731. if (CanSplit && IsHint && !BreaksHint)
  732. return true;
  733. if (A.weight > B.weight) {
  734. LLVM_DEBUG(dbgs() << "should evict: " << B << " w= " << B.weight << '\n');
  735. return true;
  736. }
  737. return false;
  738. }
  739. /// canEvictInterference - Return true if all interferences between VirtReg and
  740. /// PhysReg can be evicted.
  741. ///
  742. /// @param VirtReg Live range that is about to be assigned.
  743. /// @param PhysReg Desired register for assignment.
  744. /// @param IsHint True when PhysReg is VirtReg's preferred register.
  745. /// @param MaxCost Only look for cheaper candidates and update with new cost
  746. /// when returning true.
  747. /// @returns True when interference can be evicted cheaper than MaxCost.
  748. bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg,
  749. bool IsHint, EvictionCost &MaxCost,
  750. const SmallVirtRegSet &FixedRegisters) {
  751. // It is only possible to evict virtual register interference.
  752. if (Matrix->checkInterference(VirtReg, PhysReg) > LiveRegMatrix::IK_VirtReg)
  753. return false;
  754. bool IsLocal = LIS->intervalIsInOneMBB(VirtReg);
  755. // Find VirtReg's cascade number. This will be unassigned if VirtReg was never
  756. // involved in an eviction before. If a cascade number was assigned, deny
  757. // evicting anything with the same or a newer cascade number. This prevents
  758. // infinite eviction loops.
  759. //
  760. // This works out so a register without a cascade number is allowed to evict
  761. // anything, and it can be evicted by anything.
  762. unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
  763. if (!Cascade)
  764. Cascade = NextCascade;
  765. EvictionCost Cost;
  766. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  767. LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
  768. // If there is 10 or more interferences, chances are one is heavier.
  769. if (Q.collectInterferingVRegs(10) >= 10)
  770. return false;
  771. // Check if any interfering live range is heavier than MaxWeight.
  772. for (unsigned i = Q.interferingVRegs().size(); i; --i) {
  773. LiveInterval *Intf = Q.interferingVRegs()[i - 1];
  774. assert(Register::isVirtualRegister(Intf->reg) &&
  775. "Only expecting virtual register interference from query");
  776. // Do not allow eviction of a virtual register if we are in the middle
  777. // of last-chance recoloring and this virtual register is one that we
  778. // have scavenged a physical register for.
  779. if (FixedRegisters.count(Intf->reg))
  780. return false;
  781. // Never evict spill products. They cannot split or spill.
  782. if (getStage(*Intf) == RS_Done)
  783. return false;
  784. // Once a live range becomes small enough, it is urgent that we find a
  785. // register for it. This is indicated by an infinite spill weight. These
  786. // urgent live ranges get to evict almost anything.
  787. //
  788. // Also allow urgent evictions of unspillable ranges from a strictly
  789. // larger allocation order.
  790. bool Urgent = !VirtReg.isSpillable() &&
  791. (Intf->isSpillable() ||
  792. RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(VirtReg.reg)) <
  793. RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(Intf->reg)));
  794. // Only evict older cascades or live ranges without a cascade.
  795. unsigned IntfCascade = ExtraRegInfo[Intf->reg].Cascade;
  796. if (Cascade <= IntfCascade) {
  797. if (!Urgent)
  798. return false;
  799. // We permit breaking cascades for urgent evictions. It should be the
  800. // last resort, though, so make it really expensive.
  801. Cost.BrokenHints += 10;
  802. }
  803. // Would this break a satisfied hint?
  804. bool BreaksHint = VRM->hasPreferredPhys(Intf->reg);
  805. // Update eviction cost.
  806. Cost.BrokenHints += BreaksHint;
  807. Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight);
  808. // Abort if this would be too expensive.
  809. if (!(Cost < MaxCost))
  810. return false;
  811. if (Urgent)
  812. continue;
  813. // Apply the eviction policy for non-urgent evictions.
  814. if (!shouldEvict(VirtReg, IsHint, *Intf, BreaksHint))
  815. return false;
  816. // If !MaxCost.isMax(), then we're just looking for a cheap register.
  817. // Evicting another local live range in this case could lead to suboptimal
  818. // coloring.
  819. if (!MaxCost.isMax() && IsLocal && LIS->intervalIsInOneMBB(*Intf) &&
  820. (!EnableLocalReassign || !canReassign(*Intf, PhysReg))) {
  821. return false;
  822. }
  823. }
  824. }
  825. MaxCost = Cost;
  826. return true;
  827. }
  828. /// Return true if all interferences between VirtReg and PhysReg between
  829. /// Start and End can be evicted.
  830. ///
  831. /// \param VirtReg Live range that is about to be assigned.
  832. /// \param PhysReg Desired register for assignment.
  833. /// \param Start Start of range to look for interferences.
  834. /// \param End End of range to look for interferences.
  835. /// \param MaxCost Only look for cheaper candidates and update with new cost
  836. /// when returning true.
  837. /// \return True when interference can be evicted cheaper than MaxCost.
  838. bool RAGreedy::canEvictInterferenceInRange(LiveInterval &VirtReg,
  839. unsigned PhysReg, SlotIndex Start,
  840. SlotIndex End,
  841. EvictionCost &MaxCost) {
  842. EvictionCost Cost;
  843. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  844. LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
  845. // Check if any interfering live range is heavier than MaxWeight.
  846. for (unsigned i = Q.interferingVRegs().size(); i; --i) {
  847. LiveInterval *Intf = Q.interferingVRegs()[i - 1];
  848. // Check if interference overlast the segment in interest.
  849. if (!Intf->overlaps(Start, End))
  850. continue;
  851. // Cannot evict non virtual reg interference.
  852. if (!Register::isVirtualRegister(Intf->reg))
  853. return false;
  854. // Never evict spill products. They cannot split or spill.
  855. if (getStage(*Intf) == RS_Done)
  856. return false;
  857. // Would this break a satisfied hint?
  858. bool BreaksHint = VRM->hasPreferredPhys(Intf->reg);
  859. // Update eviction cost.
  860. Cost.BrokenHints += BreaksHint;
  861. Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight);
  862. // Abort if this would be too expensive.
  863. if (!(Cost < MaxCost))
  864. return false;
  865. }
  866. }
  867. if (Cost.MaxWeight == 0)
  868. return false;
  869. MaxCost = Cost;
  870. return true;
  871. }
  872. /// Return the physical register that will be best
  873. /// candidate for eviction by a local split interval that will be created
  874. /// between Start and End.
  875. ///
  876. /// \param Order The allocation order
  877. /// \param VirtReg Live range that is about to be assigned.
  878. /// \param Start Start of range to look for interferences
  879. /// \param End End of range to look for interferences
  880. /// \param BestEvictweight The eviction cost of that eviction
  881. /// \return The PhysReg which is the best candidate for eviction and the
  882. /// eviction cost in BestEvictweight
  883. unsigned RAGreedy::getCheapestEvicteeWeight(const AllocationOrder &Order,
  884. LiveInterval &VirtReg,
  885. SlotIndex Start, SlotIndex End,
  886. float *BestEvictweight) {
  887. EvictionCost BestEvictCost;
  888. BestEvictCost.setMax();
  889. BestEvictCost.MaxWeight = VirtReg.weight;
  890. unsigned BestEvicteePhys = 0;
  891. // Go over all physical registers and find the best candidate for eviction
  892. for (auto PhysReg : Order.getOrder()) {
  893. if (!canEvictInterferenceInRange(VirtReg, PhysReg, Start, End,
  894. BestEvictCost))
  895. continue;
  896. // Best so far.
  897. BestEvicteePhys = PhysReg;
  898. }
  899. *BestEvictweight = BestEvictCost.MaxWeight;
  900. return BestEvicteePhys;
  901. }
  902. /// evictInterference - Evict any interferring registers that prevent VirtReg
  903. /// from being assigned to Physreg. This assumes that canEvictInterference
  904. /// returned true.
  905. void RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg,
  906. SmallVectorImpl<unsigned> &NewVRegs) {
  907. // Make sure that VirtReg has a cascade number, and assign that cascade
  908. // number to every evicted register. These live ranges than then only be
  909. // evicted by a newer cascade, preventing infinite loops.
  910. unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
  911. if (!Cascade)
  912. Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++;
  913. LLVM_DEBUG(dbgs() << "evicting " << printReg(PhysReg, TRI)
  914. << " interference: Cascade " << Cascade << '\n');
  915. // Collect all interfering virtregs first.
  916. SmallVector<LiveInterval*, 8> Intfs;
  917. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  918. LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
  919. // We usually have the interfering VRegs cached so collectInterferingVRegs()
  920. // should be fast, we may need to recalculate if when different physregs
  921. // overlap the same register unit so we had different SubRanges queried
  922. // against it.
  923. Q.collectInterferingVRegs();
  924. ArrayRef<LiveInterval*> IVR = Q.interferingVRegs();
  925. Intfs.append(IVR.begin(), IVR.end());
  926. }
  927. // Evict them second. This will invalidate the queries.
  928. for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
  929. LiveInterval *Intf = Intfs[i];
  930. // The same VirtReg may be present in multiple RegUnits. Skip duplicates.
  931. if (!VRM->hasPhys(Intf->reg))
  932. continue;
  933. LastEvicted.addEviction(PhysReg, VirtReg.reg, Intf->reg);
  934. Matrix->unassign(*Intf);
  935. assert((ExtraRegInfo[Intf->reg].Cascade < Cascade ||
  936. VirtReg.isSpillable() < Intf->isSpillable()) &&
  937. "Cannot decrease cascade number, illegal eviction");
  938. ExtraRegInfo[Intf->reg].Cascade = Cascade;
  939. ++NumEvicted;
  940. NewVRegs.push_back(Intf->reg);
  941. }
  942. }
  943. /// Returns true if the given \p PhysReg is a callee saved register and has not
  944. /// been used for allocation yet.
  945. bool RAGreedy::isUnusedCalleeSavedReg(unsigned PhysReg) const {
  946. unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg);
  947. if (CSR == 0)
  948. return false;
  949. return !Matrix->isPhysRegUsed(PhysReg);
  950. }
  951. /// tryEvict - Try to evict all interferences for a physreg.
  952. /// @param VirtReg Currently unassigned virtual register.
  953. /// @param Order Physregs to try.
  954. /// @return Physreg to assign VirtReg, or 0.
  955. unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
  956. AllocationOrder &Order,
  957. SmallVectorImpl<unsigned> &NewVRegs,
  958. unsigned CostPerUseLimit,
  959. const SmallVirtRegSet &FixedRegisters) {
  960. NamedRegionTimer T("evict", "Evict", TimerGroupName, TimerGroupDescription,
  961. TimePassesIsEnabled);
  962. // Keep track of the cheapest interference seen so far.
  963. EvictionCost BestCost;
  964. BestCost.setMax();
  965. unsigned BestPhys = 0;
  966. unsigned OrderLimit = Order.getOrder().size();
  967. // When we are just looking for a reduced cost per use, don't break any
  968. // hints, and only evict smaller spill weights.
  969. if (CostPerUseLimit < ~0u) {
  970. BestCost.BrokenHints = 0;
  971. BestCost.MaxWeight = VirtReg.weight;
  972. // Check of any registers in RC are below CostPerUseLimit.
  973. const TargetRegisterClass *RC = MRI->getRegClass(VirtReg.reg);
  974. unsigned MinCost = RegClassInfo.getMinCost(RC);
  975. if (MinCost >= CostPerUseLimit) {
  976. LLVM_DEBUG(dbgs() << TRI->getRegClassName(RC) << " minimum cost = "
  977. << MinCost << ", no cheaper registers to be found.\n");
  978. return 0;
  979. }
  980. // It is normal for register classes to have a long tail of registers with
  981. // the same cost. We don't need to look at them if they're too expensive.
  982. if (TRI->getCostPerUse(Order.getOrder().back()) >= CostPerUseLimit) {
  983. OrderLimit = RegClassInfo.getLastCostChange(RC);
  984. LLVM_DEBUG(dbgs() << "Only trying the first " << OrderLimit
  985. << " regs.\n");
  986. }
  987. }
  988. Order.rewind();
  989. while (unsigned PhysReg = Order.next(OrderLimit)) {
  990. if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit)
  991. continue;
  992. // The first use of a callee-saved register in a function has cost 1.
  993. // Don't start using a CSR when the CostPerUseLimit is low.
  994. if (CostPerUseLimit == 1 && isUnusedCalleeSavedReg(PhysReg)) {
  995. LLVM_DEBUG(
  996. dbgs() << printReg(PhysReg, TRI) << " would clobber CSR "
  997. << printReg(RegClassInfo.getLastCalleeSavedAlias(PhysReg), TRI)
  998. << '\n');
  999. continue;
  1000. }
  1001. if (!canEvictInterference(VirtReg, PhysReg, false, BestCost,
  1002. FixedRegisters))
  1003. continue;
  1004. // Best so far.
  1005. BestPhys = PhysReg;
  1006. // Stop if the hint can be used.
  1007. if (Order.isHint())
  1008. break;
  1009. }
  1010. if (!BestPhys)
  1011. return 0;
  1012. evictInterference(VirtReg, BestPhys, NewVRegs);
  1013. return BestPhys;
  1014. }
  1015. //===----------------------------------------------------------------------===//
  1016. // Region Splitting
  1017. //===----------------------------------------------------------------------===//
  1018. /// addSplitConstraints - Fill out the SplitConstraints vector based on the
  1019. /// interference pattern in Physreg and its aliases. Add the constraints to
  1020. /// SpillPlacement and return the static cost of this split in Cost, assuming
  1021. /// that all preferences in SplitConstraints are met.
  1022. /// Return false if there are no bundles with positive bias.
  1023. bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf,
  1024. BlockFrequency &Cost) {
  1025. ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
  1026. // Reset interference dependent info.
  1027. SplitConstraints.resize(UseBlocks.size());
  1028. BlockFrequency StaticCost = 0;
  1029. for (unsigned i = 0; i != UseBlocks.size(); ++i) {
  1030. const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
  1031. SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
  1032. BC.Number = BI.MBB->getNumber();
  1033. Intf.moveToBlock(BC.Number);
  1034. BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
  1035. BC.Exit = (BI.LiveOut &&
  1036. !LIS->getInstructionFromIndex(BI.LastInstr)->isImplicitDef())
  1037. ? SpillPlacement::PrefReg
  1038. : SpillPlacement::DontCare;
  1039. BC.ChangesValue = BI.FirstDef.isValid();
  1040. if (!Intf.hasInterference())
  1041. continue;
  1042. // Number of spill code instructions to insert.
  1043. unsigned Ins = 0;
  1044. // Interference for the live-in value.
  1045. if (BI.LiveIn) {
  1046. if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) {
  1047. BC.Entry = SpillPlacement::MustSpill;
  1048. ++Ins;
  1049. } else if (Intf.first() < BI.FirstInstr) {
  1050. BC.Entry = SpillPlacement::PrefSpill;
  1051. ++Ins;
  1052. } else if (Intf.first() < BI.LastInstr) {
  1053. ++Ins;
  1054. }
  1055. // Abort if the spill cannot be inserted at the MBB' start
  1056. if (((BC.Entry == SpillPlacement::MustSpill) ||
  1057. (BC.Entry == SpillPlacement::PrefSpill)) &&
  1058. SlotIndex::isEarlierInstr(BI.FirstInstr,
  1059. SA->getFirstSplitPoint(BC.Number)))
  1060. return false;
  1061. }
  1062. // Interference for the live-out value.
  1063. if (BI.LiveOut) {
  1064. if (Intf.last() >= SA->getLastSplitPoint(BC.Number)) {
  1065. BC.Exit = SpillPlacement::MustSpill;
  1066. ++Ins;
  1067. } else if (Intf.last() > BI.LastInstr) {
  1068. BC.Exit = SpillPlacement::PrefSpill;
  1069. ++Ins;
  1070. } else if (Intf.last() > BI.FirstInstr) {
  1071. ++Ins;
  1072. }
  1073. }
  1074. // Accumulate the total frequency of inserted spill code.
  1075. while (Ins--)
  1076. StaticCost += SpillPlacer->getBlockFrequency(BC.Number);
  1077. }
  1078. Cost = StaticCost;
  1079. // Add constraints for use-blocks. Note that these are the only constraints
  1080. // that may add a positive bias, it is downhill from here.
  1081. SpillPlacer->addConstraints(SplitConstraints);
  1082. return SpillPlacer->scanActiveBundles();
  1083. }
  1084. /// addThroughConstraints - Add constraints and links to SpillPlacer from the
  1085. /// live-through blocks in Blocks.
  1086. bool RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf,
  1087. ArrayRef<unsigned> Blocks) {
  1088. const unsigned GroupSize = 8;
  1089. SpillPlacement::BlockConstraint BCS[GroupSize];
  1090. unsigned TBS[GroupSize];
  1091. unsigned B = 0, T = 0;
  1092. for (unsigned i = 0; i != Blocks.size(); ++i) {
  1093. unsigned Number = Blocks[i];
  1094. Intf.moveToBlock(Number);
  1095. if (!Intf.hasInterference()) {
  1096. assert(T < GroupSize && "Array overflow");
  1097. TBS[T] = Number;
  1098. if (++T == GroupSize) {
  1099. SpillPlacer->addLinks(makeArrayRef(TBS, T));
  1100. T = 0;
  1101. }
  1102. continue;
  1103. }
  1104. assert(B < GroupSize && "Array overflow");
  1105. BCS[B].Number = Number;
  1106. // Abort if the spill cannot be inserted at the MBB' start
  1107. MachineBasicBlock *MBB = MF->getBlockNumbered(Number);
  1108. if (!MBB->empty() &&
  1109. SlotIndex::isEarlierInstr(LIS->getInstructionIndex(MBB->instr_front()),
  1110. SA->getFirstSplitPoint(Number)))
  1111. return false;
  1112. // Interference for the live-in value.
  1113. if (Intf.first() <= Indexes->getMBBStartIdx(Number))
  1114. BCS[B].Entry = SpillPlacement::MustSpill;
  1115. else
  1116. BCS[B].Entry = SpillPlacement::PrefSpill;
  1117. // Interference for the live-out value.
  1118. if (Intf.last() >= SA->getLastSplitPoint(Number))
  1119. BCS[B].Exit = SpillPlacement::MustSpill;
  1120. else
  1121. BCS[B].Exit = SpillPlacement::PrefSpill;
  1122. if (++B == GroupSize) {
  1123. SpillPlacer->addConstraints(makeArrayRef(BCS, B));
  1124. B = 0;
  1125. }
  1126. }
  1127. SpillPlacer->addConstraints(makeArrayRef(BCS, B));
  1128. SpillPlacer->addLinks(makeArrayRef(TBS, T));
  1129. return true;
  1130. }
  1131. bool RAGreedy::growRegion(GlobalSplitCandidate &Cand) {
  1132. // Keep track of through blocks that have not been added to SpillPlacer.
  1133. BitVector Todo = SA->getThroughBlocks();
  1134. SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks;
  1135. unsigned AddedTo = 0;
  1136. #ifndef NDEBUG
  1137. unsigned Visited = 0;
  1138. #endif
  1139. while (true) {
  1140. ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive();
  1141. // Find new through blocks in the periphery of PrefRegBundles.
  1142. for (int i = 0, e = NewBundles.size(); i != e; ++i) {
  1143. unsigned Bundle = NewBundles[i];
  1144. // Look at all blocks connected to Bundle in the full graph.
  1145. ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle);
  1146. for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end();
  1147. I != E; ++I) {
  1148. unsigned Block = *I;
  1149. if (!Todo.test(Block))
  1150. continue;
  1151. Todo.reset(Block);
  1152. // This is a new through block. Add it to SpillPlacer later.
  1153. ActiveBlocks.push_back(Block);
  1154. #ifndef NDEBUG
  1155. ++Visited;
  1156. #endif
  1157. }
  1158. }
  1159. // Any new blocks to add?
  1160. if (ActiveBlocks.size() == AddedTo)
  1161. break;
  1162. // Compute through constraints from the interference, or assume that all
  1163. // through blocks prefer spilling when forming compact regions.
  1164. auto NewBlocks = makeArrayRef(ActiveBlocks).slice(AddedTo);
  1165. if (Cand.PhysReg) {
  1166. if (!addThroughConstraints(Cand.Intf, NewBlocks))
  1167. return false;
  1168. } else
  1169. // Provide a strong negative bias on through blocks to prevent unwanted
  1170. // liveness on loop backedges.
  1171. SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true);
  1172. AddedTo = ActiveBlocks.size();
  1173. // Perhaps iterating can enable more bundles?
  1174. SpillPlacer->iterate();
  1175. }
  1176. LLVM_DEBUG(dbgs() << ", v=" << Visited);
  1177. return true;
  1178. }
  1179. /// calcCompactRegion - Compute the set of edge bundles that should be live
  1180. /// when splitting the current live range into compact regions. Compact
  1181. /// regions can be computed without looking at interference. They are the
  1182. /// regions formed by removing all the live-through blocks from the live range.
  1183. ///
  1184. /// Returns false if the current live range is already compact, or if the
  1185. /// compact regions would form single block regions anyway.
  1186. bool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) {
  1187. // Without any through blocks, the live range is already compact.
  1188. if (!SA->getNumThroughBlocks())
  1189. return false;
  1190. // Compact regions don't correspond to any physreg.
  1191. Cand.reset(IntfCache, 0);
  1192. LLVM_DEBUG(dbgs() << "Compact region bundles");
  1193. // Use the spill placer to determine the live bundles. GrowRegion pretends
  1194. // that all the through blocks have interference when PhysReg is unset.
  1195. SpillPlacer->prepare(Cand.LiveBundles);
  1196. // The static split cost will be zero since Cand.Intf reports no interference.
  1197. BlockFrequency Cost;
  1198. if (!addSplitConstraints(Cand.Intf, Cost)) {
  1199. LLVM_DEBUG(dbgs() << ", none.\n");
  1200. return false;
  1201. }
  1202. if (!growRegion(Cand)) {
  1203. LLVM_DEBUG(dbgs() << ", cannot spill all interferences.\n");
  1204. return false;
  1205. }
  1206. SpillPlacer->finish();
  1207. if (!Cand.LiveBundles.any()) {
  1208. LLVM_DEBUG(dbgs() << ", none.\n");
  1209. return false;
  1210. }
  1211. LLVM_DEBUG({
  1212. for (int i : Cand.LiveBundles.set_bits())
  1213. dbgs() << " EB#" << i;
  1214. dbgs() << ".\n";
  1215. });
  1216. return true;
  1217. }
  1218. /// calcSpillCost - Compute how expensive it would be to split the live range in
  1219. /// SA around all use blocks instead of forming bundle regions.
  1220. BlockFrequency RAGreedy::calcSpillCost() {
  1221. BlockFrequency Cost = 0;
  1222. ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
  1223. for (unsigned i = 0; i != UseBlocks.size(); ++i) {
  1224. const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
  1225. unsigned Number = BI.MBB->getNumber();
  1226. // We normally only need one spill instruction - a load or a store.
  1227. Cost += SpillPlacer->getBlockFrequency(Number);
  1228. // Unless the value is redefined in the block.
  1229. if (BI.LiveIn && BI.LiveOut && BI.FirstDef)
  1230. Cost += SpillPlacer->getBlockFrequency(Number);
  1231. }
  1232. return Cost;
  1233. }
  1234. /// Check if splitting Evictee will create a local split interval in
  1235. /// basic block number BBNumber that may cause a bad eviction chain. This is
  1236. /// intended to prevent bad eviction sequences like:
  1237. /// movl %ebp, 8(%esp) # 4-byte Spill
  1238. /// movl %ecx, %ebp
  1239. /// movl %ebx, %ecx
  1240. /// movl %edi, %ebx
  1241. /// movl %edx, %edi
  1242. /// cltd
  1243. /// idivl %esi
  1244. /// movl %edi, %edx
  1245. /// movl %ebx, %edi
  1246. /// movl %ecx, %ebx
  1247. /// movl %ebp, %ecx
  1248. /// movl 16(%esp), %ebp # 4 - byte Reload
  1249. ///
  1250. /// Such sequences are created in 2 scenarios:
  1251. ///
  1252. /// Scenario #1:
  1253. /// %0 is evicted from physreg0 by %1.
  1254. /// Evictee %0 is intended for region splitting with split candidate
  1255. /// physreg0 (the reg %0 was evicted from).
  1256. /// Region splitting creates a local interval because of interference with the
  1257. /// evictor %1 (normally region splitting creates 2 interval, the "by reg"
  1258. /// and "by stack" intervals and local interval created when interference
  1259. /// occurs).
  1260. /// One of the split intervals ends up evicting %2 from physreg1.
  1261. /// Evictee %2 is intended for region splitting with split candidate
  1262. /// physreg1.
  1263. /// One of the split intervals ends up evicting %3 from physreg2, etc.
  1264. ///
  1265. /// Scenario #2
  1266. /// %0 is evicted from physreg0 by %1.
  1267. /// %2 is evicted from physreg2 by %3 etc.
  1268. /// Evictee %0 is intended for region splitting with split candidate
  1269. /// physreg1.
  1270. /// Region splitting creates a local interval because of interference with the
  1271. /// evictor %1.
  1272. /// One of the split intervals ends up evicting back original evictor %1
  1273. /// from physreg0 (the reg %0 was evicted from).
  1274. /// Another evictee %2 is intended for region splitting with split candidate
  1275. /// physreg1.
  1276. /// One of the split intervals ends up evicting %3 from physreg2, etc.
  1277. ///
  1278. /// \param Evictee The register considered to be split.
  1279. /// \param Cand The split candidate that determines the physical register
  1280. /// we are splitting for and the interferences.
  1281. /// \param BBNumber The number of a BB for which the region split process will
  1282. /// create a local split interval.
  1283. /// \param Order The physical registers that may get evicted by a split
  1284. /// artifact of Evictee.
  1285. /// \return True if splitting Evictee may cause a bad eviction chain, false
  1286. /// otherwise.
  1287. bool RAGreedy::splitCanCauseEvictionChain(unsigned Evictee,
  1288. GlobalSplitCandidate &Cand,
  1289. unsigned BBNumber,
  1290. const AllocationOrder &Order) {
  1291. EvictionTrack::EvictorInfo VregEvictorInfo = LastEvicted.getEvictor(Evictee);
  1292. unsigned Evictor = VregEvictorInfo.first;
  1293. unsigned PhysReg = VregEvictorInfo.second;
  1294. // No actual evictor.
  1295. if (!Evictor || !PhysReg)
  1296. return false;
  1297. float MaxWeight = 0;
  1298. unsigned FutureEvictedPhysReg =
  1299. getCheapestEvicteeWeight(Order, LIS->getInterval(Evictee),
  1300. Cand.Intf.first(), Cand.Intf.last(), &MaxWeight);
  1301. // The bad eviction chain occurs when either the split candidate is the
  1302. // evicting reg or one of the split artifact will evict the evicting reg.
  1303. if ((PhysReg != Cand.PhysReg) && (PhysReg != FutureEvictedPhysReg))
  1304. return false;
  1305. Cand.Intf.moveToBlock(BBNumber);
  1306. // Check to see if the Evictor contains interference (with Evictee) in the
  1307. // given BB. If so, this interference caused the eviction of Evictee from
  1308. // PhysReg. This suggest that we will create a local interval during the
  1309. // region split to avoid this interference This local interval may cause a bad
  1310. // eviction chain.
  1311. if (!LIS->hasInterval(Evictor))
  1312. return false;
  1313. LiveInterval &EvictorLI = LIS->getInterval(Evictor);
  1314. if (EvictorLI.FindSegmentContaining(Cand.Intf.first()) == EvictorLI.end())
  1315. return false;
  1316. // Now, check to see if the local interval we will create is going to be
  1317. // expensive enough to evict somebody If so, this may cause a bad eviction
  1318. // chain.
  1319. VirtRegAuxInfo VRAI(*MF, *LIS, VRM, getAnalysis<MachineLoopInfo>(), *MBFI);
  1320. float splitArtifactWeight =
  1321. VRAI.futureWeight(LIS->getInterval(Evictee),
  1322. Cand.Intf.first().getPrevIndex(), Cand.Intf.last());
  1323. if (splitArtifactWeight >= 0 && splitArtifactWeight < MaxWeight)
  1324. return false;
  1325. return true;
  1326. }
  1327. /// Check if splitting VirtRegToSplit will create a local split interval
  1328. /// in basic block number BBNumber that may cause a spill.
  1329. ///
  1330. /// \param VirtRegToSplit The register considered to be split.
  1331. /// \param Cand The split candidate that determines the physical
  1332. /// register we are splitting for and the interferences.
  1333. /// \param BBNumber The number of a BB for which the region split process
  1334. /// will create a local split interval.
  1335. /// \param Order The physical registers that may get evicted by a
  1336. /// split artifact of VirtRegToSplit.
  1337. /// \return True if splitting VirtRegToSplit may cause a spill, false
  1338. /// otherwise.
  1339. bool RAGreedy::splitCanCauseLocalSpill(unsigned VirtRegToSplit,
  1340. GlobalSplitCandidate &Cand,
  1341. unsigned BBNumber,
  1342. const AllocationOrder &Order) {
  1343. Cand.Intf.moveToBlock(BBNumber);
  1344. // Check if the local interval will find a non interfereing assignment.
  1345. for (auto PhysReg : Order.getOrder()) {
  1346. if (!Matrix->checkInterference(Cand.Intf.first().getPrevIndex(),
  1347. Cand.Intf.last(), PhysReg))
  1348. return false;
  1349. }
  1350. // Check if the local interval will evict a cheaper interval.
  1351. float CheapestEvictWeight = 0;
  1352. unsigned FutureEvictedPhysReg = getCheapestEvicteeWeight(
  1353. Order, LIS->getInterval(VirtRegToSplit), Cand.Intf.first(),
  1354. Cand.Intf.last(), &CheapestEvictWeight);
  1355. // Have we found an interval that can be evicted?
  1356. if (FutureEvictedPhysReg) {
  1357. VirtRegAuxInfo VRAI(*MF, *LIS, VRM, getAnalysis<MachineLoopInfo>(), *MBFI);
  1358. float splitArtifactWeight =
  1359. VRAI.futureWeight(LIS->getInterval(VirtRegToSplit),
  1360. Cand.Intf.first().getPrevIndex(), Cand.Intf.last());
  1361. // Will the weight of the local interval be higher than the cheapest evictee
  1362. // weight? If so it will evict it and will not cause a spill.
  1363. if (splitArtifactWeight >= 0 && splitArtifactWeight > CheapestEvictWeight)
  1364. return false;
  1365. }
  1366. // The local interval is not able to find non interferencing assignment and
  1367. // not able to evict a less worthy interval, therfore, it can cause a spill.
  1368. return true;
  1369. }
  1370. /// calcGlobalSplitCost - Return the global split cost of following the split
  1371. /// pattern in LiveBundles. This cost should be added to the local cost of the
  1372. /// interference pattern in SplitConstraints.
  1373. ///
  1374. BlockFrequency RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand,
  1375. const AllocationOrder &Order,
  1376. bool *CanCauseEvictionChain) {
  1377. BlockFrequency GlobalCost = 0;
  1378. const BitVector &LiveBundles = Cand.LiveBundles;
  1379. unsigned VirtRegToSplit = SA->getParent().reg;
  1380. ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
  1381. for (unsigned i = 0; i != UseBlocks.size(); ++i) {
  1382. const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
  1383. SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
  1384. bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, false)];
  1385. bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, true)];
  1386. unsigned Ins = 0;
  1387. Cand.Intf.moveToBlock(BC.Number);
  1388. // Check wheather a local interval is going to be created during the region
  1389. // split. Calculate adavanced spilt cost (cost of local intervals) if option
  1390. // is enabled.
  1391. if (EnableAdvancedRASplitCost && Cand.Intf.hasInterference() && BI.LiveIn &&
  1392. BI.LiveOut && RegIn && RegOut) {
  1393. if (CanCauseEvictionChain &&
  1394. splitCanCauseEvictionChain(VirtRegToSplit, Cand, BC.Number, Order)) {
  1395. // This interference causes our eviction from this assignment, we might
  1396. // evict somebody else and eventually someone will spill, add that cost.
  1397. // See splitCanCauseEvictionChain for detailed description of scenarios.
  1398. GlobalCost += SpillPlacer->getBlockFrequency(BC.Number);
  1399. GlobalCost += SpillPlacer->getBlockFrequency(BC.Number);
  1400. *CanCauseEvictionChain = true;
  1401. } else if (splitCanCauseLocalSpill(VirtRegToSplit, Cand, BC.Number,
  1402. Order)) {
  1403. // This interference causes local interval to spill, add that cost.
  1404. GlobalCost += SpillPlacer->getBlockFrequency(BC.Number);
  1405. GlobalCost += SpillPlacer->getBlockFrequency(BC.Number);
  1406. }
  1407. }
  1408. if (BI.LiveIn)
  1409. Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg);
  1410. if (BI.LiveOut)
  1411. Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg);
  1412. while (Ins--)
  1413. GlobalCost += SpillPlacer->getBlockFrequency(BC.Number);
  1414. }
  1415. for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) {
  1416. unsigned Number = Cand.ActiveBlocks[i];
  1417. bool RegIn = LiveBundles[Bundles->getBundle(Number, false)];
  1418. bool RegOut = LiveBundles[Bundles->getBundle(Number, true)];
  1419. if (!RegIn && !RegOut)
  1420. continue;
  1421. if (RegIn && RegOut) {
  1422. // We need double spill code if this block has interference.
  1423. Cand.Intf.moveToBlock(Number);
  1424. if (Cand.Intf.hasInterference()) {
  1425. GlobalCost += SpillPlacer->getBlockFrequency(Number);
  1426. GlobalCost += SpillPlacer->getBlockFrequency(Number);
  1427. // Check wheather a local interval is going to be created during the
  1428. // region split.
  1429. if (EnableAdvancedRASplitCost && CanCauseEvictionChain &&
  1430. splitCanCauseEvictionChain(VirtRegToSplit, Cand, Number, Order)) {
  1431. // This interference cause our eviction from this assignment, we might
  1432. // evict somebody else, add that cost.
  1433. // See splitCanCauseEvictionChain for detailed description of
  1434. // scenarios.
  1435. GlobalCost += SpillPlacer->getBlockFrequency(Number);
  1436. GlobalCost += SpillPlacer->getBlockFrequency(Number);
  1437. *CanCauseEvictionChain = true;
  1438. }
  1439. }
  1440. continue;
  1441. }
  1442. // live-in / stack-out or stack-in live-out.
  1443. GlobalCost += SpillPlacer->getBlockFrequency(Number);
  1444. }
  1445. return GlobalCost;
  1446. }
  1447. /// splitAroundRegion - Split the current live range around the regions
  1448. /// determined by BundleCand and GlobalCand.
  1449. ///
  1450. /// Before calling this function, GlobalCand and BundleCand must be initialized
  1451. /// so each bundle is assigned to a valid candidate, or NoCand for the
  1452. /// stack-bound bundles. The shared SA/SE SplitAnalysis and SplitEditor
  1453. /// objects must be initialized for the current live range, and intervals
  1454. /// created for the used candidates.
  1455. ///
  1456. /// @param LREdit The LiveRangeEdit object handling the current split.
  1457. /// @param UsedCands List of used GlobalCand entries. Every BundleCand value
  1458. /// must appear in this list.
  1459. void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit,
  1460. ArrayRef<unsigned> UsedCands) {
  1461. // These are the intervals created for new global ranges. We may create more
  1462. // intervals for local ranges.
  1463. const unsigned NumGlobalIntvs = LREdit.size();
  1464. LLVM_DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs
  1465. << " globals.\n");
  1466. assert(NumGlobalIntvs && "No global intervals configured");
  1467. // Isolate even single instructions when dealing with a proper sub-class.
  1468. // That guarantees register class inflation for the stack interval because it
  1469. // is all copies.
  1470. unsigned Reg = SA->getParent().reg;
  1471. bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
  1472. // First handle all the blocks with uses.
  1473. ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
  1474. for (unsigned i = 0; i != UseBlocks.size(); ++i) {
  1475. const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
  1476. unsigned Number = BI.MBB->getNumber();
  1477. unsigned IntvIn = 0, IntvOut = 0;
  1478. SlotIndex IntfIn, IntfOut;
  1479. if (BI.LiveIn) {
  1480. unsigned CandIn = BundleCand[Bundles->getBundle(Number, false)];
  1481. if (CandIn != NoCand) {
  1482. GlobalSplitCandidate &Cand = GlobalCand[CandIn];
  1483. IntvIn = Cand.IntvIdx;
  1484. Cand.Intf.moveToBlock(Number);
  1485. IntfIn = Cand.Intf.first();
  1486. }
  1487. }
  1488. if (BI.LiveOut) {
  1489. unsigned CandOut = BundleCand[Bundles->getBundle(Number, true)];
  1490. if (CandOut != NoCand) {
  1491. GlobalSplitCandidate &Cand = GlobalCand[CandOut];
  1492. IntvOut = Cand.IntvIdx;
  1493. Cand.Intf.moveToBlock(Number);
  1494. IntfOut = Cand.Intf.last();
  1495. }
  1496. }
  1497. // Create separate intervals for isolated blocks with multiple uses.
  1498. if (!IntvIn && !IntvOut) {
  1499. LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " isolated.\n");
  1500. if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
  1501. SE->splitSingleBlock(BI);
  1502. continue;
  1503. }
  1504. if (IntvIn && IntvOut)
  1505. SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
  1506. else if (IntvIn)
  1507. SE->splitRegInBlock(BI, IntvIn, IntfIn);
  1508. else
  1509. SE->splitRegOutBlock(BI, IntvOut, IntfOut);
  1510. }
  1511. // Handle live-through blocks. The relevant live-through blocks are stored in
  1512. // the ActiveBlocks list with each candidate. We need to filter out
  1513. // duplicates.
  1514. BitVector Todo = SA->getThroughBlocks();
  1515. for (unsigned c = 0; c != UsedCands.size(); ++c) {
  1516. ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks;
  1517. for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
  1518. unsigned Number = Blocks[i];
  1519. if (!Todo.test(Number))
  1520. continue;
  1521. Todo.reset(Number);
  1522. unsigned IntvIn = 0, IntvOut = 0;
  1523. SlotIndex IntfIn, IntfOut;
  1524. unsigned CandIn = BundleCand[Bundles->getBundle(Number, false)];
  1525. if (CandIn != NoCand) {
  1526. GlobalSplitCandidate &Cand = GlobalCand[CandIn];
  1527. IntvIn = Cand.IntvIdx;
  1528. Cand.Intf.moveToBlock(Number);
  1529. IntfIn = Cand.Intf.first();
  1530. }
  1531. unsigned CandOut = BundleCand[Bundles->getBundle(Number, true)];
  1532. if (CandOut != NoCand) {
  1533. GlobalSplitCandidate &Cand = GlobalCand[CandOut];
  1534. IntvOut = Cand.IntvIdx;
  1535. Cand.Intf.moveToBlock(Number);
  1536. IntfOut = Cand.Intf.last();
  1537. }
  1538. if (!IntvIn && !IntvOut)
  1539. continue;
  1540. SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
  1541. }
  1542. }
  1543. ++NumGlobalSplits;
  1544. SmallVector<unsigned, 8> IntvMap;
  1545. SE->finish(&IntvMap);
  1546. DebugVars->splitRegister(Reg, LREdit.regs(), *LIS);
  1547. ExtraRegInfo.resize(MRI->getNumVirtRegs());
  1548. unsigned OrigBlocks = SA->getNumLiveBlocks();
  1549. // Sort out the new intervals created by splitting. We get four kinds:
  1550. // - Remainder intervals should not be split again.
  1551. // - Candidate intervals can be assigned to Cand.PhysReg.
  1552. // - Block-local splits are candidates for local splitting.
  1553. // - DCE leftovers should go back on the queue.
  1554. for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
  1555. LiveInterval &Reg = LIS->getInterval(LREdit.get(i));
  1556. // Ignore old intervals from DCE.
  1557. if (getStage(Reg) != RS_New)
  1558. continue;
  1559. // Remainder interval. Don't try splitting again, spill if it doesn't
  1560. // allocate.
  1561. if (IntvMap[i] == 0) {
  1562. setStage(Reg, RS_Spill);
  1563. continue;
  1564. }
  1565. // Global intervals. Allow repeated splitting as long as the number of live
  1566. // blocks is strictly decreasing.
  1567. if (IntvMap[i] < NumGlobalIntvs) {
  1568. if (SA->countLiveBlocks(&Reg) >= OrigBlocks) {
  1569. LLVM_DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks
  1570. << " blocks as original.\n");
  1571. // Don't allow repeated splitting as a safe guard against looping.
  1572. setStage(Reg, RS_Split2);
  1573. }
  1574. continue;
  1575. }
  1576. // Other intervals are treated as new. This includes local intervals created
  1577. // for blocks with multiple uses, and anything created by DCE.
  1578. }
  1579. if (VerifyEnabled)
  1580. MF->verify(this, "After splitting live range around region");
  1581. }
  1582. // Global split has high compile time cost especially for large live range.
  1583. // Return false for the case here where the potential benefit will never
  1584. // worth the cost.
  1585. unsigned RAGreedy::isSplitBenefitWorthCost(LiveInterval &VirtReg) {
  1586. MachineInstr *MI = MRI->getUniqueVRegDef(VirtReg.reg);
  1587. if (MI && TII->isTriviallyReMaterializable(*MI, AA) &&
  1588. VirtReg.size() > HugeSizeForSplit)
  1589. return false;
  1590. return true;
  1591. }
  1592. unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
  1593. SmallVectorImpl<unsigned> &NewVRegs) {
  1594. if (!isSplitBenefitWorthCost(VirtReg))
  1595. return 0;
  1596. unsigned NumCands = 0;
  1597. BlockFrequency SpillCost = calcSpillCost();
  1598. BlockFrequency BestCost;
  1599. // Check if we can split this live range around a compact region.
  1600. bool HasCompact = calcCompactRegion(GlobalCand.front());
  1601. if (HasCompact) {
  1602. // Yes, keep GlobalCand[0] as the compact region candidate.
  1603. NumCands = 1;
  1604. BestCost = BlockFrequency::getMaxFrequency();
  1605. } else {
  1606. // No benefit from the compact region, our fallback will be per-block
  1607. // splitting. Make sure we find a solution that is cheaper than spilling.
  1608. BestCost = SpillCost;
  1609. LLVM_DEBUG(dbgs() << "Cost of isolating all blocks = ";
  1610. MBFI->printBlockFreq(dbgs(), BestCost) << '\n');
  1611. }
  1612. bool CanCauseEvictionChain = false;
  1613. unsigned BestCand =
  1614. calculateRegionSplitCost(VirtReg, Order, BestCost, NumCands,
  1615. false /*IgnoreCSR*/, &CanCauseEvictionChain);
  1616. // Split candidates with compact regions can cause a bad eviction sequence.
  1617. // See splitCanCauseEvictionChain for detailed description of scenarios.
  1618. // To avoid it, we need to comapre the cost with the spill cost and not the
  1619. // current max frequency.
  1620. if (HasCompact && (BestCost > SpillCost) && (BestCand != NoCand) &&
  1621. CanCauseEvictionChain) {
  1622. return 0;
  1623. }
  1624. // No solutions found, fall back to single block splitting.
  1625. if (!HasCompact && BestCand == NoCand)
  1626. return 0;
  1627. return doRegionSplit(VirtReg, BestCand, HasCompact, NewVRegs);
  1628. }
  1629. unsigned RAGreedy::calculateRegionSplitCost(LiveInterval &VirtReg,
  1630. AllocationOrder &Order,
  1631. BlockFrequency &BestCost,
  1632. unsigned &NumCands, bool IgnoreCSR,
  1633. bool *CanCauseEvictionChain) {
  1634. unsigned BestCand = NoCand;
  1635. Order.rewind();
  1636. while (unsigned PhysReg = Order.next()) {
  1637. if (IgnoreCSR && isUnusedCalleeSavedReg(PhysReg))
  1638. continue;
  1639. // Discard bad candidates before we run out of interference cache cursors.
  1640. // This will only affect register classes with a lot of registers (>32).
  1641. if (NumCands == IntfCache.getMaxCursors()) {
  1642. unsigned WorstCount = ~0u;
  1643. unsigned Worst = 0;
  1644. for (unsigned i = 0; i != NumCands; ++i) {
  1645. if (i == BestCand || !GlobalCand[i].PhysReg)
  1646. continue;
  1647. unsigned Count = GlobalCand[i].LiveBundles.count();
  1648. if (Count < WorstCount) {
  1649. Worst = i;
  1650. WorstCount = Count;
  1651. }
  1652. }
  1653. --NumCands;
  1654. GlobalCand[Worst] = GlobalCand[NumCands];
  1655. if (BestCand == NumCands)
  1656. BestCand = Worst;
  1657. }
  1658. if (GlobalCand.size() <= NumCands)
  1659. GlobalCand.resize(NumCands+1);
  1660. GlobalSplitCandidate &Cand = GlobalCand[NumCands];
  1661. Cand.reset(IntfCache, PhysReg);
  1662. SpillPlacer->prepare(Cand.LiveBundles);
  1663. BlockFrequency Cost;
  1664. if (!addSplitConstraints(Cand.Intf, Cost)) {
  1665. LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << "\tno positive bundles\n");
  1666. continue;
  1667. }
  1668. LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << "\tstatic = ";
  1669. MBFI->printBlockFreq(dbgs(), Cost));
  1670. if (Cost >= BestCost) {
  1671. LLVM_DEBUG({
  1672. if (BestCand == NoCand)
  1673. dbgs() << " worse than no bundles\n";
  1674. else
  1675. dbgs() << " worse than "
  1676. << printReg(GlobalCand[BestCand].PhysReg, TRI) << '\n';
  1677. });
  1678. continue;
  1679. }
  1680. if (!growRegion(Cand)) {
  1681. LLVM_DEBUG(dbgs() << ", cannot spill all interferences.\n");
  1682. continue;
  1683. }
  1684. SpillPlacer->finish();
  1685. // No live bundles, defer to splitSingleBlocks().
  1686. if (!Cand.LiveBundles.any()) {
  1687. LLVM_DEBUG(dbgs() << " no bundles.\n");
  1688. continue;
  1689. }
  1690. bool HasEvictionChain = false;
  1691. Cost += calcGlobalSplitCost(Cand, Order, &HasEvictionChain);
  1692. LLVM_DEBUG({
  1693. dbgs() << ", total = ";
  1694. MBFI->printBlockFreq(dbgs(), Cost) << " with bundles";
  1695. for (int i : Cand.LiveBundles.set_bits())
  1696. dbgs() << " EB#" << i;
  1697. dbgs() << ".\n";
  1698. });
  1699. if (Cost < BestCost) {
  1700. BestCand = NumCands;
  1701. BestCost = Cost;
  1702. // See splitCanCauseEvictionChain for detailed description of bad
  1703. // eviction chain scenarios.
  1704. if (CanCauseEvictionChain)
  1705. *CanCauseEvictionChain = HasEvictionChain;
  1706. }
  1707. ++NumCands;
  1708. }
  1709. if (CanCauseEvictionChain && BestCand != NoCand) {
  1710. // See splitCanCauseEvictionChain for detailed description of bad
  1711. // eviction chain scenarios.
  1712. LLVM_DEBUG(dbgs() << "Best split candidate of vreg "
  1713. << printReg(VirtReg.reg, TRI) << " may ");
  1714. if (!(*CanCauseEvictionChain))
  1715. LLVM_DEBUG(dbgs() << "not ");
  1716. LLVM_DEBUG(dbgs() << "cause bad eviction chain\n");
  1717. }
  1718. return BestCand;
  1719. }
  1720. unsigned RAGreedy::doRegionSplit(LiveInterval &VirtReg, unsigned BestCand,
  1721. bool HasCompact,
  1722. SmallVectorImpl<unsigned> &NewVRegs) {
  1723. SmallVector<unsigned, 8> UsedCands;
  1724. // Prepare split editor.
  1725. LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
  1726. SE->reset(LREdit, SplitSpillMode);
  1727. // Assign all edge bundles to the preferred candidate, or NoCand.
  1728. BundleCand.assign(Bundles->getNumBundles(), NoCand);
  1729. // Assign bundles for the best candidate region.
  1730. if (BestCand != NoCand) {
  1731. GlobalSplitCandidate &Cand = GlobalCand[BestCand];
  1732. if (unsigned B = Cand.getBundles(BundleCand, BestCand)) {
  1733. UsedCands.push_back(BestCand);
  1734. Cand.IntvIdx = SE->openIntv();
  1735. LLVM_DEBUG(dbgs() << "Split for " << printReg(Cand.PhysReg, TRI) << " in "
  1736. << B << " bundles, intv " << Cand.IntvIdx << ".\n");
  1737. (void)B;
  1738. }
  1739. }
  1740. // Assign bundles for the compact region.
  1741. if (HasCompact) {
  1742. GlobalSplitCandidate &Cand = GlobalCand.front();
  1743. assert(!Cand.PhysReg && "Compact region has no physreg");
  1744. if (unsigned B = Cand.getBundles(BundleCand, 0)) {
  1745. UsedCands.push_back(0);
  1746. Cand.IntvIdx = SE->openIntv();
  1747. LLVM_DEBUG(dbgs() << "Split for compact region in " << B
  1748. << " bundles, intv " << Cand.IntvIdx << ".\n");
  1749. (void)B;
  1750. }
  1751. }
  1752. splitAroundRegion(LREdit, UsedCands);
  1753. return 0;
  1754. }
  1755. //===----------------------------------------------------------------------===//
  1756. // Per-Block Splitting
  1757. //===----------------------------------------------------------------------===//
  1758. /// tryBlockSplit - Split a global live range around every block with uses. This
  1759. /// creates a lot of local live ranges, that will be split by tryLocalSplit if
  1760. /// they don't allocate.
  1761. unsigned RAGreedy::tryBlockSplit(LiveInterval &VirtReg, AllocationOrder &Order,
  1762. SmallVectorImpl<unsigned> &NewVRegs) {
  1763. assert(&SA->getParent() == &VirtReg && "Live range wasn't analyzed");
  1764. unsigned Reg = VirtReg.reg;
  1765. bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
  1766. LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
  1767. SE->reset(LREdit, SplitSpillMode);
  1768. ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
  1769. for (unsigned i = 0; i != UseBlocks.size(); ++i) {
  1770. const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
  1771. if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
  1772. SE->splitSingleBlock(BI);
  1773. }
  1774. // No blocks were split.
  1775. if (LREdit.empty())
  1776. return 0;
  1777. // We did split for some blocks.
  1778. SmallVector<unsigned, 8> IntvMap;
  1779. SE->finish(&IntvMap);
  1780. // Tell LiveDebugVariables about the new ranges.
  1781. DebugVars->splitRegister(Reg, LREdit.regs(), *LIS);
  1782. ExtraRegInfo.resize(MRI->getNumVirtRegs());
  1783. // Sort out the new intervals created by splitting. The remainder interval
  1784. // goes straight to spilling, the new local ranges get to stay RS_New.
  1785. for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
  1786. LiveInterval &LI = LIS->getInterval(LREdit.get(i));
  1787. if (getStage(LI) == RS_New && IntvMap[i] == 0)
  1788. setStage(LI, RS_Spill);
  1789. }
  1790. if (VerifyEnabled)
  1791. MF->verify(this, "After splitting live range around basic blocks");
  1792. return 0;
  1793. }
  1794. //===----------------------------------------------------------------------===//
  1795. // Per-Instruction Splitting
  1796. //===----------------------------------------------------------------------===//
  1797. /// Get the number of allocatable registers that match the constraints of \p Reg
  1798. /// on \p MI and that are also in \p SuperRC.
  1799. static unsigned getNumAllocatableRegsForConstraints(
  1800. const MachineInstr *MI, unsigned Reg, const TargetRegisterClass *SuperRC,
  1801. const TargetInstrInfo *TII, const TargetRegisterInfo *TRI,
  1802. const RegisterClassInfo &RCI) {
  1803. assert(SuperRC && "Invalid register class");
  1804. const TargetRegisterClass *ConstrainedRC =
  1805. MI->getRegClassConstraintEffectForVReg(Reg, SuperRC, TII, TRI,
  1806. /* ExploreBundle */ true);
  1807. if (!ConstrainedRC)
  1808. return 0;
  1809. return RCI.getNumAllocatableRegs(ConstrainedRC);
  1810. }
  1811. /// tryInstructionSplit - Split a live range around individual instructions.
  1812. /// This is normally not worthwhile since the spiller is doing essentially the
  1813. /// same thing. However, when the live range is in a constrained register
  1814. /// class, it may help to insert copies such that parts of the live range can
  1815. /// be moved to a larger register class.
  1816. ///
  1817. /// This is similar to spilling to a larger register class.
  1818. unsigned
  1819. RAGreedy::tryInstructionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
  1820. SmallVectorImpl<unsigned> &NewVRegs) {
  1821. const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg);
  1822. // There is no point to this if there are no larger sub-classes.
  1823. if (!RegClassInfo.isProperSubClass(CurRC))
  1824. return 0;
  1825. // Always enable split spill mode, since we're effectively spilling to a
  1826. // register.
  1827. LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
  1828. SE->reset(LREdit, SplitEditor::SM_Size);
  1829. ArrayRef<SlotIndex> Uses = SA->getUseSlots();
  1830. if (Uses.size() <= 1)
  1831. return 0;
  1832. LLVM_DEBUG(dbgs() << "Split around " << Uses.size()
  1833. << " individual instrs.\n");
  1834. const TargetRegisterClass *SuperRC =
  1835. TRI->getLargestLegalSuperClass(CurRC, *MF);
  1836. unsigned SuperRCNumAllocatableRegs = RCI.getNumAllocatableRegs(SuperRC);
  1837. // Split around every non-copy instruction if this split will relax
  1838. // the constraints on the virtual register.
  1839. // Otherwise, splitting just inserts uncoalescable copies that do not help
  1840. // the allocation.
  1841. for (unsigned i = 0; i != Uses.size(); ++i) {
  1842. if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]))
  1843. if (MI->isFullCopy() ||
  1844. SuperRCNumAllocatableRegs ==
  1845. getNumAllocatableRegsForConstraints(MI, VirtReg.reg, SuperRC, TII,
  1846. TRI, RCI)) {
  1847. LLVM_DEBUG(dbgs() << " skip:\t" << Uses[i] << '\t' << *MI);
  1848. continue;
  1849. }
  1850. SE->openIntv();
  1851. SlotIndex SegStart = SE->enterIntvBefore(Uses[i]);
  1852. SlotIndex SegStop = SE->leaveIntvAfter(Uses[i]);
  1853. SE->useIntv(SegStart, SegStop);
  1854. }
  1855. if (LREdit.empty()) {
  1856. LLVM_DEBUG(dbgs() << "All uses were copies.\n");
  1857. return 0;
  1858. }
  1859. SmallVector<unsigned, 8> IntvMap;
  1860. SE->finish(&IntvMap);
  1861. DebugVars->splitRegister(VirtReg.reg, LREdit.regs(), *LIS);
  1862. ExtraRegInfo.resize(MRI->getNumVirtRegs());
  1863. // Assign all new registers to RS_Spill. This was the last chance.
  1864. setStage(LREdit.begin(), LREdit.end(), RS_Spill);
  1865. return 0;
  1866. }
  1867. //===----------------------------------------------------------------------===//
  1868. // Local Splitting
  1869. //===----------------------------------------------------------------------===//
  1870. /// calcGapWeights - Compute the maximum spill weight that needs to be evicted
  1871. /// in order to use PhysReg between two entries in SA->UseSlots.
  1872. ///
  1873. /// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
  1874. ///
  1875. void RAGreedy::calcGapWeights(unsigned PhysReg,
  1876. SmallVectorImpl<float> &GapWeight) {
  1877. assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
  1878. const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
  1879. ArrayRef<SlotIndex> Uses = SA->getUseSlots();
  1880. const unsigned NumGaps = Uses.size()-1;
  1881. // Start and end points for the interference check.
  1882. SlotIndex StartIdx =
  1883. BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr;
  1884. SlotIndex StopIdx =
  1885. BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr;
  1886. GapWeight.assign(NumGaps, 0.0f);
  1887. // Add interference from each overlapping register.
  1888. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  1889. if (!Matrix->query(const_cast<LiveInterval&>(SA->getParent()), *Units)
  1890. .checkInterference())
  1891. continue;
  1892. // We know that VirtReg is a continuous interval from FirstInstr to
  1893. // LastInstr, so we don't need InterferenceQuery.
  1894. //
  1895. // Interference that overlaps an instruction is counted in both gaps
  1896. // surrounding the instruction. The exception is interference before
  1897. // StartIdx and after StopIdx.
  1898. //
  1899. LiveIntervalUnion::SegmentIter IntI =
  1900. Matrix->getLiveUnions()[*Units] .find(StartIdx);
  1901. for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
  1902. // Skip the gaps before IntI.
  1903. while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
  1904. if (++Gap == NumGaps)
  1905. break;
  1906. if (Gap == NumGaps)
  1907. break;
  1908. // Update the gaps covered by IntI.
  1909. const float weight = IntI.value()->weight;
  1910. for (; Gap != NumGaps; ++Gap) {
  1911. GapWeight[Gap] = std::max(GapWeight[Gap], weight);
  1912. if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
  1913. break;
  1914. }
  1915. if (Gap == NumGaps)
  1916. break;
  1917. }
  1918. }
  1919. // Add fixed interference.
  1920. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  1921. const LiveRange &LR = LIS->getRegUnit(*Units);
  1922. LiveRange::const_iterator I = LR.find(StartIdx);
  1923. LiveRange::const_iterator E = LR.end();
  1924. // Same loop as above. Mark any overlapped gaps as HUGE_VALF.
  1925. for (unsigned Gap = 0; I != E && I->start < StopIdx; ++I) {
  1926. while (Uses[Gap+1].getBoundaryIndex() < I->start)
  1927. if (++Gap == NumGaps)
  1928. break;
  1929. if (Gap == NumGaps)
  1930. break;
  1931. for (; Gap != NumGaps; ++Gap) {
  1932. GapWeight[Gap] = huge_valf;
  1933. if (Uses[Gap+1].getBaseIndex() >= I->end)
  1934. break;
  1935. }
  1936. if (Gap == NumGaps)
  1937. break;
  1938. }
  1939. }
  1940. }
  1941. /// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
  1942. /// basic block.
  1943. ///
  1944. unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
  1945. SmallVectorImpl<unsigned> &NewVRegs) {
  1946. // TODO: the function currently only handles a single UseBlock; it should be
  1947. // possible to generalize.
  1948. if (SA->getUseBlocks().size() != 1)
  1949. return 0;
  1950. const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
  1951. // Note that it is possible to have an interval that is live-in or live-out
  1952. // while only covering a single block - A phi-def can use undef values from
  1953. // predecessors, and the block could be a single-block loop.
  1954. // We don't bother doing anything clever about such a case, we simply assume
  1955. // that the interval is continuous from FirstInstr to LastInstr. We should
  1956. // make sure that we don't do anything illegal to such an interval, though.
  1957. ArrayRef<SlotIndex> Uses = SA->getUseSlots();
  1958. if (Uses.size() <= 2)
  1959. return 0;
  1960. const unsigned NumGaps = Uses.size()-1;
  1961. LLVM_DEBUG({
  1962. dbgs() << "tryLocalSplit: ";
  1963. for (unsigned i = 0, e = Uses.size(); i != e; ++i)
  1964. dbgs() << ' ' << Uses[i];
  1965. dbgs() << '\n';
  1966. });
  1967. // If VirtReg is live across any register mask operands, compute a list of
  1968. // gaps with register masks.
  1969. SmallVector<unsigned, 8> RegMaskGaps;
  1970. if (Matrix->checkRegMaskInterference(VirtReg)) {
  1971. // Get regmask slots for the whole block.
  1972. ArrayRef<SlotIndex> RMS = LIS->getRegMaskSlotsInBlock(BI.MBB->getNumber());
  1973. LLVM_DEBUG(dbgs() << RMS.size() << " regmasks in block:");
  1974. // Constrain to VirtReg's live range.
  1975. unsigned ri =
  1976. llvm::lower_bound(RMS, Uses.front().getRegSlot()) - RMS.begin();
  1977. unsigned re = RMS.size();
  1978. for (unsigned i = 0; i != NumGaps && ri != re; ++i) {
  1979. // Look for Uses[i] <= RMS <= Uses[i+1].
  1980. assert(!SlotIndex::isEarlierInstr(RMS[ri], Uses[i]));
  1981. if (SlotIndex::isEarlierInstr(Uses[i+1], RMS[ri]))
  1982. continue;
  1983. // Skip a regmask on the same instruction as the last use. It doesn't
  1984. // overlap the live range.
  1985. if (SlotIndex::isSameInstr(Uses[i+1], RMS[ri]) && i+1 == NumGaps)
  1986. break;
  1987. LLVM_DEBUG(dbgs() << ' ' << RMS[ri] << ':' << Uses[i] << '-'
  1988. << Uses[i + 1]);
  1989. RegMaskGaps.push_back(i);
  1990. // Advance ri to the next gap. A regmask on one of the uses counts in
  1991. // both gaps.
  1992. while (ri != re && SlotIndex::isEarlierInstr(RMS[ri], Uses[i+1]))
  1993. ++ri;
  1994. }
  1995. LLVM_DEBUG(dbgs() << '\n');
  1996. }
  1997. // Since we allow local split results to be split again, there is a risk of
  1998. // creating infinite loops. It is tempting to require that the new live
  1999. // ranges have less instructions than the original. That would guarantee
  2000. // convergence, but it is too strict. A live range with 3 instructions can be
  2001. // split 2+3 (including the COPY), and we want to allow that.
  2002. //
  2003. // Instead we use these rules:
  2004. //
  2005. // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the
  2006. // noop split, of course).
  2007. // 2. Require progress be made for ranges with getStage() == RS_Split2. All
  2008. // the new ranges must have fewer instructions than before the split.
  2009. // 3. New ranges with the same number of instructions are marked RS_Split2,
  2010. // smaller ranges are marked RS_New.
  2011. //
  2012. // These rules allow a 3 -> 2+3 split once, which we need. They also prevent
  2013. // excessive splitting and infinite loops.
  2014. //
  2015. bool ProgressRequired = getStage(VirtReg) >= RS_Split2;
  2016. // Best split candidate.
  2017. unsigned BestBefore = NumGaps;
  2018. unsigned BestAfter = 0;
  2019. float BestDiff = 0;
  2020. const float blockFreq =
  2021. SpillPlacer->getBlockFrequency(BI.MBB->getNumber()).getFrequency() *
  2022. (1.0f / MBFI->getEntryFreq());
  2023. SmallVector<float, 8> GapWeight;
  2024. Order.rewind();
  2025. while (unsigned PhysReg = Order.next()) {
  2026. // Keep track of the largest spill weight that would need to be evicted in
  2027. // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
  2028. calcGapWeights(PhysReg, GapWeight);
  2029. // Remove any gaps with regmask clobbers.
  2030. if (Matrix->checkRegMaskInterference(VirtReg, PhysReg))
  2031. for (unsigned i = 0, e = RegMaskGaps.size(); i != e; ++i)
  2032. GapWeight[RegMaskGaps[i]] = huge_valf;
  2033. // Try to find the best sequence of gaps to close.
  2034. // The new spill weight must be larger than any gap interference.
  2035. // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
  2036. unsigned SplitBefore = 0, SplitAfter = 1;
  2037. // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
  2038. // It is the spill weight that needs to be evicted.
  2039. float MaxGap = GapWeight[0];
  2040. while (true) {
  2041. // Live before/after split?
  2042. const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
  2043. const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
  2044. LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << ' ' << Uses[SplitBefore]
  2045. << '-' << Uses[SplitAfter] << " i=" << MaxGap);
  2046. // Stop before the interval gets so big we wouldn't be making progress.
  2047. if (!LiveBefore && !LiveAfter) {
  2048. LLVM_DEBUG(dbgs() << " all\n");
  2049. break;
  2050. }
  2051. // Should the interval be extended or shrunk?
  2052. bool Shrink = true;
  2053. // How many gaps would the new range have?
  2054. unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter;
  2055. // Legally, without causing looping?
  2056. bool Legal = !ProgressRequired || NewGaps < NumGaps;
  2057. if (Legal && MaxGap < huge_valf) {
  2058. // Estimate the new spill weight. Each instruction reads or writes the
  2059. // register. Conservatively assume there are no read-modify-write
  2060. // instructions.
  2061. //
  2062. // Try to guess the size of the new interval.
  2063. const float EstWeight = normalizeSpillWeight(
  2064. blockFreq * (NewGaps + 1),
  2065. Uses[SplitBefore].distance(Uses[SplitAfter]) +
  2066. (LiveBefore + LiveAfter) * SlotIndex::InstrDist,
  2067. 1);
  2068. // Would this split be possible to allocate?
  2069. // Never allocate all gaps, we wouldn't be making progress.
  2070. LLVM_DEBUG(dbgs() << " w=" << EstWeight);
  2071. if (EstWeight * Hysteresis >= MaxGap) {
  2072. Shrink = false;
  2073. float Diff = EstWeight - MaxGap;
  2074. if (Diff > BestDiff) {
  2075. LLVM_DEBUG(dbgs() << " (best)");
  2076. BestDiff = Hysteresis * Diff;
  2077. BestBefore = SplitBefore;
  2078. BestAfter = SplitAfter;
  2079. }
  2080. }
  2081. }
  2082. // Try to shrink.
  2083. if (Shrink) {
  2084. if (++SplitBefore < SplitAfter) {
  2085. LLVM_DEBUG(dbgs() << " shrink\n");
  2086. // Recompute the max when necessary.
  2087. if (GapWeight[SplitBefore - 1] >= MaxGap) {
  2088. MaxGap = GapWeight[SplitBefore];
  2089. for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
  2090. MaxGap = std::max(MaxGap, GapWeight[i]);
  2091. }
  2092. continue;
  2093. }
  2094. MaxGap = 0;
  2095. }
  2096. // Try to extend the interval.
  2097. if (SplitAfter >= NumGaps) {
  2098. LLVM_DEBUG(dbgs() << " end\n");
  2099. break;
  2100. }
  2101. LLVM_DEBUG(dbgs() << " extend\n");
  2102. MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]);
  2103. }
  2104. }
  2105. // Didn't find any candidates?
  2106. if (BestBefore == NumGaps)
  2107. return 0;
  2108. LLVM_DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] << '-'
  2109. << Uses[BestAfter] << ", " << BestDiff << ", "
  2110. << (BestAfter - BestBefore + 1) << " instrs\n");
  2111. LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
  2112. SE->reset(LREdit);
  2113. SE->openIntv();
  2114. SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
  2115. SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]);
  2116. SE->useIntv(SegStart, SegStop);
  2117. SmallVector<unsigned, 8> IntvMap;
  2118. SE->finish(&IntvMap);
  2119. DebugVars->splitRegister(VirtReg.reg, LREdit.regs(), *LIS);
  2120. // If the new range has the same number of instructions as before, mark it as
  2121. // RS_Split2 so the next split will be forced to make progress. Otherwise,
  2122. // leave the new intervals as RS_New so they can compete.
  2123. bool LiveBefore = BestBefore != 0 || BI.LiveIn;
  2124. bool LiveAfter = BestAfter != NumGaps || BI.LiveOut;
  2125. unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter;
  2126. if (NewGaps >= NumGaps) {
  2127. LLVM_DEBUG(dbgs() << "Tagging non-progress ranges: ");
  2128. assert(!ProgressRequired && "Didn't make progress when it was required.");
  2129. for (unsigned i = 0, e = IntvMap.size(); i != e; ++i)
  2130. if (IntvMap[i] == 1) {
  2131. setStage(LIS->getInterval(LREdit.get(i)), RS_Split2);
  2132. LLVM_DEBUG(dbgs() << printReg(LREdit.get(i)));
  2133. }
  2134. LLVM_DEBUG(dbgs() << '\n');
  2135. }
  2136. ++NumLocalSplits;
  2137. return 0;
  2138. }
  2139. //===----------------------------------------------------------------------===//
  2140. // Live Range Splitting
  2141. //===----------------------------------------------------------------------===//
  2142. /// trySplit - Try to split VirtReg or one of its interferences, making it
  2143. /// assignable.
  2144. /// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
  2145. unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
  2146. SmallVectorImpl<unsigned>&NewVRegs,
  2147. const SmallVirtRegSet &FixedRegisters) {
  2148. // Ranges must be Split2 or less.
  2149. if (getStage(VirtReg) >= RS_Spill)
  2150. return 0;
  2151. // Local intervals are handled separately.
  2152. if (LIS->intervalIsInOneMBB(VirtReg)) {
  2153. NamedRegionTimer T("local_split", "Local Splitting", TimerGroupName,
  2154. TimerGroupDescription, TimePassesIsEnabled);
  2155. SA->analyze(&VirtReg);
  2156. unsigned PhysReg = tryLocalSplit(VirtReg, Order, NewVRegs);
  2157. if (PhysReg || !NewVRegs.empty())
  2158. return PhysReg;
  2159. return tryInstructionSplit(VirtReg, Order, NewVRegs);
  2160. }
  2161. NamedRegionTimer T("global_split", "Global Splitting", TimerGroupName,
  2162. TimerGroupDescription, TimePassesIsEnabled);
  2163. SA->analyze(&VirtReg);
  2164. // FIXME: SplitAnalysis may repair broken live ranges coming from the
  2165. // coalescer. That may cause the range to become allocatable which means that
  2166. // tryRegionSplit won't be making progress. This check should be replaced with
  2167. // an assertion when the coalescer is fixed.
  2168. if (SA->didRepairRange()) {
  2169. // VirtReg has changed, so all cached queries are invalid.
  2170. Matrix->invalidateVirtRegs();
  2171. if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs, FixedRegisters))
  2172. return PhysReg;
  2173. }
  2174. // First try to split around a region spanning multiple blocks. RS_Split2
  2175. // ranges already made dubious progress with region splitting, so they go
  2176. // straight to single block splitting.
  2177. if (getStage(VirtReg) < RS_Split2) {
  2178. unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
  2179. if (PhysReg || !NewVRegs.empty())
  2180. return PhysReg;
  2181. }
  2182. // Then isolate blocks.
  2183. return tryBlockSplit(VirtReg, Order, NewVRegs);
  2184. }
  2185. //===----------------------------------------------------------------------===//
  2186. // Last Chance Recoloring
  2187. //===----------------------------------------------------------------------===//
  2188. /// Return true if \p reg has any tied def operand.
  2189. static bool hasTiedDef(MachineRegisterInfo *MRI, unsigned reg) {
  2190. for (const MachineOperand &MO : MRI->def_operands(reg))
  2191. if (MO.isTied())
  2192. return true;
  2193. return false;
  2194. }
  2195. /// mayRecolorAllInterferences - Check if the virtual registers that
  2196. /// interfere with \p VirtReg on \p PhysReg (or one of its aliases) may be
  2197. /// recolored to free \p PhysReg.
  2198. /// When true is returned, \p RecoloringCandidates has been augmented with all
  2199. /// the live intervals that need to be recolored in order to free \p PhysReg
  2200. /// for \p VirtReg.
  2201. /// \p FixedRegisters contains all the virtual registers that cannot be
  2202. /// recolored.
  2203. bool
  2204. RAGreedy::mayRecolorAllInterferences(unsigned PhysReg, LiveInterval &VirtReg,
  2205. SmallLISet &RecoloringCandidates,
  2206. const SmallVirtRegSet &FixedRegisters) {
  2207. const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg);
  2208. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  2209. LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
  2210. // If there is LastChanceRecoloringMaxInterference or more interferences,
  2211. // chances are one would not be recolorable.
  2212. if (Q.collectInterferingVRegs(LastChanceRecoloringMaxInterference) >=
  2213. LastChanceRecoloringMaxInterference && !ExhaustiveSearch) {
  2214. LLVM_DEBUG(dbgs() << "Early abort: too many interferences.\n");
  2215. CutOffInfo |= CO_Interf;
  2216. return false;
  2217. }
  2218. for (unsigned i = Q.interferingVRegs().size(); i; --i) {
  2219. LiveInterval *Intf = Q.interferingVRegs()[i - 1];
  2220. // If Intf is done and sit on the same register class as VirtReg,
  2221. // it would not be recolorable as it is in the same state as VirtReg.
  2222. // However, if VirtReg has tied defs and Intf doesn't, then
  2223. // there is still a point in examining if it can be recolorable.
  2224. if (((getStage(*Intf) == RS_Done &&
  2225. MRI->getRegClass(Intf->reg) == CurRC) &&
  2226. !(hasTiedDef(MRI, VirtReg.reg) && !hasTiedDef(MRI, Intf->reg))) ||
  2227. FixedRegisters.count(Intf->reg)) {
  2228. LLVM_DEBUG(
  2229. dbgs() << "Early abort: the interference is not recolorable.\n");
  2230. return false;
  2231. }
  2232. RecoloringCandidates.insert(Intf);
  2233. }
  2234. }
  2235. return true;
  2236. }
  2237. /// tryLastChanceRecoloring - Try to assign a color to \p VirtReg by recoloring
  2238. /// its interferences.
  2239. /// Last chance recoloring chooses a color for \p VirtReg and recolors every
  2240. /// virtual register that was using it. The recoloring process may recursively
  2241. /// use the last chance recoloring. Therefore, when a virtual register has been
  2242. /// assigned a color by this mechanism, it is marked as Fixed, i.e., it cannot
  2243. /// be last-chance-recolored again during this recoloring "session".
  2244. /// E.g.,
  2245. /// Let
  2246. /// vA can use {R1, R2 }
  2247. /// vB can use { R2, R3}
  2248. /// vC can use {R1 }
  2249. /// Where vA, vB, and vC cannot be split anymore (they are reloads for
  2250. /// instance) and they all interfere.
  2251. ///
  2252. /// vA is assigned R1
  2253. /// vB is assigned R2
  2254. /// vC tries to evict vA but vA is already done.
  2255. /// Regular register allocation fails.
  2256. ///
  2257. /// Last chance recoloring kicks in:
  2258. /// vC does as if vA was evicted => vC uses R1.
  2259. /// vC is marked as fixed.
  2260. /// vA needs to find a color.
  2261. /// None are available.
  2262. /// vA cannot evict vC: vC is a fixed virtual register now.
  2263. /// vA does as if vB was evicted => vA uses R2.
  2264. /// vB needs to find a color.
  2265. /// R3 is available.
  2266. /// Recoloring => vC = R1, vA = R2, vB = R3
  2267. ///
  2268. /// \p Order defines the preferred allocation order for \p VirtReg.
  2269. /// \p NewRegs will contain any new virtual register that have been created
  2270. /// (split, spill) during the process and that must be assigned.
  2271. /// \p FixedRegisters contains all the virtual registers that cannot be
  2272. /// recolored.
  2273. /// \p Depth gives the current depth of the last chance recoloring.
  2274. /// \return a physical register that can be used for VirtReg or ~0u if none
  2275. /// exists.
  2276. unsigned RAGreedy::tryLastChanceRecoloring(LiveInterval &VirtReg,
  2277. AllocationOrder &Order,
  2278. SmallVectorImpl<unsigned> &NewVRegs,
  2279. SmallVirtRegSet &FixedRegisters,
  2280. unsigned Depth) {
  2281. LLVM_DEBUG(dbgs() << "Try last chance recoloring for " << VirtReg << '\n');
  2282. // Ranges must be Done.
  2283. assert((getStage(VirtReg) >= RS_Done || !VirtReg.isSpillable()) &&
  2284. "Last chance recoloring should really be last chance");
  2285. // Set the max depth to LastChanceRecoloringMaxDepth.
  2286. // We may want to reconsider that if we end up with a too large search space
  2287. // for target with hundreds of registers.
  2288. // Indeed, in that case we may want to cut the search space earlier.
  2289. if (Depth >= LastChanceRecoloringMaxDepth && !ExhaustiveSearch) {
  2290. LLVM_DEBUG(dbgs() << "Abort because max depth has been reached.\n");
  2291. CutOffInfo |= CO_Depth;
  2292. return ~0u;
  2293. }
  2294. // Set of Live intervals that will need to be recolored.
  2295. SmallLISet RecoloringCandidates;
  2296. // Record the original mapping virtual register to physical register in case
  2297. // the recoloring fails.
  2298. DenseMap<unsigned, unsigned> VirtRegToPhysReg;
  2299. // Mark VirtReg as fixed, i.e., it will not be recolored pass this point in
  2300. // this recoloring "session".
  2301. assert(!FixedRegisters.count(VirtReg.reg));
  2302. FixedRegisters.insert(VirtReg.reg);
  2303. SmallVector<unsigned, 4> CurrentNewVRegs;
  2304. Order.rewind();
  2305. while (unsigned PhysReg = Order.next()) {
  2306. LLVM_DEBUG(dbgs() << "Try to assign: " << VirtReg << " to "
  2307. << printReg(PhysReg, TRI) << '\n');
  2308. RecoloringCandidates.clear();
  2309. VirtRegToPhysReg.clear();
  2310. CurrentNewVRegs.clear();
  2311. // It is only possible to recolor virtual register interference.
  2312. if (Matrix->checkInterference(VirtReg, PhysReg) >
  2313. LiveRegMatrix::IK_VirtReg) {
  2314. LLVM_DEBUG(
  2315. dbgs() << "Some interferences are not with virtual registers.\n");
  2316. continue;
  2317. }
  2318. // Early give up on this PhysReg if it is obvious we cannot recolor all
  2319. // the interferences.
  2320. if (!mayRecolorAllInterferences(PhysReg, VirtReg, RecoloringCandidates,
  2321. FixedRegisters)) {
  2322. LLVM_DEBUG(dbgs() << "Some interferences cannot be recolored.\n");
  2323. continue;
  2324. }
  2325. // RecoloringCandidates contains all the virtual registers that interfer
  2326. // with VirtReg on PhysReg (or one of its aliases).
  2327. // Enqueue them for recoloring and perform the actual recoloring.
  2328. PQueue RecoloringQueue;
  2329. for (SmallLISet::iterator It = RecoloringCandidates.begin(),
  2330. EndIt = RecoloringCandidates.end();
  2331. It != EndIt; ++It) {
  2332. unsigned ItVirtReg = (*It)->reg;
  2333. enqueue(RecoloringQueue, *It);
  2334. assert(VRM->hasPhys(ItVirtReg) &&
  2335. "Interferences are supposed to be with allocated variables");
  2336. // Record the current allocation.
  2337. VirtRegToPhysReg[ItVirtReg] = VRM->getPhys(ItVirtReg);
  2338. // unset the related struct.
  2339. Matrix->unassign(**It);
  2340. }
  2341. // Do as if VirtReg was assigned to PhysReg so that the underlying
  2342. // recoloring has the right information about the interferes and
  2343. // available colors.
  2344. Matrix->assign(VirtReg, PhysReg);
  2345. // Save the current recoloring state.
  2346. // If we cannot recolor all the interferences, we will have to start again
  2347. // at this point for the next physical register.
  2348. SmallVirtRegSet SaveFixedRegisters(FixedRegisters);
  2349. if (tryRecoloringCandidates(RecoloringQueue, CurrentNewVRegs,
  2350. FixedRegisters, Depth)) {
  2351. // Push the queued vregs into the main queue.
  2352. for (unsigned NewVReg : CurrentNewVRegs)
  2353. NewVRegs.push_back(NewVReg);
  2354. // Do not mess up with the global assignment process.
  2355. // I.e., VirtReg must be unassigned.
  2356. Matrix->unassign(VirtReg);
  2357. return PhysReg;
  2358. }
  2359. LLVM_DEBUG(dbgs() << "Fail to assign: " << VirtReg << " to "
  2360. << printReg(PhysReg, TRI) << '\n');
  2361. // The recoloring attempt failed, undo the changes.
  2362. FixedRegisters = SaveFixedRegisters;
  2363. Matrix->unassign(VirtReg);
  2364. // For a newly created vreg which is also in RecoloringCandidates,
  2365. // don't add it to NewVRegs because its physical register will be restored
  2366. // below. Other vregs in CurrentNewVRegs are created by calling
  2367. // selectOrSplit and should be added into NewVRegs.
  2368. for (SmallVectorImpl<unsigned>::iterator Next = CurrentNewVRegs.begin(),
  2369. End = CurrentNewVRegs.end();
  2370. Next != End; ++Next) {
  2371. if (RecoloringCandidates.count(&LIS->getInterval(*Next)))
  2372. continue;
  2373. NewVRegs.push_back(*Next);
  2374. }
  2375. for (SmallLISet::iterator It = RecoloringCandidates.begin(),
  2376. EndIt = RecoloringCandidates.end();
  2377. It != EndIt; ++It) {
  2378. unsigned ItVirtReg = (*It)->reg;
  2379. if (VRM->hasPhys(ItVirtReg))
  2380. Matrix->unassign(**It);
  2381. unsigned ItPhysReg = VirtRegToPhysReg[ItVirtReg];
  2382. Matrix->assign(**It, ItPhysReg);
  2383. }
  2384. }
  2385. // Last chance recoloring did not worked either, give up.
  2386. return ~0u;
  2387. }
  2388. /// tryRecoloringCandidates - Try to assign a new color to every register
  2389. /// in \RecoloringQueue.
  2390. /// \p NewRegs will contain any new virtual register created during the
  2391. /// recoloring process.
  2392. /// \p FixedRegisters[in/out] contains all the registers that have been
  2393. /// recolored.
  2394. /// \return true if all virtual registers in RecoloringQueue were successfully
  2395. /// recolored, false otherwise.
  2396. bool RAGreedy::tryRecoloringCandidates(PQueue &RecoloringQueue,
  2397. SmallVectorImpl<unsigned> &NewVRegs,
  2398. SmallVirtRegSet &FixedRegisters,
  2399. unsigned Depth) {
  2400. while (!RecoloringQueue.empty()) {
  2401. LiveInterval *LI = dequeue(RecoloringQueue);
  2402. LLVM_DEBUG(dbgs() << "Try to recolor: " << *LI << '\n');
  2403. unsigned PhysReg;
  2404. PhysReg = selectOrSplitImpl(*LI, NewVRegs, FixedRegisters, Depth + 1);
  2405. // When splitting happens, the live-range may actually be empty.
  2406. // In that case, this is okay to continue the recoloring even
  2407. // if we did not find an alternative color for it. Indeed,
  2408. // there will not be anything to color for LI in the end.
  2409. if (PhysReg == ~0u || (!PhysReg && !LI->empty()))
  2410. return false;
  2411. if (!PhysReg) {
  2412. assert(LI->empty() && "Only empty live-range do not require a register");
  2413. LLVM_DEBUG(dbgs() << "Recoloring of " << *LI
  2414. << " succeeded. Empty LI.\n");
  2415. continue;
  2416. }
  2417. LLVM_DEBUG(dbgs() << "Recoloring of " << *LI
  2418. << " succeeded with: " << printReg(PhysReg, TRI) << '\n');
  2419. Matrix->assign(*LI, PhysReg);
  2420. FixedRegisters.insert(LI->reg);
  2421. }
  2422. return true;
  2423. }
  2424. //===----------------------------------------------------------------------===//
  2425. // Main Entry Point
  2426. //===----------------------------------------------------------------------===//
  2427. unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
  2428. SmallVectorImpl<unsigned> &NewVRegs) {
  2429. CutOffInfo = CO_None;
  2430. LLVMContext &Ctx = MF->getFunction().getContext();
  2431. SmallVirtRegSet FixedRegisters;
  2432. unsigned Reg = selectOrSplitImpl(VirtReg, NewVRegs, FixedRegisters);
  2433. if (Reg == ~0U && (CutOffInfo != CO_None)) {
  2434. uint8_t CutOffEncountered = CutOffInfo & (CO_Depth | CO_Interf);
  2435. if (CutOffEncountered == CO_Depth)
  2436. Ctx.emitError("register allocation failed: maximum depth for recoloring "
  2437. "reached. Use -fexhaustive-register-search to skip "
  2438. "cutoffs");
  2439. else if (CutOffEncountered == CO_Interf)
  2440. Ctx.emitError("register allocation failed: maximum interference for "
  2441. "recoloring reached. Use -fexhaustive-register-search "
  2442. "to skip cutoffs");
  2443. else if (CutOffEncountered == (CO_Depth | CO_Interf))
  2444. Ctx.emitError("register allocation failed: maximum interference and "
  2445. "depth for recoloring reached. Use "
  2446. "-fexhaustive-register-search to skip cutoffs");
  2447. }
  2448. return Reg;
  2449. }
  2450. /// Using a CSR for the first time has a cost because it causes push|pop
  2451. /// to be added to prologue|epilogue. Splitting a cold section of the live
  2452. /// range can have lower cost than using the CSR for the first time;
  2453. /// Spilling a live range in the cold path can have lower cost than using
  2454. /// the CSR for the first time. Returns the physical register if we decide
  2455. /// to use the CSR; otherwise return 0.
  2456. unsigned RAGreedy::tryAssignCSRFirstTime(LiveInterval &VirtReg,
  2457. AllocationOrder &Order,
  2458. unsigned PhysReg,
  2459. unsigned &CostPerUseLimit,
  2460. SmallVectorImpl<unsigned> &NewVRegs) {
  2461. if (getStage(VirtReg) == RS_Spill && VirtReg.isSpillable()) {
  2462. // We choose spill over using the CSR for the first time if the spill cost
  2463. // is lower than CSRCost.
  2464. SA->analyze(&VirtReg);
  2465. if (calcSpillCost() >= CSRCost)
  2466. return PhysReg;
  2467. // We are going to spill, set CostPerUseLimit to 1 to make sure that
  2468. // we will not use a callee-saved register in tryEvict.
  2469. CostPerUseLimit = 1;
  2470. return 0;
  2471. }
  2472. if (getStage(VirtReg) < RS_Split) {
  2473. // We choose pre-splitting over using the CSR for the first time if
  2474. // the cost of splitting is lower than CSRCost.
  2475. SA->analyze(&VirtReg);
  2476. unsigned NumCands = 0;
  2477. BlockFrequency BestCost = CSRCost; // Don't modify CSRCost.
  2478. unsigned BestCand = calculateRegionSplitCost(VirtReg, Order, BestCost,
  2479. NumCands, true /*IgnoreCSR*/);
  2480. if (BestCand == NoCand)
  2481. // Use the CSR if we can't find a region split below CSRCost.
  2482. return PhysReg;
  2483. // Perform the actual pre-splitting.
  2484. doRegionSplit(VirtReg, BestCand, false/*HasCompact*/, NewVRegs);
  2485. return 0;
  2486. }
  2487. return PhysReg;
  2488. }
  2489. void RAGreedy::aboutToRemoveInterval(LiveInterval &LI) {
  2490. // Do not keep invalid information around.
  2491. SetOfBrokenHints.remove(&LI);
  2492. }
  2493. void RAGreedy::initializeCSRCost() {
  2494. // We use the larger one out of the command-line option and the value report
  2495. // by TRI.
  2496. CSRCost = BlockFrequency(
  2497. std::max((unsigned)CSRFirstTimeCost, TRI->getCSRFirstUseCost()));
  2498. if (!CSRCost.getFrequency())
  2499. return;
  2500. // Raw cost is relative to Entry == 2^14; scale it appropriately.
  2501. uint64_t ActualEntry = MBFI->getEntryFreq();
  2502. if (!ActualEntry) {
  2503. CSRCost = 0;
  2504. return;
  2505. }
  2506. uint64_t FixedEntry = 1 << 14;
  2507. if (ActualEntry < FixedEntry)
  2508. CSRCost *= BranchProbability(ActualEntry, FixedEntry);
  2509. else if (ActualEntry <= UINT32_MAX)
  2510. // Invert the fraction and divide.
  2511. CSRCost /= BranchProbability(FixedEntry, ActualEntry);
  2512. else
  2513. // Can't use BranchProbability in general, since it takes 32-bit numbers.
  2514. CSRCost = CSRCost.getFrequency() * (ActualEntry / FixedEntry);
  2515. }
  2516. /// Collect the hint info for \p Reg.
  2517. /// The results are stored into \p Out.
  2518. /// \p Out is not cleared before being populated.
  2519. void RAGreedy::collectHintInfo(unsigned Reg, HintsInfo &Out) {
  2520. for (const MachineInstr &Instr : MRI->reg_nodbg_instructions(Reg)) {
  2521. if (!Instr.isFullCopy())
  2522. continue;
  2523. // Look for the other end of the copy.
  2524. Register OtherReg = Instr.getOperand(0).getReg();
  2525. if (OtherReg == Reg) {
  2526. OtherReg = Instr.getOperand(1).getReg();
  2527. if (OtherReg == Reg)
  2528. continue;
  2529. }
  2530. // Get the current assignment.
  2531. Register OtherPhysReg = Register::isPhysicalRegister(OtherReg)
  2532. ? OtherReg
  2533. : VRM->getPhys(OtherReg);
  2534. // Push the collected information.
  2535. Out.push_back(HintInfo(MBFI->getBlockFreq(Instr.getParent()), OtherReg,
  2536. OtherPhysReg));
  2537. }
  2538. }
  2539. /// Using the given \p List, compute the cost of the broken hints if
  2540. /// \p PhysReg was used.
  2541. /// \return The cost of \p List for \p PhysReg.
  2542. BlockFrequency RAGreedy::getBrokenHintFreq(const HintsInfo &List,
  2543. unsigned PhysReg) {
  2544. BlockFrequency Cost = 0;
  2545. for (const HintInfo &Info : List) {
  2546. if (Info.PhysReg != PhysReg)
  2547. Cost += Info.Freq;
  2548. }
  2549. return Cost;
  2550. }
  2551. /// Using the register assigned to \p VirtReg, try to recolor
  2552. /// all the live ranges that are copy-related with \p VirtReg.
  2553. /// The recoloring is then propagated to all the live-ranges that have
  2554. /// been recolored and so on, until no more copies can be coalesced or
  2555. /// it is not profitable.
  2556. /// For a given live range, profitability is determined by the sum of the
  2557. /// frequencies of the non-identity copies it would introduce with the old
  2558. /// and new register.
  2559. void RAGreedy::tryHintRecoloring(LiveInterval &VirtReg) {
  2560. // We have a broken hint, check if it is possible to fix it by
  2561. // reusing PhysReg for the copy-related live-ranges. Indeed, we evicted
  2562. // some register and PhysReg may be available for the other live-ranges.
  2563. SmallSet<unsigned, 4> Visited;
  2564. SmallVector<unsigned, 2> RecoloringCandidates;
  2565. HintsInfo Info;
  2566. unsigned Reg = VirtReg.reg;
  2567. Register PhysReg = VRM->getPhys(Reg);
  2568. // Start the recoloring algorithm from the input live-interval, then
  2569. // it will propagate to the ones that are copy-related with it.
  2570. Visited.insert(Reg);
  2571. RecoloringCandidates.push_back(Reg);
  2572. LLVM_DEBUG(dbgs() << "Trying to reconcile hints for: " << printReg(Reg, TRI)
  2573. << '(' << printReg(PhysReg, TRI) << ")\n");
  2574. do {
  2575. Reg = RecoloringCandidates.pop_back_val();
  2576. // We cannot recolor physical register.
  2577. if (Register::isPhysicalRegister(Reg))
  2578. continue;
  2579. assert(VRM->hasPhys(Reg) && "We have unallocated variable!!");
  2580. // Get the live interval mapped with this virtual register to be able
  2581. // to check for the interference with the new color.
  2582. LiveInterval &LI = LIS->getInterval(Reg);
  2583. Register CurrPhys = VRM->getPhys(Reg);
  2584. // Check that the new color matches the register class constraints and
  2585. // that it is free for this live range.
  2586. if (CurrPhys != PhysReg && (!MRI->getRegClass(Reg)->contains(PhysReg) ||
  2587. Matrix->checkInterference(LI, PhysReg)))
  2588. continue;
  2589. LLVM_DEBUG(dbgs() << printReg(Reg, TRI) << '(' << printReg(CurrPhys, TRI)
  2590. << ") is recolorable.\n");
  2591. // Gather the hint info.
  2592. Info.clear();
  2593. collectHintInfo(Reg, Info);
  2594. // Check if recoloring the live-range will increase the cost of the
  2595. // non-identity copies.
  2596. if (CurrPhys != PhysReg) {
  2597. LLVM_DEBUG(dbgs() << "Checking profitability:\n");
  2598. BlockFrequency OldCopiesCost = getBrokenHintFreq(Info, CurrPhys);
  2599. BlockFrequency NewCopiesCost = getBrokenHintFreq(Info, PhysReg);
  2600. LLVM_DEBUG(dbgs() << "Old Cost: " << OldCopiesCost.getFrequency()
  2601. << "\nNew Cost: " << NewCopiesCost.getFrequency()
  2602. << '\n');
  2603. if (OldCopiesCost < NewCopiesCost) {
  2604. LLVM_DEBUG(dbgs() << "=> Not profitable.\n");
  2605. continue;
  2606. }
  2607. // At this point, the cost is either cheaper or equal. If it is
  2608. // equal, we consider this is profitable because it may expose
  2609. // more recoloring opportunities.
  2610. LLVM_DEBUG(dbgs() << "=> Profitable.\n");
  2611. // Recolor the live-range.
  2612. Matrix->unassign(LI);
  2613. Matrix->assign(LI, PhysReg);
  2614. }
  2615. // Push all copy-related live-ranges to keep reconciling the broken
  2616. // hints.
  2617. for (const HintInfo &HI : Info) {
  2618. if (Visited.insert(HI.Reg).second)
  2619. RecoloringCandidates.push_back(HI.Reg);
  2620. }
  2621. } while (!RecoloringCandidates.empty());
  2622. }
  2623. /// Try to recolor broken hints.
  2624. /// Broken hints may be repaired by recoloring when an evicted variable
  2625. /// freed up a register for a larger live-range.
  2626. /// Consider the following example:
  2627. /// BB1:
  2628. /// a =
  2629. /// b =
  2630. /// BB2:
  2631. /// ...
  2632. /// = b
  2633. /// = a
  2634. /// Let us assume b gets split:
  2635. /// BB1:
  2636. /// a =
  2637. /// b =
  2638. /// BB2:
  2639. /// c = b
  2640. /// ...
  2641. /// d = c
  2642. /// = d
  2643. /// = a
  2644. /// Because of how the allocation work, b, c, and d may be assigned different
  2645. /// colors. Now, if a gets evicted later:
  2646. /// BB1:
  2647. /// a =
  2648. /// st a, SpillSlot
  2649. /// b =
  2650. /// BB2:
  2651. /// c = b
  2652. /// ...
  2653. /// d = c
  2654. /// = d
  2655. /// e = ld SpillSlot
  2656. /// = e
  2657. /// This is likely that we can assign the same register for b, c, and d,
  2658. /// getting rid of 2 copies.
  2659. void RAGreedy::tryHintsRecoloring() {
  2660. for (LiveInterval *LI : SetOfBrokenHints) {
  2661. assert(Register::isVirtualRegister(LI->reg) &&
  2662. "Recoloring is possible only for virtual registers");
  2663. // Some dead defs may be around (e.g., because of debug uses).
  2664. // Ignore those.
  2665. if (!VRM->hasPhys(LI->reg))
  2666. continue;
  2667. tryHintRecoloring(*LI);
  2668. }
  2669. }
  2670. unsigned RAGreedy::selectOrSplitImpl(LiveInterval &VirtReg,
  2671. SmallVectorImpl<unsigned> &NewVRegs,
  2672. SmallVirtRegSet &FixedRegisters,
  2673. unsigned Depth) {
  2674. unsigned CostPerUseLimit = ~0u;
  2675. // First try assigning a free register.
  2676. AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix);
  2677. if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs, FixedRegisters)) {
  2678. // If VirtReg got an assignment, the eviction info is no longre relevant.
  2679. LastEvicted.clearEvicteeInfo(VirtReg.reg);
  2680. // When NewVRegs is not empty, we may have made decisions such as evicting
  2681. // a virtual register, go with the earlier decisions and use the physical
  2682. // register.
  2683. if (CSRCost.getFrequency() && isUnusedCalleeSavedReg(PhysReg) &&
  2684. NewVRegs.empty()) {
  2685. unsigned CSRReg = tryAssignCSRFirstTime(VirtReg, Order, PhysReg,
  2686. CostPerUseLimit, NewVRegs);
  2687. if (CSRReg || !NewVRegs.empty())
  2688. // Return now if we decide to use a CSR or create new vregs due to
  2689. // pre-splitting.
  2690. return CSRReg;
  2691. } else
  2692. return PhysReg;
  2693. }
  2694. LiveRangeStage Stage = getStage(VirtReg);
  2695. LLVM_DEBUG(dbgs() << StageName[Stage] << " Cascade "
  2696. << ExtraRegInfo[VirtReg.reg].Cascade << '\n');
  2697. // Try to evict a less worthy live range, but only for ranges from the primary
  2698. // queue. The RS_Split ranges already failed to do this, and they should not
  2699. // get a second chance until they have been split.
  2700. if (Stage != RS_Split)
  2701. if (unsigned PhysReg =
  2702. tryEvict(VirtReg, Order, NewVRegs, CostPerUseLimit,
  2703. FixedRegisters)) {
  2704. unsigned Hint = MRI->getSimpleHint(VirtReg.reg);
  2705. // If VirtReg has a hint and that hint is broken record this
  2706. // virtual register as a recoloring candidate for broken hint.
  2707. // Indeed, since we evicted a variable in its neighborhood it is
  2708. // likely we can at least partially recolor some of the
  2709. // copy-related live-ranges.
  2710. if (Hint && Hint != PhysReg)
  2711. SetOfBrokenHints.insert(&VirtReg);
  2712. // If VirtReg eviction someone, the eviction info for it as an evictee is
  2713. // no longre relevant.
  2714. LastEvicted.clearEvicteeInfo(VirtReg.reg);
  2715. return PhysReg;
  2716. }
  2717. assert((NewVRegs.empty() || Depth) && "Cannot append to existing NewVRegs");
  2718. // The first time we see a live range, don't try to split or spill.
  2719. // Wait until the second time, when all smaller ranges have been allocated.
  2720. // This gives a better picture of the interference to split around.
  2721. if (Stage < RS_Split) {
  2722. setStage(VirtReg, RS_Split);
  2723. LLVM_DEBUG(dbgs() << "wait for second round\n");
  2724. NewVRegs.push_back(VirtReg.reg);
  2725. return 0;
  2726. }
  2727. if (Stage < RS_Spill) {
  2728. // Try splitting VirtReg or interferences.
  2729. unsigned NewVRegSizeBefore = NewVRegs.size();
  2730. unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs, FixedRegisters);
  2731. if (PhysReg || (NewVRegs.size() - NewVRegSizeBefore)) {
  2732. // If VirtReg got split, the eviction info is no longre relevant.
  2733. LastEvicted.clearEvicteeInfo(VirtReg.reg);
  2734. return PhysReg;
  2735. }
  2736. }
  2737. // If we couldn't allocate a register from spilling, there is probably some
  2738. // invalid inline assembly. The base class will report it.
  2739. if (Stage >= RS_Done || !VirtReg.isSpillable())
  2740. return tryLastChanceRecoloring(VirtReg, Order, NewVRegs, FixedRegisters,
  2741. Depth);
  2742. // Finally spill VirtReg itself.
  2743. if (EnableDeferredSpilling && getStage(VirtReg) < RS_Memory) {
  2744. // TODO: This is experimental and in particular, we do not model
  2745. // the live range splitting done by spilling correctly.
  2746. // We would need a deep integration with the spiller to do the
  2747. // right thing here. Anyway, that is still good for early testing.
  2748. setStage(VirtReg, RS_Memory);
  2749. LLVM_DEBUG(dbgs() << "Do as if this register is in memory\n");
  2750. NewVRegs.push_back(VirtReg.reg);
  2751. } else {
  2752. NamedRegionTimer T("spill", "Spiller", TimerGroupName,
  2753. TimerGroupDescription, TimePassesIsEnabled);
  2754. LiveRangeEdit LRE(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
  2755. spiller().spill(LRE);
  2756. setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done);
  2757. if (VerifyEnabled)
  2758. MF->verify(this, "After spilling");
  2759. }
  2760. // The live virtual register requesting allocation was spilled, so tell
  2761. // the caller not to allocate anything during this round.
  2762. return 0;
  2763. }
  2764. void RAGreedy::reportNumberOfSplillsReloads(MachineLoop *L, unsigned &Reloads,
  2765. unsigned &FoldedReloads,
  2766. unsigned &Spills,
  2767. unsigned &FoldedSpills) {
  2768. Reloads = 0;
  2769. FoldedReloads = 0;
  2770. Spills = 0;
  2771. FoldedSpills = 0;
  2772. // Sum up the spill and reloads in subloops.
  2773. for (MachineLoop *SubLoop : *L) {
  2774. unsigned SubReloads;
  2775. unsigned SubFoldedReloads;
  2776. unsigned SubSpills;
  2777. unsigned SubFoldedSpills;
  2778. reportNumberOfSplillsReloads(SubLoop, SubReloads, SubFoldedReloads,
  2779. SubSpills, SubFoldedSpills);
  2780. Reloads += SubReloads;
  2781. FoldedReloads += SubFoldedReloads;
  2782. Spills += SubSpills;
  2783. FoldedSpills += SubFoldedSpills;
  2784. }
  2785. const MachineFrameInfo &MFI = MF->getFrameInfo();
  2786. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  2787. int FI;
  2788. for (MachineBasicBlock *MBB : L->getBlocks())
  2789. // Handle blocks that were not included in subloops.
  2790. if (Loops->getLoopFor(MBB) == L)
  2791. for (MachineInstr &MI : *MBB) {
  2792. SmallVector<const MachineMemOperand *, 2> Accesses;
  2793. auto isSpillSlotAccess = [&MFI](const MachineMemOperand *A) {
  2794. return MFI.isSpillSlotObjectIndex(
  2795. cast<FixedStackPseudoSourceValue>(A->getPseudoValue())
  2796. ->getFrameIndex());
  2797. };
  2798. if (TII->isLoadFromStackSlot(MI, FI) && MFI.isSpillSlotObjectIndex(FI))
  2799. ++Reloads;
  2800. else if (TII->hasLoadFromStackSlot(MI, Accesses) &&
  2801. llvm::any_of(Accesses, isSpillSlotAccess))
  2802. ++FoldedReloads;
  2803. else if (TII->isStoreToStackSlot(MI, FI) &&
  2804. MFI.isSpillSlotObjectIndex(FI))
  2805. ++Spills;
  2806. else if (TII->hasStoreToStackSlot(MI, Accesses) &&
  2807. llvm::any_of(Accesses, isSpillSlotAccess))
  2808. ++FoldedSpills;
  2809. }
  2810. if (Reloads || FoldedReloads || Spills || FoldedSpills) {
  2811. using namespace ore;
  2812. ORE->emit([&]() {
  2813. MachineOptimizationRemarkMissed R(DEBUG_TYPE, "LoopSpillReload",
  2814. L->getStartLoc(), L->getHeader());
  2815. if (Spills)
  2816. R << NV("NumSpills", Spills) << " spills ";
  2817. if (FoldedSpills)
  2818. R << NV("NumFoldedSpills", FoldedSpills) << " folded spills ";
  2819. if (Reloads)
  2820. R << NV("NumReloads", Reloads) << " reloads ";
  2821. if (FoldedReloads)
  2822. R << NV("NumFoldedReloads", FoldedReloads) << " folded reloads ";
  2823. R << "generated in loop";
  2824. return R;
  2825. });
  2826. }
  2827. }
  2828. bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
  2829. LLVM_DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
  2830. << "********** Function: " << mf.getName() << '\n');
  2831. MF = &mf;
  2832. TRI = MF->getSubtarget().getRegisterInfo();
  2833. TII = MF->getSubtarget().getInstrInfo();
  2834. RCI.runOnMachineFunction(mf);
  2835. EnableLocalReassign = EnableLocalReassignment ||
  2836. MF->getSubtarget().enableRALocalReassignment(
  2837. MF->getTarget().getOptLevel());
  2838. EnableAdvancedRASplitCost = ConsiderLocalIntervalCost ||
  2839. MF->getSubtarget().enableAdvancedRASplitCost();
  2840. if (VerifyEnabled)
  2841. MF->verify(this, "Before greedy register allocator");
  2842. RegAllocBase::init(getAnalysis<VirtRegMap>(),
  2843. getAnalysis<LiveIntervals>(),
  2844. getAnalysis<LiveRegMatrix>());
  2845. Indexes = &getAnalysis<SlotIndexes>();
  2846. MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
  2847. DomTree = &getAnalysis<MachineDominatorTree>();
  2848. ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
  2849. SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
  2850. Loops = &getAnalysis<MachineLoopInfo>();
  2851. Bundles = &getAnalysis<EdgeBundles>();
  2852. SpillPlacer = &getAnalysis<SpillPlacement>();
  2853. DebugVars = &getAnalysis<LiveDebugVariables>();
  2854. AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
  2855. initializeCSRCost();
  2856. calculateSpillWeightsAndHints(*LIS, mf, VRM, *Loops, *MBFI);
  2857. LLVM_DEBUG(LIS->dump());
  2858. SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
  2859. SE.reset(new SplitEditor(*SA, *AA, *LIS, *VRM, *DomTree, *MBFI));
  2860. ExtraRegInfo.clear();
  2861. ExtraRegInfo.resize(MRI->getNumVirtRegs());
  2862. NextCascade = 1;
  2863. IntfCache.init(MF, Matrix->getLiveUnions(), Indexes, LIS, TRI);
  2864. GlobalCand.resize(32); // This will grow as needed.
  2865. SetOfBrokenHints.clear();
  2866. LastEvicted.clear();
  2867. allocatePhysRegs();
  2868. tryHintsRecoloring();
  2869. postOptimization();
  2870. reportNumberOfSplillsReloads();
  2871. releaseMemory();
  2872. return true;
  2873. }