blob: b11ef16ab1eb6677b35ad188b5d9525b095d4d16 [file] [log] [blame]
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +00001//===-- 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
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000011/// A set of structures and functions to craft instructions for the
12/// SnippetGenerator.
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000013///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
17#define LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
18
19#include "MCInstrDescView.h"
20
21namespace exegesis {
22
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000023// A template for an Instruction holding values for each of its Variables.
24struct InstructionTemplate {
25 InstructionTemplate(const Instruction &Instr);
26
27 InstructionTemplate(const InstructionTemplate &); // default
28 InstructionTemplate &operator=(const InstructionTemplate &); // default
29 InstructionTemplate(InstructionTemplate &&); // default
30 InstructionTemplate &operator=(InstructionTemplate &&); // default
31
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;
38
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);
42
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;
47
48 Instruction Instr;
49 llvm::SmallVector<llvm::MCOperand, 4> VariableValues;
50};
51
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.
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000056struct CodeTemplate {
57 CodeTemplate() = default;
58
59 CodeTemplate(CodeTemplate &&); // default
60 CodeTemplate &operator=(CodeTemplate &&); // default
61 CodeTemplate(const CodeTemplate &) = delete;
62 CodeTemplate &operator=(const CodeTemplate &) = delete;
63
64 // Some information about how this template has been created.
65 std::string Info;
66 // The list of the instructions for this template.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000067 std::vector<InstructionTemplate> Instructions;
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000068 // 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;
71};
72
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000073// 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.
76std::mt19937 &randomGenerator();
77
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.
80size_t randomBit(const llvm::BitVector &Vector);
81
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.
84void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
85 InstructionTemplate &DefIB, InstructionTemplate &UseIB);
86
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000087} // namespace exegesis
88
89#endif // LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H