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