[llvm-exegesis][NFC] moving code around.
[llvm-complete.git] / tools / llvm-exegesis / lib / CodeTemplate.h
blobb11ef16ab1eb6677b35ad188b5d9525b095d4d16
1 //===-- CodeTemplate.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 /// A set of structures and functions to craft instructions for the
12 /// SnippetGenerator.
13 ///
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
19 #include "MCInstrDescView.h"
21 namespace exegesis {
23 // A template for an Instruction holding values for each of its Variables.
24 struct InstructionTemplate {
25 InstructionTemplate(const Instruction &Instr);
27 InstructionTemplate(const InstructionTemplate &); // default
28 InstructionTemplate &operator=(const InstructionTemplate &); // default
29 InstructionTemplate(InstructionTemplate &&); // default
30 InstructionTemplate &operator=(InstructionTemplate &&); // default
32 unsigned getOpcode() const;
33 llvm::MCOperand &getValueFor(const Variable &Var);
34 const llvm::MCOperand &getValueFor(const Variable &Var) const;
35 llvm::MCOperand &getValueFor(const Operand &Op);
36 const llvm::MCOperand &getValueFor(const Operand &Op) const;
37 bool hasImmediateVariables() const;
39 // Assigns a Random Value to all Variables that are still Invalid.
40 // Do not use any of the registers in `ForbiddenRegs`.
41 void randomizeUnsetVariables(const llvm::BitVector &ForbiddenRegs);
43 // Builds an llvm::MCInst from this InstructionTemplate setting its operands
44 // to the corresponding variable values. Precondition: All VariableValues must
45 // be set.
46 llvm::MCInst build() const;
48 Instruction Instr;
49 llvm::SmallVector<llvm::MCOperand, 4> VariableValues;
52 // A CodeTemplate is a set of InstructionTemplates that may not be fully
53 // specified (i.e. some variables are not yet set). This allows the
54 // BenchmarkRunner to instantiate it many times with specific values to study
55 // their impact on instruction's performance.
56 struct CodeTemplate {
57 CodeTemplate() = default;
59 CodeTemplate(CodeTemplate &&); // default
60 CodeTemplate &operator=(CodeTemplate &&); // default
61 CodeTemplate(const CodeTemplate &) = delete;
62 CodeTemplate &operator=(const CodeTemplate &) = delete;
64 // Some information about how this template has been created.
65 std::string Info;
66 // The list of the instructions for this template.
67 std::vector<InstructionTemplate> Instructions;
68 // If the template uses the provided scratch memory, the register in which
69 // the pointer to this memory is passed in to the function.
70 unsigned ScratchSpacePointerInReg = 0;
73 // A global Random Number Generator to randomize configurations.
74 // FIXME: Move random number generation into an object and make it seedable for
75 // unit tests.
76 std::mt19937 &randomGenerator();
78 // Picks a random bit among the bits set in Vector and returns its index.
79 // Precondition: Vector must have at least one bit set.
80 size_t randomBit(const llvm::BitVector &Vector);
82 // Picks a random configuration, then selects a random def and a random use from
83 // it and finally set the selected values in the provided InstructionInstances.
84 void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
85 InstructionTemplate &DefIB, InstructionTemplate &UseIB);
87 } // namespace exegesis
89 #endif // LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H