x86: suppress LEA optimization in a specific 16-bit case
authorJan Beulich <jbeulich@suse.com>
Thu, 10 Jun 2021 10:39:08 +0000 (12:39 +0200)
committerJan Beulich <jbeulich@suse.com>
Thu, 10 Jun 2021 10:39:08 +0000 (12:39 +0200)
In 16-bit mode a 16-bit address size LEA with a 16-bit displacement and
a 32-bit destination is shorter to encode than the corresponding MOV.
Commit fe134c656991 ("x86: optimize LEA")'s promise was to only do the
transformation when the encoding size wouldn't grow, i.e. it did go a
little too far. Restrict this specific case of the transformation to
-O2.

gas/ChangeLog
gas/config/tc-i386.c
gas/testsuite/gas/i386/i386.exp
gas/testsuite/gas/i386/lea16-optimize.d
gas/testsuite/gas/i386/lea16-optimize2.d [new file with mode: 0644]

index f07ece7ccac66ad624608de8bf6828f1cd35939f..7133c3742351fd479597f623c6e0fa880864f4ec 100644 (file)
@@ -1,3 +1,11 @@
+2021-06-10  Jan Beulich  <jbeulich@suse.com>
+
+       * config/tc-i386.c (optimize_encoding): Suppress LEA conversion
+       when it would grow code size in 16-bit mode.
+       * testsuite/gas/i386/lea16-optimize.d: Adjust expectations.
+       * testsuite/gas/i386/lea16-optimize2.d: New.
+       * testsuite/gas/i386/i386.exp: Run new test.
+
 2021-06-08  Jan Beulich  <jbeulich@suse.com>
 
        * testsuite/gas/i386/quoted.d, testsuite/gas/i386/wrap32-text.d:
index 80b70986fa5a49e130b339a675944ea2a721fda8..47c12b97651d059a301c03b5503da75b1e368823 100644 (file)
@@ -4140,6 +4140,13 @@ optimize_encoding (void)
                           ? i.op[1].regs->reg_type.bitfield.dword
                           : i.op[1].regs->reg_type.bitfield.word)))
            return;
