LlvmState.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===-- LlvmState.h ---------------------------------------------*- C++ -*-===//
  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. /// \file
  10. /// A class to set up and access common LLVM objects.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H
  14. #define LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H
  15. #include "MCInstrDescView.h"
  16. #include "RegisterAliasing.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/MC/MCAsmInfo.h"
  19. #include "llvm/MC/MCInst.h"
  20. #include "llvm/MC/MCInstrInfo.h"
  21. #include "llvm/MC/MCRegisterInfo.h"
  22. #include "llvm/MC/MCSubtargetInfo.h"
  23. #include "llvm/Target/TargetMachine.h"
  24. #include <memory>
  25. #include <string>
  26. static constexpr llvm::StringLiteral kNoRegister("%noreg");
  27. namespace llvm {
  28. namespace exegesis {
  29. class ExegesisTarget;
  30. struct PfmCountersInfo;
  31. // An object to initialize LLVM and prepare objects needed to run the
  32. // measurements.
  33. class LLVMState {
  34. public:
  35. // Factory function.
  36. // If `Triple` is empty, uses the host triple.
  37. // If `CpuName` is empty, uses the host CPU.
  38. // `Features` is intended for tests.
  39. static Expected<LLVMState> Create(std::string TripleName, std::string CpuName,
  40. StringRef Features = "");
  41. const TargetMachine &getTargetMachine() const { return *TheTargetMachine; }
  42. std::unique_ptr<LLVMTargetMachine> createTargetMachine() const;
  43. const ExegesisTarget &getExegesisTarget() const { return *TheExegesisTarget; }
  44. bool canAssemble(const MCInst &mc_inst) const;
  45. // For convenience:
  46. const MCInstrInfo &getInstrInfo() const {
  47. return *TheTargetMachine->getMCInstrInfo();
  48. }
  49. const MCRegisterInfo &getRegInfo() const {
  50. return *TheTargetMachine->getMCRegisterInfo();
  51. }
  52. const MCSubtargetInfo &getSubtargetInfo() const {
  53. return *TheTargetMachine->getMCSubtargetInfo();
  54. }
  55. const RegisterAliasingTrackerCache &getRATC() const { return *RATC; }
  56. const InstructionsCache &getIC() const { return *IC; }
  57. const PfmCountersInfo &getPfmCounters() const { return *PfmCounters; }
  58. const DenseMap<StringRef, unsigned> &getOpcodeNameToOpcodeIdxMapping() const {
  59. assert(OpcodeNameToOpcodeIdxMapping);
  60. return *OpcodeNameToOpcodeIdxMapping;
  61. };
  62. const DenseMap<StringRef, unsigned> &getRegNameToRegNoMapping() const {
  63. assert(RegNameToRegNoMapping);
  64. return *RegNameToRegNoMapping;
  65. }
  66. private:
  67. std::unique_ptr<const DenseMap<StringRef, unsigned>>
  68. createOpcodeNameToOpcodeIdxMapping() const;
  69. std::unique_ptr<const DenseMap<StringRef, unsigned>>
  70. createRegNameToRegNoMapping() const;
  71. LLVMState(std::unique_ptr<const TargetMachine> TM, const ExegesisTarget *ET,
  72. StringRef CpuName);
  73. const ExegesisTarget *TheExegesisTarget;
  74. std::unique_ptr<const TargetMachine> TheTargetMachine;
  75. std::unique_ptr<const RegisterAliasingTrackerCache> RATC;
  76. std::unique_ptr<const InstructionsCache> IC;
  77. const PfmCountersInfo *PfmCounters;
  78. std::unique_ptr<const DenseMap<StringRef, unsigned>>
  79. OpcodeNameToOpcodeIdxMapping;
  80. std::unique_ptr<const DenseMap<StringRef, unsigned>> RegNameToRegNoMapping;
  81. };
  82. } // namespace exegesis
  83. } // namespace llvm
  84. #endif // LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H