blob: dd778d35b72d8a057dd43a83d94921564efe35fb [file] [log] [blame]
Clement Courbet44b4c542018-06-19 11:28:59 +00001//===-- Target.h ------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11///
12/// Classes that handle the creation of target-specific objects. This is
13/// similar to llvm::Target/TargetRegistry.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
18#define LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
19
Clement Courbet4860b982018-06-26 08:49:30 +000020#include "BenchmarkResult.h"
21#include "BenchmarkRunner.h"
22#include "LlvmState.h"
Clement Courbetd939f6d2018-09-13 07:40:53 +000023#include "SnippetGenerator.h"
Clement Courbet44b4c542018-06-19 11:28:59 +000024#include "llvm/ADT/Triple.h"
Clement Courbet6fd00e32018-06-20 11:54:35 +000025#include "llvm/CodeGen/TargetPassConfig.h"
Guillaume Chateletfb943542018-08-01 14:41:45 +000026#include "llvm/IR/CallingConv.h"
Clement Courbet6fd00e32018-06-20 11:54:35 +000027#include "llvm/IR/LegacyPassManager.h"
Clement Courbeta51efc22018-06-25 13:12:02 +000028#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCRegisterInfo.h"
Clement Courbet44b4c542018-06-19 11:28:59 +000030
31namespace exegesis {
32
33class ExegesisTarget {
34public:
Clement Courbet6fd00e32018-06-20 11:54:35 +000035 // Targets can use this to add target-specific passes in assembleToStream();
36 virtual void addTargetSpecificPasses(llvm::PassManagerBase &PM) const {}
37
Clement Courbeta51efc22018-06-25 13:12:02 +000038 // Generates code to move a constant into a the given register.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000039 // Precondition: Value must fit into Reg.
Simon Pilgrimf652ef32018-09-18 15:38:16 +000040 virtual std::vector<llvm::MCInst>
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000041 setRegTo(const llvm::MCSubtargetInfo &STI, unsigned Reg,
42 const llvm::APInt &Value) const = 0;
Guillaume Chatelet5ad29092018-09-18 11:26:27 +000043
44 // Returns the register pointing to scratch memory, or 0 if this target
45 // does not support memory operands. The benchmark function uses the
46 // default calling convention.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000047 virtual unsigned getScratchMemoryRegister(const llvm::Triple &) const {
48 return 0;
49 }
Guillaume Chateletfb943542018-08-01 14:41:45 +000050
51 // Fills memory operands with references to the address at [Reg] + Offset.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000052 virtual void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000053 unsigned Offset) const {
54
55 llvm_unreachable(
56 "fillMemoryOperands() requires getScratchMemoryRegister() > 0");
57 }
Guillaume Chateletfb943542018-08-01 14:41:45 +000058
59 // Returns the maximum number of bytes a load/store instruction can access at
60 // once. This is typically the size of the largest register available on the
61 // processor. Note that this only used as a hint to generate independant
62 // load/stores to/from memory, so the exact returned value does not really
63 // matter as long as it's large enough.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000064 virtual unsigned getMaxMemoryAccessSize() const { return 0; }
Guillaume Chateletfb943542018-08-01 14:41:45 +000065
Clement Courbetd939f6d2018-09-13 07:40:53 +000066 // Creates a snippet generator for the given mode.
67 std::unique_ptr<SnippetGenerator>
68 createSnippetGenerator(InstructionBenchmark::ModeE Mode,
69 const LLVMState &State) const;
Clement Courbet4860b982018-06-26 08:49:30 +000070 // Creates a benchmark runner for the given mode.
71 std::unique_ptr<BenchmarkRunner>
72 createBenchmarkRunner(InstructionBenchmark::ModeE Mode,
73 const LLVMState &State) const;
74
Clement Courbet44b4c542018-06-19 11:28:59 +000075 // Returns the ExegesisTarget for the given triple or nullptr if the target
76 // does not exist.
Clement Courbet6fd00e32018-06-20 11:54:35 +000077 static const ExegesisTarget *lookup(llvm::Triple TT);
Clement Courbet4860b982018-06-26 08:49:30 +000078 // Returns the default (unspecialized) ExegesisTarget.
79 static const ExegesisTarget &getDefault();
Clement Courbet44b4c542018-06-19 11:28:59 +000080 // Registers a target. Not thread safe.
81 static void registerTarget(ExegesisTarget *T);
82
Roman Lebedev3de96642018-06-19 11:58:10 +000083 virtual ~ExegesisTarget();
Clement Courbet44b4c542018-06-19 11:28:59 +000084
85private:
86 virtual bool matchesArch(llvm::Triple::ArchType Arch) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000087
Clement Courbetd939f6d2018-09-13 07:40:53 +000088 // Targets can implement their own snippet generators/benchmarks runners by
Clement Courbet4860b982018-06-26 08:49:30 +000089 // implementing these.
Clement Courbetd939f6d2018-09-13 07:40:53 +000090 std::unique_ptr<SnippetGenerator> virtual createLatencySnippetGenerator(
91 const LLVMState &State) const;
92 std::unique_ptr<SnippetGenerator> virtual createUopsSnippetGenerator(
93 const LLVMState &State) const;
Clement Courbet4860b982018-06-26 08:49:30 +000094 std::unique_ptr<BenchmarkRunner> virtual createLatencyBenchmarkRunner(
95 const LLVMState &State) const;
96 std::unique_ptr<BenchmarkRunner> virtual createUopsBenchmarkRunner(
97 const LLVMState &State) const;
98
Clement Courbetcff2caa2018-06-25 11:22:23 +000099 const ExegesisTarget *Next = nullptr;
Clement Courbet44b4c542018-06-19 11:28:59 +0000100};
101
Clement Courbetcff2caa2018-06-25 11:22:23 +0000102} // namespace exegesis
Clement Courbet44b4c542018-06-19 11:28:59 +0000103
104#endif // LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H