| 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 |
#ifndef _IGA_OPERAND_HPP_ |
| 10 |
#define _IGA_OPERAND_HPP_ |
| 11 |
|
| 12 |
#include "ImmVal.hpp" |
| 13 |
#include "Types.hpp" |
| 14 |
|
| 15 |
namespace iga { |
| 16 |
class Block; |
| 17 |
|
| 18 |
class Operand { |
| 19 |
public: |
| 20 |
enum class Kind { |
| 21 |
INVALID, // an invalid or uninitialized operand |
| 22 |
DIRECT, // direct register reference |
| 23 |
MACRO, // madm or math.invm or math.rsqrtm |
| 24 |
INDIRECT, // register-indriect access |
| 25 |
IMMEDIATE, // immediate value |
| 26 |
LABEL, // block target (can be numeric label/m_immVal) |
| 27 |
}; |
| 28 |
|
| 29 |
Operand() |
| 30 |
: m_regOpSrcMod(SrcModifier::NONE) {} |
| 31 |
|
| 32 |
// direct destination constructor (for constants etc) |
| 33 |
Operand(DstModifier dstMod, RegName rType, const RegRef ®, |
| 34 |
const Region::Horz &rgnHz, Type type) { |
| 35 |
setDirectDestination(dstMod, rType, reg, rgnHz, type); |
| 36 |
} |
| 37 |
|
| 38 |
// direct source constructor (for constants etc) |
| 39 |
Operand(SrcModifier srcMod, RegName rType, const RegRef ®, |
| 40 |
const Region &rgn, Type type) { |
| 41 |
setDirectSource(srcMod, rType, reg, rgn, type); |
| 42 |
} |
| 43 |
|
| 44 |
// generally you want a reference to an operand, not a copy |
| 45 |
// this prevents: |
| 46 |
// auto o = i->getSource(...); // copies all state |
| 47 |
// you usually want |
| 48 |
// const auto &o = ... |
| 49 |
// but in rare cases maybe you want a copy |
| 50 |
// Operand copy(i->getSource(...)); |
| 51 |
explicit Operand(const Operand &) = default; |
| 52 |
|
| 53 |
// describes if the operand is direct (Operand::Kind::DIRECT), |
| 54 |
// indirect (Operand::Kind::INDIRECT) or immediate |
| 55 |
// (Operand::Kind::IMMEDIATE) |
| 56 |
Kind getKind() const { return m_kind; } |
| 57 |
|
| 58 |
// both labels and true immediates are considered immediates |
| 59 |
// for encoding and decoding sake |
| 60 |
bool isImm() const { |
| 61 |
switch (getKind()) { |
| 62 |
case Operand::Kind::IMMEDIATE: |
| 63 |
case Operand::Kind::LABEL: |
| 64 |
return true; |
| 65 |
default: |
| 66 |
return false; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/////////////////////////////////////////////////////////////////////////// |
| 71 |
// derived accessors |
| 72 |
|
| 73 |
// returns true if a direct register reference to the null register |
| 74 |
bool isNull() const { |
| 75 |
return getKind() == Kind::DIRECT && getDirRegName() == RegName::ARF_NULL; |
| 76 |
} |
| 77 |
|
| 78 |
/////////////////////////////////////////////////////////////////////////// |
| 79 |
// other accessors |
| 80 |
DstModifier getDstModifier() const { return m_regOpDstMod; } |
| 81 |
SrcModifier getSrcModifier() const { return m_regOpSrcMod; } |
| 82 |
|
| 83 |
// Applies to Operand::Kind::DIRECT |
| 84 |
RegName getDirRegName() const { return m_regOpName; } |
| 85 |
const RegRef &getDirRegRef() const { return m_regOpReg; } |
| 86 |
|
| 87 |
// Applies to Operand::Kind::INDIRECT only |
| 88 |
const RegRef &getIndAddrReg() const { return m_regOpReg; } |
| 89 |
int16_t getIndImmAddr() const { return m_regOpIndOff; } |
| 90 |
|
| 91 |
// Applies to Operand::Kind::DIRECT and Operand::Kind::INDIRECT |
| 92 |
Region getRegion() const { return m_regOpRgn; } |
| 93 |
MathMacroExt getMathMacroExt() const { return m_regMathMacro; } |
| 94 |
// Defined if the value is immediate |
| 95 |
const ImmVal getImmediateValue() const { return m_immValue; } |
| 96 |
// if this operand corresponds to a label, this is the target block |
| 97 |
// nullptr if we are using numeric labels |
| 98 |
const Block *getTargetBlock() const { return m_lblBlock; } |
| 99 |
|
| 100 |
// the operand type (as in :f, :d, ...) |
| 101 |
Type getType() const { return m_type; } |
| 102 |
|
| 103 |
// re-initializes this operand as an direct destination register operand |
| 104 |
void setDirectDestination(DstModifier dstMod, RegName rName, |
| 105 |
const RegRef ®, const Region::Horz &rgnHz, |
| 106 |
Type type); |
| 107 |
// re-initializes this operand as a destination register operand using |
| 108 |
// a math macro register access |
| 109 |
void setMacroDestination(DstModifier dstMod, RegName rName, const RegRef ®, |
| 110 |
MathMacroExt acc, Type type) { |
| 111 |
setMacroDestination(dstMod, rName, reg, acc, Region::Horz::HZ_1, type); |
| 112 |
} |
| 113 |
void setMacroDestination(DstModifier dstMod, RegName rName, const RegRef ®, |
| 114 |
MathMacroExt acc, Region::Horz rgnHz, Type type); |
| 115 |
// re-initializes this operand as an indirect destination register operand |
| 116 |
void setIndirectDestination(DstModifier dstMod, const RegRef ®, |
| 117 |
int16_t immediateOffset, |
| 118 |
const Region::Horz &rgnHz, Type type); |
| 119 |
|
| 120 |
// re-initializes this operand as an immeidate value with a given type |
| 121 |
void setImmediateSource(const ImmVal &val, Type type); |
| 122 |
// re-initializes this operand as a direct source register |
| 123 |
void setDirectSource(SrcModifier srcMod, RegName rName, const RegRef ®, |
| 124 |
const Region &rgn, Type type); |
| 125 |
// re-initializes this operand as a source register operand using |
| 126 |
// an math macro register access |
| 127 |
void setMacroSource(SrcModifier srcMod, RegName rName, const RegRef ®, |
| 128 |
Region /* rgn */, MathMacroExt mme, Type type) { |
| 129 |
setMacroSource(srcMod, rName, reg, mme, Region::SRC110, type); |
| 130 |
} |
| 131 |
void setMacroSource(SrcModifier srcMod, RegName rName, const RegRef ®, |
| 132 |
MathMacroExt mme, Region rgn, Type type); |
| 133 |
|
| 134 |
// re-initializes this operand as an indirect register operand |
| 135 |
void setIndirectSource(SrcModifier srcMod, RegName regName, |
| 136 |
const RegRef ®, int16_t addrImmOff, |
| 137 |
const Region &rgn, Type type); |
| 138 |
// re-initializes this operand as an immediate branch target |
| 139 |
void setLabelSource(Block *blk, Type type); |
| 140 |
void setLabelSource(int32_t jipOrUip, Type type); |
| 141 |
// set sthe operand region |
| 142 |
void setRegion(const Region &rgn) { m_regOpRgn = rgn; } |
| 143 |
// sets the operand type |
| 144 |
void setType(Type type) { m_type = type; } |
| 145 |
// set the operand reg RegRef directly |
| 146 |
void setRegRef(const RegRef ®Ref) { |
| 147 |
m_regOpReg.regNum = regRef.regNum; |
| 148 |
m_regOpReg.subRegNum = regRef.subRegNum; |
| 149 |
} |
| 150 |
|
| 151 |
private: |
| 152 |
Operand::Kind m_kind = Kind::INVALID; |
| 153 |
|
| 154 |
// direct/indirect register information |
| 155 |
union { |
| 156 |
SrcModifier m_regOpSrcMod; |
| 157 |
DstModifier m_regOpDstMod; |
| 158 |
}; |
| 159 |
RegName m_regOpName = RegName::INVALID; // exists for ind. as well |
| 160 |
MathMacroExt m_regMathMacro = MathMacroExt::INVALID; |
| 161 |
Region m_regOpRgn = Region::INVALID; |
| 162 |
|
| 163 |
// direct/indirect register |
| 164 |
RegRef m_regOpReg; |
| 165 |
|
| 166 |
// indirect register offset |
| 167 |
int16_t m_regOpIndOff = 0; |
| 168 |
|
| 169 |
// Imm field information |
| 170 |
ImmVal m_immValue; |
| 171 |
// for resolved labels |
| 172 |
// the literal value (for immediate data) |
| 173 |
// also set before labels are resolved (label is .s32) |
| 174 |
// for numeric labels the value is normalized as bytes from |
| 175 |
// the pre-increment PC of the instruction. Hence, |
| 176 |
// * Pre-BDW branches which use units of QWORDS in the |
| 177 |
// encoding will be converted to bytes. |
| 178 |
// * JMPI uses the pos-increment PC; we normalize that here |
| 179 |
// as well to a pre-increment value. |
| 180 |
Block *m_lblBlock = nullptr; |
| 181 |
|
| 182 |
// the operand type (e.g. :d, :f, etc...) |
| 183 |
Type m_type = Type::INVALID; |
| 184 |
|
| 185 |
public: |
| 186 |
// useful constants (reusable operands to streamline codegen) |
| 187 |
static const Operand DST_REG_IP_D; // brd/brc use this |
| 188 |
static const Operand SRC_REG_IP_D; |
| 189 |
static const Operand DST_REG_IP_UD; // jmpi Dst and Src0 |
| 190 |
static const Operand SRC_REG_IP_UD; |
| 191 |
static const Operand DST_REG_NULL_UD; // e.g. while.Dst |
| 192 |
static const Operand SRC_REG_NULL_UD; |
| 193 |
static const Operand SRC_REG_NULL_UB; |
| 194 |
}; // class Operand |
| 195 |
} // namespace iga |
| 196 |
#endif // _IGA_OPERAND_HPP_ |