| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2017-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#include "Instruction.hpp" |
| 10 |
#include "../Frontend/Formatter.hpp" |
| 11 |
#include "../IR/Messages.hpp" |
| 12 |
#include "Checker/IRChecker.hpp" |
| 13 |
#include "Types.hpp" |
| 14 |
|
| 15 |
#include <sstream> |
| 16 |
|
| 17 |
using namespace iga; |
| 18 |
|
| 19 |
void Instruction::setSubfunction(Subfunction sf) { |
| 20 |
IGA_ASSERT(!getOpSpec().supportsSubfunction() || sf.isValid(), |
| 21 |
"instruction given invalid subfunction"); |
| 22 |
IGA_ASSERT(getOpSpec().supportsSubfunction() || !sf.isValid(), |
| 23 |
"instruction forbids subfunction"); |
| 24 |
m_sf = sf; |
| 25 |
} |
| 26 |
|
| 27 |
void Instruction::setDirectDestination(DstModifier dstMod, RegName rnm, |
| 28 |
RegRef reg, Region::Horz rgnH, |
| 29 |
Type type) { |
| 30 |
if (getOpSpec().isAnySendFormat() && m_sendDstLength < 0 && |
| 31 |
rnm == RegName::ARF_NULL) { |
| 32 |
m_sendDstLength = 0; |
| 33 |
} else if (getOpSpec().isSendgFormat()) { |
| 34 |
// try and infer the destination length since XE3 sendg |
| 35 |
// no longer explicitly encodes an rlen field in the descriptor |
| 36 |
auto di = tryDecode(*this, nullptr); |
| 37 |
if (di && di.info.dstLenBytes >= 0) { |
| 38 |
const int GRF_SIZE = 64; |
| 39 |
setDstLength(std::max<int>(di.info.dstLenBytes, GRF_SIZE)); |
| 40 |
} |
| 41 |
} |
| 42 |
m_dst.setDirectDestination(dstMod, rnm, reg, rgnH, type); |
| 43 |
} |
| 44 |
|
| 45 |
void Instruction::setMacroDestination(DstModifier dstMod, RegName rnm, |
| 46 |
RegRef reg, MathMacroExt mme, |
| 47 |
Region::Horz rgnHz, Type type) { |
| 48 |
m_dst.setMacroDestination(dstMod, rnm, reg, mme, rgnHz, type); |
| 49 |
} |
| 50 |
|
| 51 |
void Instruction::setIndirectDestination(DstModifier dstMod, RegRef reg, |
| 52 |
int16_t addrImmOff, Region::Horz rgnH, |
| 53 |
Type type) { |
| 54 |
m_dst.setIndirectDestination(dstMod, reg, addrImmOff, rgnH, type); |
| 55 |
} |
| 56 |
|
| 57 |
void Instruction::setDirectSource(SourceIndex srcIx, SrcModifier srcMod, |
| 58 |
RegName rnm, RegRef reg, Region rgn, |
| 59 |
Type type) { |
| 60 |
unsigned ix = static_cast<unsigned>(srcIx); |
| 61 |
if (getOpSpec().isAnySendFormat() && rnm == RegName::ARF_NULL) { |
| 62 |
// send with a null operand must have a 0 length |
| 63 |
// we only check this if we didn't get the length via the |
| 64 |
// descriptor |
| 65 |
if (ix == 0 && m_sendSrc0Length < 0) |
| 66 |
m_sendSrc0Length = 0; |
| 67 |
else if (ix == 1 && m_sendSrc1Length < 0) |
| 68 |
m_sendSrc1Length = 0; |
| 69 |
} |
| 70 |
m_srcs[ix].setDirectSource(srcMod, rnm, reg, rgn, type); |
| 71 |
} |
| 72 |
|
| 73 |
void Instruction::setMacroSource(SourceIndex srcIx, SrcModifier srcMod, |
| 74 |
RegName rName, RegRef reg, MathMacroExt acc, |
| 75 |
Region rgn, Type type) { |
| 76 |
unsigned ix = static_cast<unsigned>(srcIx); |
| 77 |
m_srcs[ix].setMacroSource(srcMod, rName, reg, acc, rgn, type); |
| 78 |
} |
| 79 |
|
| 80 |
void Instruction::setIndirectSource(SourceIndex srcIx, SrcModifier srcMod, |
| 81 |
RegName regName, RegRef reg, |
| 82 |
int16_t immediateOffset, Region rgn, |
| 83 |
Type type) { |
| 84 |
unsigned ix = static_cast<unsigned>(srcIx); |
| 85 |
m_srcs[ix].setIndirectSource(srcMod, regName, reg, immediateOffset, rgn, |
| 86 |
type); |
| 87 |
} |
| 88 |
|
| 89 |
void Instruction::setImmediateSource(SourceIndex srcIx, const ImmVal &val, |
| 90 |
Type type) { |
| 91 |
unsigned ix = static_cast<unsigned>(srcIx); |
| 92 |
m_srcs[ix].setImmediateSource(val, type); |
| 93 |
} |
| 94 |
|
| 95 |
void Instruction::setLabelSource(SourceIndex srcIx, int32_t pc, Type type) { |
| 96 |
unsigned ix = static_cast<unsigned>(srcIx); |
| 97 |
m_srcs[ix].setLabelSource(pc, type); |
| 98 |
} |
| 99 |
void Instruction::setLabelSource(SourceIndex srcIx, Block *block, Type type) { |
| 100 |
unsigned ix = static_cast<unsigned>(srcIx); |
| 101 |
m_srcs[ix].setLabelSource(block, type); |
| 102 |
} |
| 103 |
|
| 104 |
void Instruction::setSource(SourceIndex srcIx, const Operand &op) { |
| 105 |
unsigned ix = static_cast<unsigned>(srcIx); |
| 106 |
if (getOpSpec().isAnySendFormat() && op.getKind() == Operand::Kind::DIRECT && |
| 107 |
op.getDirRegName() == RegName::ARF_NULL) { |
| 108 |
// send with a null operand must have a 0 length |
| 109 |
// we only check this if we didn't get the length via the |
| 110 |
// descriptor |
| 111 |
if (ix == 0 && m_sendSrc0Length < 0) |
| 112 |
m_sendSrc0Length = 0; |
| 113 |
else if (ix == 1 && m_sendSrc1Length < 0) |
| 114 |
m_sendSrc1Length = 0; |
| 115 |
} |
| 116 |
m_srcs[ix] = op; |
| 117 |
} |
| 118 |
|
| 119 |
void Instruction::setExtImmOffDesc(uint32_t imm) { |
| 120 |
IGA_ASSERT(!getOpSpec().isSendgFormat(), "invalid field for format"); |
| 121 |
m_exImmOffDesc = imm; |
| 122 |
} |
| 123 |
void Instruction::setExtMsgDesc(const SendDesc &msg) { |
| 124 |
IGA_ASSERT(!getOpSpec().isSendgFormat(), "invalid field for format"); |
| 125 |
m_exDesc = msg; |
| 126 |
} |
| 127 |
void Instruction::setMsgDesc(const SendDesc &msg) { |
| 128 |
IGA_ASSERT(!getOpSpec().isSendgFormat(), "invalid field for format"); |
| 129 |
m_desc = msg; |
| 130 |
} |
| 131 |
void Instruction::setSendgDesc(uint64_t imm) { |
| 132 |
IGA_ASSERT(getOpSpec().isSendgFormat(), "invalid field for format"); |
| 133 |
m_sendgDesc = imm; |
| 134 |
} |
| 135 |
void Instruction::setSendgIndDesc0(RegRef rr) { |
| 136 |
IGA_ASSERT(getOpSpec().isSendgFormat(), "invalid field for format"); |
| 137 |
m_sendgIndDesc0Reg = rr; |
| 138 |
} |
| 139 |
void Instruction::setSendgIndDesc1(RegRef rr) { |
| 140 |
IGA_ASSERT(getOpSpec().isSendgFormat(), "invalid field for format"); |
| 141 |
m_sendgIndDesc1Reg = rr; |
| 142 |
} |
| 143 |
|
| 144 |
const Model &Instruction::model() const { |
| 145 |
return Model::LookupModelRef(platform()); |
| 146 |
} |
| 147 |
|
| 148 |
SWSB::InstType Instruction::getSWSBInstType(SWSB_ENCODE_MODE mode) const { |
| 149 |
if (mode == SWSB_ENCODE_MODE::SWSBInvalidMode) |
| 150 |
return SWSB::InstType::UNKNOWN; |
| 151 |
|
| 152 |
if (getOpSpec().isAnySendFormat()) |
| 153 |
return SWSB::InstType::SEND; |
| 154 |
|
| 155 |
if (is(Op::MATH)) |
| 156 |
return SWSB::InstType::MATH; |
| 157 |
|
| 158 |
// instruction with any of operand type is DF belongs to Math pipe |
| 159 |
if (mode == SWSB_ENCODE_MODE::ThreeDistPipeDPMath && isDF()) |
| 160 |
return SWSB::InstType::MATH; |
| 161 |
|
| 162 |
if (getOpSpec().isDpasFormat()) { |
| 163 |
return SWSB::InstType::DPAS; |
| 164 |
} |
| 165 |
|
| 166 |
return SWSB::InstType::OTHERS; |
| 167 |
} |
| 168 |
|
| 169 |
bool Instruction::isMacro() const { |
| 170 |
return is(Op::MADM) || (is(Op::MATH) && IsMacro(m_sf.math)); |
| 171 |
} |
| 172 |
|
| 173 |
bool Instruction::isDF() const { |
| 174 |
auto isDPType = [](Type ty) { return TypeIs64b(ty) && TypeIsFloating(ty); }; |
| 175 |
if (m_dst.getType() != Type::INVALID) |
| 176 |
if (isDPType(m_dst.getType())) |
| 177 |
return true; |
| 178 |
for (size_t i = 0; i < getSourceCount(); ++i) { |
| 179 |
if (m_srcs[i].getType() != Type::INVALID) { |
| 180 |
if (isDPType(m_srcs[i].getType())) |
| 181 |
return true; |
| 182 |
} |
| 183 |
} |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
bool Instruction::isMovWithLabel() const { |
| 188 |
return (getOp() == Op::MOV && getSource(0).getKind() == Operand::Kind::LABEL); |
| 189 |
} |
| 190 |
|
| 191 |
bool Instruction::isGatherSend() const { |
| 192 |
return getOpSpec().isAnySendFormat() && |
| 193 |
getSource(0).getDirRegName() == RegName::ARF_S; |
| 194 |
} |
| 195 |
|
| 196 |
void Instruction::setInlineBinary(const Instruction::InlineBinaryType &binary) { |
| 197 |
|
| 198 |
m_inlineBinary = binary; |
| 199 |
m_isInlineBinaryInst = true; |
| 200 |
} |
| 201 |
|
| 202 |
void Instruction::validate() const { iga::SanityCheckIR(*this); } |
| 203 |
|
| 204 |
std::string Instruction::str() const { |
| 205 |
ErrorHandler eh; |
| 206 |
std::stringstream ss; |
| 207 |
FormatOpts fopt(model()); |
| 208 |
fopt.setSWSBEncodingMode( |
| 209 |
Model::LookupModelRef(getOpSpec().platform).getSWSBEncodeMode()); |
| 210 |
FormatInstruction(eh, ss, fopt, *this); |
| 211 |
return ss.str(); |
| 212 |
} |
| 213 |
|
| 214 |
static unsigned getSourceCountBrc(const Instruction &i) { |
| 215 |
// brc (..) IMM IMM |
| 216 |
// brc (..) REG |
| 217 |
switch (i.getSource(0).getKind()) { |
| 218 |
case Operand::Kind::DIRECT: |
| 219 |
case Operand::Kind::INDIRECT: |
| 220 |
return 1; |
| 221 |
default: |
| 222 |
return 2; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
unsigned Instruction::getSourceCount() const { |
| 227 |
// BRC can have 1 or 2 operands, everyone else is simple |
| 228 |
if (is(Op::BRC)) { |
| 229 |
return getSourceCountBrc(*this); |
| 230 |
} else { |
| 231 |
return getOpSpec().getSourceCount(getSubfunction()); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
uint32_t Instruction::getExtImmOffDescriptor() const { |
| 236 |
IGA_ASSERT(!getOpSpec().isSendgFormat(), "invalid field for format"); |
| 237 |
return m_exImmOffDesc; |
| 238 |
} |
| 239 |
|
| 240 |
SendDesc Instruction::getExtMsgDescriptor() const { |
| 241 |
IGA_ASSERT(!getOpSpec().isSendgFormat(), "invalid field for format"); |
| 242 |
return m_exDesc; |
| 243 |
} |
| 244 |
SendDesc Instruction::getMsgDescriptor() const { |
| 245 |
IGA_ASSERT(!getOpSpec().isSendgFormat(), "invalid field for format"); |
| 246 |
return m_desc; |
| 247 |
} |
| 248 |
|
| 249 |
uint64_t Instruction::getSendgDesc() const { |
| 250 |
IGA_ASSERT(getOpSpec().isSendgFormat(), "invalid field for format"); |
| 251 |
return m_sendgDesc; |
| 252 |
} |
| 253 |
RegRef Instruction::getSendgIndDesc0Reg() const { |
| 254 |
IGA_ASSERT(getOpSpec().isSendgFormat(), "invalid field for format"); |
| 255 |
return m_sendgIndDesc0Reg; |
| 256 |
} |
| 257 |
RegRef Instruction::getSendgIndDesc1Reg() const { |
| 258 |
IGA_ASSERT(getOpSpec().isSendgFormat(), "invalid field for format"); |
| 259 |
return m_sendgIndDesc1Reg; |
| 260 |
} |