[binutils-gdb] x86/APX: optimize {nf} forms of AND with small negative immediates
Jan Beulich
jbeulich@sourceware.org
Mon Nov 9 10:12:48 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e2a5e671c0d27364c2510c0999f3515e731bd713
commit e2a5e671c0d27364c2510c0999f3515e731bd713
Author: Jan Beulich <jbeulich@suse.com>
Date: Mon Nov 9 11:07:24 2026 +0100
x86/APX: optimize {nf} forms of AND with small negative immediates
These can be expressed with byte or word forms, leveraging that non-
EVEX.ND byte/word operations leave the upper bits unaltered. Since
- word forms incur length-changing-prefix effects,
- byte and word forms can incur partial register stalls,
restrict transforming those to merely size optimization.
Diff:
---
gas/config/tc-i386.c | 51 ++++++++++++++++++++++
.../gas/i386/x86-64-apx-nf-optimize-size.d | 5 +++
gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d | 5 +++
gas/testsuite/gas/i386/x86-64-apx-nf.s | 6 +++
4 files changed, 67 insertions(+)
diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index 12fd3d86fe2..308edb9e480 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6346,6 +6346,57 @@ optimize_nf_encoding (void)
i.tm.opcode_modifier.vexw = VEXW0;
}
+ i.types[0].bitfield.imm32 = 0;
+ i.types[0].bitfield.imm32s = 0;
+ }
+ else if (optimize_for_space
+ && 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 >= ~0xffff
+ && is_plausible_suffix (1)
+ && (i.types[1].bitfield.qword
+ ? (i.reg_operands == 1
+ || i.op[1].regs == i.op[2].regs)
+ : (i.types[1].bitfield.word
+ && i.reg_operands == 1
+ && i.op[0].imms->X_add_number >= ~0xff)))
+ {
+ /* Optimize: -Os:
+ {nf} andq $~imm8, %rN, %rN -> {nf} andb $~imm8, %rN
+ {nf} andq $~imm8, %rN -> {nf} andb $~imm8, %rN
+ {nf} andw $~imm8, %rN -> {nf} andb $~imm8, %rN
+
+ {nf} andq $~imm16, %rN, %rN -> {nf} andw $~imm16, %rN
+ {nf} andq $~imm16, %rN -> {nf} andw $~imm16, %rN
+ */
+ if (i.reg_operands == 2)
+ {
+ i.tm.opcode_modifier.vexvvvv = 0;
+ i.reg_operands = 1;
+ i.operands = 2;
+ }
+
+ if (i.op[0].imms->X_add_number >= ~0xff)
+ {
+ i.types[0].bitfield.imm8 = 1;
+ i.types[0].bitfield.imm16 = 0;
+ i.types[1].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;
}
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 3780d4bc09a..f4463079ecb 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
@@ -1525,6 +1525,11 @@ Disassembly of section \.text:
[ ]*[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 80 e1 55[ ]+\{nf\} and \$0x55,%cl
+[ ]*[a-f0-9]+:[ ]*62 f4 7c 0c 80 e1 55[ ]+\{nf\} and \$0x55,%cl
+[ ]*[a-f0-9]+:[ ]*62 f4 7c 0c 80 e1 55[ ]+\{nf\} and \$0x55,%cl
+[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c 81 e1 aa 55[ ]+\{nf\} and \$0x55aa,%cx
+[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c 81 e1 aa 55[ ]+\{nf\} and \$0x55aa,%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 140ac635237..ca89d55b491 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
@@ -1525,6 +1525,11 @@ Disassembly of section \.text:
[ ]*[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 f4 1c 81 e1 55 ff ff ff[ ]+\{nf\} and \$0xf+55,%rcx,%rcx
+[ ]*[a-f0-9]+:[ ]*62 f4 fc 0c 81 e1 55 ff ff ff[ ]+\{nf\} and \$0xf+55,%rcx
+[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c 81 e1 55 ff[ ]+\{nf\} and \$0xff55,%cx
+[ ]*[a-f0-9]+:[ ]*62 f4 f4 1c 81 e1 aa 55 ff ff[ ]+\{nf\} and \$0xf+55aa,%rcx,%rcx
+[ ]*[a-f0-9]+:[ ]*62 f4 fc 0c 81 e1 aa 55 ff ff[ ]+\{nf\} and \$0xf+55aa,%rcx
[ ]*[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 c66e235bf2b..347e8b34d08 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf.s
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf.s
@@ -1482,6 +1482,12 @@ optimize:
{nf} and $0xaa55, %rcx
{nf} and $0xaa55, %ecx
+ {nf} and $~0xaa, %rcx, %rcx
+ {nf} and $~0xaa, %rcx
+ {nf} and $~0xaa, %cx
+ {nf} and $~0xaa55, %rcx, %rcx
+ {nf} and $~0xaa55, %rcx
+
.irp dir, l, r
{nf} ro\dir $7, %dl
{nf} ro\dir $7, %dl, %al
More information about the Binutils-cvs
mailing list