RegAllocGreedy.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //==- RegAllocGreedy.h ------- greedy register allocator ----------*-C++-*-==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // This file defines the RAGreedy function pass for register allocation in
  9. // optimized builds.
  10. //===----------------------------------------------------------------------===//
  11. #ifndef LLVM_CODEGEN_REGALLOCGREEDY_H_
  12. #define LLVM_CODEGEN_REGALLOCGREEDY_H_
  13. #include "AllocationOrder.h"
  14. #include "InterferenceCache.h"
  15. #include "LiveDebugVariables.h"
  16. #include "RegAllocBase.h"
  17. #include "RegAllocEvictionAdvisor.h"
  18. #include "SpillPlacement.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/StringRef.h"
  30. #include "llvm/Analysis/AliasAnalysis.h"
  31. #include "llvm/CodeGen/CalcSpillWeights.h"
  32. #include "llvm/CodeGen/EdgeBundles.h"
  33. #include "llvm/CodeGen/LiveInterval.h"
  34. #include "llvm/CodeGen/LiveIntervalUnion.h"
  35. #include "llvm/CodeGen/LiveIntervals.h"
  36. #include "llvm/CodeGen/LiveRangeEdit.h"
  37. #include "llvm/CodeGen/LiveRegMatrix.h"
  38. #include "llvm/CodeGen/LiveStacks.h"
  39. #include "llvm/CodeGen/MachineBasicBlock.h"
  40. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  41. #include "llvm/CodeGen/MachineDominators.h"
  42. #include "llvm/CodeGen/MachineFrameInfo.h"
  43. #include "llvm/CodeGen/MachineFunction.h"
  44. #include "llvm/CodeGen/MachineFunctionPass.h"
  45. #include "llvm/CodeGen/MachineLoopInfo.h"
  46. #include "llvm/CodeGen/MachineRegisterInfo.h"
  47. #include "llvm/CodeGen/RegisterClassInfo.h"
  48. #include "llvm/CodeGen/SlotIndexes.h"
  49. #include "llvm/CodeGen/Spiller.h"
  50. #include "llvm/CodeGen/TargetInstrInfo.h"
  51. #include "llvm/CodeGen/TargetRegisterInfo.h"
  52. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  53. #include "llvm/CodeGen/VirtRegMap.h"
  54. #include "llvm/IR/DebugInfoMetadata.h"
  55. #include "llvm/IR/Function.h"
  56. #include "llvm/IR/LLVMContext.h"
  57. #include "llvm/MC/MCRegisterInfo.h"
  58. #include "llvm/Pass.h"
  59. #include "llvm/Support/BranchProbability.h"
  60. #include "llvm/Target/TargetMachine.h"
  61. #include <algorithm>
  62. #include <cassert>
  63. #include <cstdint>
  64. #include <memory>
  65. #include <queue>
  66. #include <tuple>
  67. #include <utility>
  68. namespace llvm {
  69. class LLVM_LIBRARY_VISIBILITY RAGreedy : public MachineFunctionPass,
  70. public RegAllocBase,
  71. private LiveRangeEdit::Delegate {
  72. // Interface to eviction advisers
  73. public:
  74. /// Track allocation stage and eviction loop prevention during allocation.
  75. class ExtraRegInfo final {
  76. // RegInfo - Keep additional information about each live range.
  77. struct RegInfo {
  78. LiveRangeStage Stage = RS_New;
  79. // Cascade - Eviction loop prevention. See
  80. // canEvictInterferenceBasedOnCost().
  81. unsigned Cascade = 0;
  82. RegInfo() = default;
  83. };
  84. IndexedMap<RegInfo, VirtReg2IndexFunctor> Info;
  85. unsigned NextCascade = 1;
  86. public:
  87. ExtraRegInfo() = default;
  88. ExtraRegInfo(const ExtraRegInfo &) = delete;
  89. LiveRangeStage getStage(Register Reg) const { return Info[Reg].Stage; }
  90. LiveRangeStage getStage(const LiveInterval &VirtReg) const {
  91. return getStage(VirtReg.reg());
  92. }
  93. void setStage(Register Reg, LiveRangeStage Stage) {
  94. Info.grow(Reg.id());
  95. Info[Reg].Stage = Stage;
  96. }
  97. void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) {
  98. setStage(VirtReg.reg(), Stage);
  99. }
  100. /// Return the current stage of the register, if present, otherwise
  101. /// initialize it and return that.
  102. LiveRangeStage getOrInitStage(Register Reg) {
  103. Info.grow(Reg.id());
  104. return getStage(Reg);
  105. }
  106. unsigned getCascade(Register Reg) const { return Info[Reg].Cascade; }
  107. void setCascade(Register Reg, unsigned Cascade) {
  108. Info.grow(Reg.id());
  109. Info[Reg].Cascade = Cascade;
  110. }
  111. unsigned getOrAssignNewCascade(Register Reg) {
  112. unsigned Cascade = getCascade(Reg);
  113. if (!Cascade) {
  114. Cascade = NextCascade++;
  115. setCascade(Reg, Cascade);
  116. }
  117. return Cascade;
  118. }
  119. unsigned getCascadeOrCurrentNext(Register Reg) const {
  120. unsigned Cascade = getCascade(Reg);
  121. if (!Cascade)
  122. Cascade = NextCascade;
  123. return Cascade;
  124. }
  125. template <typename Iterator>
  126. void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) {
  127. for (; Begin != End; ++Begin) {
  128. Register Reg = *Begin;
  129. Info.grow(Reg.id());
  130. if (Info[Reg].Stage == RS_New)
  131. Info[Reg].Stage = NewStage;
  132. }
  133. }
  134. void LRE_DidCloneVirtReg(Register New, Register Old);
  135. };
  136. LiveRegMatrix *getInterferenceMatrix() const { return Matrix; }
  137. LiveIntervals *getLiveIntervals() const { return LIS; }
  138. VirtRegMap *getVirtRegMap() const { return VRM; }
  139. const RegisterClassInfo &getRegClassInfo() const { return RegClassInfo; }
  140. const ExtraRegInfo &getExtraInfo() const { return *ExtraInfo; }
  141. size_t getQueueSize() const { return Queue.size(); }
  142. // end (interface to eviction advisers)
  143. private:
  144. // Convenient shortcuts.
  145. using PQueue = std::priority_queue<std::pair<unsigned, unsigned>>;
  146. using SmallLISet = SmallPtrSet<LiveInterval *, 4>;
  147. // context
  148. MachineFunction *MF;
  149. // Shortcuts to some useful interface.
  150. const TargetInstrInfo *TII;
  151. const TargetRegisterInfo *TRI;
  152. RegisterClassInfo RCI;
  153. // analyses
  154. SlotIndexes *Indexes;
  155. MachineBlockFrequencyInfo *MBFI;
  156. MachineDominatorTree *DomTree;
  157. MachineLoopInfo *Loops;
  158. MachineOptimizationRemarkEmitter *ORE;
  159. EdgeBundles *Bundles;
  160. SpillPlacement *SpillPlacer;
  161. LiveDebugVariables *DebugVars;
  162. AliasAnalysis *AA;
  163. // state
  164. std::unique_ptr<Spiller> SpillerInstance;
  165. PQueue Queue;
  166. std::unique_ptr<VirtRegAuxInfo> VRAI;
  167. Optional<ExtraRegInfo> ExtraInfo;
  168. std::unique_ptr<RegAllocEvictionAdvisor> EvictAdvisor;
  169. // Enum CutOffStage to keep a track whether the register allocation failed
  170. // because of the cutoffs encountered in last chance recoloring.
  171. // Note: This is used as bitmask. New value should be next power of 2.
  172. enum CutOffStage {
  173. // No cutoffs encountered
  174. CO_None = 0,
  175. // lcr-max-depth cutoff encountered
  176. CO_Depth = 1,
  177. // lcr-max-interf cutoff encountered
  178. CO_Interf = 2
  179. };
  180. uint8_t CutOffInfo;
  181. #ifndef NDEBUG
  182. static const char *const StageName[];
  183. #endif
  184. /// EvictionTrack - Keeps track of past evictions in order to optimize region
  185. /// split decision.
  186. class EvictionTrack {
  187. public:
  188. using EvictorInfo =
  189. std::pair<Register /* evictor */, MCRegister /* physreg */>;
  190. using EvicteeInfo = llvm::DenseMap<Register /* evictee */, EvictorInfo>;
  191. private:
  192. /// Each Vreg that has been evicted in the last stage of selectOrSplit will
  193. /// be mapped to the evictor Vreg and the PhysReg it was evicted from.
  194. EvicteeInfo Evictees;
  195. public:
  196. /// Clear all eviction information.
  197. void clear() { Evictees.clear(); }
  198. /// Clear eviction information for the given evictee Vreg.
  199. /// E.g. when Vreg get's a new allocation, the old eviction info is no
  200. /// longer relevant.
  201. /// \param Evictee The evictee Vreg for whom we want to clear collected
  202. /// eviction info.
  203. void clearEvicteeInfo(Register Evictee) { Evictees.erase(Evictee); }
  204. /// Track new eviction.
  205. /// The Evictor vreg has evicted the Evictee vreg from Physreg.
  206. /// \param PhysReg The physical register Evictee was evicted from.
  207. /// \param Evictor The evictor Vreg that evicted Evictee.
  208. /// \param Evictee The evictee Vreg.
  209. void addEviction(MCRegister PhysReg, Register Evictor, Register Evictee) {
  210. Evictees[Evictee].first = Evictor;
  211. Evictees[Evictee].second = PhysReg;
  212. }
  213. /// Return the Evictor Vreg which evicted Evictee Vreg from PhysReg.
  214. /// \param Evictee The evictee vreg.
  215. /// \return The Evictor vreg which evicted Evictee vreg from PhysReg. 0 if
  216. /// nobody has evicted Evictee from PhysReg.
  217. EvictorInfo getEvictor(Register Evictee) {
  218. if (Evictees.count(Evictee)) {
  219. return Evictees[Evictee];
  220. }
  221. return EvictorInfo(0, 0);
  222. }
  223. };
  224. // Keeps track of past evictions in order to optimize region split decision.
  225. EvictionTrack LastEvicted;
  226. // splitting state.
  227. std::unique_ptr<SplitAnalysis> SA;
  228. std::unique_ptr<SplitEditor> SE;
  229. /// Cached per-block interference maps
  230. InterferenceCache IntfCache;
  231. /// All basic blocks where the current register has uses.
  232. SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints;
  233. /// Global live range splitting candidate info.
  234. struct GlobalSplitCandidate {
  235. // Register intended for assignment, or 0.
  236. MCRegister PhysReg;
  237. // SplitKit interval index for this candidate.
  238. unsigned IntvIdx;
  239. // Interference for PhysReg.
  240. InterferenceCache::Cursor Intf;
  241. // Bundles where this candidate should be live.
  242. BitVector LiveBundles;
  243. SmallVector<unsigned, 8> ActiveBlocks;
  244. void reset(InterferenceCache &Cache, MCRegister Reg) {
  245. PhysReg = Reg;
  246. IntvIdx = 0;
  247. Intf.setPhysReg(Cache, Reg);
  248. LiveBundles.clear();
  249. ActiveBlocks.clear();
  250. }
  251. // Set B[I] = C for every live bundle where B[I] was NoCand.
  252. unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) {
  253. unsigned Count = 0;
  254. for (unsigned I : LiveBundles.set_bits())
  255. if (B[I] == NoCand) {
  256. B[I] = C;
  257. Count++;
  258. }
  259. return Count;
  260. }
  261. };
  262. /// Candidate info for each PhysReg in AllocationOrder.
  263. /// This vector never shrinks, but grows to the size of the largest register
  264. /// class.
  265. SmallVector<GlobalSplitCandidate, 32> GlobalCand;
  266. enum : unsigned { NoCand = ~0u };
  267. /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to
  268. /// NoCand which indicates the stack interval.
  269. SmallVector<unsigned, 32> BundleCand;
  270. /// Callee-save register cost, calculated once per machine function.
  271. BlockFrequency CSRCost;
  272. /// Enable or not the consideration of the cost of local intervals created
  273. /// by a split candidate when choosing the best split candidate.
  274. bool EnableAdvancedRASplitCost;
  275. /// Set of broken hints that may be reconciled later because of eviction.
  276. SmallSetVector<LiveInterval *, 8> SetOfBrokenHints;
  277. /// The register cost values. This list will be recreated for each Machine
  278. /// Function
  279. ArrayRef<uint8_t> RegCosts;
  280. public:
  281. RAGreedy(const RegClassFilterFunc F = allocateAllRegClasses);
  282. /// Return the pass name.
  283. StringRef getPassName() const override { return "Greedy Register Allocator"; }
  284. /// RAGreedy analysis usage.
  285. void getAnalysisUsage(AnalysisUsage &AU) const override;
  286. void releaseMemory() override;
  287. Spiller &spiller() override { return *SpillerInstance; }
  288. void enqueueImpl(LiveInterval *LI) override;
  289. LiveInterval *dequeue() override;
  290. MCRegister selectOrSplit(LiveInterval &,
  291. SmallVectorImpl<Register> &) override;
  292. void aboutToRemoveInterval(LiveInterval &) override;
  293. /// Perform register allocation.
  294. bool runOnMachineFunction(MachineFunction &mf) override;
  295. MachineFunctionProperties getRequiredProperties() const override {
  296. return MachineFunctionProperties().set(
  297. MachineFunctionProperties::Property::NoPHIs);
  298. }
  299. MachineFunctionProperties getClearedProperties() const override {
  300. return MachineFunctionProperties().set(
  301. MachineFunctionProperties::Property::IsSSA);
  302. }
  303. static char ID;
  304. private:
  305. MCRegister selectOrSplitImpl(LiveInterval &, SmallVectorImpl<Register> &,
  306. SmallVirtRegSet &, unsigned = 0);
  307. bool LRE_CanEraseVirtReg(Register) override;
  308. void LRE_WillShrinkVirtReg(Register) override;
  309. void LRE_DidCloneVirtReg(Register, Register) override;
  310. void enqueue(PQueue &CurQueue, LiveInterval *LI);
  311. LiveInterval *dequeue(PQueue &CurQueue);
  312. BlockFrequency calcSpillCost();
  313. bool addSplitConstraints(InterferenceCache::Cursor, BlockFrequency &);
  314. bool addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>);
  315. bool growRegion(GlobalSplitCandidate &Cand);
  316. bool splitCanCauseEvictionChain(Register Evictee, GlobalSplitCandidate &Cand,
  317. unsigned BBNumber,
  318. const AllocationOrder &Order);
  319. bool splitCanCauseLocalSpill(unsigned VirtRegToSplit,
  320. GlobalSplitCandidate &Cand, unsigned BBNumber,
  321. const AllocationOrder &Order);
  322. BlockFrequency calcGlobalSplitCost(GlobalSplitCandidate &,
  323. const AllocationOrder &Order,
  324. bool *CanCauseEvictionChain);
  325. bool calcCompactRegion(GlobalSplitCandidate &);
  326. void splitAroundRegion(LiveRangeEdit &, ArrayRef<unsigned>);
  327. void calcGapWeights(MCRegister, SmallVectorImpl<float> &);
  328. bool canEvictInterferenceInRange(const LiveInterval &VirtReg,
  329. MCRegister PhysReg, SlotIndex Start,
  330. SlotIndex End, EvictionCost &MaxCost) const;
  331. MCRegister getCheapestEvicteeWeight(const AllocationOrder &Order,
  332. const LiveInterval &VirtReg,
  333. SlotIndex Start, SlotIndex End,
  334. float *BestEvictWeight) const;
  335. void evictInterference(LiveInterval &, MCRegister,
  336. SmallVectorImpl<Register> &);
  337. bool mayRecolorAllInterferences(MCRegister PhysReg, LiveInterval &VirtReg,
  338. SmallLISet &RecoloringCandidates,
  339. const SmallVirtRegSet &FixedRegisters);
  340. MCRegister tryAssign(LiveInterval &, AllocationOrder &,
  341. SmallVectorImpl<Register> &, const SmallVirtRegSet &);
  342. MCRegister tryEvict(LiveInterval &, AllocationOrder &,
  343. SmallVectorImpl<Register> &, uint8_t,
  344. const SmallVirtRegSet &);
  345. MCRegister tryRegionSplit(LiveInterval &, AllocationOrder &,
  346. SmallVectorImpl<Register> &);
  347. /// Calculate cost of region splitting.
  348. unsigned calculateRegionSplitCost(LiveInterval &VirtReg,
  349. AllocationOrder &Order,
  350. BlockFrequency &BestCost,
  351. unsigned &NumCands, bool IgnoreCSR,
  352. bool *CanCauseEvictionChain = nullptr);
  353. /// Perform region splitting.
  354. unsigned doRegionSplit(LiveInterval &VirtReg, unsigned BestCand,
  355. bool HasCompact, SmallVectorImpl<Register> &NewVRegs);
  356. /// Check other options before using a callee-saved register for the first
  357. /// time.
  358. MCRegister tryAssignCSRFirstTime(LiveInterval &VirtReg,
  359. AllocationOrder &Order, MCRegister PhysReg,
  360. uint8_t &CostPerUseLimit,
  361. SmallVectorImpl<Register> &NewVRegs);
  362. void initializeCSRCost();
  363. unsigned tryBlockSplit(LiveInterval &, AllocationOrder &,
  364. SmallVectorImpl<Register> &);
  365. unsigned tryInstructionSplit(LiveInterval &, AllocationOrder &,
  366. SmallVectorImpl<Register> &);
  367. unsigned tryLocalSplit(LiveInterval &, AllocationOrder &,
  368. SmallVectorImpl<Register> &);
  369. unsigned trySplit(LiveInterval &, AllocationOrder &,
  370. SmallVectorImpl<Register> &, const SmallVirtRegSet &);
  371. unsigned tryLastChanceRecoloring(LiveInterval &, AllocationOrder &,
  372. SmallVectorImpl<Register> &,
  373. SmallVirtRegSet &, unsigned);
  374. bool tryRecoloringCandidates(PQueue &, SmallVectorImpl<Register> &,
  375. SmallVirtRegSet &, unsigned);
  376. void tryHintRecoloring(LiveInterval &);
  377. void tryHintsRecoloring();
  378. /// Model the information carried by one end of a copy.
  379. struct HintInfo {
  380. /// The frequency of the copy.
  381. BlockFrequency Freq;
  382. /// The virtual register or physical register.
  383. Register Reg;
  384. /// Its currently assigned register.
  385. /// In case of a physical register Reg == PhysReg.
  386. MCRegister PhysReg;
  387. HintInfo(BlockFrequency Freq, Register Reg, MCRegister PhysReg)
  388. : Freq(Freq), Reg(Reg), PhysReg(PhysReg) {}
  389. };
  390. using HintsInfo = SmallVector<HintInfo, 4>;
  391. BlockFrequency getBrokenHintFreq(const HintsInfo &, MCRegister);
  392. void collectHintInfo(Register, HintsInfo &);
  393. /// Greedy RA statistic to remark.
  394. struct RAGreedyStats {
  395. unsigned Reloads = 0;
  396. unsigned FoldedReloads = 0;
  397. unsigned ZeroCostFoldedReloads = 0;
  398. unsigned Spills = 0;
  399. unsigned FoldedSpills = 0;
  400. unsigned Copies = 0;
  401. float ReloadsCost = 0.0f;
  402. float FoldedReloadsCost = 0.0f;
  403. float SpillsCost = 0.0f;
  404. float FoldedSpillsCost = 0.0f;
  405. float CopiesCost = 0.0f;
  406. bool isEmpty() {
  407. return !(Reloads || FoldedReloads || Spills || FoldedSpills ||
  408. ZeroCostFoldedReloads || Copies);
  409. }
  410. void add(RAGreedyStats other) {
  411. Reloads += other.Reloads;
  412. FoldedReloads += other.FoldedReloads;
  413. ZeroCostFoldedReloads += other.ZeroCostFoldedReloads;
  414. Spills += other.Spills;
  415. FoldedSpills += other.FoldedSpills;
  416. Copies += other.Copies;
  417. ReloadsCost += other.ReloadsCost;
  418. FoldedReloadsCost += other.FoldedReloadsCost;
  419. SpillsCost += other.SpillsCost;
  420. FoldedSpillsCost += other.FoldedSpillsCost;
  421. CopiesCost += other.CopiesCost;
  422. }
  423. void report(MachineOptimizationRemarkMissed &R);
  424. };
  425. /// Compute statistic for a basic block.
  426. RAGreedyStats computeStats(MachineBasicBlock &MBB);
  427. /// Compute and report statistic through a remark.
  428. RAGreedyStats reportStats(MachineLoop *L);
  429. /// Report the statistic for each loop.
  430. void reportStats();
  431. };
  432. } // namespace llvm
  433. #endif // #ifndef LLVM_CODEGEN_REGALLOCGREEDY_H_