LlvmState.cpp 3.4 KB

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