Skip to content

Commit

Permalink
[X86] Emit REX prefix immediately before the opcode
Browse files Browse the repository at this point in the history
Fix prefix emission order to emit REX immediately before the opcode (SDM vol2,
2.1, Figure 2-1). According to SDM vol2 2.2.1, "Other placements are ignored".

This fix has a side effect of outputting segment override prefix in a different
order than previously (benign).

Follow-up to https://reviews.llvm.org/D120592

Reviewed By: skan, craig.topper

Differential Revision: https://reviews.llvm.org/D120871
  • Loading branch information
aaupov committed Mar 16, 2022
1 parent 1d37198 commit 2c4e38f
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 77 deletions.
17 changes: 9 additions & 8 deletions llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,6 @@ bool X86MCCodeEmitter::emitPrefixImpl(unsigned &CurOp, const MCInst &MI,
Flags & X86::IP_HAS_AD_SIZE)
emitByte(0x67, OS);

// Encoding type for this instruction.
uint64_t Encoding = TSFlags & X86II::EncodingMask;
bool HasREX = false;
if (Encoding)
emitVEXOpcodePrefix(MemoryOperand, MI, OS);
else
HasREX = emitOpcodePrefix(MemoryOperand, MI, STI, OS);

uint64_t Form = TSFlags & X86II::FormMask;
switch (Form) {
default:
Expand Down Expand Up @@ -654,6 +646,15 @@ bool X86MCCodeEmitter::emitPrefixImpl(unsigned &CurOp, const MCInst &MI,
}
}

// REX prefix is optional, but if used must be immediately before the opcode
// Encoding type for this instruction.
uint64_t Encoding = TSFlags & X86II::EncodingMask;
bool HasREX = false;
if (Encoding)
emitVEXOpcodePrefix(MemoryOperand, MI, OS);
else
HasREX = emitOpcodePrefix(MemoryOperand, MI, STI, OS);

return HasREX;
}

Expand Down
12 changes: 6 additions & 6 deletions llvm/test/MC/Disassembler/X86/moffs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
# 64: movw 0x5a5a5a5a, %ax # encoding: [0x67,0x66,0xa1,0x5a,0x5a,0x5a,0x5a]
0x67 0x66 0xa1 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a

# 16: movl %es:0x5a5a5a5a, %eax # encoding: [0x67,0x66,0x26,0xa1,0x5a,0x5a,0x5a,0x5a]
# 32: movw %es:0x5a5a, %ax # encoding: [0x67,0x66,0x26,0xa1,0x5a,0x5a]
# 64: movw %es:0x5a5a5a5a, %ax # encoding: [0x67,0x66,0x26,0xa1,0x5a,0x5a,0x5a,0x5a]
# 16: movl %es:0x5a5a5a5a, %eax # encoding: [0x67,0x26,0x66,0xa1,0x5a,0x5a,0x5a,0x5a]
# 32: movw %es:0x5a5a, %ax # encoding: [0x67,0x26,0x66,0xa1,0x5a,0x5a]
# 64: movw %es:0x5a5a5a5a, %ax # encoding: [0x67,0x26,0x66,0xa1,0x5a,0x5a,0x5a,0x5a]
0x67 0x26 0x66 0xa1 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a # encoding: [0xa0,0x5a,0x5a]


Expand Down Expand Up @@ -79,8 +79,8 @@
# 64: movw %ax, 0x5a5a5a5a # encoding: [0x67,0x66,0xa3,0x5a,0x5a,0x5a,0x5a]
0x67 0x66 0xa3 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a

# 16: movl %eax, %es:0x5a5a5a5a # encoding: [0x67,0x66,0x26,0xa3,0x5a,0x5a,0x5a,0x5a]
# 32: movw %ax, %es:0x5a5a # encoding: [0x67,0x66,0x26,0xa3,0x5a,0x5a]
# 64: movw %ax, %es:0x5a5a5a5a # encoding: [0x67,0x66,0x26,0xa3,0x5a,0x5a,0x5a,0x5a]
# 16: movl %eax, %es:0x5a5a5a5a # encoding: [0x67,0x26,0x66,0xa3,0x5a,0x5a,0x5a,0x5a]
# 32: movw %ax, %es:0x5a5a # encoding: [0x67,0x26,0x66,0xa3,0x5a,0x5a]
# 64: movw %ax, %es:0x5a5a5a5a # encoding: [0x67,0x26,0x66,0xa3,0x5a,0x5a,0x5a,0x5a]
0x67 0x26 0x66 0xa3 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a 0x5a

