blob: 2e94727d78db0b01dc2bff4fab99811dd25f5486 [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
Fangrui Song32401af2018-10-22 17:10:47 +000031namespace llvm {
Clement Courbet44b4c542018-06-19 11:28:59 +000032namespace exegesis {
33
34class ExegesisTarget {
35public:
Clement Courbet6fd00e32018-06-20 11:54:35 +000036 // Targets can use this to add target-specific passes in assembleToStream();
37 virtual void addTargetSpecificPasses(llvm::PassManagerBase &PM) const {}
38
Clement Courbeta51efc22018-06-25 13:12:02 +000039 // Generates code to move a constant into a the given register.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000040 // Precondition: Value must fit into Reg.
Simon Pilgrimf652ef32018-09-18 15:38:16 +000041 virtual std::vector<llvm::MCInst>
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000042 setRegTo(const llvm::MCSubtargetInfo &STI, unsigned Reg,
43 const llvm::APInt &Value) const = 0;
Guillaume Chatelet5ad29092018-09-18 11:26:27 +000044
45 // Returns the register pointing to scratch memory, or 0 if this target
46 // does not support memory operands. The benchmark function uses the
47 // default calling convention.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000048 virtual unsigned getScratchMemoryRegister(const llvm::Triple &) const {
49 return 0;
50 }
Guillaume Chateletfb943542018-08-01 14:41:45 +000051
52 // Fills memory operands with references to the address at [Reg] + Offset.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000053 virtual void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000054 unsigned Offset) const {
55
56 llvm_unreachable(
57 "fillMemoryOperands() requires getScratchMemoryRegister() > 0");
58 }
Guillaume Chateletfb943542018-08-01 14:41:45 +000059
60 // Returns the maximum number of bytes a load/store instruction can access at
61 // once. This is typically the size of the largest register available on the
62 // processor. Note that this only used as a hint to generate independant
63 // load/stores to/from memory, so the exact returned value does not really
64 // matter as long as it's large enough.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000065 virtual unsigned getMaxMemoryAccessSize() const { return 0; }
Guillaume Chateletfb943542018-08-01 14:41:45 +000066
Clement Courbetd939f6d2018-09-13 07:40:53 +000067 // Creates a snippet generator for the given mode.
68 std::unique_ptr<SnippetGenerator>
69 createSnippetGenerator(InstructionBenchmark::ModeE Mode,
70 const LLVMState &State) const;
Clement Courbet4860b982018-06-26 08:49:30 +000071 // Creates a benchmark runner for the given mode.
72 std::unique_ptr<BenchmarkRunner>
73 createBenchmarkRunner(InstructionBenchmark::ModeE Mode,
74 const LLVMState &State) const;
75
Clement Courbet44b4c542018-06-19 11:28:59 +000076 // Returns the ExegesisTarget for the given triple or nullptr if the target
77 // does not exist.
Clement Courbet6fd00e32018-06-20 11:54:35 +000078 static const ExegesisTarget *lookup(llvm::Triple TT);
Clement Courbet4860b982018-06-26 08:49:30 +000079 // Returns the default (unspecialized) ExegesisTarget.
80 static const ExegesisTarget &getDefault();
Clement Courbet44b4c542018-06-19 11:28:59 +000081 // Registers a target. Not thread safe.
82 static void registerTarget(ExegesisTarget *T);
83
Roman Lebedev3de96642018-06-19 11:58:10 +000084 virtual ~ExegesisTarget();
Clement Courbet44b4c542018-06-19 11:28:59 +000085
86private:
87 virtual bool matchesArch(llvm::Triple::ArchType Arch) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000088
Clement Courbetd939f6d2018-09-13 07:40:53 +000089 // Targets can implement their own snippet generators/benchmarks runners by
Clement Courbet4860b982018-06-26 08:49:30 +000090 // implementing these.
Clement Courbetd939f6d2018-09-13 07:40:53 +000091 std::unique_ptr<SnippetGenerator> virtual createLatencySnippetGenerator(
92 const LLVMState &State) const;
93 std::unique_ptr<SnippetGenerator> virtual createUopsSnippetGenerator(
94 const LLVMState &State) const;
Clement Courbet4860b982018-06-26 08:49:30 +000095 std::unique_ptr<BenchmarkRunner> virtual createLatencyBenchmarkRunner(
96 const LLVMState &State) const;
97 std::unique_ptr<BenchmarkRunner> virtual createUopsBenchmarkRunner(
98 const LLVMState &State) const;
99
Clement Courbetcff2caa2018-06-25 11:22:23 +0000100 const ExegesisTarget *Next = nullptr;
Clement Courbet44b4c542018-06-19 11:28:59 +0000101};
102
Clement Courbetcff2caa2018-06-25 11:22:23 +0000103} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000104} // namespace llvm
Clement Courbet44b4c542018-06-19 11:28:59 +0000105
106#endif // LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H