1 //===- RegAllocEvictionAdvisor.cpp - eviction advisor ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implementation of the default eviction advisor and of the Analysis pass.
11 //===----------------------------------------------------------------------===//
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"
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"),
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")));
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"),
45 #define DEBUG_TYPE "regalloc"
47 char RegAllocEvictionAdvisorAnalysis::ID
= 0;
48 INITIALIZE_PASS(RegAllocEvictionAdvisorAnalysis
, "regalloc-evict",
49 "Regalloc eviction policy", false, true)
52 class DefaultEvictionAdvisorAnalysis final
53 : public RegAllocEvictionAdvisorAnalysis
{
55 DefaultEvictionAdvisorAnalysis(bool NotAsRequested
)
56 : RegAllocEvictionAdvisorAnalysis(AdvisorMode::Default
),
57 NotAsRequested(NotAsRequested
) {}
59 // support for isa<> and dyn_cast.
60 static bool classof(const RegAllocEvictionAdvisorAnalysis
*R
) {
61 return R
->getAdvisorMode() == AdvisorMode::Default
;
65 std::unique_ptr
<RegAllocEvictionAdvisor
>
66 getAdvisor(const MachineFunction
&MF
, const RAGreedy
&RA
) override
{
67 return std::make_unique
<DefaultEvictionAdvisor
>(MF
, RA
);
69 bool doInitialization(Module
&M
) override
{
71 M
.getContext().emitError("Requested regalloc eviction advisor analysis "
72 "could be created. Using default");
73 return RegAllocEvictionAdvisorAnalysis::doInitialization(M
);
75 const bool NotAsRequested
;
79 template <> Pass
*llvm::callDefaultCtor
<RegAllocEvictionAdvisorAnalysis
>() {
82 case RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default
:
83 Ret
= new DefaultEvictionAdvisorAnalysis(/*NotAsRequested*/ false);
85 case RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development
:
86 // TODO(mtrofin): add implementation
88 case RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release
:
89 // TODO(mtrofin): add implementation
94 return new DefaultEvictionAdvisorAnalysis(/*NotAsRequested*/ true);
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";
106 llvm_unreachable("Unknown advisor kind");
109 RegAllocEvictionAdvisor::RegAllocEvictionAdvisor(const MachineFunction
&MF
,
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())) {}