6 changes: 3 additions & 3 deletions llvm/test/MC/X86/I186-32.s
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ outsb %es:(%esi), %dx
outsl %es:(%esi), %dx

// CHECK: outsw %es:(%esi), %dx
// CHECK: encoding: [0x66,0x26,0x6f]
// CHECK: encoding: [0x26,0x66,0x6f]
outsw %es:(%esi), %dx

// CHECK: popal
Expand Down Expand Up @@ -269,7 +269,7 @@ repne outsb %es:(%esi), %dx
repne outsl %es:(%esi), %dx

// CHECK: repne outsw %es:(%esi), %dx
// CHECK: encoding: [0xf2,0x66,0x26,0x6f]
// CHECK: encoding: [0xf2,0x26,0x66,0x6f]
repne outsw %es:(%esi), %dx

// CHECK: rep outsb %es:(%esi), %dx
Expand All @@ -281,7 +281,7 @@ rep outsb %es:(%esi), %dx
rep outsl %es:(%esi), %dx

// CHECK: rep outsw %es:(%esi), %dx
// CHECK: encoding: [0xf3,0x66,0x26,0x6f]
// CHECK: encoding: [0xf3,0x26,0x66,0x6f]
rep outsw %es:(%esi), %dx

// CHECK: rolb $0, -485498096(%edx,%eax,4)
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/MC/X86/I186-64.s
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ outsb %gs:(%rsi), %dx
outsl %gs:(%rsi), %dx

// CHECK: outsw %gs:(%rsi), %dx
// CHECK: encoding: [0x66,0x65,0x6f]
// CHECK: encoding: [0x65,0x66,0x6f]
outsw %gs:(%rsi), %dx

// CHECK: pushq $0
Expand Down Expand Up @@ -289,7 +289,7 @@ repne outsb %gs:(%rsi), %dx
repne outsl %gs:(%rsi), %dx

// CHECK: repne outsw %gs:(%rsi), %dx
// CHECK: encoding: [0xf2,0x66,0x65,0x6f]
// CHECK: encoding: [0xf2,0x65,0x66,0x6f]
repne outsw %gs:(%rsi), %dx

// CHECK: rep outsb %gs:(%rsi), %dx
Expand All @@ -301,7 +301,7 @@ rep outsb %gs:(%rsi), %dx
rep outsl %gs:(%rsi), %dx

// CHECK: rep outsw %gs:(%rsi), %dx
// CHECK: encoding: [0xf3,0x66,0x65,0x6f]
// CHECK: encoding: [0xf3,0x65,0x66,0x6f]
rep outsw %gs:(%rsi), %dx

// CHECK: rolb $0, 485498096
Expand Down
18 changes: 9 additions & 9 deletions llvm/test/MC/X86/I386-32.s
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ cmpsb %es:(%edi), %es:(%esi)
cmpsl %es:(%edi), %es:(%esi)

// CHECK: cmpsw %es:(%edi), %es:(%esi)
// CHECK: encoding: [0x66,0x26,0xa7]
// CHECK: encoding: [0x26,0x66,0xa7]
cmpsw %es:(%edi), %es:(%esi)

// CHECK: insb %dx, %es:(%edi)
Expand Down Expand Up @@ -193,7 +193,7 @@ movsb %es:(%esi), %es:(%edi)
movsl %es:(%esi), %es:(%edi)

// CHECK: movsw %es:(%esi), %es:(%edi)
// CHECK: encoding: [0x66,0x26,0xa5]
// CHECK: encoding: [0x26,0x66,0xa5]
movsw %es:(%esi), %es:(%edi)

// CHECK: outsb %es:(%esi), %dx
Expand All @@ -205,7 +205,7 @@ outsb %es:(%esi), %dx
outsl %es:(%esi), %dx

// CHECK: outsw %es:(%esi), %dx
// CHECK: encoding: [0x66,0x26,0x6f]
// CHECK: encoding: [0x26,0x66,0x6f]
outsw %es:(%esi), %dx

// CHECK: popal
Expand Down Expand Up @@ -249,7 +249,7 @@ rep cmpsb %es:(%edi), %es:(%esi)
rep cmpsl %es:(%edi), %es:(%esi)

// CHECK: rep cmpsw %es:(%edi), %es:(%esi)
// CHECK: encoding: [0xf3,0x66,0x26,0xa7]
// CHECK: encoding: [0xf3,0x26,0x66,0xa7]
rep cmpsw %es:(%edi), %es:(%esi)

