[llvm-exegesis] Implements a cache of Instruction objects.
[llvm-core.git] / tools / llvm-exegesis / lib / SnippetGenerator.h
blob967b273182b74b3f2e00fd83b56bf1ce169512f7
1 //===-- SnippetGenerator.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 /// Defines the abstract SnippetGenerator class for generating code that allows
12 /// measuring a certain property of instructions (e.g. latency).
13 ///
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H
19 #include "Assembler.h"
20 #include "BenchmarkCode.h"
21 #include "CodeTemplate.h"
22 #include "LlvmState.h"
23 #include "MCInstrDescView.h"
24 #include "RegisterAliasing.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/Support/Error.h"
27 #include <cstdlib>
28 #include <memory>
29 #include <vector>
31 namespace llvm {
32 namespace exegesis {
34 std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT);
36 // Generates code templates that has a self-dependency.
37 llvm::Expected<std::vector<CodeTemplate>>
38 generateSelfAliasingCodeTemplates(const Instruction &Instr);
40 // Generates code templates without assignment constraints.
41 llvm::Expected<std::vector<CodeTemplate>>
42 generateUnconstrainedCodeTemplates(const Instruction &Instr,
43 llvm::StringRef Msg);
45 // A class representing failures that happened during Benchmark, they are used
46 // to report informations to the user.
47 class SnippetGeneratorFailure : public llvm::StringError {
48 public:
49 SnippetGeneratorFailure(const llvm::Twine &S);
52 // Common code for all benchmark modes.
53 class SnippetGenerator {
54 public:
55 explicit SnippetGenerator(const LLVMState &State);
57 virtual ~SnippetGenerator();
59 // Calls generateCodeTemplate and expands it into one or more BenchmarkCode.
60 llvm::Expected<std::vector<BenchmarkCode>>
61 generateConfigurations(const Instruction &Instr) const;
63 // Given a snippet, computes which registers the setup code needs to define.
64 std::vector<RegisterValue> computeRegisterInitialValues(
65 const std::vector<InstructionTemplate> &Snippet) const;
67 protected:
68 const LLVMState &State;
70 private:
71 // API to be implemented by subclasses.
72 virtual llvm::Expected<std::vector<CodeTemplate>>
73 generateCodeTemplates(const Instruction &Instr) const = 0;
76 // A global Random Number Generator to randomize configurations.
77 // FIXME: Move random number generation into an object and make it seedable for
78 // unit tests.
79 std::mt19937 &randomGenerator();
81 // Picks a random bit among the bits set in Vector and returns its index.
82 // Precondition: Vector must have at least one bit set.
83 size_t randomBit(const llvm::BitVector &Vector);
85 // Picks a random configuration, then selects a random def and a random use from
86 // it and finally set the selected values in the provided InstructionInstances.
87 void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
88 InstructionTemplate &DefIB, InstructionTemplate &UseIB);
90 // Assigns a Random Value to all Variables in IT that are still Invalid.
91 // Do not use any of the registers in `ForbiddenRegs`.
92 void randomizeUnsetVariables(const llvm::BitVector &ForbiddenRegs,
93 InstructionTemplate &IT);
95 } // namespace exegesis
96 } // namespace llvm
98 #endif // LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H