xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/RegAllocGreedy.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
104eeddc0SDimitry Andric //==- RegAllocGreedy.h ------- greedy register allocator  ----------*-C++-*-==//
204eeddc0SDimitry Andric //
304eeddc0SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
404eeddc0SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
504eeddc0SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
604eeddc0SDimitry Andric //
704eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
804eeddc0SDimitry Andric // This file defines the RAGreedy function pass for register allocation in
904eeddc0SDimitry Andric // optimized builds.
1004eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
1104eeddc0SDimitry Andric 
1204eeddc0SDimitry Andric #ifndef LLVM_CODEGEN_REGALLOCGREEDY_H_
1304eeddc0SDimitry Andric #define LLVM_CODEGEN_REGALLOCGREEDY_H_
1404eeddc0SDimitry Andric 
1504eeddc0SDimitry Andric #include "InterferenceCache.h"
1604eeddc0SDimitry Andric #include "RegAllocBase.h"
1704eeddc0SDimitry Andric #include "RegAllocEvictionAdvisor.h"
18*bdd1243dSDimitry Andric #include "RegAllocPriorityAdvisor.h"
1904eeddc0SDimitry Andric #include "SpillPlacement.h"
2004eeddc0SDimitry Andric #include "SplitKit.h"
2104eeddc0SDimitry Andric #include "llvm/ADT/ArrayRef.h"
2204eeddc0SDimitry Andric #include "llvm/ADT/BitVector.h"
2304eeddc0SDimitry Andric #include "llvm/ADT/DenseMap.h"
2404eeddc0SDimitry Andric #include "llvm/ADT/IndexedMap.h"
2504eeddc0SDimitry Andric #include "llvm/ADT/SetVector.h"
2604eeddc0SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
2704eeddc0SDimitry Andric #include "llvm/ADT/SmallVector.h"
2804eeddc0SDimitry Andric #include "llvm/ADT/StringRef.h"
2904eeddc0SDimitry Andric #include "llvm/CodeGen/CalcSpillWeights.h"
3004eeddc0SDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
3104eeddc0SDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
3204eeddc0SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
3304eeddc0SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
3404eeddc0SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
3504eeddc0SDimitry Andric #include "llvm/CodeGen/Spiller.h"
3604eeddc0SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
3704eeddc0SDimitry Andric #include <algorithm>
3804eeddc0SDimitry Andric #include <cstdint>
3904eeddc0SDimitry Andric #include <memory>
4004eeddc0SDimitry Andric #include <queue>
4104eeddc0SDimitry Andric #include <utility>
4204eeddc0SDimitry Andric 
4304eeddc0SDimitry Andric namespace llvm {
4481ad6265SDimitry Andric class AllocationOrder;
4581ad6265SDimitry Andric class AnalysisUsage;
4681ad6265SDimitry Andric class EdgeBundles;
4781ad6265SDimitry Andric class LiveDebugVariables;
4881ad6265SDimitry Andric class LiveIntervals;
4981ad6265SDimitry Andric class LiveRegMatrix;
5081ad6265SDimitry Andric class MachineBasicBlock;
5181ad6265SDimitry Andric class MachineBlockFrequencyInfo;
5281ad6265SDimitry Andric class MachineDominatorTree;
5381ad6265SDimitry Andric class MachineLoop;
5481ad6265SDimitry Andric class MachineLoopInfo;
5581ad6265SDimitry Andric class MachineOptimizationRemarkEmitter;
5681ad6265SDimitry Andric class MachineOptimizationRemarkMissed;
5781ad6265SDimitry Andric class SlotIndexes;
5881ad6265SDimitry Andric class TargetInstrInfo;
5981ad6265SDimitry Andric class VirtRegMap;
6081ad6265SDimitry Andric 
6104eeddc0SDimitry Andric class LLVM_LIBRARY_VISIBILITY RAGreedy : public MachineFunctionPass,
6204eeddc0SDimitry Andric                                          public RegAllocBase,
6304eeddc0SDimitry Andric                                          private LiveRangeEdit::Delegate {
6404eeddc0SDimitry Andric   // Interface to eviction advisers
6504eeddc0SDimitry Andric public:
6604eeddc0SDimitry Andric   /// Track allocation stage and eviction loop prevention during allocation.
6704eeddc0SDimitry Andric   class ExtraRegInfo final {
6804eeddc0SDimitry Andric     // RegInfo - Keep additional information about each live range.
6904eeddc0SDimitry Andric     struct RegInfo {
7004eeddc0SDimitry Andric       LiveRangeStage Stage = RS_New;
7104eeddc0SDimitry Andric 
7204eeddc0SDimitry Andric       // Cascade - Eviction loop prevention. See
7304eeddc0SDimitry Andric       // canEvictInterferenceBasedOnCost().
7404eeddc0SDimitry Andric       unsigned Cascade = 0;
7504eeddc0SDimitry Andric 
7604eeddc0SDimitry Andric       RegInfo() = default;
7704eeddc0SDimitry Andric     };
7804eeddc0SDimitry Andric 
7904eeddc0SDimitry Andric     IndexedMap<RegInfo, VirtReg2IndexFunctor> Info;
8004eeddc0SDimitry Andric     unsigned NextCascade = 1;
8104eeddc0SDimitry Andric 
8204eeddc0SDimitry Andric   public:
83*bdd1243dSDimitry Andric     ExtraRegInfo() {}
8404eeddc0SDimitry Andric     ExtraRegInfo(const ExtraRegInfo &) = delete;
8504eeddc0SDimitry Andric 
8604eeddc0SDimitry Andric     LiveRangeStage getStage(Register Reg) const { return Info[Reg].Stage; }
8704eeddc0SDimitry Andric 
8804eeddc0SDimitry Andric     LiveRangeStage getStage(const LiveInterval &VirtReg) const {
8904eeddc0SDimitry Andric       return getStage(VirtReg.reg());
9004eeddc0SDimitry Andric     }
9104eeddc0SDimitry Andric 
9204eeddc0SDimitry Andric     void setStage(Register Reg, LiveRangeStage Stage) {
9304eeddc0SDimitry Andric       Info.grow(Reg.id());
9404eeddc0SDimitry Andric       Info[Reg].Stage = Stage;
9504eeddc0SDimitry Andric     }
9604eeddc0SDimitry Andric 
9704eeddc0SDimitry Andric     void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) {
9804eeddc0SDimitry Andric       setStage(VirtReg.reg(), Stage);
9904eeddc0SDimitry Andric     }
10004eeddc0SDimitry Andric 
10104eeddc0SDimitry Andric     /// Return the current stage of the register, if present, otherwise
10204eeddc0SDimitry Andric     /// initialize it and return that.
10304eeddc0SDimitry Andric     LiveRangeStage getOrInitStage(Register Reg) {
10404eeddc0SDimitry Andric       Info.grow(Reg.id());
10504eeddc0SDimitry Andric       return getStage(Reg);
10604eeddc0SDimitry Andric     }
10704eeddc0SDimitry Andric 
10804eeddc0SDimitry Andric     unsigned getCascade(Register Reg) const { return Info[Reg].Cascade; }
10904eeddc0SDimitry Andric 
11004eeddc0SDimitry Andric     void setCascade(Register Reg, unsigned Cascade) {
11104eeddc0SDimitry Andric       Info.grow(Reg.id());
11204eeddc0SDimitry Andric       Info[Reg].Cascade = Cascade;
11304eeddc0SDimitry Andric     }
11404eeddc0SDimitry Andric 
11504eeddc0SDimitry Andric     unsigned getOrAssignNewCascade(Register Reg) {
11604eeddc0SDimitry Andric       unsigned Cascade = getCascade(Reg);
11704eeddc0SDimitry Andric       if (!Cascade) {
11804eeddc0SDimitry Andric         Cascade = NextCascade++;
11904eeddc0SDimitry Andric         setCascade(Reg, Cascade);
12004eeddc0SDimitry Andric       }
12104eeddc0SDimitry Andric       return Cascade;
12204eeddc0SDimitry Andric     }
12304eeddc0SDimitry Andric 
12404eeddc0SDimitry Andric     unsigned getCascadeOrCurrentNext(Register Reg) const {
12504eeddc0SDimitry Andric       unsigned Cascade = getCascade(Reg);
12604eeddc0SDimitry Andric       if (!Cascade)
12704eeddc0SDimitry Andric         Cascade = NextCascade;
12804eeddc0SDimitry Andric       return Cascade;
12904eeddc0SDimitry Andric     }
13004eeddc0SDimitry Andric 
13104eeddc0SDimitry Andric     template <typename Iterator>
13204eeddc0SDimitry Andric     void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) {
13304eeddc0SDimitry Andric       for (; Begin != End; ++Begin) {
13404eeddc0SDimitry Andric         Register Reg = *Begin;
13504eeddc0SDimitry Andric         Info.grow(Reg.id());
13604eeddc0SDimitry Andric         if (Info[Reg].Stage == RS_New)
13704eeddc0SDimitry Andric           Info[Reg].Stage = NewStage;
13804eeddc0SDimitry Andric       }
13904eeddc0SDimitry Andric     }
14004eeddc0SDimitry Andric     void LRE_DidCloneVirtReg(Register New, Register Old);
14104eeddc0SDimitry Andric   };
14204eeddc0SDimitry Andric 
14304eeddc0SDimitry Andric   LiveRegMatrix *getInterferenceMatrix() const { return Matrix; }
14404eeddc0SDimitry Andric   LiveIntervals *getLiveIntervals() const { return LIS; }
14504eeddc0SDimitry Andric   VirtRegMap *getVirtRegMap() const { return VRM; }
14604eeddc0SDimitry Andric   const RegisterClassInfo &getRegClassInfo() const { return RegClassInfo; }
14704eeddc0SDimitry Andric   const ExtraRegInfo &getExtraInfo() const { return *ExtraInfo; }
14804eeddc0SDimitry Andric   size_t getQueueSize() const { return Queue.size(); }
14904eeddc0SDimitry Andric   // end (interface to eviction advisers)
15004eeddc0SDimitry Andric 
151*bdd1243dSDimitry Andric   // Interface to priority advisers
152*bdd1243dSDimitry Andric   bool getRegClassPriorityTrumpsGlobalness() const {
153*bdd1243dSDimitry Andric     return RegClassPriorityTrumpsGlobalness;
154*bdd1243dSDimitry Andric   }
155*bdd1243dSDimitry Andric   bool getReverseLocalAssignment() const { return ReverseLocalAssignment; }
156*bdd1243dSDimitry Andric   // end (interface to priority advisers)
157*bdd1243dSDimitry Andric 
15804eeddc0SDimitry Andric private:
15904eeddc0SDimitry Andric   // Convenient shortcuts.
16004eeddc0SDimitry Andric   using PQueue = std::priority_queue<std::pair<unsigned, unsigned>>;
161*bdd1243dSDimitry Andric   using SmallLISet = SmallSetVector<const LiveInterval *, 4>;
16281ad6265SDimitry Andric 
16381ad6265SDimitry Andric   // We need to track all tentative recolorings so we can roll back any
16481ad6265SDimitry Andric   // successful and unsuccessful recoloring attempts.
16581ad6265SDimitry Andric   using RecoloringStack =
16681ad6265SDimitry Andric       SmallVector<std::pair<const LiveInterval *, MCRegister>, 8>;
16704eeddc0SDimitry Andric 
16804eeddc0SDimitry Andric   // context
16904eeddc0SDimitry Andric   MachineFunction *MF;
17004eeddc0SDimitry Andric 
17104eeddc0SDimitry Andric   // Shortcuts to some useful interface.
17204eeddc0SDimitry Andric   const TargetInstrInfo *TII;
17304eeddc0SDimitry Andric 
17404eeddc0SDimitry Andric   // analyses
17504eeddc0SDimitry Andric   SlotIndexes *Indexes;
17604eeddc0SDimitry Andric   MachineBlockFrequencyInfo *MBFI;
17704eeddc0SDimitry Andric   MachineDominatorTree *DomTree;
17804eeddc0SDimitry Andric   MachineLoopInfo *Loops;
17904eeddc0SDimitry Andric   MachineOptimizationRemarkEmitter *ORE;
18004eeddc0SDimitry Andric   EdgeBundles *Bundles;
18104eeddc0SDimitry Andric   SpillPlacement *SpillPlacer;
18204eeddc0SDimitry Andric   LiveDebugVariables *DebugVars;
18304eeddc0SDimitry Andric 
18404eeddc0SDimitry Andric   // state
18504eeddc0SDimitry Andric   std::unique_ptr<Spiller> SpillerInstance;
18604eeddc0SDimitry Andric   PQueue Queue;
18704eeddc0SDimitry Andric   std::unique_ptr<VirtRegAuxInfo> VRAI;
188*bdd1243dSDimitry Andric   std::optional<ExtraRegInfo> ExtraInfo;
18904eeddc0SDimitry Andric   std::unique_ptr<RegAllocEvictionAdvisor> EvictAdvisor;
19004eeddc0SDimitry Andric 
191*bdd1243dSDimitry Andric   std::unique_ptr<RegAllocPriorityAdvisor> PriorityAdvisor;
192*bdd1243dSDimitry Andric 
19304eeddc0SDimitry Andric   // Enum CutOffStage to keep a track whether the register allocation failed
19404eeddc0SDimitry Andric   // because of the cutoffs encountered in last chance recoloring.
19504eeddc0SDimitry Andric   // Note: This is used as bitmask. New value should be next power of 2.
19604eeddc0SDimitry Andric   enum CutOffStage {
19704eeddc0SDimitry Andric     // No cutoffs encountered
19804eeddc0SDimitry Andric     CO_None = 0,
19904eeddc0SDimitry Andric 
20004eeddc0SDimitry Andric     // lcr-max-depth cutoff encountered
20104eeddc0SDimitry Andric     CO_Depth = 1,
20204eeddc0SDimitry Andric 
20304eeddc0SDimitry Andric     // lcr-max-interf cutoff encountered
20404eeddc0SDimitry Andric     CO_Interf = 2
20504eeddc0SDimitry Andric   };
20604eeddc0SDimitry Andric 
20704eeddc0SDimitry Andric   uint8_t CutOffInfo;
20804eeddc0SDimitry Andric 
20904eeddc0SDimitry Andric #ifndef NDEBUG
21004eeddc0SDimitry Andric   static const char *const StageName[];
21104eeddc0SDimitry Andric #endif
21204eeddc0SDimitry Andric 
21304eeddc0SDimitry Andric   // splitting state.
21404eeddc0SDimitry Andric   std::unique_ptr<SplitAnalysis> SA;
21504eeddc0SDimitry Andric   std::unique_ptr<SplitEditor> SE;
21604eeddc0SDimitry Andric 
21704eeddc0SDimitry Andric   /// Cached per-block interference maps
21804eeddc0SDimitry Andric   InterferenceCache IntfCache;
21904eeddc0SDimitry Andric 
22004eeddc0SDimitry Andric   /// All basic blocks where the current register has uses.
22104eeddc0SDimitry Andric   SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints;
22204eeddc0SDimitry Andric 
22304eeddc0SDimitry Andric   /// Global live range splitting candidate info.
22404eeddc0SDimitry Andric   struct GlobalSplitCandidate {
22504eeddc0SDimitry Andric     // Register intended for assignment, or 0.
22604eeddc0SDimitry Andric     MCRegister PhysReg;
22704eeddc0SDimitry Andric 
22804eeddc0SDimitry Andric     // SplitKit interval index for this candidate.
22904eeddc0SDimitry Andric     unsigned IntvIdx;
23004eeddc0SDimitry Andric 
23104eeddc0SDimitry Andric     // Interference for PhysReg.
23204eeddc0SDimitry Andric     InterferenceCache::Cursor Intf;
23304eeddc0SDimitry Andric 
23404eeddc0SDimitry Andric     // Bundles where this candidate should be live.
23504eeddc0SDimitry Andric     BitVector LiveBundles;
23604eeddc0SDimitry Andric     SmallVector<unsigned, 8> ActiveBlocks;
23704eeddc0SDimitry Andric 
23804eeddc0SDimitry Andric     void reset(InterferenceCache &Cache, MCRegister Reg) {
23904eeddc0SDimitry Andric       PhysReg = Reg;
24004eeddc0SDimitry Andric       IntvIdx = 0;
24104eeddc0SDimitry Andric       Intf.setPhysReg(Cache, Reg);
24204eeddc0SDimitry Andric       LiveBundles.clear();
24304eeddc0SDimitry Andric       ActiveBlocks.clear();
24404eeddc0SDimitry Andric     }
24504eeddc0SDimitry Andric 
24604eeddc0SDimitry Andric     // Set B[I] = C for every live bundle where B[I] was NoCand.
24704eeddc0SDimitry Andric     unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) {
24804eeddc0SDimitry Andric       unsigned Count = 0;
24904eeddc0SDimitry Andric       for (unsigned I : LiveBundles.set_bits())
25004eeddc0SDimitry Andric         if (B[I] == NoCand) {
25104eeddc0SDimitry Andric           B[I] = C;
25204eeddc0SDimitry Andric           Count++;
25304eeddc0SDimitry Andric         }
25404eeddc0SDimitry Andric       return Count;
25504eeddc0SDimitry Andric     }
25604eeddc0SDimitry Andric   };
25704eeddc0SDimitry Andric 
25804eeddc0SDimitry Andric   /// Candidate info for each PhysReg in AllocationOrder.
25904eeddc0SDimitry Andric   /// This vector never shrinks, but grows to the size of the largest register
26004eeddc0SDimitry Andric   /// class.
26104eeddc0SDimitry Andric   SmallVector<GlobalSplitCandidate, 32> GlobalCand;
26204eeddc0SDimitry Andric 
26304eeddc0SDimitry Andric   enum : unsigned { NoCand = ~0u };
26404eeddc0SDimitry Andric 
26504eeddc0SDimitry Andric   /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to
26604eeddc0SDimitry Andric   /// NoCand which indicates the stack interval.
26704eeddc0SDimitry Andric   SmallVector<unsigned, 32> BundleCand;
26804eeddc0SDimitry Andric 
26904eeddc0SDimitry Andric   /// Callee-save register cost, calculated once per machine function.
27004eeddc0SDimitry Andric   BlockFrequency CSRCost;
27104eeddc0SDimitry Andric 
27204eeddc0SDimitry Andric   /// Set of broken hints that may be reconciled later because of eviction.
27381ad6265SDimitry Andric   SmallSetVector<const LiveInterval *, 8> SetOfBrokenHints;
27404eeddc0SDimitry Andric 
27504eeddc0SDimitry Andric   /// The register cost values. This list will be recreated for each Machine
27604eeddc0SDimitry Andric   /// Function
27704eeddc0SDimitry Andric   ArrayRef<uint8_t> RegCosts;
27804eeddc0SDimitry Andric 
27981ad6265SDimitry Andric   /// Flags for the live range priority calculation, determined once per
28081ad6265SDimitry Andric   /// machine function.
28181ad6265SDimitry Andric   bool RegClassPriorityTrumpsGlobalness;
28281ad6265SDimitry Andric 
283972a253aSDimitry Andric   bool ReverseLocalAssignment;
284972a253aSDimitry Andric 
28504eeddc0SDimitry Andric public:
28604eeddc0SDimitry Andric   RAGreedy(const RegClassFilterFunc F = allocateAllRegClasses);
28704eeddc0SDimitry Andric 
28804eeddc0SDimitry Andric   /// Return the pass name.
28904eeddc0SDimitry Andric   StringRef getPassName() const override { return "Greedy Register Allocator"; }
29004eeddc0SDimitry Andric 
29104eeddc0SDimitry Andric   /// RAGreedy analysis usage.
29204eeddc0SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override;
29304eeddc0SDimitry Andric   void releaseMemory() override;
29404eeddc0SDimitry Andric   Spiller &spiller() override { return *SpillerInstance; }
29581ad6265SDimitry Andric   void enqueueImpl(const LiveInterval *LI) override;
29681ad6265SDimitry Andric   const LiveInterval *dequeue() override;
29781ad6265SDimitry Andric   MCRegister selectOrSplit(const LiveInterval &,
29804eeddc0SDimitry Andric                            SmallVectorImpl<Register> &) override;
29981ad6265SDimitry Andric   void aboutToRemoveInterval(const LiveInterval &) override;
30004eeddc0SDimitry Andric 
30104eeddc0SDimitry Andric   /// Perform register allocation.
30204eeddc0SDimitry Andric   bool runOnMachineFunction(MachineFunction &mf) override;
30304eeddc0SDimitry Andric 
30404eeddc0SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
30504eeddc0SDimitry Andric     return MachineFunctionProperties().set(
30604eeddc0SDimitry Andric         MachineFunctionProperties::Property::NoPHIs);
30704eeddc0SDimitry Andric   }
30804eeddc0SDimitry Andric 
30904eeddc0SDimitry Andric   MachineFunctionProperties getClearedProperties() const override {
31004eeddc0SDimitry Andric     return MachineFunctionProperties().set(
31104eeddc0SDimitry Andric         MachineFunctionProperties::Property::IsSSA);
31204eeddc0SDimitry Andric   }
31304eeddc0SDimitry Andric 
31404eeddc0SDimitry Andric   static char ID;
31504eeddc0SDimitry Andric 
31604eeddc0SDimitry Andric private:
31781ad6265SDimitry Andric   MCRegister selectOrSplitImpl(const LiveInterval &,
31881ad6265SDimitry Andric                                SmallVectorImpl<Register> &, SmallVirtRegSet &,
31981ad6265SDimitry Andric                                RecoloringStack &, unsigned = 0);
32004eeddc0SDimitry Andric 
32104eeddc0SDimitry Andric   bool LRE_CanEraseVirtReg(Register) override;
32204eeddc0SDimitry Andric   void LRE_WillShrinkVirtReg(Register) override;
32304eeddc0SDimitry Andric   void LRE_DidCloneVirtReg(Register, Register) override;
32481ad6265SDimitry Andric   void enqueue(PQueue &CurQueue, const LiveInterval *LI);
32581ad6265SDimitry Andric   const LiveInterval *dequeue(PQueue &CurQueue);
32604eeddc0SDimitry Andric 
32781ad6265SDimitry Andric   bool hasVirtRegAlloc();
32804eeddc0SDimitry Andric   BlockFrequency calcSpillCost();
32904eeddc0SDimitry Andric   bool addSplitConstraints(InterferenceCache::Cursor, BlockFrequency &);
33004eeddc0SDimitry Andric   bool addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>);
33104eeddc0SDimitry Andric   bool growRegion(GlobalSplitCandidate &Cand);
33204eeddc0SDimitry Andric   BlockFrequency calcGlobalSplitCost(GlobalSplitCandidate &,
33381ad6265SDimitry Andric                                      const AllocationOrder &Order);
33404eeddc0SDimitry Andric   bool calcCompactRegion(GlobalSplitCandidate &);
33504eeddc0SDimitry Andric   void splitAroundRegion(LiveRangeEdit &, ArrayRef<unsigned>);
33604eeddc0SDimitry Andric   void calcGapWeights(MCRegister, SmallVectorImpl<float> &);
33781ad6265SDimitry Andric   void evictInterference(const LiveInterval &, MCRegister,
33804eeddc0SDimitry Andric                          SmallVectorImpl<Register> &);
33981ad6265SDimitry Andric   bool mayRecolorAllInterferences(MCRegister PhysReg,
34081ad6265SDimitry Andric                                   const LiveInterval &VirtReg,
34104eeddc0SDimitry Andric                                   SmallLISet &RecoloringCandidates,
34204eeddc0SDimitry Andric                                   const SmallVirtRegSet &FixedRegisters);
34304eeddc0SDimitry Andric 
34481ad6265SDimitry Andric   MCRegister tryAssign(const LiveInterval &, AllocationOrder &,
34504eeddc0SDimitry Andric                        SmallVectorImpl<Register> &, const SmallVirtRegSet &);
34681ad6265SDimitry Andric   MCRegister tryEvict(const LiveInterval &, AllocationOrder &,
34704eeddc0SDimitry Andric                       SmallVectorImpl<Register> &, uint8_t,
34804eeddc0SDimitry Andric                       const SmallVirtRegSet &);
34981ad6265SDimitry Andric   MCRegister tryRegionSplit(const LiveInterval &, AllocationOrder &,
35004eeddc0SDimitry Andric                             SmallVectorImpl<Register> &);
35104eeddc0SDimitry Andric   /// Calculate cost of region splitting.
35281ad6265SDimitry Andric   unsigned calculateRegionSplitCost(const LiveInterval &VirtReg,
35304eeddc0SDimitry Andric                                     AllocationOrder &Order,
35404eeddc0SDimitry Andric                                     BlockFrequency &BestCost,
35581ad6265SDimitry Andric                                     unsigned &NumCands, bool IgnoreCSR);
35604eeddc0SDimitry Andric   /// Perform region splitting.
35781ad6265SDimitry Andric   unsigned doRegionSplit(const LiveInterval &VirtReg, unsigned BestCand,
35804eeddc0SDimitry Andric                          bool HasCompact, SmallVectorImpl<Register> &NewVRegs);
35904eeddc0SDimitry Andric   /// Check other options before using a callee-saved register for the first
36004eeddc0SDimitry Andric   /// time.
36181ad6265SDimitry Andric   MCRegister tryAssignCSRFirstTime(const LiveInterval &VirtReg,
36204eeddc0SDimitry Andric                                    AllocationOrder &Order, MCRegister PhysReg,
36304eeddc0SDimitry Andric                                    uint8_t &CostPerUseLimit,
36404eeddc0SDimitry Andric                                    SmallVectorImpl<Register> &NewVRegs);
36504eeddc0SDimitry Andric   void initializeCSRCost();
36681ad6265SDimitry Andric   unsigned tryBlockSplit(const LiveInterval &, AllocationOrder &,
36704eeddc0SDimitry Andric                          SmallVectorImpl<Register> &);
36881ad6265SDimitry Andric   unsigned tryInstructionSplit(const LiveInterval &, AllocationOrder &,
36904eeddc0SDimitry Andric                                SmallVectorImpl<Register> &);
37081ad6265SDimitry Andric   unsigned tryLocalSplit(const LiveInterval &, AllocationOrder &,
37104eeddc0SDimitry Andric                          SmallVectorImpl<Register> &);
37281ad6265SDimitry Andric   unsigned trySplit(const LiveInterval &, AllocationOrder &,
37304eeddc0SDimitry Andric                     SmallVectorImpl<Register> &, const SmallVirtRegSet &);
37481ad6265SDimitry Andric   unsigned tryLastChanceRecoloring(const LiveInterval &, AllocationOrder &,
37504eeddc0SDimitry Andric                                    SmallVectorImpl<Register> &,
37681ad6265SDimitry Andric                                    SmallVirtRegSet &, RecoloringStack &,
37781ad6265SDimitry Andric                                    unsigned);
37804eeddc0SDimitry Andric   bool tryRecoloringCandidates(PQueue &, SmallVectorImpl<Register> &,
37981ad6265SDimitry Andric                                SmallVirtRegSet &, RecoloringStack &, unsigned);
38081ad6265SDimitry Andric   void tryHintRecoloring(const LiveInterval &);
38104eeddc0SDimitry Andric   void tryHintsRecoloring();
38204eeddc0SDimitry Andric 
38304eeddc0SDimitry Andric   /// Model the information carried by one end of a copy.
38404eeddc0SDimitry Andric   struct HintInfo {
38504eeddc0SDimitry Andric     /// The frequency of the copy.
38604eeddc0SDimitry Andric     BlockFrequency Freq;
38704eeddc0SDimitry Andric     /// The virtual register or physical register.
38804eeddc0SDimitry Andric     Register Reg;
38904eeddc0SDimitry Andric     /// Its currently assigned register.
39004eeddc0SDimitry Andric     /// In case of a physical register Reg == PhysReg.
39104eeddc0SDimitry Andric     MCRegister PhysReg;
39204eeddc0SDimitry Andric 
39304eeddc0SDimitry Andric     HintInfo(BlockFrequency Freq, Register Reg, MCRegister PhysReg)
39404eeddc0SDimitry Andric         : Freq(Freq), Reg(Reg), PhysReg(PhysReg) {}
39504eeddc0SDimitry Andric   };
39604eeddc0SDimitry Andric   using HintsInfo = SmallVector<HintInfo, 4>;
39704eeddc0SDimitry Andric 
39804eeddc0SDimitry Andric   BlockFrequency getBrokenHintFreq(const HintsInfo &, MCRegister);
39904eeddc0SDimitry Andric   void collectHintInfo(Register, HintsInfo &);
40004eeddc0SDimitry Andric 
40104eeddc0SDimitry Andric   /// Greedy RA statistic to remark.
40204eeddc0SDimitry Andric   struct RAGreedyStats {
40304eeddc0SDimitry Andric     unsigned Reloads = 0;
40404eeddc0SDimitry Andric     unsigned FoldedReloads = 0;
40504eeddc0SDimitry Andric     unsigned ZeroCostFoldedReloads = 0;
40604eeddc0SDimitry Andric     unsigned Spills = 0;
40704eeddc0SDimitry Andric     unsigned FoldedSpills = 0;
40804eeddc0SDimitry Andric     unsigned Copies = 0;
40904eeddc0SDimitry Andric     float ReloadsCost = 0.0f;
41004eeddc0SDimitry Andric     float FoldedReloadsCost = 0.0f;
41104eeddc0SDimitry Andric     float SpillsCost = 0.0f;
41204eeddc0SDimitry Andric     float FoldedSpillsCost = 0.0f;
41304eeddc0SDimitry Andric     float CopiesCost = 0.0f;
41404eeddc0SDimitry Andric 
41504eeddc0SDimitry Andric     bool isEmpty() {
41604eeddc0SDimitry Andric       return !(Reloads || FoldedReloads || Spills || FoldedSpills ||
41704eeddc0SDimitry Andric                ZeroCostFoldedReloads || Copies);
41804eeddc0SDimitry Andric     }
41904eeddc0SDimitry Andric 
42004eeddc0SDimitry Andric     void add(RAGreedyStats other) {
42104eeddc0SDimitry Andric       Reloads += other.Reloads;
42204eeddc0SDimitry Andric       FoldedReloads += other.FoldedReloads;
42304eeddc0SDimitry Andric       ZeroCostFoldedReloads += other.ZeroCostFoldedReloads;
42404eeddc0SDimitry Andric       Spills += other.Spills;
42504eeddc0SDimitry Andric       FoldedSpills += other.FoldedSpills;
42604eeddc0SDimitry Andric       Copies += other.Copies;
42704eeddc0SDimitry Andric       ReloadsCost += other.ReloadsCost;
42804eeddc0SDimitry Andric       FoldedReloadsCost += other.FoldedReloadsCost;
42904eeddc0SDimitry Andric       SpillsCost += other.SpillsCost;
43004eeddc0SDimitry Andric       FoldedSpillsCost += other.FoldedSpillsCost;
43104eeddc0SDimitry Andric       CopiesCost += other.CopiesCost;
43204eeddc0SDimitry Andric     }
43304eeddc0SDimitry Andric 
43404eeddc0SDimitry Andric     void report(MachineOptimizationRemarkMissed &R);
43504eeddc0SDimitry Andric   };
43604eeddc0SDimitry Andric 
43704eeddc0SDimitry Andric   /// Compute statistic for a basic block.
43804eeddc0SDimitry Andric   RAGreedyStats computeStats(MachineBasicBlock &MBB);
43904eeddc0SDimitry Andric 
44004eeddc0SDimitry Andric   /// Compute and report statistic through a remark.
44104eeddc0SDimitry Andric   RAGreedyStats reportStats(MachineLoop *L);
44204eeddc0SDimitry Andric 
44304eeddc0SDimitry Andric   /// Report the statistic for each loop.
44404eeddc0SDimitry Andric   void reportStats();
44504eeddc0SDimitry Andric };
44604eeddc0SDimitry Andric } // namespace llvm
44704eeddc0SDimitry Andric #endif // #ifndef LLVM_CODEGEN_REGALLOCGREEDY_H_
448