Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / tools / llvm-exegesis / lib / LlvmState.cpp
blob8aeea456fba1fe57a065ca943ad32f349ec8ee1c
1 //===-- LlvmState.cpp -------------------------------------------*- 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 //===----------------------------------------------------------------------===//
9 #include "LlvmState.h"
10 #include "Target.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCFixup.h"
15 #include "llvm/MC/MCObjectFileInfo.h"
16 #include "llvm/MC/TargetRegistry.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/TargetOptions.h"
19 #include "llvm/TargetParser/Host.h"
21 namespace llvm {
22 namespace exegesis {
24 Expected<LLVMState> LLVMState::Create(std::string TripleName,
25 std::string CpuName,
26 const StringRef Features,
27 bool UseDummyPerfCounters) {
28 if (TripleName.empty())
29 TripleName = Triple::normalize(sys::getDefaultTargetTriple());
31 Triple TheTriple(TripleName);
33 // Get the target specific parser.
34 std::string Error;
35 const Target *TheTarget =
36 TargetRegistry::lookupTarget(/*MArch=*/"", TheTriple, Error);
37 if (!TheTarget) {
38 return llvm::make_error<llvm::StringError>("no LLVM target for triple " +
39 TripleName,
40 llvm::inconvertibleErrorCode());
43 // Update Triple with the updated triple from the target lookup.
44 TripleName = TheTriple.str();
46 if (CpuName == "native")
47 CpuName = std::string(llvm::sys::getHostCPUName());
49 std::unique_ptr<MCSubtargetInfo> STI(
50 TheTarget->createMCSubtargetInfo(TripleName, CpuName, ""));
51 assert(STI && "Unable to create subtarget info!");
52 if (!STI->isCPUStringValid(CpuName)) {
53 return llvm::make_error<llvm::StringError>(Twine("invalid CPU name (")
54 .concat(CpuName)
55 .concat(") for triple ")
56 .concat(TripleName),
57 llvm::inconvertibleErrorCode());
59 const TargetOptions Options;
60 std::unique_ptr<const TargetMachine> TM(
61 static_cast<LLVMTargetMachine *>(TheTarget->createTargetMachine(
62 TripleName, CpuName, Features, Options, Reloc::Model::Static)));
63 if (!TM) {
64 return llvm::make_error<llvm::StringError>(
65 "unable to create target machine", llvm::inconvertibleErrorCode());
68 const ExegesisTarget *ET =
69 TripleName.empty() ? &ExegesisTarget::getDefault()
70 : ExegesisTarget::lookup(TM->getTargetTriple());
71 if (!ET) {
72 return llvm::make_error<llvm::StringError>(
73 "no Exegesis target for triple " + TripleName,
74 llvm::inconvertibleErrorCode());
76 const PfmCountersInfo &PCI = UseDummyPerfCounters
77 ? ET->getDummyPfmCounters()
78 : ET->getPfmCounters(CpuName);
79 return LLVMState(std::move(TM), ET, &PCI);
82 LLVMState::LLVMState(std::unique_ptr<const TargetMachine> TM,
83 const ExegesisTarget *ET, const PfmCountersInfo *PCI)
84 : TheExegesisTarget(ET), TheTargetMachine(std::move(TM)), PfmCounters(PCI),
85 OpcodeNameToOpcodeIdxMapping(createOpcodeNameToOpcodeIdxMapping()),
86 RegNameToRegNoMapping(createRegNameToRegNoMapping()) {
87 BitVector ReservedRegs = getFunctionReservedRegs(getTargetMachine());
88 for (const unsigned Reg : TheExegesisTarget->getUnavailableRegisters())
89 ReservedRegs.set(Reg);
90 RATC.reset(
91 new RegisterAliasingTrackerCache(getRegInfo(), std::move(ReservedRegs)));
92 IC.reset(new InstructionsCache(getInstrInfo(), getRATC()));
95 std::unique_ptr<LLVMTargetMachine> LLVMState::createTargetMachine() const {
96 return std::unique_ptr<LLVMTargetMachine>(static_cast<LLVMTargetMachine *>(
97 TheTargetMachine->getTarget().createTargetMachine(
98 TheTargetMachine->getTargetTriple().normalize(),
99 TheTargetMachine->getTargetCPU(),
100 TheTargetMachine->getTargetFeatureString(), TheTargetMachine->Options,
101 Reloc::Model::Static)));
104 std::unique_ptr<const DenseMap<StringRef, unsigned>>
105 LLVMState::createOpcodeNameToOpcodeIdxMapping() const {
106 const MCInstrInfo &InstrInfo = getInstrInfo();
107 auto Map = std::make_unique<DenseMap<StringRef, unsigned>>(
108 InstrInfo.getNumOpcodes());
109 for (unsigned I = 0, E = InstrInfo.getNumOpcodes(); I < E; ++I)
110 (*Map)[InstrInfo.getName(I)] = I;
111 assert(Map->size() == InstrInfo.getNumOpcodes() && "Size prediction failed");
112 return std::move(Map);
115 std::unique_ptr<const DenseMap<StringRef, unsigned>>
116 LLVMState::createRegNameToRegNoMapping() const {
117 const MCRegisterInfo &RegInfo = getRegInfo();
118 auto Map =
119 std::make_unique<DenseMap<StringRef, unsigned>>(RegInfo.getNumRegs());
120 // Special-case RegNo 0, which would otherwise be spelled as ''.
121 (*Map)[kNoRegister] = 0;
122 for (unsigned I = 1, E = RegInfo.getNumRegs(); I < E; ++I)
123 (*Map)[RegInfo.getName(I)] = I;
124 assert(Map->size() == RegInfo.getNumRegs() && "Size prediction failed");
125 return std::move(Map);
128 bool LLVMState::canAssemble(const MCInst &Inst) const {
129 MCContext Context(TheTargetMachine->getTargetTriple(),
130 TheTargetMachine->getMCAsmInfo(),
131 TheTargetMachine->getMCRegisterInfo(),
132 TheTargetMachine->getMCSubtargetInfo());
133 std::unique_ptr<const MCCodeEmitter> CodeEmitter(
134 TheTargetMachine->getTarget().createMCCodeEmitter(
135 *TheTargetMachine->getMCInstrInfo(), Context));
136 assert(CodeEmitter && "unable to create code emitter");
137 SmallVector<char, 16> Tmp;
138 SmallVector<MCFixup, 4> Fixups;
139 CodeEmitter->encodeInstruction(Inst, Tmp, Fixups,
140 *TheTargetMachine->getMCSubtargetInfo());
141 return Tmp.size() > 0;
144 } // namespace exegesis
145 } // namespace llvm