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/Support/TargetRegistry.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetOptions.h"
24 LLVMState::LLVMState(const std::string
&Triple
, const std::string
&CpuName
,
25 const std::string
&Features
) {
27 const llvm::Target
*const TheTarget
=
28 llvm::TargetRegistry::lookupTarget(Triple
, Error
);
29 assert(TheTarget
&& "unknown target for host");
30 const llvm::TargetOptions Options
;
32 static_cast<llvm::LLVMTargetMachine
*>(TheTarget
->createTargetMachine(
33 Triple
, CpuName
, Features
, Options
, llvm::Reloc::Model::Static
)));
34 TheExegesisTarget
= ExegesisTarget::lookup(TargetMachine
->getTargetTriple());
35 if (!TheExegesisTarget
) {
36 llvm::errs() << "no exegesis target for " << Triple
<< ", using default\n";
37 TheExegesisTarget
= &ExegesisTarget::getDefault();
39 PfmCounters
= &TheExegesisTarget
->getPfmCounters(CpuName
);
41 BitVector ReservedRegs
= getFunctionReservedRegs(getTargetMachine());
42 for (const unsigned Reg
: TheExegesisTarget
->getUnavailableRegisters())
43 ReservedRegs
.set(Reg
);
45 new RegisterAliasingTrackerCache(getRegInfo(), std::move(ReservedRegs
)));
46 IC
.reset(new InstructionsCache(getInstrInfo(), getRATC()));
49 LLVMState::LLVMState(const std::string
&CpuName
)
50 : LLVMState(llvm::sys::getProcessTriple(),
51 CpuName
.empty() ? llvm::sys::getHostCPUName().str() : CpuName
,
54 std::unique_ptr
<llvm::LLVMTargetMachine
>
55 LLVMState::createTargetMachine() const {
56 return std::unique_ptr
<llvm::LLVMTargetMachine
>(
57 static_cast<llvm::LLVMTargetMachine
*>(
58 TargetMachine
->getTarget().createTargetMachine(
59 TargetMachine
->getTargetTriple().normalize(),
60 TargetMachine
->getTargetCPU(),
61 TargetMachine
->getTargetFeatureString(), TargetMachine
->Options
,
62 llvm::Reloc::Model::Static
)));
65 bool LLVMState::canAssemble(const llvm::MCInst
&Inst
) const {
66 llvm::MCObjectFileInfo ObjectFileInfo
;
67 llvm::MCContext
Context(TargetMachine
->getMCAsmInfo(),
68 TargetMachine
->getMCRegisterInfo(), &ObjectFileInfo
);
69 std::unique_ptr
<const llvm::MCCodeEmitter
> CodeEmitter(
70 TargetMachine
->getTarget().createMCCodeEmitter(
71 *TargetMachine
->getMCInstrInfo(), *TargetMachine
->getMCRegisterInfo(),
73 llvm::SmallVector
<char, 16> Tmp
;
74 llvm::raw_svector_ostream
OS(Tmp
);
75 llvm::SmallVector
<llvm::MCFixup
, 4> Fixups
;
76 CodeEmitter
->encodeInstruction(Inst
, OS
, Fixups
,
77 *TargetMachine
->getMCSubtargetInfo());
78 return Tmp
.size() > 0;
81 } // namespace exegesis