[llvm-exegesis][NFC] Simplify LLVMState.
authorClement Courbet <courbet@google.com>
Thu, 21 Jun 2018 14:11:09 +0000 (14:11 +0000)
committerClement Courbet <courbet@google.com>
Thu, 21 Jun 2018 14:11:09 +0000 (14:11 +0000)
Summary: Pretty much everything we need is in llvm::TargetMachine.

Reviewers: gchatelet

Subscribers: llvm-commits, tschuett

Differential Revision: https://reviews.llvm.org/D48428

llvm-svn: 335237

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
llvm/tools/llvm-exegesis/lib/LlvmState.cpp
llvm/tools/llvm-exegesis/lib/LlvmState.h

index e5b6225..3074cb4 100644 (file)
@@ -63,8 +63,9 @@ BenchmarkRunner::runOne(const BenchmarkConfiguration &Configuration,
                         unsigned Opcode, unsigned NumRepetitions) const {
   InstructionBenchmark InstrBenchmark;
   InstrBenchmark.Mode = getMode();
-  InstrBenchmark.CpuName = State.getCpuName();
-  InstrBenchmark.LLVMTriple = State.getTriple();
+  InstrBenchmark.CpuName = State.getTargetMachine().getTargetCPU();
+  InstrBenchmark.LLVMTriple =
+      State.getTargetMachine().getTargetTriple().normalize();
   InstrBenchmark.NumRepetitions = NumRepetitions;
   InstrBenchmark.Info = Configuration.Info;
 
index e286853..efb5c83 100644 (file)
 
 namespace exegesis {
 
-LLVMState::LLVMState(const std::string &Triple, const std::string &CpuName)
-    : TheTriple(Triple), CpuName(CpuName) {
+LLVMState::LLVMState(const std::string &Triple, const std::string &CpuName) {
   std::string Error;
-  TheTarget = llvm::TargetRegistry::lookupTarget(TheTriple, Error);
+  const llvm::Target *const TheTarget =
+      llvm::TargetRegistry::lookupTarget(Triple, Error);
   assert(TheTarget && "unknown target for host");
-  SubtargetInfo.reset(
-      TheTarget->createMCSubtargetInfo(TheTriple, CpuName, Features));
-  InstrInfo.reset(TheTarget->createMCInstrInfo());
-  RegInfo.reset(TheTarget->createMCRegInfo(TheTriple));
-  AsmInfo.reset(TheTarget->createMCAsmInfo(*RegInfo, TheTriple));
+  const llvm::TargetOptions Options;
+  TargetMachine.reset(static_cast<llvm::LLVMTargetMachine *>(
+      TheTarget->createTargetMachine(Triple, CpuName, /*Features*/ "", Options,
+                                     llvm::Reloc::Model::Static)));
 }
 
 LLVMState::LLVMState()
@@ -38,21 +37,28 @@ LLVMState::LLVMState()
 
 std::unique_ptr<llvm::LLVMTargetMachine>
 LLVMState::createTargetMachine() const {
-  const llvm::TargetOptions Options;
   return std::unique_ptr<llvm::LLVMTargetMachine>(
-      static_cast<llvm::LLVMTargetMachine *>(TheTarget->createTargetMachine(
-          TheTriple, CpuName, Features, Options, llvm::Reloc::Model::Static)));
+      static_cast<llvm::LLVMTargetMachine *>(
+          TargetMachine->getTarget().createTargetMachine(
+              TargetMachine->getTargetTriple().normalize(),
+              TargetMachine->getTargetCPU(),
+              TargetMachine->getTargetFeatureString(), TargetMachine->Options,
+              llvm::Reloc::Model::Static)));
 }
 
 bool LLVMState::canAssemble(const llvm::MCInst &Inst) const {
   llvm::MCObjectFileInfo ObjectFileInfo;
-  llvm::MCContext Context(AsmInfo.get(), RegInfo.get(), &ObjectFileInfo);
+  llvm::MCContext Context(TargetMachine->getMCAsmInfo(),
+                          TargetMachine->getMCRegisterInfo(), &ObjectFileInfo);
   std::unique_ptr<const llvm::MCCodeEmitter> CodeEmitter(
-      TheTarget->createMCCodeEmitter(*InstrInfo, *RegInfo, Context));
+      TargetMachine->getTarget().createMCCodeEmitter(
+          *TargetMachine->getMCInstrInfo(), *TargetMachine->getMCRegisterInfo(),
+          Context));
   llvm::SmallVector<char, 16> Tmp;
   llvm::raw_svector_ostream OS(Tmp);
   llvm::SmallVector<llvm::MCFixup, 4> Fixups;
-  CodeEmitter->encodeInstruction(Inst, OS, Fixups, *SubtargetInfo);
+  CodeEmitter->encodeInstruction(Inst, OS, Fixups,
+                                 *TargetMachine->getMCSubtargetInfo());
   return Tmp.size() > 0;
 }
 
index 3c8a4a0..5c09014 100644 (file)
@@ -26,6 +26,8 @@
 
 namespace exegesis {
 
+class ExegesisTarget;
+
 // An object to initialize LLVM and prepare objects needed to run the
 // measurements.
 class LLVMState {
@@ -35,31 +37,27 @@ public:
   LLVMState(const std::string &Triple,
             const std::string &CpuName); // For tests.
 
-  llvm::StringRef getTriple() const { return TheTriple; }
-  llvm::StringRef getCpuName() const { return CpuName; }
-  llvm::StringRef getFeatures() const { return Features; }
+  const llvm::TargetMachine &getTargetMachine() const { return *TargetMachine; }
+  std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() const;
 
-  const llvm::MCInstrInfo &getInstrInfo() const { return *InstrInfo; }
+  const ExegesisTarget *getExegesisTarget() const { return TheExegesisTarget; }
 
-  const llvm::MCRegisterInfo &getRegInfo() const { return *RegInfo; }
+  bool canAssemble(const llvm::MCInst &mc_inst) const;
 
+  // For convenience:
+  const llvm::MCInstrInfo &getInstrInfo() const {
+    return *TargetMachine->getMCInstrInfo();
+  }
+  const llvm::MCRegisterInfo &getRegInfo() const {
+    return *TargetMachine->getMCRegisterInfo();
+  }
   const llvm::MCSubtargetInfo &getSubtargetInfo() const {
-    return *SubtargetInfo;
+    return *TargetMachine->getMCSubtargetInfo();
   }
 
-  std::unique_ptr<llvm::LLVMTargetMachine> createTargetMachine() const;
-
-  bool canAssemble(const llvm::MCInst &mc_inst) const;
-
 private:
-  std::string TheTriple;
-  std::string CpuName;
-  std::string Features;
-  const llvm::Target *TheTarget = nullptr;
-  std::unique_ptr<const llvm::MCSubtargetInfo> SubtargetInfo;
-  std::unique_ptr<const llvm::MCInstrInfo> InstrInfo;
-  std::unique_ptr<const llvm::MCRegisterInfo> RegInfo;
-  std::unique_ptr<const llvm::MCAsmInfo> AsmInfo;
+  const ExegesisTarget *TheExegesisTarget = nullptr;
+  std::unique_ptr<const llvm::TargetMachine> TargetMachine;
 };
 
 } // namespace exegesis