Localizer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //== llvm/CodeGen/GlobalISel/Localizer.h - Localizer -------------*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file This file describes the interface of the Localizer pass.
  15. /// This pass moves/duplicates constant-like instructions close to their uses.
  16. /// Its primarily goal is to workaround the deficiencies of the fast register
  17. /// allocator.
  18. /// With GlobalISel constants are all materialized in the entry block of
  19. /// a function. However, the fast allocator cannot rematerialize constants and
  20. /// has a lot more live-ranges to deal with and will most likely end up
  21. /// spilling a lot.
  22. /// By pushing the constants close to their use, we only create small
  23. /// live-ranges.
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_CODEGEN_GLOBALISEL_LOCALIZER_H
  26. #define LLVM_CODEGEN_GLOBALISEL_LOCALIZER_H
  27. #include "llvm/ADT/SetVector.h"
  28. #include "llvm/CodeGen/MachineFunctionPass.h"
  29. namespace llvm {
  30. // Forward declarations.
  31. class AnalysisUsage;
  32. class MachineBasicBlock;
  33. class MachineInstr;
  34. class MachineOperand;
  35. class MachineRegisterInfo;
  36. class TargetTransformInfo;
  37. /// This pass implements the localization mechanism described at the
  38. /// top of this file. One specificity of the implementation is that
  39. /// it will materialize one and only one instance of a constant per
  40. /// basic block, thus enabling reuse of that constant within that block.
  41. /// Moreover, it only materializes constants in blocks where they
  42. /// are used. PHI uses are considered happening at the end of the
  43. /// related predecessor.
  44. class Localizer : public MachineFunctionPass {
  45. public:
  46. static char ID;
  47. private:
  48. /// An input function to decide if the pass should run or not
  49. /// on the given MachineFunction.
  50. std::function<bool(const MachineFunction &)> DoNotRunPass;
  51. /// MRI contains all the register class/bank information that this
  52. /// pass uses and updates.
  53. MachineRegisterInfo *MRI;
  54. /// TTI used for getting remat costs for instructions.
  55. TargetTransformInfo *TTI;
  56. /// Check if \p MOUse is used in the same basic block as \p Def.
  57. /// If the use is in the same block, we say it is local.
  58. /// When the use is not local, \p InsertMBB will contain the basic
  59. /// block when to insert \p Def to have a local use.
  60. static bool isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
  61. MachineBasicBlock *&InsertMBB);
  62. /// Initialize the field members using \p MF.
  63. void init(MachineFunction &MF);
  64. typedef SmallSetVector<MachineInstr *, 32> LocalizedSetVecT;
  65. /// If \p Op is a phi operand and not unique in that phi, that is,
  66. /// there are other operands in the phi with the same register,
  67. /// return true.
  68. bool isNonUniquePhiValue(MachineOperand &Op) const;
  69. /// Do inter-block localization from the entry block.
  70. bool localizeInterBlock(MachineFunction &MF,
  71. LocalizedSetVecT &LocalizedInstrs);
  72. /// Do intra-block localization of already localized instructions.
  73. bool localizeIntraBlock(LocalizedSetVecT &LocalizedInstrs);
  74. public:
  75. Localizer();
  76. Localizer(std::function<bool(const MachineFunction &)>);
  77. StringRef getPassName() const override { return "Localizer"; }
  78. MachineFunctionProperties getRequiredProperties() const override {
  79. return MachineFunctionProperties()
  80. .set(MachineFunctionProperties::Property::IsSSA);
  81. }
  82. void getAnalysisUsage(AnalysisUsage &AU) const override;
  83. bool runOnMachineFunction(MachineFunction &MF) override;
  84. };
  85. } // End namespace llvm.
  86. #endif
  87. #ifdef __GNUC__
  88. #pragma GCC diagnostic pop
  89. #endif