blob: c4be621b291242b01cdf2d5c202971d2fe8e764b [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
Clement Courbet41c8af32018-10-25 07:44:01 +000034struct PfmCountersInfo {
35 // An optional name of a performance counter that can be used to measure
36 // cycles.
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000037 const char *CycleCounter;
Clement Courbet41c8af32018-10-25 07:44:01 +000038
39 // An optional name of a performance counter that can be used to measure
40 // uops.
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000041 const char *UopsCounter;
Clement Courbet41c8af32018-10-25 07:44:01 +000042
43 // An IssueCounter specifies how to measure uops issued to specific proc
44 // resources.
45 struct IssueCounter {
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000046 const char *Counter;
Clement Courbet41c8af32018-10-25 07:44:01 +000047 // The name of the ProcResource that this counter measures.
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000048 const char *ProcResName;
Clement Courbet41c8af32018-10-25 07:44:01 +000049 };
50 // An optional list of IssueCounters.
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000051 const IssueCounter *IssueCounters;
52 unsigned NumIssueCounters;
Clement Courbet41c8af32018-10-25 07:44:01 +000053
54 static const PfmCountersInfo Default;
55};
56
57struct CpuAndPfmCounters {
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000058 const char *CpuName;
59 const PfmCountersInfo *PCI;
Clement Courbet41c8af32018-10-25 07:44:01 +000060 bool operator<(llvm::StringRef S) const {
61 return llvm::StringRef(CpuName) < S;
62 }
63};
64
Clement Courbet44b4c542018-06-19 11:28:59 +000065class ExegesisTarget {
66public:
Clement Courbet41c8af32018-10-25 07:44:01 +000067 explicit ExegesisTarget(llvm::ArrayRef<CpuAndPfmCounters> CpuPfmCounters)
68 : CpuPfmCounters(CpuPfmCounters) {}
69
Clement Courbet6fd00e32018-06-20 11:54:35 +000070 // Targets can use this to add target-specific passes in assembleToStream();
71 virtual void addTargetSpecificPasses(llvm::PassManagerBase &PM) const {}
72
Clement Courbeta51efc22018-06-25 13:12:02 +000073 // Generates code to move a constant into a the given register.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000074 // Precondition: Value must fit into Reg.
Simon Pilgrimf652ef32018-09-18 15:38:16 +000075 virtual std::vector<llvm::MCInst>
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000076 setRegTo(const llvm::MCSubtargetInfo &STI, unsigned Reg,
77 const llvm::APInt &Value) const = 0;
Guillaume Chatelet5ad29092018-09-18 11:26:27 +000078
Clement Courbetc0950ae2018-11-08 11:45:14 +000079 // Generates code to copy `FromReg` to `ToReg`.
80 // Precondition: Registers must be the same size.
81 virtual std::vector<llvm::MCInst>
82 copyReg(const llvm::MCSubtargetInfo &STI, unsigned ToReg, unsigned FromReg) const = 0;
83
Guillaume Chatelet5ad29092018-09-18 11:26:27 +000084 // Returns the register pointing to scratch memory, or 0 if this target
85 // does not support memory operands. The benchmark function uses the
86 // default calling convention.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000087 virtual unsigned getScratchMemoryRegister(const llvm::Triple &) const {
88 return 0;
89 }
Guillaume Chateletfb943542018-08-01 14:41:45 +000090
Clement Courbetc0950ae2018-11-08 11:45:14 +000091 // Returns the opcode to move the value at `[Reg]` into `Reg`, where `Reg` is
92 // the from the same register class as getScratchMemoryRegister().
93 virtual unsigned getChaseRegOpcode() const {
94 llvm_unreachable(
95 "fillMemoryOperands() requires getScratchMemoryRegister() > 0");
96 }
97
Guillaume Chateletfb943542018-08-01 14:41:45 +000098 // Fills memory operands with references to the address at [Reg] + Offset.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000099 virtual void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
Guillaume Chateletc96a97b2018-09-20 12:22:18 +0000100 unsigned Offset) const {
Guillaume Chateletc96a97b2018-09-20 12:22:18 +0000101 llvm_unreachable(
102 "fillMemoryOperands() requires getScratchMemoryRegister() > 0");
103 }
Guillaume Chateletfb943542018-08-01 14:41:45 +0000104
105 // Returns the maximum number of bytes a load/store instruction can access at
106 // once. This is typically the size of the largest register available on the
107 // processor. Note that this only used as a hint to generate independant
108 // load/stores to/from memory, so the exact returned value does not really
109 // matter as long as it's large enough.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +0000110 virtual unsigned getMaxMemoryAccessSize() const { return 0; }
Guillaume Chateletfb943542018-08-01 14:41:45 +0000111
Clement Courbetd939f6d2018-09-13 07:40:53 +0000112 // Creates a snippet generator for the given mode.
113 std::unique_ptr<SnippetGenerator>
114 createSnippetGenerator(InstructionBenchmark::ModeE Mode,
115 const LLVMState &State) const;
Clement Courbet4860b982018-06-26 08:49:30 +0000116 // Creates a benchmark runner for the given mode.
117 std::unique_ptr<BenchmarkRunner>
118 createBenchmarkRunner(InstructionBenchmark::ModeE Mode,
119 const LLVMState &State) const;
120
Clement Courbet44b4c542018-06-19 11:28:59 +0000121 // Returns the ExegesisTarget for the given triple or nullptr if the target
122 // does not exist.
Clement Courbet6fd00e32018-06-20 11:54:35 +0000123 static const ExegesisTarget *lookup(llvm::Triple TT);
Clement Courbet4860b982018-06-26 08:49:30 +0000124 // Returns the default (unspecialized) ExegesisTarget.
125 static const ExegesisTarget &getDefault();
Clement Courbet44b4c542018-06-19 11:28:59 +0000126 // Registers a target. Not thread safe.
127 static void registerTarget(ExegesisTarget *T);
128
Roman Lebedev3de96642018-06-19 11:58:10 +0000129 virtual ~ExegesisTarget();
Clement Courbet44b4c542018-06-19 11:28:59 +0000130
Clement Courbet41c8af32018-10-25 07:44:01 +0000131 // Returns the Pfm counters for the given CPU (or the default if no pfm
132 // counters are defined for this CPU).
133 const PfmCountersInfo &getPfmCounters(llvm::StringRef CpuName) const;
134
Clement Courbet44b4c542018-06-19 11:28:59 +0000135private:
136 virtual bool matchesArch(llvm::Triple::ArchType Arch) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +0000137
Clement Courbetd939f6d2018-09-13 07:40:53 +0000138 // Targets can implement their own snippet generators/benchmarks runners by
Clement Courbet4860b982018-06-26 08:49:30 +0000139 // implementing these.
Clement Courbetd939f6d2018-09-13 07:40:53 +0000140 std::unique_ptr<SnippetGenerator> virtual createLatencySnippetGenerator(
141 const LLVMState &State) const;
142 std::unique_ptr<SnippetGenerator> virtual createUopsSnippetGenerator(
143 const LLVMState &State) const;
Clement Courbet4860b982018-06-26 08:49:30 +0000144 std::unique_ptr<BenchmarkRunner> virtual createLatencyBenchmarkRunner(
145 const LLVMState &State) const;
146 std::unique_ptr<BenchmarkRunner> virtual createUopsBenchmarkRunner(
147 const LLVMState &State) const;
148
Clement Courbetcff2caa2018-06-25 11:22:23 +0000149 const ExegesisTarget *Next = nullptr;
Clement Courbet41c8af32018-10-25 07:44:01 +0000150 const llvm::ArrayRef<CpuAndPfmCounters> CpuPfmCounters;
Clement Courbet44b4c542018-06-19 11:28:59 +0000151};
152
Clement Courbetcff2caa2018-06-25 11:22:23 +0000153} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000154} // namespace llvm
Clement Courbet44b4c542018-06-19 11:28:59 +0000155
156#endif // LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H