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

Message ID ef9251a1-5192-459d-840a-9297350abfd3@suse.com
State New
Headers
Series x86/APX: more {nf} optimizations |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Jan Beulich Oct. 30, 2026, 10:49 a.m. UTC
  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.
  

Patch

--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6400,6 +6400,58 @@  optimize_nf_encoding (void)
       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;
+    }
   else if (i.tm.base_opcode == 0xc0
 	   && i.op[0].imms->X_op == O_constant
 	   && i.op[0].imms->X_add_number
--- 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
--- 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 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\{nf\} ror \$1,%dx
--- 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
--- 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, +