[binutils-gdb] x86/APX: optimize {nf} forms of OR/XOR with small immediates

Jan Beulich jbeulich@sourceware.org
Mon Nov 9 10:12:53 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=cfa4bcc9d055700c91e3bbaa280c34d765b5918c

commit cfa4bcc9d055700c91e3bbaa280c34d765b5918c
Author: Jan Beulich <jbeulich@suse.com>
Date:   Mon Nov 9 11:07:45 2026 +0100

    x86/APX: optimize {nf} forms of OR/XOR with small 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                               | 52 ++++++++++++++++++++++
 .../gas/i386/x86-64-apx-nf-optimize-size.d         | 10 +++++
 gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d    | 10 +++++
 gas/testsuite/gas/i386/x86-64-apx-nf.s             |  8 ++++
 opcodes/i386-opc.tbl                               |  2 +-
 opcodes/i386-tbl.h                                 |  8 ++--
 6 files changed, 85 insertions(+), 5 deletions(-)

diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index 308edb9e480..0738dcddc21 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6397,6 +6397,58 @@ 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 == 1 || i.tm.extension_opcode == 6)
+	   && 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:
+	   OR one of or / xor:
+	   {nf} ORq $imm8, %rN, %rN   -> {nf} ORb $imm8, %rN
+	   {nf} ORq $imm8, %rN        -> {nf} ORb $imm8, %rN
+	   {nf} ORw $imm8, %rN        -> {nf} ORb $imm8, %rN
+
+	   {nf} ORq $imm16, %rN, %rN  -> {nf} ORw $imm16, %rN
+	   {nf} ORq $imm16, %rN       -> {nf} ORw $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 f4463079ecb..4d3331500ca 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
@@ -1530,6 +1530,16 @@ Disassembly of section \.text:
 [ 	]*[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 80 c9 aa[ 	]+\{nf\} or \$0xaa,%cl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7c 0c 80 c9 aa[ 	]+\{nf\} or \$0xaa,%cl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7c 0c 80 c9 aa[ 	]+\{nf\} or \$0xaa,%cl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7d 0c 81 c9 55 aa[ 	]+\{nf\} or \$0xaa55,%cx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7d 0c 81 c9 55 aa[ 	]+\{nf\} or \$0xaa55,%cx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7c 0c 80 f1 aa[ 	]+\{nf\} xor \$0xaa,%cl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7c 0c 80 f1 aa[ 	]+\{nf\} xor \$0xaa,%cl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7c 0c 80 f1 aa[ 	]+\{nf\} xor \$0xaa,%cl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7d 0c 81 f1 55 aa[ 	]+\{nf\} xor \$0xaa55,%cx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7d 0c 81 f1 55 aa[ 	]+\{nf\} xor \$0xaa55,%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 ca89d55b491..706f61d0b3a 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
@@ -1530,6 +1530,16 @@ Disassembly of section \.text:
 [ 	]*[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 f4 1c 81 c9 aa 00 00 00[ 	]+\{nf\} or \$0xaa,%rcx,%rcx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 fc 0c 81 c9 aa 00 00 00[ 	]+\{nf\} or \$0xaa,%rcx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7d 0c 81 c9 aa 00[ 	]+\{nf\} or \$0xaa,%cx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 f4 1c 81 c9 55 aa 00 00[ 	]+\{nf\} or \$0xaa55,%rcx,%rcx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 fc 0c 81 c9 55 aa 00 00[ 	]+\{nf\} or \$0xaa55,%rcx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 f4 1c 81 f1 aa 00 00 00[ 	]+\{nf\} xor \$0xaa,%rcx,%rcx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 fc 0c 81 f1 aa 00 00 00[ 	]+\{nf\} xor \$0xaa,%rcx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7d 0c 81 f1 aa 00[ 	]+\{nf\} xor \$0xaa,%cx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 f4 1c 81 f1 55 aa 00 00[ 	]+\{nf\} xor \$0xaa55,%rcx,%rcx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 fc 0c 81 f1 55 aa 00 00[ 	]+\{nf\} xor \$0xaa55,%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 347e8b34d08..776e777cca4 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf.s
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf.s
@@ -1488,6 +1488,14 @@ optimize:
 	{nf}	and	$~0xaa55, %rcx, %rcx
 	{nf}	and	$~0xaa55, %rcx
 
+	.irp op, or, xor
+	{nf}	\op	$0xaa, %rcx, %rcx
+	{nf}	\op	$0xaa, %rcx
+	{nf}	\op	$0xaa, %cx
+	{nf}	\op	$0xaa55, %rcx, %rcx
+	{nf}	\op	$0xaa55, %rcx
+	.endr
+
 	.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 b3f7328de3b..b364f43b415 100644
--- a/opcodes/i386-opc.tbl
+++ b/opcodes/i386-opc.tbl
@@ -319,7 +319,7 @@ sti, 0xfb, 0, NoSuf, {}
 
 <alu2:opc:c:optz:optt:opti:optiE:nf, +
     add:0:C:::::NF|Optimize, +
-    or:1:C::Optimize:::NF, +
+    or:1:C::Optimize::Optimize:NF, +
     adc:2:C:::::, +
     sbb:3::::::, +
     and:4:C::Optimize:Optimize:Optimize:NF, +
diff --git a/opcodes/i386-tbl.h b/opcodes/i386-tbl.h
index cac034dff89..0bacb1442a2 100644
--- a/opcodes/i386-tbl.h
+++ b/opcodes/i386-tbl.h
@@ -712,7 +712,7 @@ static const insn_template i386_optab[] =
     OPREF_2_60 },
   { MN_or, 0x83, 3, SPACE_MAP4, 1,
     { 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 } },
@@ -726,7 +726,7 @@ static const insn_template i386_optab[] =
     OPREF_2_62 },
   { MN_or, 0x83, 2, SPACE_MAP4, 1,
     { 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 } },
@@ -740,7 +740,7 @@ static const insn_template i386_optab[] =
     OPREF_2_66 },
   { MN_or, 0x80, 3, SPACE_MAP4, 1,
     { 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 } },
@@ -754,7 +754,7 @@ static const insn_template i386_optab[] =
     OPREF_2_8 },
   { MN_or, 0x80, 2, SPACE_MAP4, 1,
     { 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