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/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Target/TargetMachine.h"
28 static constexpr llvm::StringLiteral
kNoRegister("%noreg");
34 struct PfmCountersInfo
;
36 // An object to initialize LLVM and prepare objects needed to run the
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
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);
50 const TargetMachine
&getTargetMachine() const { return *TheTargetMachine
; }
51 std::unique_ptr
<LLVMTargetMachine
> createTargetMachine() const;
53 const ExegesisTarget
&getExegesisTarget() const { return *TheExegesisTarget
; }
55 bool canAssemble(const MCInst
&mc_inst
) const;
58 const MCInstrInfo
&getInstrInfo() const {
59 return *TheTargetMachine
->getMCInstrInfo();
61 const MCRegisterInfo
&getRegInfo() const {
62 return *TheTargetMachine
->getMCRegisterInfo();
64 const MCSubtargetInfo
&getSubtargetInfo() const {
65 return *TheTargetMachine
->getMCSubtargetInfo();
68 const RegisterAliasingTrackerCache
&getRATC() const { return *RATC
; }
69 const InstructionsCache
&getIC() const { return *IC
; }
71 const PfmCountersInfo
&getPfmCounters() const { return *PfmCounters
; }
73 const DenseMap
<StringRef
, unsigned> &getOpcodeNameToOpcodeIdxMapping() const {
74 assert(OpcodeNameToOpcodeIdxMapping
);
75 return *OpcodeNameToOpcodeIdxMapping
;
78 const DenseMap
<StringRef
, unsigned> &getRegNameToRegNoMapping() const {
79 assert(RegNameToRegNoMapping
);
80 return *RegNameToRegNoMapping
;
84 std::unique_ptr
<const DenseMap
<StringRef
, unsigned>>
85 createOpcodeNameToOpcodeIdxMapping() const;
87 std::unique_ptr
<const DenseMap
<StringRef
, unsigned>>
88 createRegNameToRegNoMapping() const;
90 LLVMState(std::unique_ptr
<const TargetMachine
> TM
, const ExegesisTarget
*ET
,
91 const PfmCountersInfo
*PCI
);
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
;
103 } // namespace exegesis
106 #endif // LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H