[InstCombine] Signed saturation patterns
[llvm-core.git] / tools / llvm-exegesis / lib / SnippetGenerator.h
blob70f81f942be41cc02355334c2f929dc0b8c9f69d
1 //===-- SnippetGenerator.h --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Defines the abstract SnippetGenerator class for generating code that allows
11 /// measuring a certain property of instructions (e.g. latency).
12 ///
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H
18 #include "Assembler.h"
19 #include "BenchmarkCode.h"
20 #include "CodeTemplate.h"
21 #include "LlvmState.h"
22 #include "MCInstrDescView.h"
23 #include "RegisterAliasing.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/Support/Error.h"
26 #include <cstdlib>
27 #include <memory>
28 #include <vector>
30 namespace llvm {
31 namespace exegesis {
33 std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT);
35 // Generates code templates that has a self-dependency.
36 Expected<std::vector<CodeTemplate>>
37 generateSelfAliasingCodeTemplates(const Instruction &Instr);
39 // Generates code templates without assignment constraints.
40 Expected<std::vector<CodeTemplate>>
41 generateUnconstrainedCodeTemplates(const Instruction &Instr, StringRef Msg);
43 // A class representing failures that happened during Benchmark, they are used
44 // to report informations to the user.
45 class SnippetGeneratorFailure : public StringError {
46 public:
47 SnippetGeneratorFailure(const Twine &S);
50 // Common code for all benchmark modes.
51 class SnippetGenerator {
52 public:
53 struct Options {
54 unsigned MaxConfigsPerOpcode = 1;
57 explicit SnippetGenerator(const LLVMState &State, const Options &Opts);
59 virtual ~SnippetGenerator();
61 // Calls generateCodeTemplate and expands it into one or more BenchmarkCode.
62 Expected<std::vector<BenchmarkCode>>
63 generateConfigurations(const Instruction &Instr,
64 const BitVector &ExtraForbiddenRegs) const;
66 // Given a snippet, computes which registers the setup code needs to define.
67 std::vector<RegisterValue> computeRegisterInitialValues(
68 const std::vector<InstructionTemplate> &Snippet) const;
70 protected:
71 const LLVMState &State;
72 const Options Opts;
74 private:
75 // API to be implemented by subclasses.
76 virtual Expected<std::vector<CodeTemplate>>
77 generateCodeTemplates(const Instruction &Instr,
78 const BitVector &ForbiddenRegisters) const = 0;
81 // A global Random Number Generator to randomize configurations.
82 // FIXME: Move random number generation into an object and make it seedable for
83 // unit tests.
84 std::mt19937 &randomGenerator();
86 // Picks a random unsigned integer from 0 to Max (inclusive).
87 size_t randomIndex(size_t Max);
89 // Picks a random bit among the bits set in Vector and returns its index.
90 // Precondition: Vector must have at least one bit set.
91 size_t randomBit(const BitVector &Vector);
93 // Picks a random configuration, then selects a random def and a random use from
94 // it and finally set the selected values in the provided InstructionInstances.
95 void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
96 InstructionTemplate &DefIB, InstructionTemplate &UseIB);
98 // Assigns a Random Value to all Variables in IT that are still Invalid.
99 // Do not use any of the registers in `ForbiddenRegs`.
100 void randomizeUnsetVariables(const ExegesisTarget &Target,
101 const BitVector &ForbiddenRegs,
102 InstructionTemplate &IT);
104 } // namespace exegesis
105 } // namespace llvm
107 #endif // LLVM_TOOLS_LLVM_EXEGESIS_SNIPPETGENERATOR_H