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