RegAllocPriorityAdvisor.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "RegAllocPriorityAdvisor.h"
  13. #include "RegAllocGreedy.h"
  14. #include "llvm/CodeGen/MachineFunction.h"
  15. #include "llvm/CodeGen/VirtRegMap.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/Pass.h"
  19. using namespace llvm;
  20. static cl::opt<RegAllocPriorityAdvisorAnalysis::AdvisorMode> Mode(
  21. "regalloc-enable-priority-advisor", cl::Hidden,
  22. cl::init(RegAllocPriorityAdvisorAnalysis::AdvisorMode::Default),
  23. cl::desc("Enable regalloc advisor mode"),
  24. cl::values(
  25. clEnumValN(RegAllocPriorityAdvisorAnalysis::AdvisorMode::Default,
  26. "default", "Default"),
  27. clEnumValN(RegAllocPriorityAdvisorAnalysis::AdvisorMode::Release,
  28. "release", "precompiled"),
  29. clEnumValN(RegAllocPriorityAdvisorAnalysis::AdvisorMode::Development,
  30. "development", "for training")));
  31. char RegAllocPriorityAdvisorAnalysis::ID = 0;
  32. INITIALIZE_PASS(RegAllocPriorityAdvisorAnalysis, "regalloc-priority",
  33. "Regalloc priority policy", false, true)
  34. namespace {
  35. class DefaultPriorityAdvisorAnalysis final
  36. : public RegAllocPriorityAdvisorAnalysis {
  37. public:
  38. DefaultPriorityAdvisorAnalysis(bool NotAsRequested)
  39. : RegAllocPriorityAdvisorAnalysis(AdvisorMode::Default),
  40. NotAsRequested(NotAsRequested) {}
  41. // support for isa<> and dyn_cast.
  42. static bool classof(const RegAllocPriorityAdvisorAnalysis *R) {
  43. return R->getAdvisorMode() == AdvisorMode::Default;
  44. }
  45. private:
  46. void getAnalysisUsage(AnalysisUsage &AU) const override {
  47. AU.addRequired<SlotIndexes>();
  48. RegAllocPriorityAdvisorAnalysis::getAnalysisUsage(AU);
  49. }
  50. std::unique_ptr<RegAllocPriorityAdvisor>
  51. getAdvisor(const MachineFunction &MF, const RAGreedy &RA) override {
  52. return std::make_unique<DefaultPriorityAdvisor>(
  53. MF, RA, &getAnalysis<SlotIndexes>());
  54. }
  55. bool doInitialization(Module &M) override {
  56. if (NotAsRequested)
  57. M.getContext().emitError("Requested regalloc priority advisor analysis "
  58. "could be created. Using default");
  59. return RegAllocPriorityAdvisorAnalysis::doInitialization(M);
  60. }
  61. const bool NotAsRequested;
  62. };
  63. } // namespace
  64. template <> Pass *llvm::callDefaultCtor<RegAllocPriorityAdvisorAnalysis>() {
  65. Pass *Ret = nullptr;
  66. switch (Mode) {
  67. case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Default:
  68. Ret = new DefaultPriorityAdvisorAnalysis(/*NotAsRequested*/ false);
  69. break;
  70. case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Development:
  71. #if defined(LLVM_HAVE_TFLITE)
  72. Ret = createDevelopmentModePriorityAdvisor();
  73. #endif
  74. break;
  75. case RegAllocPriorityAdvisorAnalysis::AdvisorMode::Release:
  76. #if defined(LLVM_HAVE_TF_AOT_REGALLOCPRIORITYMODEL)
  77. Ret = createReleaseModePriorityAdvisor();
  78. #endif
  79. break;
  80. }
  81. if (Ret)
  82. return Ret;
  83. return new DefaultPriorityAdvisorAnalysis(/*NotAsRequested*/ true);
  84. }
  85. StringRef RegAllocPriorityAdvisorAnalysis::getPassName() const {
  86. switch (getAdvisorMode()) {
  87. case AdvisorMode::Default:
  88. return "Default Regalloc Priority Advisor";
  89. case AdvisorMode::Release:
  90. return "Release mode Regalloc Priority Advisor";
  91. case AdvisorMode::Development:
  92. return "Development mode Regalloc Priority Advisor";
  93. }
  94. llvm_unreachable("Unknown advisor kind");
  95. }
  96. RegAllocPriorityAdvisor::RegAllocPriorityAdvisor(const MachineFunction &MF,
  97. const RAGreedy &RA,
  98. SlotIndexes *const Indexes)
  99. : RA(RA), LIS(RA.getLiveIntervals()), VRM(RA.getVirtRegMap()),
  100. MRI(&VRM->getRegInfo()), TRI(MF.getSubtarget().getRegisterInfo()),
  101. RegClassInfo(RA.getRegClassInfo()), Indexes(Indexes),
  102. RegClassPriorityTrumpsGlobalness(
  103. RA.getRegClassPriorityTrumpsGlobalness()),
  104. ReverseLocalAssignment(RA.getReverseLocalAssignment()) {}