[binutils-gdb] x86/APX: optimize {nf} forms of XOR with all-ones immediate

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


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

commit 967665848fb8b219044a07a4ed11d56a76ac2ad1
Author: Jan Beulich <jbeulich@suse.com>
Date:   Mon Nov 9 11:06:29 2026 +0100

    x86/APX: optimize {nf} forms of XOR with all-ones immediate
    
    These can be expressed by NOT, in many cases even by its legacy encoding.

Diff:
---
 gas/config/tc-i386.c                               | 46 ++++++++++++++++++++++
 .../gas/i386/x86-64-apx-nf-optimize-size.d         | 20 ++++++++++
 gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d    | 20 ++++++++++
 gas/testsuite/gas/i386/x86-64-apx-nf.s             | 21 ++++++++++
 opcodes/i386-opc.tbl                               |  2 +-
 opcodes/i386-tbl.h                                 |  8 ++--
 6 files changed, 112 insertions(+), 5 deletions(-)

diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index c4b21c38d6d..a899229d731 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6247,6 +6247,52 @@ optimize_nf_encoding (void)
       i.imm_operands = 0;
       --i.operands;
     }
+  else if ((i.tm.base_opcode | 3) == 0x83
+      && i.tm.extension_opcode == 6
+      && i.op[0].imms->X_op == O_constant
+      && (i.op[0].imms->X_add_number == -1
+	  /* See comment above.  */
+	  || (i.op[0].imms->X_add_number == 0xff
+	      && (i.suffix == BYTE_MNEM_SUFFIX
+		  || i.types[i.operands - 1].bitfield.byte))))
+    {
+      /* Optimize: -O:
+	   {nf} xorb $~0, ...       -> notb ...
+	   {nf} xorb $0xff, ...     -> notb ...
+
+	   {nf} xorw $~0, ...       -> notw ...
+
+	   {nf} xorl $~0, %N, %N    -> notl %N
+	   {nf} xorl $~0, %M/m, %N  -> notl %M/m, %N
+	   {nf} xorl $~0, %N/m      -> notl %N/m
+
+	   {nf} xorq $~0, %N, %N    -> notq %N
+	   {nf} xorq $~0, %M/m, %N  -> notq %M/m, %N
+	   {nf} xorq $~0, %N/m      -> notq %N/m
+       */
+      i.tm.base_opcode = 0xf6;
+      i.tm.extension_opcode = 2;
+      i.tm.opcode_modifier.w = 1;
+      pp.has_nf = false;
+
+      copy_operand (0, 1);
+      copy_operand (1, 2);
+
+      i.imm_operands = 0;
+      if (i.operands == 2
+	  || (i.reg_operands == 2
+	      && (i.types[1].bitfield.dword || i.types[1].bitfield.qword)
+	      && i.op[0].regs == i.op[1].regs))
+	{
+       i.tm.opcode_space = SPACE_BASE;
+       i.tm.opcode_modifier.evex = 0;
+       i.tm.opcode_modifier.vexvvvv = 0;
+	  i.operands = 1;
+	}
+      else
+	i.operands = 2;
+      i.reg_operands = i.operands - i.mem_operands;
+    }
   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 49a042bda89..56747719b0b 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
@@ -1480,6 +1480,26 @@ Disassembly of section \.text:
 [ 	]*[a-f0-9]+:[ 	]*62 f4 74 1c ff 00[ 	]+\{nf\} inc \(%rax\),%ecx
 [ 	]*[a-f0-9]+:[ 	]*62 f4 fc 0c ff 00[ 	]+\{nf\} incq \(%rax\)
 [ 	]*[a-f0-9]+:[ 	]*62 f4 b4 1c ff 00[ 	]+\{nf\} inc \(%rax\),%r9
