InstrBuilder.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/MC/MCInstrAnalysis.h"
  21. #include "llvm/MC/MCInstrInfo.h"
  22. #include "llvm/MC/MCRegisterInfo.h"
  23. #include "llvm/MC/MCSubtargetInfo.h"
  24. #include "llvm/MCA/Instruction.h"
  25. #include "llvm/MCA/Support.h"
  26. #include "llvm/Support/Error.h"
  27. namespace llvm {
  28. namespace mca {
  29. /// A builder class that knows how to construct Instruction objects.
  30. ///
  31. /// Every llvm-mca Instruction is described by an object of class InstrDesc.
  32. /// An InstrDesc describes which registers are read/written by the instruction,
  33. /// as well as the instruction latency and hardware resources consumed.
  34. ///
  35. /// This class is used by the tool to construct Instructions and instruction
  36. /// descriptors (i.e. InstrDesc objects).
  37. /// Information from the machine scheduling model is used to identify processor
  38. /// resources that are consumed by an instruction.
  39. class InstrBuilder {
  40. const MCSubtargetInfo &STI;
  41. const MCInstrInfo &MCII;
  42. const MCRegisterInfo &MRI;
  43. const MCInstrAnalysis *MCIA;
  44. SmallVector<uint64_t, 8> ProcResourceMasks;
  45. DenseMap<unsigned short, std::unique_ptr<const InstrDesc>> Descriptors;
  46. DenseMap<const MCInst *, std::unique_ptr<const InstrDesc>> VariantDescriptors;
  47. bool FirstCallInst;
  48. bool FirstReturnInst;
  49. Expected<const InstrDesc &> createInstrDescImpl(const MCInst &MCI);
  50. Expected<const InstrDesc &> getOrCreateInstrDesc(const MCInst &MCI);
  51. InstrBuilder(const InstrBuilder &) = delete;
  52. InstrBuilder &operator=(const InstrBuilder &) = delete;
  53. void populateWrites(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
  54. void populateReads(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
  55. Error verifyInstrDesc(const InstrDesc &ID, const MCInst &MCI) const;
  56. public:
  57. InstrBuilder(const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
  58. const MCRegisterInfo &RI, const MCInstrAnalysis *IA);
  59. void clear() {
  60. Descriptors.clear();
  61. VariantDescriptors.clear();
  62. FirstCallInst = true;
  63. FirstReturnInst = true;
  64. }
  65. Expected<std::unique_ptr<Instruction>> createInstruction(const MCInst &MCI);
  66. };
  67. } // namespace mca
  68. } // namespace llvm
  69. #endif // LLVM_MCA_INSTRBUILDER_H
  70. #ifdef __GNUC__
  71. #pragma GCC diagnostic pop
  72. #endif