1 //===-- LlvmState.cpp -------------------------------------------*- 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 //===----------------------------------------------------------------------===//
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"
24 Expected
<LLVMState
> LLVMState::Create(std::string TripleName
,
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.
35 const Target
*TheTarget
=
36 TargetRegistry::lookupTarget(/*MArch=*/"", TheTriple
, Error
);
38 return llvm::make_error
<llvm::StringError
>("no LLVM target for triple " +
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 (")
55 .concat(") for triple ")
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
)));
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());
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
);
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();
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