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/MCRegister.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/Target/TargetMachine.h"
26#include <memory>
27#include <string>
28
29static constexpr llvm::StringLiteral kNoRegister("%noreg");
30
31namespace llvm {
32namespace exegesis {
33
34class ExegesisTarget;
35struct PfmCountersInfo;
36
37// An object to initialize LLVM and prepare objects needed to run the
38// measurements.
39class LLVMState {
40public:
41 // Factory function.
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
45 // for event counts.
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);
50
51 const TargetMachine &getTargetMachine() const { return *TheTargetMachine; }
52 std::unique_ptr<TargetMachine> createTargetMachine() const;
53
54 const ExegesisTarget &getExegesisTarget() const { return *TheExegesisTarget; }
55
56 bool canAssemble(const MCInst &mc_inst) const;
57
58 // For convenience:
59 const MCInstrInfo &getInstrInfo() const {
60 return *TheTargetMachine->getMCInstrInfo();
61 }
62 const MCRegisterInfo &getRegInfo() const {
63 return *TheTargetMachine->getMCRegisterInfo();
64 }
65 const MCSubtargetInfo &getSubtargetInfo() const {
66 return *TheTargetMachine->getMCSubtargetInfo();
67 }
68
69 const RegisterAliasingTrackerCache &getRATC() const { return *RATC; }
70 const InstructionsCache &getIC() const { return *IC; }
71
72 const PfmCountersInfo &getPfmCounters() const { return *PfmCounters; }
73
74 const DenseMap<StringRef, unsigned> &getOpcodeNameToOpcodeIdxMapping() const {
75 assert(OpcodeNameToOpcodeIdxMapping);
76 return *OpcodeNameToOpcodeIdxMapping;
77 };
78
79 std::optional<MCRegister>
80 getRegisterNumberFromName(StringRef RegisterName) const;
81
82private:
83 std::unique_ptr<const DenseMap<StringRef, unsigned>>
84 createOpcodeNameToOpcodeIdxMapping() const;
85
86 std::unique_ptr<const DenseMap<StringRef, MCRegister>>
87 createRegNameToRegNoMapping() const;
88
89 LLVMState(std::unique_ptr<const TargetMachine> TM, const ExegesisTarget *ET,
90 const PfmCountersInfo *PCI);
91
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;
100};
101
102} // namespace exegesis
103} // namespace llvm
104
105#endif // LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H
106