// CHECK: rep insb %dx, %es:(%edi)
Expand Down Expand Up @@ -277,7 +277,7 @@ rep movsb %es:(%esi), %es:(%edi)
rep movsl %es:(%esi), %es:(%edi)

// CHECK: rep movsw %es:(%esi), %es:(%edi)
// CHECK: encoding: [0xf3,0x66,0x26,0xa5]
// CHECK: encoding: [0xf3,0x26,0x66,0xa5]
rep movsw %es:(%esi), %es:(%edi)

// CHECK: repne cmpsb %es:(%edi), %es:(%esi)
Expand All @@ -289,7 +289,7 @@ repne cmpsb %es:(%edi), %es:(%esi)
repne cmpsl %es:(%edi), %es:(%esi)

// CHECK: repne cmpsw %es:(%edi), %es:(%esi)
// CHECK: encoding: [0xf2,0x66,0x26,0xa7]
// CHECK: encoding: [0xf2,0x26,0x66,0xa7]
repne cmpsw %es:(%edi), %es:(%esi)

// CHECK: repne insb %dx, %es:(%edi)
Expand Down Expand Up @@ -317,7 +317,7 @@ repne movsb %es:(%esi), %es:(%edi)
repne movsl %es:(%esi), %es:(%edi)

// CHECK: repne movsw %es:(%esi), %es:(%edi)
// CHECK: encoding: [0xf2,0x66,0x26,0xa5]
// CHECK: encoding: [0xf2,0x26,0x66,0xa5]
repne movsw %es:(%esi), %es:(%edi)

// CHECK: repne outsb %es:(%esi), %dx
Expand All @@ -329,7 +329,7 @@ repne outsb %es:(%esi), %dx
repne outsl %es:(%esi), %dx

// CHECK: repne outsw %es:(%esi), %dx
// CHECK: encoding: [0xf2,0x66,0x26,0x6f]
// CHECK: encoding: [0xf2,0x26,0x66,0x6f]
repne outsw %es:(%esi), %dx

// CHECK: repne scasl %es:(%edi), %eax
Expand All @@ -349,7 +349,7 @@ rep outsb %es:(%esi), %dx
rep outsl %es:(%esi), %dx

// CHECK: rep outsw %es:(%esi), %dx
// CHECK: encoding: [0xf3,0x66,0x26,0x6f]
// CHECK: encoding: [0xf3,0x26,0x66,0x6f]
rep outsw %es:(%esi), %dx

// CHECK: rep scasl %es:(%edi), %eax
Expand Down
30 changes: 15 additions & 15 deletions llvm/test/MC/X86/I386-64.s
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ cmpsb %es:(%rdi), %gs:(%rsi)
cmpsl %es:(%rdi), %gs:(%rsi)

// CHECK: cmpsq %es:(%rdi), %gs:(%rsi)
// CHECK: encoding: [0x48,0x65,0xa7]
// CHECK: encoding: [0x65,0x48,0xa7]
cmpsq %es:(%rdi), %gs:(%rsi)

// CHECK: cmpsw %es:(%rdi), %gs:(%rsi)
// CHECK: encoding: [0x66,0x65,0xa7]
// CHECK: encoding: [0x65,0x66,0xa7]
cmpsw %es:(%rdi), %gs:(%rsi)

// CHECK: insb %dx, %es:(%rdi)
Expand Down Expand Up @@ -306,11 +306,11 @@ movsbl (%rdx), %r13d
movsl %gs:(%rsi), %es:(%rdi)

// CHECK: movsq %gs:(%rsi), %es:(%rdi)
// CHECK: encoding: [0x48,0x65,0xa5]
// CHECK: encoding: [0x65,0x48,0xa5]
movsq %gs:(%rsi), %es:(%rdi)

// CHECK: movsw %gs:(%rsi), %es:(%rdi)
// CHECK: encoding: [0x66,0x65,0xa5]
// CHECK: encoding: [0x65,0x66,0xa5]
movsw %gs:(%rsi), %es:(%rdi)

// CHECK: movswl 485498096, %r13d
Expand Down Expand Up @@ -418,7 +418,7 @@ outsb %gs:(%rsi), %dx
outsl %gs:(%rsi), %dx

// CHECK: outsw %gs:(%rsi), %dx
// CHECK: encoding: [0x66,0x65,0x6f]
// CHECK: encoding: [0x65,0x66,0x6f]
outsw %gs:(%rsi), %dx

