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