1 //===-- LlvmState.h ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// A class to set up and access common LLVM objects.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H
15 #define LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H
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/MCRegister.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Target/TargetMachine.h"
29 static constexpr llvm::StringLiteral
kNoRegister("%noreg");
35 struct PfmCountersInfo
;
37 // An object to initialize LLVM and prepare objects needed to run the
42 // If `Triple` is empty, uses the host triple.
43 // If `CpuName` is empty, uses the host CPU.
44 // If `UseDummyPerfCounters` is set, does not query the kernel
46 // `UseDummyPerfCounters` and `Features` are intended for tests.
47 static Expected
<LLVMState
> Create(std::string TripleName
, std::string CpuName
,
48 StringRef Features
= "",
49 bool UseDummyPerfCounters
= false);
51 const TargetMachine
&getTargetMachine() const { return *TheTargetMachine
; }
52 std::unique_ptr
<TargetMachine
> createTargetMachine() const;
54 const ExegesisTarget
&getExegesisTarget() const { return *TheExegesisTarget
; }
56 bool canAssemble(const MCInst
&mc_inst
) const;
59 const MCInstrInfo
&getInstrInfo() const {
60 return *TheTargetMachine
->getMCInstrInfo();
62 const MCRegisterInfo
&getRegInfo() const {
63 return *TheTargetMachine
->getMCRegisterInfo();
65 const MCSubtargetInfo
&getSubtargetInfo() const {
66 return *TheTargetMachine
->getMCSubtargetInfo();
69 const RegisterAliasingTrackerCache
&getRATC() const { return *RATC
; }
70 const InstructionsCache
&getIC() const { return *IC
; }
72 const PfmCountersInfo
&getPfmCounters() const { return *PfmCounters
; }
74 const DenseMap
<StringRef
, unsigned> &getOpcodeNameToOpcodeIdxMapping() const {
75 assert(OpcodeNameToOpcodeIdxMapping
);
76 return *OpcodeNameToOpcodeIdxMapping
;
79 std::optional
<MCRegister
>
80 getRegisterNumberFromName(StringRef RegisterName
) const;
83 std::unique_ptr
<const DenseMap
<StringRef
, unsigned>>
84 createOpcodeNameToOpcodeIdxMapping() const;
86 std::unique_ptr
<const DenseMap
<StringRef
, MCRegister
>>
87 createRegNameToRegNoMapping() const;
89 LLVMState(std::unique_ptr
<const TargetMachine
> TM
, const ExegesisTarget
*ET
,
90 const PfmCountersInfo
*PCI
);
92 const ExegesisTarget
*TheExegesisTarget
;
93 std::unique_ptr
<const TargetMachine
> TheTargetMachine
;
94 std::unique_ptr
<const RegisterAliasingTrackerCache
> RATC
;
95 std::unique_ptr
<const InstructionsCache
> IC
;
96 const PfmCountersInfo
*PfmCounters
;
97 std::unique_ptr
<const DenseMap
<StringRef
, unsigned>>
98 OpcodeNameToOpcodeIdxMapping
;
99 std::unique_ptr
<const DenseMap
<StringRef
, MCRegister
>> RegNameToRegNoMapping
;
102 } // namespace exegesis
105 #endif // LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H