Guillaume Chatelet | 7f8d310 | 2018-09-26 11:57:24 +0000 | [diff] [blame] | 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 |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame^] | 11 | /// A set of structures and functions to craft instructions for the |
| 12 | /// SnippetGenerator. |
Guillaume Chatelet | 7f8d310 | 2018-09-26 11:57:24 +0000 | [diff] [blame] | 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H |
| 17 | #define LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H |
| 18 | |
| 19 | #include "MCInstrDescView.h" |
| 20 | |
| 21 | namespace exegesis { |
| 22 | |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame^] | 23 | // A template for an Instruction holding values for each of its Variables. |
| 24 | struct 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 Chatelet | 7f8d310 | 2018-09-26 11:57:24 +0000 | [diff] [blame] | 56 | struct 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 Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame^] | 67 | std::vector<InstructionTemplate> Instructions; |
Guillaume Chatelet | 7f8d310 | 2018-09-26 11:57:24 +0000 | [diff] [blame] | 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; |
| 71 | }; |
| 72 | |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame^] | 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(); |
| 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. |
| 80 | size_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. |
| 84 | void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations, |
| 85 | InstructionTemplate &DefIB, InstructionTemplate &UseIB); |
| 86 | |
Guillaume Chatelet | 7f8d310 | 2018-09-26 11:57:24 +0000 | [diff] [blame] | 87 | } // namespace exegesis |
| 88 | |
| 89 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H |