[binutils-gdb] x86/APX: optimize {nf} forms of IMUL with all-ones immediate
Jan Beulich
jbeulich@sourceware.org
Mon Nov 9 10:12:38 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=000ea2e5285bbad6baab304172c76aaa748089f2
commit 000ea2e5285bbad6baab304172c76aaa748089f2
Author: Jan Beulich <jbeulich@suse.com>
Date: Mon Nov 9 11:06:54 2026 +0100
x86/APX: optimize {nf} forms of IMUL with all-ones immediate
These can be expressed by NEG.
Diff:
---
gas/config/tc-i386.c | 36 ++++++++++++++++++++++
.../gas/i386/x86-64-apx-nf-optimize-size.d | 16 ++++++++++
gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d | 16 ++++++++++
gas/testsuite/gas/i386/x86-64-apx-nf.s | 17 ++++++++++
4 files changed, 85 insertions(+)
diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index a899229d731..8a886478769 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6380,6 +6380,42 @@ optimize_nf_encoding (void)
copy_operand (2, 1);
}
}
+ else if (i.tm.base_opcode == 0x6b
+ && i.op[0].imms->X_op == O_constant
+ && i.op[0].imms->X_add_number == -1
+ && (!i.types[i.operands - 1].bitfield.word
+ || i.tm.mnem_off == MN_imulzu
+ || i.operands == 2
+ || (i.reg_operands == 2
+ && i.op[1].regs == i.op[2].regs)))
+ {
+ /* Optimize: -O:
+ {nf} imulzuw $~0, %M/m, %N -> {nf} negw %M/m, %N
+ {nf} imulzuw $~0, %N -> {nf} negw %N, %N
+ {nf} imulw $~0, %N, %N -> {nf} negw %N
+ {nf} imulw $~0, %N -> {nf} negw %N
+
+ {nf} imull $~0, ... -> {nf} negl ...
+ {nf} imulq $~0, ... -> {nf} negq ...
+ */
+ i.tm.base_opcode = 0xf7;
+ i.tm.extension_opcode = 3;
+ i.tm.opcode_modifier.operandconstraint = 0;
+
+ copy_operand (0, 1);
+ if (i.operands == 3)
+ copy_operand (1, 2);
+
+ i.imm_operands = 0;
+ if (!i.types[1].bitfield.word)
+ --i.operands;
+ else
+ i.operands = 1 + (i.tm.mnem_off == MN_imulzu);
+ i.reg_operands = i.operands - i.mem_operands;
+
+ if (i.operands == 2)
+ i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
+ }
if (optimize_for_space
&& pp.encoding != encoding_evex
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 56747719b0b..c07838ad23c 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
@@ -1500,6 +1500,22 @@ Disassembly of section \.text:
[ ]*[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 7d 0c f7 da[ ]+\{nf\} neg %dx
+[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c 6b da ff[ ]+\{nf\} imul \$0xffff,%dx,%bx
+[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c f7 db[ ]+\{nf\} neg %bx
+[ ]*[a-f0-9]+:[ ]*62 f4 6d 1c f7 da[ ]+\{nf\} neg %dx,%dx
+[ ]*[a-f0-9]+:[ ]*62 f4 65 1c f7 da[ ]+\{nf\} neg %dx,%bx
+[ ]*[a-f0-9]+:[ ]*62 f4 65 1c f7 db[ ]+\{nf\} neg %bx,%bx
+[ ]*[a-f0-9]+:[ ]*62 f4 7c 0c f7 d9[ ]+\{nf\} neg %ecx
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c f7 d9[ ]+\{nf\} neg %ecx,%edx
+[ ]*[a-f0-9]+:[ ]*62 f4 6c 1c f7 da[ ]+\{nf\} neg %edx,%edx
+[ ]*[a-f0-9]+:[ ]*62 d4 fc 0c f7 d9[ ]+\{nf\} neg %r9
+[ ]*[a-f0-9]+:[ ]*62 d4 84 14 f7 d9[ ]+\{nf\} neg %r9,%r31
+[ ]*[a-f0-9]+:[ ]*62 dc 84 14 f7 df[ ]+\{nf\} neg %r31,%r31
+[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c 6b 10 ff[ ]+\{nf\} imul \$0xffff,\(%rax\),%dx
+[ ]*[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 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 a0565c60708..07b35daf0aa 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf-optimize.d
@@ -1500,6 +1500,22 @@ Disassembly of section \.text:
[ ]*[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 7d 0c f7 da[ ]+\{nf\} neg %dx
+[ ]*[a-f0-9]+:[ ]*62 f4 7d 0c 6b da ff[ ]+\{nf\} imul \$0xffff,%dx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-apx-nf.s
index 446b394913c..e3c9a230ec3 100644
--- a/gas/testsuite/gas/i386/x86-64-apx-nf.s
+++ b/gas/testsuite/gas/i386/x86-64-apx-nf.s
@@ -1455,6 +1455,23 @@ optimize:
{nf} xorq $-1, (%rax)
{nf} xor $-1, (%rax), %r9
+ {nf} imul $-1, %dx
+ {nf} imul $-1, %dx, %bx
+ {nf} imul $-1, %bx, %bx
+ {nf} imulzu $-1, %dx
+ {nf} imulzu $-1, %dx, %bx
+ {nf} imulzu $-1, %bx, %bx
+ {nf} imul $-1, %ecx
+ {nf} imul $-1, %ecx, %edx
+ {nf} imul $-1, %edx, %edx
+ {nf} imul $-1, %r9
+ {nf} imul $-1, %r9, %r31
+ {nf} imul $-1, %r31, %r31
+ {nf} imul $-1, (%rax), %dx
+ {nf} imulzu $-1, (%rax), %bx
+ {nf} imul $-1, (%rax), %ecx
+ {nf} imul $-1, (%rax), %r9
+
.irp dir, l, r
{nf} ro\dir $7, %dl
{nf} ro\dir $7, %dl, %al
More information about the Binutils-cvs
mailing list