blob: 2edd51481695765a724bbcb93e89207f45581449 [file] [log] [blame]
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +00001//===-- CodeTemplate.h ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000010/// A set of structures and functions to craft instructions for the
11/// SnippetGenerator.
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000012///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
16#define LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H
17
18#include "MCInstrDescView.h"
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000019#include "llvm/ADT/BitmaskEnum.h"
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000020
Fangrui Song32401af2018-10-22 17:10:47 +000021namespace llvm {
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +000022namespace exegesis {
23
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000024// A template for an Instruction holding values for each of its Variables.
25struct InstructionTemplate {
26 InstructionTemplate(const Instruction &Instr);
27
28 InstructionTemplate(const InstructionTemplate &); // default
29 InstructionTemplate &operator=(const InstructionTemplate &); // default
30 InstructionTemplate(InstructionTemplate &&); // default
31 InstructionTemplate &operator=(InstructionTemplate &&); // default
32
33 unsigned getOpcode() const;
34 llvm::MCOperand &getValueFor(const Variable &Var);
35 const llvm::MCOperand &getValueFor(const Variable &Var) const;
36 llvm::MCOperand &getValueFor(const Operand &Op);
37 const llvm::MCOperand &getValueFor(const Operand &Op) const;
38 bool hasImmediateVariables() const;
39
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000040 // Builds an llvm::MCInst from this InstructionTemplate setting its operands
41 // to the corresponding variable values. Precondition: All VariableValues must
42 // be set.
43 llvm::MCInst build() const;
44
45 Instruction Instr;
46 llvm::SmallVector<llvm::MCOperand, 4> VariableValues;
47};
48
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000049enum class ExecutionMode : uint8_t {
50 UNKNOWN = 0U,
51 // The instruction is always serial because implicit Use and Def alias.
52 // e.g. AAA (alias via EFLAGS)
53 ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS = 1u << 0,
54
55 // The instruction is always serial because one Def is tied to a Use.
56 // e.g. AND32ri (alias via tied GR32)
57 ALWAYS_SERIAL_TIED_REGS_ALIAS = 1u << 1,
58
59 // The execution can be made serial by inserting a second instruction that
60 // clobbers/reads memory.
61 // e.g. MOV8rm
62 SERIAL_VIA_MEMORY_INSTR = 1u << 2,
63
64 // The execution can be made serial by picking one Def that aliases with one
65 // Use.
66 // e.g. VXORPSrr XMM1, XMM1, XMM2
67 SERIAL_VIA_EXPLICIT_REGS = 1u << 3,
68
69 // The execution can be made serial by inserting a second instruction that
70 // uses one of the Defs and defs one of the Uses.
71 // e.g.
72 // 1st instruction: MMX_PMOVMSKBrr ECX, MM7
73 // 2nd instruction: MMX_MOVD64rr MM7, ECX
74 // or instruction: MMX_MOVD64to64rr MM7, ECX
75 // or instruction: MMX_PINSRWrr MM7, MM7, ECX, 1
76 SERIAL_VIA_NON_MEMORY_INSTR = 1u << 4,
77
78 // The execution is always parallel because the instruction is missing Use or
79 // Def operands.
80 ALWAYS_PARALLEL_MISSING_USE_OR_DEF = 1u << 5,
81
82 // The execution can be made parallel by repeating the same instruction but
83 // making sure that Defs of one instruction do not alias with Uses of the
84 // second one.
85 PARALLEL_VIA_EXPLICIT_REGS = 1u << 6,
86
87 LLVM_MARK_AS_BITMASK_ENUM(/*Largest*/ PARALLEL_VIA_EXPLICIT_REGS)
88};
89
90// Returns whether Execution is one of the values defined in the enum above.
91bool isEnumValue(ExecutionMode Execution);
92
93// Returns a human readable string for the enum.
94llvm::StringRef getName(ExecutionMode Execution);
95
96// Returns a sequence of increasing powers of two corresponding to all the
97// Execution flags.
98llvm::ArrayRef<ExecutionMode> getAllExecutionBits();
99
100// Decomposes Execution into individual set bits.
101llvm::SmallVector<ExecutionMode, 4> getExecutionModeBits(ExecutionMode);
102
103LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
104
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000105// A CodeTemplate is a set of InstructionTemplates that may not be fully
106// specified (i.e. some variables are not yet set). This allows the
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000107// SnippetGenerator to instantiate it many times with specific values to study
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000108// their impact on instruction's performance.
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000109struct CodeTemplate {
110 CodeTemplate() = default;
111
112 CodeTemplate(CodeTemplate &&); // default
113 CodeTemplate &operator=(CodeTemplate &&); // default
114 CodeTemplate(const CodeTemplate &) = delete;
115 CodeTemplate &operator=(const CodeTemplate &) = delete;
116
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +0000117 ExecutionMode Execution = ExecutionMode::UNKNOWN;
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000118 // Some information about how this template has been created.
119 std::string Info;
120 // The list of the instructions for this template.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000121 std::vector<InstructionTemplate> Instructions;
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000122 // If the template uses the provided scratch memory, the register in which
123 // the pointer to this memory is passed in to the function.
124 unsigned ScratchSpacePointerInReg = 0;
125};
126
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000127} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000128} // namespace llvm
Guillaume Chatelet7f8d3102018-09-26 11:57:24 +0000129
130#endif // LLVM_TOOLS_LLVM_EXEGESIS_CODETEMPLATE_H