// CHECK: rep cmpsb %es:(%rdi), %gs:(%rsi)
Expand All @@ -430,11 +430,11 @@ rep cmpsb %es:(%rdi), %gs:(%rsi)
rep cmpsl %es:(%rdi), %gs:(%rsi)

// CHECK: rep cmpsq %es:(%rdi), %gs:(%rsi)
// CHECK: encoding: [0xf3,0x48,0x65,0xa7]
// CHECK: encoding: [0xf3,0x65,0x48,0xa7]
rep cmpsq %es:(%rdi), %gs:(%rsi)

// CHECK: rep cmpsw %es:(%rdi), %gs:(%rsi)
// CHECK: encoding: [0xf3,0x66,0x65,0xa7]
// CHECK: encoding: [0xf3,0x65,0x66,0xa7]
rep cmpsw %es:(%rdi), %gs:(%rsi)

// CHECK: rep insb %dx, %es:(%rdi)
Expand Down Expand Up @@ -462,11 +462,11 @@ rep movsb %gs:(%rsi), %es:(%rdi)
rep movsl %gs:(%rsi), %es:(%rdi)

// CHECK: rep movsq %gs:(%rsi), %es:(%rdi)
// CHECK: encoding: [0xf3,0x48,0x65,0xa5]
// CHECK: encoding: [0xf3,0x65,0x48,0xa5]
rep movsq %gs:(%rsi), %es:(%rdi)

// CHECK: rep movsw %gs:(%rsi), %es:(%rdi)
// CHECK: encoding: [0xf3,0x66,0x65,0xa5]
// CHECK: encoding: [0xf3,0x65,0x66,0xa5]
rep movsw %gs:(%rsi), %es:(%rdi)

// CHECK: repne cmpsb %es:(%rdi), %gs:(%rsi)
Expand All @@ -478,11 +478,11 @@ repne cmpsb %es:(%rdi), %gs:(%rsi)
repne cmpsl %es:(%rdi), %gs:(%rsi)

// CHECK: repne cmpsq %es:(%rdi), %gs:(%rsi)
// CHECK: encoding: [0xf2,0x48,0x65,0xa7]
// CHECK: encoding: [0xf2,0x65,0x48,0xa7]
repne cmpsq %es:(%rdi), %gs:(%rsi)

// CHECK: repne cmpsw %es:(%rdi), %gs:(%rsi)
// CHECK: encoding: [0xf2,0x66,0x65,0xa7]
// CHECK: encoding: [0xf2,0x65,0x66,0xa7]
repne cmpsw %es:(%rdi), %gs:(%rsi)

// CHECK: repne insb %dx, %es:(%rdi)
Expand Down Expand Up @@ -510,11 +510,11 @@ repne movsb %gs:(%rsi), %es:(%rdi)
repne movsl %gs:(%rsi), %es:(%rdi)

// CHECK: repne movsq %gs:(%rsi), %es:(%rdi)
// CHECK: encoding: [0xf2,0x48,0x65,0xa5]
// CHECK: encoding: [0xf2,0x65,0x48,0xa5]
repne movsq %gs:(%rsi), %es:(%rdi)

// CHECK: repne movsw %gs:(%rsi), %es:(%rdi)
// CHECK: encoding: [0xf2,0x66,0x65,0xa5]
// CHECK: encoding: [0xf2,0x65,0x66,0xa5]
repne movsw %gs:(%rsi), %es:(%rdi)

// CHECK: repne outsb %gs:(%rsi), %dx
Expand All @@ -526,7 +526,7 @@ repne outsb %gs:(%rsi), %dx
repne outsl %gs:(%rsi), %dx

// CHECK: repne outsw %gs:(%rsi), %dx
// CHECK: encoding: [0xf2,0x66,0x65,0x6f]
// CHECK: encoding: [0xf2,0x65,0x66,0x6f]
repne outsw %gs:(%rsi), %dx

// CHECK: repne scasl %es:(%rdi), %eax
Expand All @@ -546,7 +546,7 @@ rep outsb %gs:(%rsi), %dx
rep outsl %gs:(%rsi), %dx

// CHECK: rep outsw %gs:(%rsi), %dx
// CHECK: encoding: [0xf3,0x66,0x65,0x6f]
// CHECK: encoding: [0xf3,0x65,0x66,0x6f]
rep outsw %gs:(%rsi), %dx

