CSEInfo.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/GlobalISel/CSEInfo.h ------------------*- 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. /// \file
  14. /// Provides analysis for continuously CSEing during GISel passes.
  15. ///
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_GLOBALISEL_CSEINFO_H
  18. #define LLVM_CODEGEN_GLOBALISEL_CSEINFO_H
  19. #include "llvm/ADT/FoldingSet.h"
  20. #include "llvm/CodeGen/CSEConfigBase.h"
  21. #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
  22. #include "llvm/CodeGen/GlobalISel/GISelWorkList.h"
  23. #include "llvm/CodeGen/MachineFunctionPass.h"
  24. #include "llvm/Support/Allocator.h"
  25. #include "llvm/Support/CodeGen.h"
  26. namespace llvm {
  27. class MachineBasicBlock;
  28. /// A class that wraps MachineInstrs and derives from FoldingSetNode in order to
  29. /// be uniqued in a CSEMap. The tradeoff here is extra memory allocations for
  30. /// UniqueMachineInstr vs making MachineInstr bigger.
  31. class UniqueMachineInstr : public FoldingSetNode {
  32. friend class GISelCSEInfo;
  33. const MachineInstr *MI;
  34. explicit UniqueMachineInstr(const MachineInstr *MI) : MI(MI) {}
  35. public:
  36. void Profile(FoldingSetNodeID &ID);
  37. };
  38. // A CSE config for fully optimized builds.
  39. class CSEConfigFull : public CSEConfigBase {
  40. public:
  41. virtual ~CSEConfigFull() = default;
  42. virtual bool shouldCSEOpc(unsigned Opc) override;
  43. };
  44. // Commonly used for O0 config.
  45. class CSEConfigConstantOnly : public CSEConfigBase {
  46. public:
  47. virtual ~CSEConfigConstantOnly() = default;
  48. virtual bool shouldCSEOpc(unsigned Opc) override;
  49. };
  50. // Returns the standard expected CSEConfig for the given optimization level.
  51. // We have this logic here so targets can make use of it from their derived
  52. // TargetPassConfig, but can't put this logic into TargetPassConfig directly
  53. // because the CodeGen library can't depend on GlobalISel.
  54. std::unique_ptr<CSEConfigBase>
  55. getStandardCSEConfigForOpt(CodeGenOpt::Level Level);
  56. /// The CSE Analysis object.
  57. /// This installs itself as a delegate to the MachineFunction to track
  58. /// new instructions as well as deletions. It however will not be able to
  59. /// track instruction mutations. In such cases, recordNewInstruction should be
  60. /// called (for eg inside MachineIRBuilder::recordInsertion).
  61. /// Also because of how just the instruction can be inserted without adding any
  62. /// operands to the instruction, instructions are uniqued and inserted lazily.
  63. /// CSEInfo should assert when trying to enter an incomplete instruction into
  64. /// the CSEMap. There is Opcode level granularity on which instructions can be
  65. /// CSE'd and for now, only Generic instructions are CSEable.
  66. class GISelCSEInfo : public GISelChangeObserver {
  67. // Make it accessible only to CSEMIRBuilder.
  68. friend class CSEMIRBuilder;
  69. BumpPtrAllocator UniqueInstrAllocator;
  70. FoldingSet<UniqueMachineInstr> CSEMap;
  71. MachineRegisterInfo *MRI = nullptr;
  72. MachineFunction *MF = nullptr;
  73. std::unique_ptr<CSEConfigBase> CSEOpt;
  74. /// Keep a cache of UniqueInstrs for each MachineInstr. In GISel,
  75. /// often instructions are mutated (while their ID has completely changed).
  76. /// Whenever mutation happens, invalidate the UniqueMachineInstr for the
  77. /// MachineInstr
  78. DenseMap<const MachineInstr *, UniqueMachineInstr *> InstrMapping;
  79. /// Store instructions that are not fully formed in TemporaryInsts.
  80. /// Also because CSE insertion happens lazily, we can remove insts from this
  81. /// list and avoid inserting and then removing from the CSEMap.
  82. GISelWorkList<8> TemporaryInsts;
  83. // Only used in asserts.
  84. DenseMap<unsigned, unsigned> OpcodeHitTable;
  85. bool isUniqueMachineInstValid(const UniqueMachineInstr &UMI) const;
  86. void invalidateUniqueMachineInstr(UniqueMachineInstr *UMI);
  87. UniqueMachineInstr *getNodeIfExists(FoldingSetNodeID &ID,
  88. MachineBasicBlock *MBB, void *&InsertPos);
  89. /// Allocate and construct a new UniqueMachineInstr for MI and return.
  90. UniqueMachineInstr *getUniqueInstrForMI(const MachineInstr *MI);
  91. void insertNode(UniqueMachineInstr *UMI, void *InsertPos = nullptr);
  92. /// Get the MachineInstr(Unique) if it exists already in the CSEMap and the
  93. /// same MachineBasicBlock.
  94. MachineInstr *getMachineInstrIfExists(FoldingSetNodeID &ID,
  95. MachineBasicBlock *MBB,
  96. void *&InsertPos);
  97. /// Use this method to allocate a new UniqueMachineInstr for MI and insert it
  98. /// into the CSEMap. MI should return true for shouldCSE(MI->getOpcode())
  99. void insertInstr(MachineInstr *MI, void *InsertPos = nullptr);
  100. public:
  101. GISelCSEInfo() = default;
  102. virtual ~GISelCSEInfo();
  103. void setMF(MachineFunction &MF);
  104. Error verify();
  105. /// Records a newly created inst in a list and lazily insert it to the CSEMap.
  106. /// Sometimes, this method might be called with a partially constructed
  107. /// MachineInstr,
  108. // (right after BuildMI without adding any operands) - and in such cases,
  109. // defer the hashing of the instruction to a later stage.
  110. void recordNewInstruction(MachineInstr *MI);
  111. /// Use this callback to inform CSE about a newly fully created instruction.
  112. void handleRecordedInst(MachineInstr *MI);
  113. /// Use this callback to insert all the recorded instructions. At this point,
  114. /// all of these insts need to be fully constructed and should not be missing
  115. /// any operands.
  116. void handleRecordedInsts();
  117. /// Remove this inst from the CSE map. If this inst has not been inserted yet,
  118. /// it will be removed from the Tempinsts list if it exists.
  119. void handleRemoveInst(MachineInstr *MI);
  120. void releaseMemory();
  121. void setCSEConfig(std::unique_ptr<CSEConfigBase> Opt) {
  122. CSEOpt = std::move(Opt);
  123. }
  124. bool shouldCSE(unsigned Opc) const;
  125. void analyze(MachineFunction &MF);
  126. void countOpcodeHit(unsigned Opc);
  127. void print();
  128. // Observer API
  129. void erasingInstr(MachineInstr &MI) override;
  130. void createdInstr(MachineInstr &MI) override;
  131. void changingInstr(MachineInstr &MI) override;
  132. void changedInstr(MachineInstr &MI) override;
  133. };
  134. class TargetRegisterClass;
  135. class RegisterBank;
  136. // Simple builder class to easily profile properties about MIs.
  137. class GISelInstProfileBuilder {
  138. FoldingSetNodeID &ID;
  139. const MachineRegisterInfo &MRI;
  140. public:
  141. GISelInstProfileBuilder(FoldingSetNodeID &ID, const MachineRegisterInfo &MRI)
  142. : ID(ID), MRI(MRI) {}
  143. // Profiling methods.
  144. const GISelInstProfileBuilder &addNodeIDOpcode(unsigned Opc) const;
  145. const GISelInstProfileBuilder &addNodeIDRegType(const LLT Ty) const;
  146. const GISelInstProfileBuilder &addNodeIDRegType(const Register) const;
  147. const GISelInstProfileBuilder &
  148. addNodeIDRegType(const TargetRegisterClass *RC) const;
  149. const GISelInstProfileBuilder &addNodeIDRegType(const RegisterBank *RB) const;
  150. const GISelInstProfileBuilder &addNodeIDRegNum(Register Reg) const;
  151. const GISelInstProfileBuilder &addNodeIDReg(Register Reg) const;
  152. const GISelInstProfileBuilder &addNodeIDImmediate(int64_t Imm) const;
  153. const GISelInstProfileBuilder &
  154. addNodeIDMBB(const MachineBasicBlock *MBB) const;
  155. const GISelInstProfileBuilder &
  156. addNodeIDMachineOperand(const MachineOperand &MO) const;
  157. const GISelInstProfileBuilder &addNodeIDFlag(unsigned Flag) const;
  158. const GISelInstProfileBuilder &addNodeID(const MachineInstr *MI) const;
  159. };
  160. /// Simple wrapper that does the following.
  161. /// 1) Lazily evaluate the MachineFunction to compute CSEable instructions.
  162. /// 2) Allows configuration of which instructions are CSEd through CSEConfig
  163. /// object. Provides a method called get which takes a CSEConfig object.
  164. class GISelCSEAnalysisWrapper {
  165. GISelCSEInfo Info;
  166. MachineFunction *MF = nullptr;
  167. bool AlreadyComputed = false;
  168. public:
  169. /// Takes a CSEConfigBase object that defines what opcodes get CSEd.
  170. /// If CSEConfig is already set, and the CSE Analysis has been preserved,
  171. /// it will not use the new CSEOpt(use Recompute to force using the new
  172. /// CSEOpt).
  173. GISelCSEInfo &get(std::unique_ptr<CSEConfigBase> CSEOpt,
  174. bool ReCompute = false);
  175. void setMF(MachineFunction &MFunc) { MF = &MFunc; }
  176. void setComputed(bool Computed) { AlreadyComputed = Computed; }
  177. void releaseMemory() { Info.releaseMemory(); }
  178. };
  179. /// The actual analysis pass wrapper.
  180. class GISelCSEAnalysisWrapperPass : public MachineFunctionPass {
  181. GISelCSEAnalysisWrapper Wrapper;
  182. public:
  183. static char ID;
  184. GISelCSEAnalysisWrapperPass();
  185. void getAnalysisUsage(AnalysisUsage &AU) const override;
  186. const GISelCSEAnalysisWrapper &getCSEWrapper() const { return Wrapper; }
  187. GISelCSEAnalysisWrapper &getCSEWrapper() { return Wrapper; }
  188. bool runOnMachineFunction(MachineFunction &MF) override;
  189. void releaseMemory() override {
  190. Wrapper.releaseMemory();
  191. Wrapper.setComputed(false);
  192. }
  193. };
  194. } // namespace llvm
  195. #endif
  196. #ifdef __GNUC__
  197. #pragma GCC diagnostic pop
  198. #endif