[InstCombine] Signed saturation patterns
[llvm-core.git] / tools / llvm-exegesis / lib / CodeTemplate.cpp
blob9d01c7d40b0d217b66a0dc8e791f0a09b0eba468
1 //===-- CodeTemplate.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "CodeTemplate.h"
11 namespace llvm {
12 namespace exegesis {
14 CodeTemplate::CodeTemplate(CodeTemplate &&) = default;
16 CodeTemplate &CodeTemplate::operator=(CodeTemplate &&) = default;
18 InstructionTemplate::InstructionTemplate(const Instruction &Instr)
19 : Instr(Instr), VariableValues(Instr.Variables.size()) {}
21 InstructionTemplate::InstructionTemplate(InstructionTemplate &&) = default;
23 InstructionTemplate &InstructionTemplate::
24 operator=(InstructionTemplate &&) = default;
26 InstructionTemplate::InstructionTemplate(const InstructionTemplate &) = default;
28 InstructionTemplate &InstructionTemplate::
29 operator=(const InstructionTemplate &) = default;
31 unsigned InstructionTemplate::getOpcode() const {
32 return Instr.Description->getOpcode();
35 MCOperand &InstructionTemplate::getValueFor(const Variable &Var) {
36 return VariableValues[Var.getIndex()];
39 const MCOperand &InstructionTemplate::getValueFor(const Variable &Var) const {
40 return VariableValues[Var.getIndex()];
43 MCOperand &InstructionTemplate::getValueFor(const Operand &Op) {
44 return getValueFor(Instr.Variables[Op.getVariableIndex()]);
47 const MCOperand &InstructionTemplate::getValueFor(const Operand &Op) const {
48 return getValueFor(Instr.Variables[Op.getVariableIndex()]);
51 bool InstructionTemplate::hasImmediateVariables() const {
52 return any_of(Instr.Variables, [this](const Variable &Var) {
53 return Instr.getPrimaryOperand(Var).isImmediate();
54 });
57 MCInst InstructionTemplate::build() const {
58 MCInst Result;
59 Result.setOpcode(Instr.Description->Opcode);
60 for (const auto &Op : Instr.Operands)
61 if (Op.isExplicit())
62 Result.addOperand(getValueFor(Op));
63 return Result;
66 bool isEnumValue(ExecutionMode Execution) {
67 return isPowerOf2_32(static_cast<uint32_t>(Execution));
70 StringRef getName(ExecutionMode Bit) {
71 assert(isEnumValue(Bit) && "Bit must be a power of two");
72 switch (Bit) {
73 case ExecutionMode::UNKNOWN:
74 return "UNKNOWN";
75 case ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS:
76 return "ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS";
77 case ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS:
78 return "ALWAYS_SERIAL_TIED_REGS_ALIAS";
79 case ExecutionMode::SERIAL_VIA_MEMORY_INSTR:
80 return "SERIAL_VIA_MEMORY_INSTR";
81 case ExecutionMode::SERIAL_VIA_EXPLICIT_REGS:
82 return "SERIAL_VIA_EXPLICIT_REGS";
83 case ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR:
84 return "SERIAL_VIA_NON_MEMORY_INSTR";
85 case ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF:
86 return "ALWAYS_PARALLEL_MISSING_USE_OR_DEF";
87 case ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS:
88 return "PARALLEL_VIA_EXPLICIT_REGS";
90 llvm_unreachable("Missing enum case");
93 ArrayRef<ExecutionMode> getAllExecutionBits() {
94 static const ExecutionMode kAllExecutionModeBits[] = {
95 ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS,
96 ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS,
97 ExecutionMode::SERIAL_VIA_MEMORY_INSTR,
98 ExecutionMode::SERIAL_VIA_EXPLICIT_REGS,
99 ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR,
100 ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF,
101 ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS,
103 return makeArrayRef(kAllExecutionModeBits);
106 SmallVector<ExecutionMode, 4> getExecutionModeBits(ExecutionMode Execution) {
107 SmallVector<ExecutionMode, 4> Result;
108 for (const auto Bit : getAllExecutionBits())
109 if ((Execution & Bit) == Bit)
110 Result.push_back(Bit);
111 return Result;
114 } // namespace exegesis
115 } // namespace llvm