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