// CHECK: rep scasl %es:(%rdi), %eax
Expand Down
18 changes: 9 additions & 9 deletions llvm/test/MC/X86/I86-32.s
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ cmpsb %es:(%edi), %es:(%esi)
cmpsl %es:(%edi), %es:(%esi)

// CHECK: cmpsw %es:(%edi), %es:(%esi)
// CHECK: encoding: [0x66,0x26,0xa7]
// CHECK: encoding: [0x26,0x66,0xa7]
cmpsw %es:(%edi), %es:(%esi)

// CHECK: cmpw $0, -485498096(%edx,%eax,4)
Expand Down Expand Up @@ -1197,7 +1197,7 @@ lock xchgl %eax, (%edx)
lodsb %es:(%esi), %al

// CHECK: lodsw %es:(%esi), %ax
// CHECK: encoding: [0x66,0x26,0xad]
// CHECK: encoding: [0x26,0x66,0xad]
lodsw %es:(%esi), %ax

// CHECK: loop 64
Expand Down Expand Up @@ -1345,7 +1345,7 @@ movsb %es:(%esi), %es:(%edi)
movsl %es:(%esi), %es:(%edi)

// CHECK: movsw %es:(%esi), %es:(%edi)
// CHECK: encoding: [0x66,0x26,0xa5]
// CHECK: encoding: [0x26,0x66,0xa5]
movsw %es:(%esi), %es:(%edi)

// CHECK: movw $0, -485498096(%edx,%eax,4)
Expand Down Expand Up @@ -2221,15 +2221,15 @@ rep cmpsb %es:(%edi), %es:(%esi)
rep cmpsl %es:(%edi), %es:(%esi)

// CHECK: rep cmpsw %es:(%edi), %es:(%esi)
// CHECK: encoding: [0xf3,0x66,0x26,0xa7]
// CHECK: encoding: [0xf3,0x26,0x66,0xa7]
rep cmpsw %es:(%edi), %es:(%esi)

// CHECK: rep lodsb %es:(%esi), %al
// CHECK: encoding: [0xf3,0x26,0xac]
rep lodsb %es:(%esi), %al

// CHECK: rep lodsw %es:(%esi), %ax
// CHECK: encoding: [0xf3,0x66,0x26,0xad]
// CHECK: encoding: [0xf3,0x26,0x66,0xad]
rep lodsw %es:(%esi), %ax

// CHECK: rep movsb %es:(%esi), %es:(%edi)
Expand All @@ -2241,7 +2241,7 @@ rep movsb %es:(%esi), %es:(%edi)
rep movsl %es:(%esi), %es:(%edi)

// CHECK: rep movsw %es:(%esi), %es:(%edi)
// CHECK: encoding: [0xf3,0x66,0x26,0xa5]
// CHECK: encoding: [0xf3,0x26,0x66,0xa5]
rep movsw %es:(%esi), %es:(%edi)

// CHECK: repne cmpsb %es:(%edi), %es:(%esi)
Expand All @@ -2253,15 +2253,15 @@ repne cmpsb %es:(%edi), %es:(%esi)
repne cmpsl %es:(%edi), %es:(%esi)

// CHECK: repne cmpsw %es:(%edi), %es:(%esi)
// CHECK: encoding: [0xf2,0x66,0x26,0xa7]
// CHECK: encoding: [0xf2,0x26,0x66,0xa7]
repne cmpsw %es:(%edi), %es:(%esi)

// CHECK: repne lodsb %es:(%esi), %al
// CHECK: encoding: [0xf2,0x26,0xac]
repne lodsb %es:(%esi), %al

// CHECK: repne lodsw %es:(%esi), %ax
// CHECK: encoding: [0xf2,0x66,0x26,0xad]
// CHECK: encoding: [0xf2,0x26,0x66,0xad]
repne lodsw %es:(%esi), %ax

// CHECK: repne movsb %es:(%esi), %es:(%edi)
Expand All @@ -2273,7 +2273,7 @@ repne movsb %es:(%esi), %es:(%edi)
repne movsl %es:(%esi), %es:(%edi)

// CHECK: repne movsw %es:(%esi), %es:(%edi)
// CHECK: encoding: [0xf2,0x66,0x26,0xa5]
// CHECK: encoding: [0xf2,0x26,0x66,0xa5]
repne movsw %es:(%esi), %es:(%edi)

// CHECK: repne scasb %es:(%edi), %al
Expand Down
Loading

0 comments on commit 2c4e38f

Please sign in to comment.