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 
147f8d310bSGuillaume Chatelet CodeTemplate::CodeTemplate(CodeTemplate &&) = default;
157f8d310bSGuillaume Chatelet 
167f8d310bSGuillaume Chatelet CodeTemplate &CodeTemplate::operator=(CodeTemplate &&) = default;
177f8d310bSGuillaume Chatelet 
InstructionTemplate(const Instruction * Instr)18*32d384c0SGuillaume Chatelet InstructionTemplate::InstructionTemplate(const Instruction *Instr)
19*32d384c0SGuillaume Chatelet     : Instr(Instr), VariableValues(Instr->Variables.size()) {}
2070ac019eSGuillaume Chatelet 
2170ac019eSGuillaume Chatelet InstructionTemplate::InstructionTemplate(InstructionTemplate &&) = default;
2270ac019eSGuillaume Chatelet 
2370ac019eSGuillaume Chatelet InstructionTemplate &InstructionTemplate::
2470ac019eSGuillaume Chatelet operator=(InstructionTemplate &&) = default;
2570ac019eSGuillaume Chatelet 
2670ac019eSGuillaume Chatelet InstructionTemplate::InstructionTemplate(const InstructionTemplate &) = default;
2770ac019eSGuillaume Chatelet 
2870ac019eSGuillaume Chatelet InstructionTemplate &InstructionTemplate::
2970ac019eSGuillaume Chatelet operator=(const InstructionTemplate &) = default;
3070ac019eSGuillaume Chatelet 
getOpcode() const3170ac019eSGuillaume Chatelet unsigned InstructionTemplate::getOpcode() const {
32*32d384c0SGuillaume Chatelet   return Instr->Description.getOpcode();
3370ac019eSGuillaume Chatelet }
3470ac019eSGuillaume Chatelet 
getValueFor(const Variable & Var)3550cdd56bSClement Courbet MCOperand &InstructionTemplate::getValueFor(const Variable &Var) {
3609c2839cSGuillaume Chatelet   return VariableValues[Var.getIndex()];
3770ac019eSGuillaume Chatelet }
3870ac019eSGuillaume Chatelet 
getValueFor(const Variable & Var) const3950cdd56bSClement Courbet const MCOperand &InstructionTemplate::getValueFor(const Variable &Var) const {
4009c2839cSGuillaume Chatelet   return VariableValues[Var.getIndex()];
4170ac019eSGuillaume Chatelet }
4270ac019eSGuillaume Chatelet 
getValueFor(const Operand & Op)4350cdd56bSClement Courbet MCOperand &InstructionTemplate::getValueFor(const Operand &Op) {
44*32d384c0SGuillaume Chatelet   return getValueFor(Instr->Variables[Op.getVariableIndex()]);
4570ac019eSGuillaume Chatelet }
4670ac019eSGuillaume Chatelet 
getValueFor(const Operand & Op) const4750cdd56bSClement Courbet const MCOperand &InstructionTemplate::getValueFor(const Operand &Op) const {
48*32d384c0SGuillaume Chatelet   return getValueFor(Instr->Variables[Op.getVariableIndex()]);
4970ac019eSGuillaume Chatelet }
5070ac019eSGuillaume Chatelet 
hasImmediateVariables() const5170ac019eSGuillaume Chatelet bool InstructionTemplate::hasImmediateVariables() const {
52*32d384c0SGuillaume Chatelet   return any_of(Instr->Variables, [this](const Variable &Var) {
53*32d384c0SGuillaume Chatelet     return Instr->getPrimaryOperand(Var).isImmediate();
5470ac019eSGuillaume Chatelet   });
5570ac019eSGuillaume Chatelet }
5670ac019eSGuillaume Chatelet 
build() const5750cdd56bSClement Courbet MCInst InstructionTemplate::build() const {
5850cdd56bSClement Courbet   MCInst Result;
59*32d384c0SGuillaume Chatelet   Result.setOpcode(Instr->Description.Opcode);
60*32d384c0SGuillaume Chatelet   for (const auto &Op : Instr->Operands)
6109c2839cSGuillaume Chatelet     if (Op.isExplicit())
6270ac019eSGuillaume Chatelet       Result.addOperand(getValueFor(Op));
6370ac019eSGuillaume Chatelet   return Result;
6470ac019eSGuillaume Chatelet }
6570ac019eSGuillaume Chatelet 
isEnumValue(ExecutionMode Execution)66fcbb6f3cSGuillaume Chatelet bool isEnumValue(ExecutionMode Execution) {
6750cdd56bSClement Courbet   return isPowerOf2_32(static_cast<uint32_t>(Execution));
68fcbb6f3cSGuillaume Chatelet }
69fcbb6f3cSGuillaume Chatelet 
getName(ExecutionMode Bit)7050cdd56bSClement Courbet StringRef getName(ExecutionMode Bit) {
71fcbb6f3cSGuillaume Chatelet   assert(isEnumValue(Bit) && "Bit must be a power of two");
72fcbb6f3cSGuillaume Chatelet   switch (Bit) {
73fcbb6f3cSGuillaume Chatelet   case ExecutionMode::UNKNOWN:
74fcbb6f3cSGuillaume Chatelet     return "UNKNOWN";
75fcbb6f3cSGuillaume Chatelet   case ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS:
76fcbb6f3cSGuillaume Chatelet     return "ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS";
77fcbb6f3cSGuillaume Chatelet   case ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS:
78fcbb6f3cSGuillaume Chatelet     return "ALWAYS_SERIAL_TIED_REGS_ALIAS";
79fcbb6f3cSGuillaume Chatelet   case ExecutionMode::SERIAL_VIA_MEMORY_INSTR:
80fcbb6f3cSGuillaume Chatelet     return "SERIAL_VIA_MEMORY_INSTR";
81fcbb6f3cSGuillaume Chatelet   case ExecutionMode::SERIAL_VIA_EXPLICIT_REGS:
82fcbb6f3cSGuillaume Chatelet     return "SERIAL_VIA_EXPLICIT_REGS";
83fcbb6f3cSGuillaume Chatelet   case ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR:
84fcbb6f3cSGuillaume Chatelet     return "SERIAL_VIA_NON_MEMORY_INSTR";
85fcbb6f3cSGuillaume Chatelet   case ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF:
86fcbb6f3cSGuillaume Chatelet     return "ALWAYS_PARALLEL_MISSING_USE_OR_DEF";
87fcbb6f3cSGuillaume Chatelet   case ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS:
88fcbb6f3cSGuillaume Chatelet     return "PARALLEL_VIA_EXPLICIT_REGS";
89fcbb6f3cSGuillaume Chatelet   }
90fcbb6f3cSGuillaume Chatelet   llvm_unreachable("Missing enum case");
91fcbb6f3cSGuillaume Chatelet }
92fcbb6f3cSGuillaume Chatelet 
getAllExecutionBits()9350cdd56bSClement Courbet ArrayRef<ExecutionMode> getAllExecutionBits() {
94fcbb6f3cSGuillaume Chatelet   static const ExecutionMode kAllExecutionModeBits[] = {
95fcbb6f3cSGuillaume Chatelet       ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS,
96fcbb6f3cSGuillaume Chatelet       ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS,
97fcbb6f3cSGuillaume Chatelet       ExecutionMode::SERIAL_VIA_MEMORY_INSTR,
98fcbb6f3cSGuillaume Chatelet       ExecutionMode::SERIAL_VIA_EXPLICIT_REGS,
99fcbb6f3cSGuillaume Chatelet       ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR,
100fcbb6f3cSGuillaume Chatelet       ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF,
101fcbb6f3cSGuillaume Chatelet       ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS,
102fcbb6f3cSGuillaume Chatelet   };
10350cdd56bSClement Courbet   return makeArrayRef(kAllExecutionModeBits);
104fcbb6f3cSGuillaume Chatelet }
105fcbb6f3cSGuillaume Chatelet 
getExecutionModeBits(ExecutionMode Execution)10650cdd56bSClement Courbet SmallVector<ExecutionMode, 4> getExecutionModeBits(ExecutionMode Execution) {
10750cdd56bSClement Courbet   SmallVector<ExecutionMode, 4> Result;
108fcbb6f3cSGuillaume Chatelet   for (const auto Bit : getAllExecutionBits())
109fcbb6f3cSGuillaume Chatelet     if ((Execution & Bit) == Bit)
110fcbb6f3cSGuillaume Chatelet       Result.push_back(Bit);
111fcbb6f3cSGuillaume Chatelet   return Result;
112fcbb6f3cSGuillaume Chatelet }
113fcbb6f3cSGuillaume Chatelet 
1147f8d310bSGuillaume Chatelet } // namespace exegesis
11532401afdSFangrui Song } // namespace llvm
116