aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/tools/llvm-exegesis/lib/LlvmState.cpp
blob: d7940ce367569eaf0a5762b5d824a9569f61b640 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//===-- LlvmState.cpp -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "LlvmState.h"
#include "Target.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"

namespace llvm {
namespace exegesis {

Expected<LLVMState> LLVMState::Create(std::string TripleName,
                                      std::string CpuName,
                                      const StringRef Features) {
  if (TripleName.empty())
    TripleName = Triple::normalize(sys::getDefaultTargetTriple());

  Triple TheTriple(TripleName);

  // Get the target specific parser.
  std::string Error;
  const Target *TheTarget =
      TargetRegistry::lookupTarget(/*MArch=*/"", TheTriple, Error);
  if (!TheTarget) {
    return llvm::make_error<llvm::StringError>("no LLVM target for triple " +
                                                   TripleName,
                                               llvm::inconvertibleErrorCode());
  }

  // Update Triple with the updated triple from the target lookup.
  TripleName = TheTriple.str();

  if (CpuName == "native")
    CpuName = std::string(llvm::sys::getHostCPUName());

  std::unique_ptr<MCSubtargetInfo> STI(
      TheTarget->createMCSubtargetInfo(TripleName, CpuName, ""));
  assert(STI && "Unable to create subtarget info!");
  if (!STI->isCPUStringValid(CpuName)) {
    return llvm::make_error<llvm::StringError>(Twine("invalid CPU name (")
                                                   .concat(CpuName)
                                                   .concat(") for triple ")
                                                   .concat(TripleName),
                                               llvm::inconvertibleErrorCode());
  }
  const TargetOptions Options;
  std::unique_ptr<const TargetMachine> TM(
      static_cast<LLVMTargetMachine *>(TheTarget->createTargetMachine(
          TripleName, CpuName, Features, Options, Reloc::Model::Static)));
  if (!TM) {
    return llvm::make_error<llvm::StringError>(
        "unable to create target machine", llvm::inconvertibleErrorCode());
  }

  const ExegesisTarget *ET =
      TripleName.empty() ? &ExegesisTarget::getDefault()
                         : ExegesisTarget::lookup(TM->getTargetTriple());
  if (!ET) {
    return llvm::make_error<llvm::StringError>(
        "no Exegesis target for triple " + TripleName,
        llvm::inconvertibleErrorCode());
  }
  return LLVMState(std::move(TM), ET, CpuName);
}

LLVMState::LLVMState(std::unique_ptr<const TargetMachine> TM,
                     const ExegesisTarget *ET, const StringRef CpuName)
    : TheExegesisTarget(ET), TheTargetMachine(std::move(TM)),
      OpcodeNameToOpcodeIdxMapping(createOpcodeNameToOpcodeIdxMapping()),
      RegNameToRegNoMapping(createRegNameToRegNoMapping()) {
  PfmCounters = &TheExegesisTarget->getPfmCounters(CpuName);

  BitVector ReservedRegs = getFunctionReservedRegs(getTargetMachine());
  for (const unsigned Reg : TheExegesisTarget->getUnavailableRegisters())
    ReservedRegs.set(Reg);
  RATC.reset(
      new RegisterAliasingTrackerCache(getRegInfo(), std::move(ReservedRegs)));
  IC.reset(new InstructionsCache(getInstrInfo(), getRATC()));
}

std::unique_ptr<LLVMTargetMachine> LLVMState::createTargetMachine() const {
  return std::unique_ptr<LLVMTargetMachine>(static_cast<LLVMTargetMachine *>(
      TheTargetMachine->getTarget().createTargetMachine(
          TheTargetMachine->getTargetTriple().normalize(),
          TheTargetMachine->getTargetCPU(),
          TheTargetMachine->getTargetFeatureString(), TheTargetMachine->Options,
          Reloc::Model::Static)));
}

std::unique_ptr<const DenseMap<StringRef, unsigned>>
LLVMState::createOpcodeNameToOpcodeIdxMapping() const {
  const MCInstrInfo &InstrInfo = getInstrInfo();
  auto Map = std::make_unique<DenseMap<StringRef, unsigned>>(
      InstrInfo.getNumOpcodes());
  for (unsigned I = 0, E = InstrInfo.getNumOpcodes(); I < E; ++I)
    (*Map)[InstrInfo.getName(I)] = I;
  assert(Map->size() == InstrInfo.getNumOpcodes() && "Size prediction failed");
  return std::move(Map);
}

std::unique_ptr<const DenseMap<StringRef, unsigned>>
LLVMState::createRegNameToRegNoMapping() const {
  const MCRegisterInfo &RegInfo = getRegInfo();
  auto Map =
      std::make_unique<DenseMap<StringRef, unsigned>>(RegInfo.getNumRegs());
  // Special-case RegNo 0, which would otherwise be spelled as ''.
  (*Map)[kNoRegister] = 0;
  for (unsigned I = 1, E = RegInfo.getNumRegs(); I < E; ++I)
    (*Map)[RegInfo.getName(I)] = I;
  assert(Map->size() == RegInfo.getNumRegs() && "Size prediction failed");
  return std::move(Map);
}

bool LLVMState::canAssemble(const MCInst &Inst) const {
  MCContext Context(TheTargetMachine->getTargetTriple(),
                    TheTargetMachine->getMCAsmInfo(),
                    TheTargetMachine->getMCRegisterInfo(),
                    TheTargetMachine->getMCSubtargetInfo());
  std::unique_ptr<const MCCodeEmitter> CodeEmitter(
      TheTargetMachine->getTarget().createMCCodeEmitter(
          *TheTargetMachine->getMCInstrInfo(), Context));
  assert(CodeEmitter && "unable to create code emitter");
  SmallVector<char, 16> Tmp;
  raw_svector_ostream OS(Tmp);
  SmallVector<MCFixup, 4> Fixups;
  CodeEmitter->encodeInstruction(Inst, OS, Fixups,
                                 *TheTargetMachine->getMCSubtargetInfo());
  return Tmp.size() > 0;
}

} // namespace exegesis
} // namespace llvm