xref: /llvm-project/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h (revision 11e482c4a32be6a315e5bf2ae7599cf10eb84836)
1ad8eb855SEric Wang //===- RegAllocPriorityAdvisor.h - live ranges priority advisor -*- C++ -*-===//
2ad8eb855SEric Wang //
3ad8eb855SEric Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ad8eb855SEric Wang // See https://llvm.org/LICENSE.txt for license information.
5ad8eb855SEric Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ad8eb855SEric Wang //
7ad8eb855SEric Wang //===----------------------------------------------------------------------===//
8ad8eb855SEric Wang 
9ad8eb855SEric Wang #ifndef LLVM_CODEGEN_REGALLOCPRIORITYADVISOR_H
10ad8eb855SEric Wang #define LLVM_CODEGEN_REGALLOCPRIORITYADVISOR_H
11ad8eb855SEric Wang 
12ad8eb855SEric Wang #include "RegAllocEvictionAdvisor.h"
13ad8eb855SEric Wang #include "llvm/CodeGen/SlotIndexes.h"
14ad8eb855SEric Wang #include "llvm/Pass.h"
15ad8eb855SEric Wang 
16ad8eb855SEric Wang namespace llvm {
17ad8eb855SEric Wang 
18ad8eb855SEric Wang class MachineFunction;
19ad8eb855SEric Wang class VirtRegMap;
20ad8eb855SEric Wang class RAGreedy;
21ad8eb855SEric Wang 
22ad8eb855SEric Wang /// Interface to the priority advisor, which is responsible for prioritizing
23ad8eb855SEric Wang /// live ranges.
24ad8eb855SEric Wang class RegAllocPriorityAdvisor {
25ad8eb855SEric Wang public:
26ad8eb855SEric Wang   RegAllocPriorityAdvisor(const RegAllocPriorityAdvisor &) = delete;
27ad8eb855SEric Wang   RegAllocPriorityAdvisor(RegAllocPriorityAdvisor &&) = delete;
28ad8eb855SEric Wang   virtual ~RegAllocPriorityAdvisor() = default;
29ad8eb855SEric Wang 
30ad8eb855SEric Wang   /// Find the priority value for a live range. A float value is used since ML
31ad8eb855SEric Wang   /// prefers it.
32ad8eb855SEric Wang   virtual unsigned getPriority(const LiveInterval &LI) const = 0;
33ad8eb855SEric Wang 
34d8a2d3f7SEric Wang   RegAllocPriorityAdvisor(const MachineFunction &MF, const RAGreedy &RA,
35d8a2d3f7SEric Wang                           SlotIndexes *const Indexes);
36ad8eb855SEric Wang 
37ad8eb855SEric Wang protected:
38ad8eb855SEric Wang   const RAGreedy &RA;
39ad8eb855SEric Wang   LiveIntervals *const LIS;
40ad8eb855SEric Wang   VirtRegMap *const VRM;
41ad8eb855SEric Wang   MachineRegisterInfo *const MRI;
42ad8eb855SEric Wang   const TargetRegisterInfo *const TRI;
43ad8eb855SEric Wang   const RegisterClassInfo &RegClassInfo;
44ad8eb855SEric Wang   SlotIndexes *const Indexes;
45ad8eb855SEric Wang   const bool RegClassPriorityTrumpsGlobalness;
46ad8eb855SEric Wang   const bool ReverseLocalAssignment;
47ad8eb855SEric Wang };
48ad8eb855SEric Wang 
49ad8eb855SEric Wang class DefaultPriorityAdvisor : public RegAllocPriorityAdvisor {
50ad8eb855SEric Wang public:
51d8a2d3f7SEric Wang   DefaultPriorityAdvisor(const MachineFunction &MF, const RAGreedy &RA,
52d8a2d3f7SEric Wang                          SlotIndexes *const Indexes)
53d8a2d3f7SEric Wang       : RegAllocPriorityAdvisor(MF, RA, Indexes) {}
54ad8eb855SEric Wang 
55ad8eb855SEric Wang private:
56ad8eb855SEric Wang   unsigned getPriority(const LiveInterval &LI) const override;
57ad8eb855SEric Wang };
58d8a2d3f7SEric Wang 
59*11e482c4SMatt Arsenault /// Stupid priority advisor which just enqueues in virtual register number
60*11e482c4SMatt Arsenault /// order, for debug purposes only.
61*11e482c4SMatt Arsenault class DummyPriorityAdvisor : public RegAllocPriorityAdvisor {
62*11e482c4SMatt Arsenault public:
63*11e482c4SMatt Arsenault   DummyPriorityAdvisor(const MachineFunction &MF, const RAGreedy &RA,
64*11e482c4SMatt Arsenault                        SlotIndexes *const Indexes)
65*11e482c4SMatt Arsenault       : RegAllocPriorityAdvisor(MF, RA, Indexes) {}
66*11e482c4SMatt Arsenault 
67*11e482c4SMatt Arsenault private:
68*11e482c4SMatt Arsenault   unsigned getPriority(const LiveInterval &LI) const override;
69*11e482c4SMatt Arsenault };
70*11e482c4SMatt Arsenault 
71d8a2d3f7SEric Wang class RegAllocPriorityAdvisorAnalysis : public ImmutablePass {
72d8a2d3f7SEric Wang public:
73*11e482c4SMatt Arsenault   enum class AdvisorMode : int { Default, Release, Development, Dummy };
74d8a2d3f7SEric Wang 
75d8a2d3f7SEric Wang   RegAllocPriorityAdvisorAnalysis(AdvisorMode Mode)
76d8a2d3f7SEric Wang       : ImmutablePass(ID), Mode(Mode){};
77d8a2d3f7SEric Wang   static char ID;
78d8a2d3f7SEric Wang 
79d8a2d3f7SEric Wang   /// Get an advisor for the given context (i.e. machine function, etc)
80d8a2d3f7SEric Wang   virtual std::unique_ptr<RegAllocPriorityAdvisor>
81d8a2d3f7SEric Wang   getAdvisor(const MachineFunction &MF, const RAGreedy &RA) = 0;
82d8a2d3f7SEric Wang   AdvisorMode getAdvisorMode() const { return Mode; }
838a24e0cbSMircea Trofin   virtual void logRewardIfNeeded(const MachineFunction &MF,
848a24e0cbSMircea Trofin                                  llvm::function_ref<float()> GetReward){};
85d8a2d3f7SEric Wang 
86d8a2d3f7SEric Wang protected:
87d8a2d3f7SEric Wang   // This analysis preserves everything, and subclasses may have additional
88d8a2d3f7SEric Wang   // requirements.
89d8a2d3f7SEric Wang   void getAnalysisUsage(AnalysisUsage &AU) const override {
90d8a2d3f7SEric Wang     AU.setPreservesAll();
91d8a2d3f7SEric Wang   }
92d8a2d3f7SEric Wang 
93d8a2d3f7SEric Wang private:
94d8a2d3f7SEric Wang   StringRef getPassName() const override;
95d8a2d3f7SEric Wang   const AdvisorMode Mode;
96d8a2d3f7SEric Wang };
97d8a2d3f7SEric Wang 
98d8a2d3f7SEric Wang /// Specialization for the API used by the analysis infrastructure to create
99d8a2d3f7SEric Wang /// an instance of the priority advisor.
100d8a2d3f7SEric Wang template <> Pass *callDefaultCtor<RegAllocPriorityAdvisorAnalysis>();
101d8a2d3f7SEric Wang 
102d8a2d3f7SEric Wang RegAllocPriorityAdvisorAnalysis *createReleaseModePriorityAdvisor();
103d8a2d3f7SEric Wang 
104d8a2d3f7SEric Wang RegAllocPriorityAdvisorAnalysis *createDevelopmentModePriorityAdvisor();
105d8a2d3f7SEric Wang 
106ad8eb855SEric Wang } // namespace llvm
107ad8eb855SEric Wang 
108ad8eb855SEric Wang #endif // LLVM_CODEGEN_REGALLOCPRIORITYADVISOR_H
109