+         /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
+            destination is going to grow encoding size.  */
+         else if (flag_code == CODE_16BIT
+                  && (optimize <= 1 || optimize_for_space)
+                  && !i.prefix[ADDR_PREFIX]
+                  && i.op[1].regs->reg_type.bitfield.dword)
+           return;
          else
            {
              i.tm.base_opcode = 0xb8;
index ff88bae92b2d0d01e2eb22f41a34b16ad328d825..cf215117cadb9b6ba16dd6b499f4b9fa264d49af 100644 (file)
@@ -544,6 +544,7 @@ if [gas_32_check] then {
     run_list_test "optimize-7" "-I${srcdir}/$subdir -march=+noavx2 -al"
     run_dump_test "lea-optimize"
     run_dump_test "lea16-optimize"
+    run_dump_test "lea16-optimize2"
     run_dump_test "align-branch-1a"
     run_dump_test "align-branch-1b"
     run_dump_test "align-branch-1c"
index 3c43b6c94386a6e643d7163b054180ecd66d0fad..be3075886a8de9a452656f5ac66099fa58adf7fb 100644 (file)
@@ -31,9 +31,9 @@ Disassembly of section .text:
 [      ]*[0-9a-f]+:[   ]+8b f6[        ]+mov[  ]+%si,%si
 [      ]*[0-9a-f]+:[   ]+66 8b c9[     ]+mov[  ]+%ecx,%ecx
 [      ]*[0-9a-f]+:[   ]+66 8b c1[     ]+mov[  ]+%ecx,%eax
-[      ]*[0-9a-f]+:[   ]+66 b8 01 00 00 00[    ]+mov[  ]+\$0x1,%eax
+[      ]*[0-9a-f]+:[   ]+66 8d 06 01 00[       ]+lea[  ]+0x1,%eax
 [      ]*[0-9a-f]+:[   ]+b8 02 00[     ]+mov[  ]+\$0x2,%ax
-[      ]*[0-9a-f]+:[   ]+66 b8 ff ff 00 00[    ]+mov[  ]+\$0xffff,%eax
+[      ]*[0-9a-f]+:[   ]+66 8d 06 ff ff[       ]+lea[  ]+-0x1,%eax
 [      ]*[0-9a-f]+:[   ]+b8 fe ff[     ]+mov[  ]+\$0xfffe,%ax
 [      ]*[0-9a-f]+:[   ]+66 b8 01 00 00 00[    ]+mov[  ]+\$0x1,%eax
 [      ]*[0-9a-f]+:[   ]+b8 02 00[     ]+mov[  ]+\$0x2,%ax
@@ -43,7 +43,7 @@ Disassembly of section .text:
 [      ]*[0-9a-f]+:[   ]+b8 00 00[     ]+mov[  ]+\$0x0,%ax
 [      ]*[0-9a-f]+:[   ]+66 b8 00 00 00 00[    ]+mov[  ]+\$0x0,%eax
 [      ]*[0-9a-f]+:[   ]+67 8d 05 00 00 00 00[         ]+addr32 lea[   ]+0x0,%ax
-[      ]*[0-9a-f]+:[   ]+66 b8 00 00 00 00[    ]+mov[  ]+\$0x0,%eax
+[      ]*[0-9a-f]+:[   ]+66 8d 06 00 00[       ]+lea[  ]+0x0,%eax
 [      ]*[0-9a-f]+:[   ]+b8 00 00[     ]+mov[  ]+\$0x0,%ax
 [      ]*[0-9a-f]+:[   ]+8d 47 ff[     ]+lea[  ]+-0x1\(%bx\),%ax
 [      ]*[0-9a-f]+:[   ]+8d 87 01 00[  ]+lea[  ]+0x1\(%bx\),%ax
diff --git a/gas/testsuite/gas/i386/lea16-optimize2.d b/gas/testsuite/gas/i386/lea16-optimize2.d
new file mode 100644 (file)
index 0000000..d95f9ac
--- /dev/null
@@ -0,0 +1,50 @@
+#as: -O2 -q -I${srcdir}/$subdir
+#objdump: -dw -Mi8086
+#name: i386 16-bit LEA optimizations at -O2
+#source: lea16.s
+
+.*: +file format .*
+
+Disassembly of section .text:
+
+0+ <start>:
+[      ]*[0-9a-f]+:[   ]+67 66 8d 04 08[       ]+lea[  ]+\(%eax,%ecx(,1)?\),%eax
+[      ]*[0-9a-f]+:[   ]+67 66 8d 04 08[       ]+lea[  ]+\(%eax,%ecx(,1)?\),%eax
+[      ]*[0-9a-f]+:[   ]+67 66 8d 48 01[       ]+lea[  ]+0x1\(%eax\),%ecx
+[      ]*[0-9a-f]+:[   ]+67 66 8d 88 00 00 00 00[      ]+lea[  ]+0x0\(%eax\),%ecx
+[      ]*[0-9a-f]+:[   ]+67 66 8d 0c 25 00 00 00 00[   ]+addr32 lea[   ]+0x0,%ecx
+[      ]*[0-9a-f]+:[   ]+67 66 8d 04 00[       ]+lea[  ]+\(%eax,%eax(,1)?\),%eax
+[      ]*[0-9a-f]+:[   ]+67 66 8d 04 45 00 00 00 00[   ]+lea[  ]+0x0\(,%eax,2\),%eax
+[      ]*[0-9a-f]+:[   ]+67 66 8d 04 25 00 00 00 00[   ]+addr32 lea[   ]+0x0,%eax
+[      ]*[0-9a-f]+:[   ]+66 8d 00[     ]+lea[  ]+\(%bx,%si\),%eax
+[      ]*[0-9a-f]+:[   ]+66 8b c0[     ]+mov[  ]+%eax,%eax
+[      ]*[0-9a-f]+:[   ]+66 8b c8[     ]+mov[  ]+%eax,%ecx
+[      ]*[0-9a-f]+:[   ]+66 8b c8[     ]+mov[  ]+%eax,%ecx
+[      ]*[0-9a-f]+:[   ]+66 8b c8[     ]+mov[  ]+%eax,%ecx
+[      ]*[0-9a-f]+:[   ]+66 8d 04[     ]+lea[  ]+\(%si\),%eax
+[      ]*[0-9a-f]+:[   ]+66 8d 34[     ]+lea[  ]+\(%si\),%esi
+[      ]*[0-9a-f]+:[   ]+66 8d 04[     ]+lea[  ]+\(%si\),%eax
+[      ]*[0-9a-f]+:[   ]+8b c0[        ]+mov[  ]+%ax,%ax
+[      ]*[0-9a-f]+:[   ]+8b c8[        ]+mov[  ]+%ax,%cx
+[      ]*[0-9a-f]+:[   ]+8b c8[        ]+mov[  ]+%ax,%cx
+[      ]*[0-9a-f]+:[   ]+8b c6[        ]+mov[  ]+%si,%ax
+[      ]*[0-9a-f]+:[   ]+8b f6[        ]+mov[  ]+%si,%si
+[      ]*[0-9a-f]+:[   ]+66 8b c9[     ]+mov[  ]+%ecx,%ecx
+[      ]*[0-9a-f]+:[   ]+66 8b c1[     ]+mov[  ]+%ecx,%eax
+[      ]*[0-9a-f]+:[   ]+66 b8 01 00 00 00[    ]+mov[  ]+\$0x1,%eax
+[      ]*[0-9a-f]+:[   ]+b8 02 00[     ]+mov[  ]+\$0x2,%ax
+[      ]*[0-9a-f]+:[   ]+66 b8 ff ff 00 00[    ]+mov[  ]+\$0xffff,%eax
+[      ]*[0-9a-f]+:[   ]+b8 fe ff[     ]+mov[  ]+\$0xfffe,%ax
+[      ]*[0-9a-f]+:[   ]+66 b8 01 00 00 00[    ]+mov[  ]+\$0x1,%eax
+[      ]*[0-9a-f]+:[   ]+b8 02 00[     ]+mov[  ]+\$0x2,%ax
+[      ]*[0-9a-f]+:[   ]+66 b8 ff ff ff ff[    ]+mov[  ]+\$0xffffffff,%eax
+[      ]*[0-9a-f]+:[   ]+b8 fe ff[     ]+mov[  ]+\$0xfffe,%ax
+[      ]*[0-9a-f]+:[   ]+66 8d 06 00 00[       ]+lea[  ]+0x0,%eax
+[      ]*[0-9a-f]+:[   ]+b8 00 00[     ]+mov[  ]+\$0x0,%ax
+[      ]*[0-9a-f]+:[   ]+66 b8 00 00 00 00[    ]+mov[  ]+\$0x0,%eax
+[      ]*[0-9a-f]+:[   ]+67 8d 05 00 00 00 00[         ]+addr32 lea[   ]+0x0,%ax
+[      ]*[0-9a-f]+:[   ]+66 b8 00 00 00 00[    ]+mov[  ]+\$0x0,%eax
+[      ]*[0-9a-f]+:[   ]+b8 00 00[     ]+mov[  ]+\$0x0,%ax
+[      ]*[0-9a-f]+:[   ]+8d 47 ff[     ]+lea[  ]+-0x1\(%bx\),%ax
+[      ]*[0-9a-f]+:[   ]+8d 87 01 00[  ]+lea[  ]+0x1\(%bx\),%ax
+#pass