[binutils-gdb] x86/APX: optimize {nf} forms of AND with small immediates
Jan Beulich
jbeulich@sourceware.org
Mon Nov 9 10:12:43 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3edafaed42994c13b5aaa96b1bfdc149f9a30aa3
commit 3edafaed42994c13b5aaa96b1bfdc149f9a30aa3
Author: Jan Beulich <jbeulich@suse.com>
Date: Mon Nov 9 11:07:09 2026 +0100
x86/APX: optimize {nf} forms of AND with small immediates
These can be expressed with byte or word forms, leveraging the implied
zero-extension of EVEX.ND encodings. Since word forms incur length-
changing-prefix effects, restrict transforming those to merely size
optimization.
Diff:
---
gas/config/tc-i386.c | 56 ++++++++++++++++++++++
.../gas/i386/x86-64-apx-nf-optimize-size.d | 9 ++++
gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d | 9 ++++
gas/testsuite/gas/i386/x86-64-apx-nf.s | 10 ++++
opcodes/i386-opc.tbl | 2 +-
opcodes/i386-tbl.h | 8 ++--
6 files changed, 89 insertions(+), 5 deletions(-)
diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index 8a886478769..12fd3d86fe2 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6293,6 +6293,62 @@ optimize_nf_encoding (void)
i.operands = 2;
i.reg_operands = i.operands - i.mem_operands;
}
+ else if (i.tm.base_opcode == 0x80
+ && i.tm.extension_opcode == 4
+ && i.reg_operands + 1 == i.operands
+ && !i.prefix[DATA_PREFIX]
+ && !i.prefix[REX_PREFIX]
+ && i.op[0].imms->X_op == O_constant
+ && i.op[0].imms->X_add_number >= 0
+ && i.op[0].imms->X_add_number <= (optimize_for_space ? 0xffff : 0xff)
+ && is_plausible_suffix (1)
+ && !i.types[1].bitfield.byte
+ && (!i.types[1].bitfield.word
+ || (i.reg_operands == 2
+ && i.op[0].imms->X_add_number <= 0xff)))
+ {
+ /* Optimize: -O:
+ {nf} andq $imm8, %rN, %rM -> {nf} andb $imm8, %rN, %rM
+ {nf} andl $imm8, %rN, %rM -> {nf} andb $imm8, %rN, %rM
+ {nf} andw $imm8, %rN, %rM -> {nf} andb $imm8, %rN, %rM
+
+ {nf} andq $imm8, %rN -> {nf} andb $imm8, %rN, %rN
+ {nf} andl $imm8, %rN -> {nf} andb $imm8, %rN, %rN
+
+ Optimize: -Os:
+ {nf} andq $imm16, %rN, %rM -> {nf} andw $imm16, %rN, %rM
+ {nf} andl $imm16, %rN, %rM -> {nf} andw $imm16, %rN, %rM
+
+ {nf} andq $imm16, %rN -> {nf} andw $imm16, %rN, %rN
+ {nf} andl $imm16, %rN -> {nf} andw $imm16, %rN, %rN
+ */
+ if (i.reg_operands == 1)
+ {
+ copy_operand (2, 1);
+ i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
+ i.reg_operands = 2;
+ i.operands = 3;
+ }
+
+ if (i.op[0].imms->X_add_number <= 0xff)
+ {
+ i.types[0].bitfield.imm8 = 1;
+ i.types[0].bitfield.imm16 = 0;
+ i.types[2].bitfield.byte = 1;
+ i.suffix = 0;
+ i.tm.opcode_modifier.w = 0;
+ i.tm.opcode_modifier.vexw = VEXWIG;
+ }
+ else
+ {
+ i.types[0].bitfield.imm16 = 1;
+ i.tm.opcode_modifier.size = SIZE16;
+ i.tm.opcode_modifier.vexw = VEXW0;
+ }
+
+ i.types[0].bitfield.imm32 = 0;
+ i.types[0].bitfield.imm32s = 0;
+ }
else if (i.tm.base_opcode == 0xc0
&& i.op[0].imms->X_op == O_constant
&& i.op[0].imms->X_add_number
diff --git a/gas/testsuite/gas/i386/x86-64-apx-nf-optimize-size.d b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize-size.d
index c07838ad23c..3780d4bc09a 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf-optimize-size.d
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize-size.d
@@ -1516,6 +1516,15 @@ Disassembly of section \.text:
[ ]*[a-f0-9]+:[ ]*62 f4 65 1c f7 18[ ]+\{nf\} neg \(%rax\),%bx
[ ]*[a-f0-9]+:[ ]*62 f4 74 1c f7 18[ ]+\{nf\} neg \(%rax\),%ecx
[ ]*[a-f0-9]+:[ ]*62 f4 b4 1c f7 18[ ]+\{nf\} neg \(%rax\),%r9
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%dl
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%dl
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%dl
+[ ]*[a-f0-9]+:[ ]*62 f4 74 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%cl
+[ ]*[a-f0-9]+:[ ]*62 f4 74 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%cl
+[ ]*[a-f0-9]+:[ ]*62 f4 6d 1c 81 e1 55 aa[ ]+\{nf\} and \$0xaa55,%cx,%dx
+[ ]*[a-f0-9]+:[ ]*62 f4 6d 1c 81 e1 55 aa[ ]+\{nf\} and \$0xaa55,%cx,%dx
+[ ]*[a-f0-9]+:[ ]*62 f4 75 1c 81 e1 55 aa[ ]+\{nf\} and \$0xaa55,%cx,%cx
+[ ]*[a-f0-9]+:[ ]*62 f4 75 1c 81 e1 55 aa[ ]+\{nf\} and \$0xaa55,%cx,%cx
[ ]*[a-f0-9]+:[ ]*62 f4 7c 0c d0 ca[ ]+\{nf\} ror \$1,%dl
[ ]*[a-f0-9]+:[ ]*62 f4 7c 1c d0 ca[ ]+\{nf\} ror \$1,%dl,%al
[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c d1 ca[ ]+\{nf\} ror \$1,%dx
diff --git a/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
index 07b35daf0aa..140ac635237 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
@@ -1516,6 +1516,15 @@ Disassembly of section \.text:
[ ]*[a-f0-9]+:[ ]*62 f4 65 1c f7 18[ ]+\{nf\} neg \(%rax\),%bx
[ ]*[a-f0-9]+:[ ]*62 f4 74 1c f7 18[ ]+\{nf\} neg \(%rax\),%ecx
[ ]*[a-f0-9]+:[ ]*62 f4 b4 1c f7 18[ ]+\{nf\} neg \(%rax\),%r9
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%dl
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%dl
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%dl
+[ ]*[a-f0-9]+:[ ]*62 f4 74 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%cl
+[ ]*[a-f0-9]+:[ ]*62 f4 74 1c 80 e1 aa[ ]+\{nf\} and \$0xaa,%cl,%cl
+[ ]*[a-f0-9]+:[ ]*62 f4 ec 1c 81 e1 55 aa 00 00[ ]+\{nf\} and \$0xaa55,%rcx,%rdx
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c 81 e1 55 aa 00 00[ ]+\{nf\} and \$0xaa55,%ecx,%edx
+[ ]*[a-f0-9]+:[ ]*62 f4 fc 0c 81 e1 55 aa 00 00[ ]+\{nf\} and \$0xaa55,%rcx
+[ ]*[a-f0-9]+:[ ]*62 f4 7c 0c 81 e1 55 aa 00 00[ ]+\{nf\} and \$0xaa55,%ecx
[ ]*[a-f0-9]+:[ ]*62 f4 7c 0c d0 ca[ ]+\{nf\} ror \$1,%dl
[ ]*[a-f0-9]+:[ ]*62 f4 7c 1c d0 ca[ ]+\{nf\} ror \$1,%dl,%al
[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c d1 ca[ ]+\{nf\} ror \$1,%dx
diff --git a/gas/testsuite/gas/i386/x86-64-apx-nf.s b/gas/testsuite/gas/i386/x86-64-apx-nf.s
index e3c9a230ec3..c66e235bf2b 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf.s
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf.s
@@ -1472,6 +1472,16 @@ optimize:
{nf} imul $-1, (%rax), %ecx
{nf} imul $-1, (%rax), %r9
+ {nf} and $0xaa, %rcx, %rdx
+ {nf} and $0xaa, %ecx, %edx
+ {nf} and $0xaa, %cx, %dx
+ {nf} and $0xaa, %rcx
+ {nf} and $0xaa, %ecx
+ {nf} and $0xaa55, %rcx, %rdx
+ {nf} and $0xaa55, %ecx, %edx
+ {nf} and $0xaa55, %rcx
+ {nf} and $0xaa55, %ecx
+
.irp dir, l, r
{nf} ro\dir $7, %dl
{nf} ro\dir $7, %dl, %al
diff --git a/opcodes/i386-opc.tbl b/opcodes/i386-opc.tbl
index 70dedf78caf..b3f7328de3b 100644
--- a/opcodes/i386-opc.tbl
+++ b/opcodes/i386-opc.tbl
@@ -322,7 +322,7 @@ sti, 0xfb, 0, NoSuf, {}
or:1:C::Optimize:::NF, +
adc:2:C:::::, +
sbb:3::::::, +
- and:4:C::Optimize:Optimize::NF, +
+ and:4:C::Optimize:Optimize:Optimize:NF, +
sub:5::Optimize:::Optimize:NF, +
xor:6:C:Optimize:::Optimize:NF>
diff --git a/opcodes/i386-tbl.h b/opcodes/i386-tbl.h
index cfb5e348aff..cac034dff89 100644
--- a/opcodes/i386-tbl.h
+++ b/opcodes/i386-tbl.h
@@ -922,7 +922,7 @@ static const insn_template i386_optab[] =
OPREF_2_60 },
{ MN_and, 0x83, 3, SPACE_MAP4, 4,
{ 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0 },
{ { 155, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
@@ -936,7 +936,7 @@ static const insn_template i386_optab[] =
OPREF_2_62 },
{ MN_and, 0x83, 2, SPACE_MAP4, 4,
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0 },
{ { 155, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
@@ -950,7 +950,7 @@ static const insn_template i386_optab[] =
OPREF_2_66 },
{ MN_and, 0x80, 3, SPACE_MAP4, 4,
{ 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0 },
{ { 155, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
@@ -964,7 +964,7 @@ static const insn_template i386_optab[] =
OPREF_2_8 },
{ MN_and, 0x80, 2, SPACE_MAP4, 4,
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0 },
{ { 155, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
More information about the Binutils-cvs
mailing list