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 // `LibraryOptions` must outlive the returned state.
48 static Expected<LLVMState>
49 create(const cl::LibraryOptionsContext &LibraryOptions,
50 std::string TripleName, std::string CpuName, StringRef Features = "",
51 bool UseDummyPerfCounters = false);
52
53 const TargetMachine &getTargetMachine() const { return *TheTargetMachine; }
54 std::unique_ptr<TargetMachine> createTargetMachine() const;
55
56 const ExegesisTarget &getExegesisTarget() const { return *TheExegesisTarget; }
57
58 bool canAssemble(const MCInst &mc_inst) const;
59
60 // For convenience:
61 const MCInstrInfo &getInstrInfo() const {
62 return *TheTargetMachine->getMCInstrInfo();
63 }
64 const MCRegisterInfo &getRegInfo() const {
65 return TheTargetMachine->getMCRegisterInfo();
66 }
67 const MCSubtargetInfo &getSubtargetInfo() const {
68 return TheTargetMachine->getMCSubtargetInfo();
69 }
70
71 const RegisterAliasingTrackerCache &getRATC() const { return *RATC; }
72 const InstructionsCache &getIC() const { return *IC; }
73
74 const PfmCountersInfo &getPfmCounters() const { return *PfmCounters; }
75
76 const DenseMap<StringRef, unsigned> &getOpcodeNameToOpcodeIdxMapping() const {
77 assert(OpcodeNameToOpcodeIdxMapping);
78 return *OpcodeNameToOpcodeIdxMapping;
79 };
80
81 std::optional<MCRegister>
82 getRegisterNumberFromName(StringRef RegisterName) const;
83
84private:
85 std::unique_ptr<const DenseMap<StringRef, unsigned>>
86 createOpcodeNameToOpcodeIdxMapping() const;
87
88 std::unique_ptr<const DenseMap<StringRef, MCRegister>>
89 createRegNameToRegNoMapping() const;
90
91 LLVMState(std::unique_ptr<const TargetMachine> TM, const ExegesisTarget *ET,
92 const PfmCountersInfo *PCI);
93
94 const ExegesisTarget *TheExegesisTarget;
95 std::unique_ptr<const TargetMachine> TheTargetMachine;
96 std::unique_ptr<const RegisterAliasingTrackerCache> RATC;
97 std::unique_ptr<const InstructionsCache> IC;
98 const PfmCountersInfo *PfmCounters;
99 std::unique_ptr<const DenseMap<StringRef, unsigned>>
100 OpcodeNameToOpcodeIdxMapping;
101 std::unique_ptr<const DenseMap<StringRef, MCRegister>> RegNameToRegNoMapping;
102};
103
104} // namespace exegesis
105} // namespace llvm
106
107#endif // LLVM_TOOLS_LLVM_EXEGESIS_LLVMSTATE_H
108