Skip to content

Commit 4dd5e9c

Browse files
XinWang10xin10.wang
authored and
xin10.wang
committed
[X86][MC]Fix wrong action for encode movdir64b
Movdir64b is special for its mem operand, 67 prefex can not only modify its add size, so it's mem base and index reg should be the same type as source reg, such as movdir64b (%rdx), rcx, and could not be movdir64b (%edx), rcx. Now llvm-mc can encode the asm 'movdir64b (%edx), rcx' but the result is the same as 'movdir64b (%edx), ecx', which offend users' intention, while gcc will object this action and give a warning. I add 3 new mem descriptions to let llvm-mc to report the same error. Reviewed By: skan, craig.topper Differential Revision: https://reviews.llvm.org/D145893
1 parent bde91fd commit 4dd5e9c

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

llvm/lib/Target/X86/AsmParser/X86Operand.h

+34
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,40 @@ struct X86Operand final : public MCParsedAsmOperand {
380380
bool isMem512_RC512() const {
381381
return isMem512() && isMemIndexReg(X86::ZMM0, X86::ZMM31);
382382
}
383+
bool isMem512_GR16() const {
384+
if (!isMem512())
385+
return false;
386+
if (getMemBaseReg() &&
387+
!X86MCRegisterClasses[X86::GR16RegClassID].contains(getMemBaseReg()))
388+
return false;
389+
return true;
390+
}
391+
bool isMem512_GR32() const {
392+
if (!isMem512())
393+
return false;
394+
if (getMemBaseReg() &&
395+
!X86MCRegisterClasses[X86::GR32RegClassID].contains(getMemBaseReg()) &&
396+
getMemBaseReg() != X86::EIP)
397+
return false;
398+
if (getMemIndexReg() &&
399+
!X86MCRegisterClasses[X86::GR32RegClassID].contains(getMemIndexReg()) &&
400+
getMemIndexReg() != X86::EIZ)
401+
return false;
402+
return true;
403+
}
404+
bool isMem512_GR64() const {
405+
if (!isMem512())
406+
return false;
407+
if (getMemBaseReg() &&
408+
!X86MCRegisterClasses[X86::GR64RegClassID].contains(getMemBaseReg()) &&
409+
getMemBaseReg() != X86::RIP)
410+
return false;
411+
if (getMemIndexReg() &&
412+
!X86MCRegisterClasses[X86::GR64RegClassID].contains(getMemIndexReg()) &&
413+
getMemIndexReg() != X86::RIZ)
414+
return false;
415+
return true;
416+
}
383417

384418
bool isAbsMem() const {
385419
return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&

llvm/lib/Target/X86/X86InstrInfo.td

+8
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ let RenderMethod = "addMemOperands", SuperClasses = [X86MemAsmOperand] in {
380380
def X86Mem512_RC256XOperand : AsmOperandClass { let Name = "Mem512_RC256X"; }
381381
def X86Mem256_RC512Operand : AsmOperandClass { let Name = "Mem256_RC512"; }
382382
def X86Mem512_RC512Operand : AsmOperandClass { let Name = "Mem512_RC512"; }
383+
def X86Mem512_GR16Operand : AsmOperandClass { let Name = "Mem512_GR16"; }
384+
def X86Mem512_GR32Operand : AsmOperandClass { let Name = "Mem512_GR32"; }
385+
def X86Mem512_GR64Operand : AsmOperandClass { let Name = "Mem512_GR64"; }
383386

384387
def X86SibMemOperand : AsmOperandClass { let Name = "SibMem"; }
385388
}
@@ -432,6 +435,11 @@ def f128mem : X86MemOperand<"printxmmwordmem", X86Mem128AsmOperand, 128>;
432435
def f256mem : X86MemOperand<"printymmwordmem", X86Mem256AsmOperand, 256>;
433436
def f512mem : X86MemOperand<"printzmmwordmem", X86Mem512AsmOperand, 512>;
434437

438+
// 32/64 mode specific mem operands
439+
def i512mem_GR16 : X86MemOperand<"printzmmwordmem", X86Mem512_GR16Operand, 512>;
440+
def i512mem_GR32 : X86MemOperand<"printzmmwordmem", X86Mem512_GR32Operand, 512>;
441+
def i512mem_GR64 : X86MemOperand<"printzmmwordmem", X86Mem512_GR64Operand, 512>;
442+
435443
// Gather mem operands
436444
def vx64mem : X86VMemOperand<VR128, "printqwordmem", X86Mem64_RC128Operand, 64>;
437445
def vx128mem : X86VMemOperand<VR128, "printxmmwordmem", X86Mem128_RC128Operand, 128>;

llvm/lib/Target/X86/X86InstrMisc.td

+3-3
Original file line numberDiff line numberDiff line change
@@ -1521,14 +1521,14 @@ def MOVDIRI64 : RI<0xF9, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
15211521
// MOVDIR64B - Move 64 bytes as direct store
15221522
//
15231523
let SchedRW = [WriteStore] in {
1524-
def MOVDIR64B16 : I<0xF8, MRMSrcMem, (outs), (ins GR16:$dst, i512mem:$src),
1524+
def MOVDIR64B16 : I<0xF8, MRMSrcMem, (outs), (ins GR16:$dst, i512mem_GR16:$src),
15251525
"movdir64b\t{$src, $dst|$dst, $src}", []>,
15261526
T8PD, AdSize16, Requires<[HasMOVDIR64B, Not64BitMode]>;
1527-
def MOVDIR64B32 : I<0xF8, MRMSrcMem, (outs), (ins GR32:$dst, i512mem:$src),
1527+
def MOVDIR64B32 : I<0xF8, MRMSrcMem, (outs), (ins GR32:$dst, i512mem_GR32:$src),
15281528
"movdir64b\t{$src, $dst|$dst, $src}",
15291529
[(int_x86_movdir64b GR32:$dst, addr:$src)]>,
15301530
T8PD, AdSize32, Requires<[HasMOVDIR64B]>;
1531-
def MOVDIR64B64 : I<0xF8, MRMSrcMem, (outs), (ins GR64:$dst, i512mem:$src),
1531+
def MOVDIR64B64 : I<0xF8, MRMSrcMem, (outs), (ins GR64:$dst, i512mem_GR64:$src),
15321532
"movdir64b\t{$src, $dst|$dst, $src}",
15331533
[(int_x86_movdir64b GR64:$dst, addr:$src)]>,
15341534
T8PD, AdSize64, Requires<[HasMOVDIR64B, In64BitMode]>;

llvm/test/MC/X86/index-operations.s

+26
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,29 @@ insw %dx, (%rbx)
160160
// ERR32: 64-bit
161161
// ERR16: 64-bit
162162

163+
movdir64b 291(%si), %ecx
164+
// ERR32: invalid operand
165+
// ERR16: invalid operand
166+
167+
movdir64b 291(%esi), %cx
168+
// ERR32: invalid operand
169+
// ERR16: invalid operand
170+
171+
movdir64b (%rdx), %r15d
172+
// ERR64: invalid operand
173+
174+
movdir64b (%edx), %r15
175+
// ERR64: invalid operand
176+
177+
movdir64b (%eip), %ebx
178+
// 64: movdir64b (%eip), %ebx # encoding: [0x67,0x66,0x0f,0x38,0xf8,0x1d,0x00,0x00,0x00,0x00]
179+
180+
movdir64b (%rip), %rbx
181+
// 64: movdir64b (%rip), %rbx # encoding: [0x66,0x0f,0x38,0xf8,0x1d,0x00,0x00,0x00,0x00]
182+
183+
movdir64b 291(%esi, %eiz, 4), %ebx
184+
// 64: movdir64b 291(%esi,%eiz,4), %ebx # encoding: [0x67,0x66,0x0f,0x38,0xf8,0x9c,0xa6,0x23,0x01,0x00,0x00]
185+
// 32: movdir64b 291(%esi,%eiz,4), %ebx # encoding: [0x66,0x0f,0x38,0xf8,0x9c,0xa6,0x23,0x01,0x00,0x00]
186+
187+
movdir64b 291(%rsi, %riz, 4), %rbx
188+
// 64: movdir64b 291(%rsi,%riz,4), %rbx # encoding: [0x66,0x0f,0x38,0xf8,0x9c,0xa6,0x23,0x01,0x00,0x00]

llvm/utils/TableGen/X86RecognizableInstr.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,9 @@ OperandType RecognizableInstr::typeFromString(const std::string &s,
955955
TYPE("i128mem", TYPE_M)
956956
TYPE("i256mem", TYPE_M)
957957
TYPE("i512mem", TYPE_M)
958+
TYPE("i512mem_GR16", TYPE_M)
959+
TYPE("i512mem_GR32", TYPE_M)
960+
TYPE("i512mem_GR64", TYPE_M)
958961
TYPE("i64i32imm_brtarget", TYPE_REL)
959962
TYPE("i16imm_brtarget", TYPE_REL)
960963
TYPE("i32imm_brtarget", TYPE_REL)
@@ -1221,6 +1224,9 @@ RecognizableInstr::memoryEncodingFromString(const std::string &s,
12211224
ENCODING("i128mem", ENCODING_RM)
12221225
ENCODING("i256mem", ENCODING_RM)
12231226
ENCODING("i512mem", ENCODING_RM)
1227+
ENCODING("i512mem_GR16", ENCODING_RM)
1228+
ENCODING("i512mem_GR32", ENCODING_RM)
1229+
ENCODING("i512mem_GR64", ENCODING_RM)
12241230
ENCODING("f80mem", ENCODING_RM)
12251231
ENCODING("lea64_32mem", ENCODING_RM)
12261232
ENCODING("lea64mem", ENCODING_RM)

0 commit comments

Comments
 (0)