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