1 //===-- CodeTemplate.h ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// A set of structures and functions to craft instructions for the
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
19 #include "MCInstrDescView.h"
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
46 llvm::MCInst
build() const;
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.
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.
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
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