17f8d310bSGuillaume Chatelet //===-- CodeTemplate.cpp ----------------------------------------*- C++ -*-===//
27f8d310bSGuillaume Chatelet //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67f8d310bSGuillaume Chatelet //
77f8d310bSGuillaume Chatelet //===----------------------------------------------------------------------===//
87f8d310bSGuillaume Chatelet
97f8d310bSGuillaume Chatelet #include "CodeTemplate.h"
107f8d310bSGuillaume Chatelet
1132401afdSFangrui Song namespace llvm {
127f8d310bSGuillaume Chatelet namespace exegesis {
137f8d310bSGuillaume Chatelet
142ffe225dSRoman Lebedev CodeTemplate::CodeTemplate(const CodeTemplate &) = default;
152ffe225dSRoman Lebedev
167f8d310bSGuillaume Chatelet CodeTemplate::CodeTemplate(CodeTemplate &&) = default;
177f8d310bSGuillaume Chatelet
187f8d310bSGuillaume Chatelet CodeTemplate &CodeTemplate::operator=(CodeTemplate &&) = default;
197f8d310bSGuillaume Chatelet
202ffe225dSRoman Lebedev CodeTemplate &CodeTemplate::operator=(const CodeTemplate &) = default;
212ffe225dSRoman Lebedev
clone() const222ffe225dSRoman Lebedev CodeTemplate CodeTemplate::clone() const {
232ffe225dSRoman Lebedev CodeTemplate CT = *this;
242ffe225dSRoman Lebedev return CT;
252ffe225dSRoman Lebedev }
262ffe225dSRoman Lebedev
InstructionTemplate(const Instruction * Instr)2732d384c0SGuillaume Chatelet InstructionTemplate::InstructionTemplate(const Instruction *Instr)
2832d384c0SGuillaume Chatelet : Instr(Instr), VariableValues(Instr->Variables.size()) {}
2970ac019eSGuillaume Chatelet
3070ac019eSGuillaume Chatelet InstructionTemplate::InstructionTemplate(InstructionTemplate &&) = default;
3170ac019eSGuillaume Chatelet
3270ac019eSGuillaume Chatelet InstructionTemplate &InstructionTemplate::
3370ac019eSGuillaume Chatelet operator=(InstructionTemplate &&) = default;
3470ac019eSGuillaume Chatelet
3570ac019eSGuillaume Chatelet InstructionTemplate::InstructionTemplate(const InstructionTemplate &) = default;
3670ac019eSGuillaume Chatelet
3770ac019eSGuillaume Chatelet InstructionTemplate &InstructionTemplate::
3870ac019eSGuillaume Chatelet operator=(const InstructionTemplate &) = default;
3970ac019eSGuillaume Chatelet
getOpcode() const4070ac019eSGuillaume Chatelet unsigned InstructionTemplate::getOpcode() const {
4132d384c0SGuillaume Chatelet return Instr->Description.getOpcode();
4270ac019eSGuillaume Chatelet }
4370ac019eSGuillaume Chatelet
getValueFor(const Variable & Var)4450cdd56bSClement Courbet MCOperand &InstructionTemplate::getValueFor(const Variable &Var) {
4509c2839cSGuillaume Chatelet return VariableValues[Var.getIndex()];
4670ac019eSGuillaume Chatelet }
4770ac019eSGuillaume Chatelet
getValueFor(const Variable & Var) const4850cdd56bSClement Courbet const MCOperand &InstructionTemplate::getValueFor(const Variable &Var) const {
4909c2839cSGuillaume Chatelet return VariableValues[Var.getIndex()];
5070ac019eSGuillaume Chatelet }
5170ac019eSGuillaume Chatelet
getValueFor(const Operand & Op)5250cdd56bSClement Courbet MCOperand &InstructionTemplate::getValueFor(const Operand &Op) {
5332d384c0SGuillaume Chatelet return getValueFor(Instr->Variables[Op.getVariableIndex()]);
5470ac019eSGuillaume Chatelet }
5570ac019eSGuillaume Chatelet
getValueFor(const Operand & Op) const5650cdd56bSClement Courbet const MCOperand &InstructionTemplate::getValueFor(const Operand &Op) const {
5732d384c0SGuillaume Chatelet return getValueFor(Instr->Variables[Op.getVariableIndex()]);
5870ac019eSGuillaume Chatelet }
5970ac019eSGuillaume Chatelet
hasImmediateVariables() const6070ac019eSGuillaume Chatelet bool InstructionTemplate::hasImmediateVariables() const {
6132d384c0SGuillaume Chatelet return any_of(Instr->Variables, [this](const Variable &Var) {
6232d384c0SGuillaume Chatelet return Instr->getPrimaryOperand(Var).isImmediate();
6370ac019eSGuillaume Chatelet });
6470ac019eSGuillaume Chatelet }
6570ac019eSGuillaume Chatelet
build() const6650cdd56bSClement Courbet MCInst InstructionTemplate::build() const {
6750cdd56bSClement Courbet MCInst Result;
6832d384c0SGuillaume Chatelet Result.setOpcode(Instr->Description.Opcode);
6932d384c0SGuillaume Chatelet for (const auto &Op : Instr->Operands)
7009c2839cSGuillaume Chatelet if (Op.isExplicit())
7170ac019eSGuillaume Chatelet Result.addOperand(getValueFor(Op));
7270ac019eSGuillaume Chatelet return Result;
7370ac019eSGuillaume Chatelet }
7470ac019eSGuillaume Chatelet
isEnumValue(ExecutionMode Execution)75fcbb6f3cSGuillaume Chatelet bool isEnumValue(ExecutionMode Execution) {
7650cdd56bSClement Courbet return isPowerOf2_32(static_cast<uint32_t>(Execution));
77fcbb6f3cSGuillaume Chatelet }
78fcbb6f3cSGuillaume Chatelet
getName(ExecutionMode Bit)7950cdd56bSClement Courbet StringRef getName(ExecutionMode Bit) {
80fcbb6f3cSGuillaume Chatelet assert(isEnumValue(Bit) && "Bit must be a power of two");
81fcbb6f3cSGuillaume Chatelet switch (Bit) {
82fcbb6f3cSGuillaume Chatelet case ExecutionMode::UNKNOWN:
83fcbb6f3cSGuillaume Chatelet return "UNKNOWN";
84fcbb6f3cSGuillaume Chatelet case ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS:
85fcbb6f3cSGuillaume Chatelet return "ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS";
86fcbb6f3cSGuillaume Chatelet case ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS:
87fcbb6f3cSGuillaume Chatelet return "ALWAYS_SERIAL_TIED_REGS_ALIAS";
88fcbb6f3cSGuillaume Chatelet case ExecutionMode::SERIAL_VIA_MEMORY_INSTR:
89fcbb6f3cSGuillaume Chatelet return "SERIAL_VIA_MEMORY_INSTR";
90fcbb6f3cSGuillaume Chatelet case ExecutionMode::SERIAL_VIA_EXPLICIT_REGS:
91fcbb6f3cSGuillaume Chatelet return "SERIAL_VIA_EXPLICIT_REGS";
92fcbb6f3cSGuillaume Chatelet case ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR:
93fcbb6f3cSGuillaume Chatelet return "SERIAL_VIA_NON_MEMORY_INSTR";
94fcbb6f3cSGuillaume Chatelet case ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF:
95fcbb6f3cSGuillaume Chatelet return "ALWAYS_PARALLEL_MISSING_USE_OR_DEF";
96fcbb6f3cSGuillaume Chatelet case ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS:
97fcbb6f3cSGuillaume Chatelet return "PARALLEL_VIA_EXPLICIT_REGS";
98fcbb6f3cSGuillaume Chatelet }
99fcbb6f3cSGuillaume Chatelet llvm_unreachable("Missing enum case");
100fcbb6f3cSGuillaume Chatelet }
101fcbb6f3cSGuillaume Chatelet
getAllExecutionBits()10250cdd56bSClement Courbet ArrayRef<ExecutionMode> getAllExecutionBits() {
103fcbb6f3cSGuillaume Chatelet static const ExecutionMode kAllExecutionModeBits[] = {
104fcbb6f3cSGuillaume Chatelet ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS,
105fcbb6f3cSGuillaume Chatelet ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS,
106fcbb6f3cSGuillaume Chatelet ExecutionMode::SERIAL_VIA_MEMORY_INSTR,
107fcbb6f3cSGuillaume Chatelet ExecutionMode::SERIAL_VIA_EXPLICIT_REGS,
108fcbb6f3cSGuillaume Chatelet ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR,
109fcbb6f3cSGuillaume Chatelet ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF,
110fcbb6f3cSGuillaume Chatelet ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS,
111fcbb6f3cSGuillaume Chatelet };
112*38818b60Sserge-sans-paille return ArrayRef(kAllExecutionModeBits);
113fcbb6f3cSGuillaume Chatelet }
114fcbb6f3cSGuillaume Chatelet
getExecutionModeBits(ExecutionMode Execution)11550cdd56bSClement Courbet SmallVector<ExecutionMode, 4> getExecutionModeBits(ExecutionMode Execution) {
11650cdd56bSClement Courbet SmallVector<ExecutionMode, 4> Result;
117fcbb6f3cSGuillaume Chatelet for (const auto Bit : getAllExecutionBits())
118fcbb6f3cSGuillaume Chatelet if ((Execution & Bit) == Bit)
119fcbb6f3cSGuillaume Chatelet Result.push_back(Bit);
120fcbb6f3cSGuillaume Chatelet return Result;
121fcbb6f3cSGuillaume Chatelet }
122fcbb6f3cSGuillaume Chatelet
1237f8d310bSGuillaume Chatelet } // namespace exegesis
12432401afdSFangrui Song } // namespace llvm
125