blob: 4c55487f3d12161c977c5097c2b5de16967ddb23 [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"
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000020#include "llvm/ADT/BitmaskEnum.h"
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000021
Fangrui Song32401af2018-10-22 17:10:47 +000022namespace llvm {
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000023namespace exegesis {
24
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000025// A template for an Instruction holding values for each of its Variables.
26struct InstructionTemplate {
27 InstructionTemplate(const Instruction &Instr);
28
29 InstructionTemplate(const InstructionTemplate &); // default
30 InstructionTemplate &operator=(const InstructionTemplate &); // default
31 InstructionTemplate(InstructionTemplate &&); // default
32 InstructionTemplate &operator=(InstructionTemplate &&); // default
33
34 unsigned getOpcode() const;
35 llvm::MCOperand &getValueFor(const Variable &Var);
36 const llvm::MCOperand &getValueFor(const Variable &Var) const;
37 llvm::MCOperand &getValueFor(const Operand &Op);
38 const llvm::MCOperand &getValueFor(const Operand &Op) const;
39 bool hasImmediateVariables() const;
40
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000041 // Builds an llvm::MCInst from this InstructionTemplate setting its operands
42 // to the corresponding variable values. Precondition: All VariableValues must
43 // be set.
44 llvm::MCInst build() const;
45
46 Instruction Instr;
47 llvm::SmallVector<llvm::MCOperand, 4> VariableValues;
48};
49
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000050enum class ExecutionMode : uint8_t {
51 UNKNOWN = 0U,
52 // The instruction is always serial because implicit Use and Def alias.
53 // e.g. AAA (alias via EFLAGS)
54 ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS = 1u << 0,
55
56 // The instruction is always serial because one Def is tied to a Use.
57 // e.g. AND32ri (alias via tied GR32)
58 ALWAYS_SERIAL_TIED_REGS_ALIAS = 1u << 1,
59
60 // The execution can be made serial by inserting a second instruction that
61 // clobbers/reads memory.
62 // e.g. MOV8rm
63 SERIAL_VIA_MEMORY_INSTR = 1u << 2,
64
65 // The execution can be made serial by picking one Def that aliases with one
66 // Use.
67 // e.g. VXORPSrr XMM1, XMM1, XMM2
68 SERIAL_VIA_EXPLICIT_REGS = 1u << 3,
69
70 // The execution can be made serial by inserting a second instruction that
71 // uses one of the Defs and defs one of the Uses.
72 // e.g.
73 // 1st instruction: MMX_PMOVMSKBrr ECX, MM7
74 // 2nd instruction: MMX_MOVD64rr MM7, ECX
75 // or instruction: MMX_MOVD64to64rr MM7, ECX
76 // or instruction: MMX_PINSRWrr MM7, MM7, ECX, 1
77 SERIAL_VIA_NON_MEMORY_INSTR = 1u << 4,
78
79 // The execution is always parallel because the instruction is missing Use or
80 // Def operands.
81 ALWAYS_PARALLEL_MISSING_USE_OR_DEF = 1u << 5,
82
83 // The execution can be made parallel by repeating the same instruction but
84 // making sure that Defs of one instruction do not alias with Uses of the
85 // second one.
86 PARALLEL_VIA_EXPLICIT_REGS = 1u << 6,
87
88 LLVM_MARK_AS_BITMASK_ENUM(/*Largest*/ PARALLEL_VIA_EXPLICIT_REGS)
89};
90
91// Returns whether Execution is one of the values defined in the enum above.
92bool isEnumValue(ExecutionMode Execution);
93
94// Returns a human readable string for the enum.
95llvm::StringRef getName(ExecutionMode Execution);
96
97// Returns a sequence of increasing powers of two corresponding to all the
98// Execution flags.
99llvm::ArrayRef<ExecutionMode> getAllExecutionBits();
100
101// Decomposes Execution into individual set bits.
102llvm::SmallVector<ExecutionMode, 4> getExecutionModeBits(ExecutionMode);
103
104LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
105
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000106// A CodeTemplate is a set of InstructionTemplates that may not be fully
107// specified (i.e. some variables are not yet set). This allows the
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000108// SnippetGenerator to instantiate it many times with specific values to study
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000109// their impact on instruction's performance.
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000110struct CodeTemplate {
111 CodeTemplate() = default;
112
113 CodeTemplate(CodeTemplate &&); // default
114 CodeTemplate &operator=(CodeTemplate &&); // default
115 CodeTemplate(const CodeTemplate &) = delete;
116 CodeTemplate &operator=(const CodeTemplate &) = delete;
117
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000118 ExecutionMode Execution = ExecutionMode::UNKNOWN;
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000119 // Some information about how this template has been created.
120 std::string Info;
121 // The list of the instructions for this template.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000122 std::vector<InstructionTemplate> Instructions;
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000123 // If the template uses the provided scratch memory, the register in which
124 // the pointer to this memory is passed in to the function.
125 unsigned ScratchSpacePointerInReg = 0;
126};
127
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000128} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000129} // namespace llvm
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000130
131#endif // LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H