xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/RegAllocEvictionAdvisor.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1 //===- RegAllocEvictionAdvisor.cpp - eviction advisor ---------------------===//
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 // Implementation of the default eviction advisor and of the Analysis pass.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RegAllocEvictionAdvisor.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/RegisterClassInfo.h"
16 #include "llvm/CodeGen/VirtRegMap.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
19 #include "llvm/PassRegistry.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Target/TargetMachine.h"
23 
24 using namespace llvm;
25 
26 static cl::opt<RegAllocEvictionAdvisorAnalysis::AdvisorMode> Mode(
27     "regalloc-enable-advisor", cl::Hidden,
28     cl::init(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default),
29     cl::desc("Enable regalloc advisor mode"),
30     cl::values(
31         clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default,
32                    "default", "Default"),
33         clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release,
34                    "release", "precompiled"),
35         clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development,
36                    "development", "for training")));
37 
38 static cl::opt<bool> EnableLocalReassignment(
39     "enable-local-reassign", cl::Hidden,
40     cl::desc("Local reassignment can yield better allocation decisions, but "
41              "may be compile time intensive"),
42     cl::init(false));
43 
44 #define DEBUG_TYPE "regalloc"
45 
46 char RegAllocEvictionAdvisorAnalysis::ID = 0;
47 INITIALIZE_PASS(RegAllocEvictionAdvisorAnalysis, "regalloc-evict",
48                 "Regalloc eviction policy", false, true)
49 
50 namespace {
51 class DefaultEvictionAdvisorAnalysis final
52     : public RegAllocEvictionAdvisorAnalysis {
53 public:
54   DefaultEvictionAdvisorAnalysis(bool NotAsRequested)
55       : RegAllocEvictionAdvisorAnalysis(AdvisorMode::Default),
56         NotAsRequested(NotAsRequested) {}
57 
58   // support for isa<> and dyn_cast.
59   static bool classof(const RegAllocEvictionAdvisorAnalysis *R) {
60     return R->getAdvisorMode() == AdvisorMode::Default;
61   }
62 
63 private:
64   std::unique_ptr<RegAllocEvictionAdvisor>
65   getAdvisor(const MachineFunction &MF, LiveRegMatrix *Matrix,
66              LiveIntervals *LIS, VirtRegMap *VRM,
67              const RegisterClassInfo &RegClassInfo,
68              ExtraRegInfo *ExtraInfo) override {
69     return std::make_unique<DefaultEvictionAdvisor>(MF, Matrix, LIS, VRM,
70                                                     RegClassInfo, ExtraInfo);
71   }
72   bool doInitialization(Module &M) override {
73     if (NotAsRequested)
74       M.getContext().emitError("Requested regalloc eviction advisor analysis "
75                                "could be created. Using default");
76     return RegAllocEvictionAdvisorAnalysis::doInitialization(M);
77   }
78   const bool NotAsRequested;
79 };
80 } // namespace
81 
82 template <> Pass *llvm::callDefaultCtor<RegAllocEvictionAdvisorAnalysis>() {
83   Pass *Ret = nullptr;
84   switch (Mode) {
85   case RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default:
86     Ret = new DefaultEvictionAdvisorAnalysis(/*NotAsRequested*/ false);
87     break;
88   case RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development:
89     // TODO(mtrofin): add implementation
90     break;
91   case RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release:
92     // TODO(mtrofin): add implementation
93     break;
94   }
95   if (Ret)
96     return Ret;
97   return new DefaultEvictionAdvisorAnalysis(/*NotAsRequested*/ true);
98 }
99 
100 StringRef RegAllocEvictionAdvisorAnalysis::getPassName() const {
101   switch (getAdvisorMode()) {
102   case AdvisorMode::Default:
103     return "Default Regalloc Eviction Advisor";
104   case AdvisorMode::Release:
105     return "Release mode Regalloc Eviction Advisor";
106   case AdvisorMode::Development:
107     return "Development mode Regalloc Eviction Advisor";
108   }
109   llvm_unreachable("Unknown advisor kind");
110 }
111 
112 RegAllocEvictionAdvisor::RegAllocEvictionAdvisor(
113     const MachineFunction &MF, LiveRegMatrix *Matrix, LiveIntervals *LIS,
114     VirtRegMap *VRM, const RegisterClassInfo &RegClassInfo,
115     ExtraRegInfo *ExtraInfo)
116     : MF(MF), Matrix(Matrix), LIS(LIS), VRM(VRM), MRI(&VRM->getRegInfo()),
117       TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RegClassInfo),
118       RegCosts(TRI->getRegisterCosts(MF)), ExtraInfo(ExtraInfo),
119       EnableLocalReassign(EnableLocalReassignment ||
120                           MF.getSubtarget().enableRALocalReassignment(
121                               MF.getTarget().getOptLevel())) {}
122