Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'w' and 's' bit to xarch instruction flags. #61198

Merged
merged 6 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions src/coreclr/jit/emitxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,45 @@ bool emitter::IsDstSrcSrcAVXInstruction(instruction ins)
return ((CodeGenInterface::instInfo[ins] & INS_Flags_IsDstSrcSrcAVXInstruction) != 0) && IsAVXInstruction(ins);
}

//------------------------------------------------------------------------
// HasRegularWideForm: Many x86/x64 instructions follow a regular encoding scheme where the
// byte-sized version of an instruction has the lowest bit of the opcode cleared
// while the 32-bit version of the instruction (taking potential prefixes to
// override operand size) has the lowest bit set. This function returns true if
// the instruction follows this format.
//
// Note that this bit is called `w` in the encoding table in Section B.2 of
// Volume 2 of the Intel Architecture Software Developer Manual.
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if instruction has a regular form where the 'w' bit needs to be set.
bool emitter::HasRegularWideForm(instruction ins)
{
return ((CodeGenInterface::instInfo[ins] & INS_FLAGS_Has_Wbit) != 0);
}

//------------------------------------------------------------------------
// HasRegularWideImmediateForm: As above in HasRegularWideForm, many instructions taking
// immediates have a regular form used to encode whether the instruction takes a sign-extended
// 1-byte immediate or a (in 64-bit sign-extended) 4-byte immediate, by respectively setting and
// clearing the second lowest bit.
//
// Note that this bit is called `s` in the encoding table in Section B.2 of
// Volume 2 of the Intel Architecture Software Developer Manual.
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if instruction has a regular wide immediate form where the 's' bit needs to set.
bool emitter::HasRegularWideImmediateForm(instruction ins)
{
return ((CodeGenInterface::instInfo[ins] & INS_FLAGS_Has_Sbit) != 0);
}

//------------------------------------------------------------------------
// DoesWriteZeroFlag: check if the instruction write the
// ZF flag.
Expand Down Expand Up @@ -10336,10 +10375,9 @@ BYTE* emitter::emitOutputAM(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)

// Use the large version if this is not a byte. This trick will not
// work in case of SSE2 and AVX instructions.
if ((size != EA_1BYTE) && (ins != INS_imul) && (ins != INS_bsf) && (ins != INS_bsr) && !IsSSEInstruction(ins) &&
!IsAVXInstruction(ins))
if ((size != EA_1BYTE) && HasRegularWideForm(ins))
{
code++;
code |= 0x1;
}
}
else if (CodeGen::instIsFP(ins))
Expand Down Expand Up @@ -11103,9 +11141,7 @@ BYTE* emitter::emitOutputSV(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)
}

// Use the large version if this is not a byte
// TODO-XArch-Cleanup Can the need for the 'w' size bit be encoded in the instruction flags?
if ((size != EA_1BYTE) && (ins != INS_imul) && (ins != INS_bsf) && (ins != INS_bsr) && (!insIsCMOV(ins)) &&
!IsSSEInstruction(ins) && !IsAVXInstruction(ins))
if ((size != EA_1BYTE) && HasRegularWideForm(ins))
{
code |= 0x1;
}
Expand Down Expand Up @@ -11568,12 +11604,9 @@ BYTE* emitter::emitOutputCV(BYTE* dst, instrDesc* id, code_t code, CnsVal* addc)
code &= 0x0000FFFF;
}

if ((ins == INS_movsx || ins == INS_movzx || ins == INS_cmpxchg || ins == INS_xchg || ins == INS_xadd ||
insIsCMOV(ins)) &&
size != EA_1BYTE)
if (size != EA_1BYTE && HasRegularWideForm(ins))
{
// movsx and movzx are 'big' opcodes but also have the 'w' bit
code++;
code |= 0x1;
}
}
else if (CodeGen::instIsFP(ins))
Expand Down Expand Up @@ -12740,7 +12773,7 @@ BYTE* emitter::emitOutputRI(BYTE* dst, instrDesc* id)
}

// "test" has no 's' bit
if (ins == INS_test)
if (!HasRegularWideImmediateForm(ins))
{
useSigned = false;
}
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/emitxarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ static bool IsAVXOnlyInstruction(instruction ins);
static bool IsFMAInstruction(instruction ins);
static bool IsAVXVNNIInstruction(instruction ins);
static bool IsBMIInstruction(instruction ins);

static regNumber getBmiRegNumber(instruction ins);
static regNumber getSseShiftRegNumber(instruction ins);
bool IsAVXInstruction(instruction ins) const;
Expand Down Expand Up @@ -190,6 +191,8 @@ void SetContains256bitAVX(bool value)

bool IsDstDstSrcAVXInstruction(instruction ins);
bool IsDstSrcSrcAVXInstruction(instruction ins);
bool HasRegularWideForm(instruction ins);
bool HasRegularWideImmediateForm(instruction ins);
bool DoesWriteZeroFlag(instruction ins);
bool DoesWriteSignFlag(instruction ins);
bool DoesResetOverflowAndCarryFlags(instruction ins);
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/jit/instr.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ enum insFlags : uint32_t
// Avx
INS_Flags_IsDstDstSrcAVXInstruction = 1 << 25,
INS_Flags_IsDstSrcSrcAVXInstruction = 1 << 26,

// w and s bits
INS_FLAGS_Has_Wbit = 1 << 27,
INS_FLAGS_Has_Sbit = 1 << 28,

// TODO-Cleanup: Remove this flag and its usage from TARGET_XARCH
INS_FLAGS_DONT_CARE = 0x00,
Expand Down
Loading