xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/RegAllocPriorityAdvisor.cpp (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 //===- RegAllocPriorityAdvisor.cpp - live ranges priority 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 priority advisor and of the Analysis pass.
10 //
11 //===----------------------------------------------------------------------===//
12 
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"
20 
21 using namespace llvm;
22 
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"),
27     cl::values(
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")));
34 
35 char RegAllocPriorityAdvisorAnalysis::ID = 0;
36 INITIALIZE_PASS(RegAllocPriorityAdvisorAnalysis, "regalloc-priority",
37                 "Regalloc priority policy", false, true)
38 
39 namespace {
40 class DefaultPriorityAdvisorAnalysis final
41     : public RegAllocPriorityAdvisorAnalysis {
42 public:
43   DefaultPriorityAdvisorAnalysis(bool NotAsRequested)
44       : RegAllocPriorityAdvisorAnalysis(AdvisorMode::Default),
45         NotAsRequested(NotAsRequested) {}
46 
47   // support for isa<> and dyn_cast.
48   static bool classof(const RegAllocPriorityAdvisorAnalysis *R) {
49     return R->getAdvisorMode() == AdvisorMode::Default;
50   }
51 
52 private:
53   void getAnalysisUsage(AnalysisUsage &AU) const override {
54     AU.addRequired<SlotIndexes>();
55     RegAllocPriorityAdvisorAnalysis::getAnalysisUsage(AU);
56   }
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>());
61   }
62   bool doInitialization(Module &M) override {
63     if (NotAsRequested)
64       M.getContext().emitError("Requested regalloc priority advisor analysis "
65                                "could be created. Using default");
66     return RegAllocPriorityAdvisorAnalysis::doInitialization(M);
67   }
68   const bool NotAsRequested;
69 };
70 } // namespace
71 
72 template <> Pass *llvm::callDefaultCtor<RegAllocPriorityAdvisorAnalysis>() {
73   Pass *Ret = nullptr;
74   switch (Mode) {
75   case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Default:
76     Ret = new DefaultPriorityAdvisorAnalysis(/*NotAsRequested*/ false);
77     break;
78   case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Development:
79 #if defined(LLVM_HAVE_TFLITE)
80     Ret = createDevelopmentModePriorityAdvisor();
81 #endif
82     break;
83   case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Release:
84     Ret = createReleaseModePriorityAdvisor();
85     break;
86   }
87   if (Ret)
88     return Ret;
89   return new DefaultPriorityAdvisorAnalysis(/*NotAsRequested*/ true);
90 }
91 
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";
100   }
101   llvm_unreachable("Unknown advisor kind");
102 }
103 
104 RegAllocPriorityAdvisor::RegAllocPriorityAdvisor(const MachineFunction &MF,
105                                                  const RAGreedy &RA,
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()) {}
113