1 //===- RegAllocPriorityAdvisor.cpp - live ranges priority 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 priority advisor and of the Analysis pass.
11 //===----------------------------------------------------------------------===//
13 #include "RegAllocPriorityAdvisor.h"
14 #include "RegAllocGreedy.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/VirtRegMap.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Pass.h"
23 static cl::opt
<RegAllocPriorityAdvisorAnalysis::AdvisorMode
> Mode(
24 "regalloc-enable-priority-advisor", cl::Hidden
,
25 cl::init(RegAllocPriorityAdvisorAnalysis::AdvisorMode::Default
),
26 cl::desc("Enable regalloc advisor mode"),
28 clEnumValN(RegAllocPriorityAdvisorAnalysis::AdvisorMode::Default
,
29 "default", "Default"),
30 clEnumValN(RegAllocPriorityAdvisorAnalysis::AdvisorMode::Release
,
31 "release", "precompiled"),
32 clEnumValN(RegAllocPriorityAdvisorAnalysis::AdvisorMode::Development
,
33 "development", "for training")));
35 char RegAllocPriorityAdvisorAnalysis::ID
= 0;
36 INITIALIZE_PASS(RegAllocPriorityAdvisorAnalysis
, "regalloc-priority",
37 "Regalloc priority policy", false, true)
40 class DefaultPriorityAdvisorAnalysis final
41 : public RegAllocPriorityAdvisorAnalysis
{
43 DefaultPriorityAdvisorAnalysis(bool NotAsRequested
)
44 : RegAllocPriorityAdvisorAnalysis(AdvisorMode::Default
),
45 NotAsRequested(NotAsRequested
) {}
47 // support for isa<> and dyn_cast.
48 static bool classof(const RegAllocPriorityAdvisorAnalysis
*R
) {
49 return R
->getAdvisorMode() == AdvisorMode::Default
;
53 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
54 AU
.addRequired
<SlotIndexes
>();
55 RegAllocPriorityAdvisorAnalysis::getAnalysisUsage(AU
);
57 std::unique_ptr
<RegAllocPriorityAdvisor
>
58 getAdvisor(const MachineFunction
&MF
, const RAGreedy
&RA
) override
{
59 return std::make_unique
<DefaultPriorityAdvisor
>(
60 MF
, RA
, &getAnalysis
<SlotIndexes
>());
62 bool doInitialization(Module
&M
) override
{
64 M
.getContext().emitError("Requested regalloc priority advisor analysis "
65 "could be created. Using default");
66 return RegAllocPriorityAdvisorAnalysis::doInitialization(M
);
68 const bool NotAsRequested
;
72 template <> Pass
*llvm::callDefaultCtor
<RegAllocPriorityAdvisorAnalysis
>() {
75 case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Default
:
76 Ret
= new DefaultPriorityAdvisorAnalysis(/*NotAsRequested*/ false);
78 case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Development
:
79 #if defined(LLVM_HAVE_TFLITE)
80 Ret
= createDevelopmentModePriorityAdvisor();
83 case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Release
:
84 Ret
= createReleaseModePriorityAdvisor();
89 return new DefaultPriorityAdvisorAnalysis(/*NotAsRequested*/ true);
92 StringRef
RegAllocPriorityAdvisorAnalysis::getPassName() const {
93 switch (getAdvisorMode()) {
94 case AdvisorMode::Default
:
95 return "Default Regalloc Priority Advisor";
96 case AdvisorMode::Release
:
97 return "Release mode Regalloc Priority Advisor";
98 case AdvisorMode::Development
:
99 return "Development mode Regalloc Priority Advisor";
101 llvm_unreachable("Unknown advisor kind");
104 RegAllocPriorityAdvisor::RegAllocPriorityAdvisor(const MachineFunction
&MF
,
106 SlotIndexes
*const Indexes
)
107 : RA(RA
), LIS(RA
.getLiveIntervals()), VRM(RA
.getVirtRegMap()),
108 MRI(&VRM
->getRegInfo()), TRI(MF
.getSubtarget().getRegisterInfo()),
109 RegClassInfo(RA
.getRegClassInfo()), Indexes(Indexes
),
110 RegClassPriorityTrumpsGlobalness(
111 RA
.getRegClassPriorityTrumpsGlobalness()),
112 ReverseLocalAssignment(RA
.getReverseLocalAssignment()) {}