+[ 	]*[a-f0-9]+:[ 	]*f6 d3[ 	]+not    %bl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 6c 18 f6 d3[ 	]+not    %bl,%dl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 6c 18 f6 d2[ 	]+not    %dl,%dl
+[ 	]*[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x86-64-apx-nf-optimize.d b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
index 9a2bc31ccff..a0565c60708 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
@@ -1480,6 +1480,26 @@ Disassembly of section \.text:
 [ 	]*[a-f0-9]+:[ 	]*62 f4 74 1c ff 00[ 	]+\{nf\} inc \(%rax\),%ecx
 [ 	]*[a-f0-9]+:[ 	]*62 f4 fc 0c ff 00[ 	]+\{nf\} incq \(%rax\)
 [ 	]*[a-f0-9]+:[ 	]*62 f4 b4 1c ff 00[ 	]+\{nf\} inc \(%rax\),%r9
+[ 	]*[a-f0-9]+:[ 	]*f6 d3[ 	]+not    %bl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 6c 18 f6 d3[ 	]+not    %bl,%dl
+[ 	]*[a-f0-9]+:[ 	]*62 f4 6c 18 f6 d2[ 	]+not    %dl,%dl
+[ 	]*[a-f0-9]+:[ 	]*66 f7 d2[ 	]+not    %dx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7d 18 f7 d2[ 	]+not    %dx,%ax
+[ 	]*[a-f0-9]+:[ 	]*62 f4 7d 18 f7 d0[ 	]+not    %ax,%ax
+[ 	]*[a-f0-9]+:[ 	]*f7 d1[ 	]+not    %ecx
+[ 	]*[a-f0-9]+:[ 	]*62 f4 6c 18 f7 d1[ 	]+not    %ecx,%edx
+[ 	]*[a-f0-9]+:[ 	]*f7 d2[ 	]+not    %edx
+[ 	]*[a-f0-9]+:[ 	]*49 f7 d1[ 	]+not    %r9
+[ 	]*[a-f0-9]+:[ 	]*62 d4 84 10 f7 d1[ 	]+not    %r9,%r31
+[ 	]*[a-f0-9]+:[ 	]*d5 19 f7 d7[ 	]+not    %r31
+[ 	]*[a-f0-9]+:[ 	]*f6 10[ 	]+notb   \(%rax\)
+[ 	]*[a-f0-9]+:[ 	]*62 f4 64 18 f6 10[ 	]+not    \(%rax\),%bl
+[ 	]*[a-f0-9]+:[ 	]*66 f7 10[ 	]+notw   \(%rax\)
+[ 	]*[a-f0-9]+:[ 	]*62 f4 6d 18 f7 10[ 	]+not    \(%rax\),%dx
+[ 	]*[a-f0-9]+:[ 	]*f7 10[ 	]+notl   \(%rax\)
+[ 	]*[a-f0-9]+:[ 	]*62 f4 74 18 f7 10[ 	]+not    \(%rax\),%ecx
+[ 	]*[a-f0-9]+:[ 	]*48 f7 10[ 	]+notq   \(%rax\)
+[ 	]*[a-f0-9]+:[ 	]*62 f4 b4 18 f7 10[ 	]+not    \(%rax\),%r9
 [ 	]*[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 fbd4cadd983..446b394913c 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf.s
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf.s
@@ -1434,6 +1434,27 @@ optimize:
 	{nf}	\op	$-1, (%rax), %r9
 	.endr
 
+	{nf}	xor	$0xff, %bl
+	{nf}	xor	$-1, %bl, %dl
+	{nf}	xor	$-1, %dl, %dl
+	{nf}	xor	$0xffff, %dx
+	{nf}	xor	$-1, %dx, %ax
+	{nf}	xor	$-1, %ax, %ax
+	{nf}	xor	$0xffffffff, %ecx
+	{nf}	xor	$-1, %ecx, %edx
+	{nf}	xor	$-1, %edx, %edx
+	{nf}	xor	$-1, %r9
+	{nf}	xor	$-1, %r9, %r31
+	{nf}	xor	$-1, %r31, %r31
+	{nf}	xorb	$0xff, (%rax)
+	{nf}	xor	$-1, (%rax), %bl
+	{nf}	xorw	$0xffff, (%rax)
+	{nf}	xor	$-1, (%rax), %dx
+	{nf}	xorl	$0xffffffff, (%rax)
+	{nf}	xor	$-1, (%rax), %ecx
+	{nf}	xorq	$-1, (%rax)
+	{nf}	xor	$-1, (%rax), %r9
+
 	.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 e3844443afa..70dedf78caf 100644
--- a/opcodes/i386-opc.tbl
+++ b/opcodes/i386-opc.tbl
@@ -324,7 +324,7 @@ sti, 0xfb, 0, NoSuf, {}
     sbb:3::::::, +
     and:4:C::Optimize:Optimize::NF, +
     sub:5::Optimize:::Optimize:NF, +
-    xor:6:C:Optimize::::NF>
+    xor:6:C:Optimize:::Optimize:NF>
 
 <alu2>, <alu2:opc> << 3, APX_NDD, D|<alu2:c>|W|CheckOperandSize|Modrm|No_sSuf|DstVVVV|EVexMap4|<alu2:nf>|<alu2:optz>, { Reg8|Reg16|Reg32|Reg64, Reg8|Reg16|Reg32|Reg64|Unspecified|BaseIndex, Reg8|Reg16|Reg32|Reg64 }
 <alu2>, <alu2:opc> << 3, 0, D|W|CheckOperandSize|Modrm|No_sSuf|HLEPrefixLock|<alu2:optz>|<alu2:optt>, { Reg8|Reg16|Reg32|Reg64, Reg8|Reg16|Reg32|Reg64|Unspecified|BaseIndex }
diff --git a/opcodes/i386-tbl.h b/opcodes/i386-tbl.h
index 63c778928fe..cfb5e348aff 100644
--- a/opcodes/i386-tbl.h
+++ b/opcodes/i386-tbl.h
@@ -1062,7 +1062,7 @@ static const insn_template i386_optab[] =
     OPREF_2_60 },
   { MN_xor, 0x83, 3, SPACE_MAP4, 6,
     { 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 } },
@@ -1076,7 +1076,7 @@ static const insn_template i386_optab[] =
     OPREF_2_62 },
   { MN_xor, 0x83, 2, SPACE_MAP4, 6,
     { 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 } },
@@ -1090,7 +1090,7 @@ static const insn_template i386_optab[] =
     OPREF_2_66 },
   { MN_xor, 0x80, 3, SPACE_MAP4, 6,
     { 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 } },
@@ -1104,7 +1104,7 @@ static const insn_template i386_optab[] =
     OPREF_2_8 },
   { MN_xor, 0x80, 2, SPACE_MAP4, 6,
     { 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