InstrBuilder.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--------------------- InstrBuilder.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. ///
  15. /// A builder class for instructions that are statically analyzed by llvm-mca.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_MCA_INSTRBUILDER_H
  19. #define LLVM_MCA_INSTRBUILDER_H
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/MC/MCInstrAnalysis.h"
  22. #include "llvm/MC/MCInstrInfo.h"
  23. #include "llvm/MC/MCRegisterInfo.h"
  24. #include "llvm/MC/MCSubtargetInfo.h"
  25. #include "llvm/MCA/CustomBehaviour.h"
  26. #include "llvm/MCA/Instruction.h"
  27. #include "llvm/MCA/Support.h"
  28. #include "llvm/Support/Error.h"
  29. namespace llvm {
  30. namespace mca {
  31. class RecycledInstErr : public ErrorInfo<RecycledInstErr> {
  32. Instruction *RecycledInst;
  33. public:
  34. static char ID;
  35. explicit RecycledInstErr(Instruction *Inst) : RecycledInst(Inst) {}
  36. // Always need to carry an Instruction
  37. RecycledInstErr() = delete;
  38. Instruction *getInst() const { return RecycledInst; }
  39. void log(raw_ostream &OS) const override {
  40. OS << "Instruction is recycled\n";
  41. }
  42. std::error_code convertToErrorCode() const override {
  43. return llvm::inconvertibleErrorCode();
  44. }
  45. };
  46. /// A builder class that knows how to construct Instruction objects.
  47. ///
  48. /// Every llvm-mca Instruction is described by an object of class InstrDesc.
  49. /// An InstrDesc describes which registers are read/written by the instruction,
  50. /// as well as the instruction latency and hardware resources consumed.
  51. ///
  52. /// This class is used by the tool to construct Instructions and instruction
  53. /// descriptors (i.e. InstrDesc objects).
  54. /// Information from the machine scheduling model is used to identify processor
  55. /// resources that are consumed by an instruction.
  56. class InstrBuilder {
  57. const MCSubtargetInfo &STI;
  58. const MCInstrInfo &MCII;
  59. const MCRegisterInfo &MRI;
  60. const MCInstrAnalysis *MCIA;
  61. const InstrumentManager &IM;
  62. SmallVector<uint64_t, 8> ProcResourceMasks;
  63. // Key is the MCI.Opcode and SchedClassID the describe the value InstrDesc
  64. DenseMap<std::pair<unsigned short, unsigned>,
  65. std::unique_ptr<const InstrDesc>>
  66. Descriptors;
  67. // Key is the MCIInst and SchedClassID the describe the value InstrDesc
  68. DenseMap<std::pair<const MCInst *, unsigned>,
  69. std::unique_ptr<const InstrDesc>>
  70. VariantDescriptors;
  71. bool FirstCallInst;
  72. bool FirstReturnInst;
  73. using InstRecycleCallback =
  74. llvm::function_ref<Instruction *(const InstrDesc &)>;
  75. InstRecycleCallback InstRecycleCB;
  76. Expected<const InstrDesc &>
  77. createInstrDescImpl(const MCInst &MCI,
  78. const SmallVector<SharedInstrument> &IVec);
  79. Expected<const InstrDesc &>
  80. getOrCreateInstrDesc(const MCInst &MCI,
  81. const SmallVector<SharedInstrument> &IVec);
  82. InstrBuilder(const InstrBuilder &) = delete;
  83. InstrBuilder &operator=(const InstrBuilder &) = delete;
  84. void populateWrites(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
  85. void populateReads(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
  86. Error verifyInstrDesc(const InstrDesc &ID, const MCInst &MCI) const;
  87. public:
  88. InstrBuilder(const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
  89. const MCRegisterInfo &RI, const MCInstrAnalysis *IA,
  90. const InstrumentManager &IM);
  91. void clear() {
  92. Descriptors.clear();
  93. VariantDescriptors.clear();
  94. FirstCallInst = true;
  95. FirstReturnInst = true;
  96. }
  97. /// Set a callback which is invoked to retrieve a recycled mca::Instruction
  98. /// or null if there isn't any.
  99. void setInstRecycleCallback(InstRecycleCallback CB) { InstRecycleCB = CB; }
  100. Expected<std::unique_ptr<Instruction>>
  101. createInstruction(const MCInst &MCI,
  102. const SmallVector<SharedInstrument> &IVec);
  103. };
  104. } // namespace mca
  105. } // namespace llvm
  106. #endif // LLVM_MCA_INSTRBUILDER_H
  107. #ifdef __GNUC__
  108. #pragma GCC diagnostic pop
  109. #endif