https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128874

            Bug ID: 128874
           Summary: [pdp11] mulhisi3 multiplies the wrong register of the
                    output pair
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: romethorstenson at gmail dot com
  Target Milestone: ---

Created attachment 66221
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66221&action=edit
proof-of-concept fix diff

Component: target.
Version: 17.0 (trunk 44f26668c6b3, 2026-11-18; BASE-VER 17.0.0). Known to fail:
8.5.0, 13.3.0, 17.0.
Keywords: wrong-code.
Host and build: x86_64-pc-linux-gnu.
Target: pdp11-aout.

```
With EIS (-m40 or -m45, the default), (long) a * b on 16-bit int operands
computes the wrong product.
The same pattern open-codes every 32-bit multiply, and libgcc's __muldi3 is
built with it, so 32- and 64-bit multiplies are all wrong.

Testcase:

  long f (int a, int b) { return (long) a * b; }

pdp11-aout-gcc -O2 -S gives (13.3.0 and trunk at 44f26668c6b3):

  _f:
          mov     r2,-(sp)
          mov     r3,-(sp)
          mov     06(sp),r3     ; a goes to r3, the odd register of r2:r3
          mul     010(sp),r2    ; multiplies r2, which holds the caller's r2
          mov     r2,r0
          mov     r3,r1
          mov     (sp)+,r3
          mov     (sp)+,r2
          rts     pc

Expected, for example:

  _f:
          mov     02(sp),r0
          mul     04(sp),r0
          rts     pc

It is wrong at -O0, -O1, -O2 and -Os, with -m40 and -m45, with reload and with
-mlra.
On SIMH (11/45 and 11/70), (long) 3 * 5 returns 0, 0x12345678L * 0x0fedcba9L
returns 0xabe40000, and a 64-bit multiply through __muldi3 is wrong too.
Signed edge cases fail too: at -O2 on trunk, (long) -32768 * -32768 returns
0x100e0000, (long) -123 * 456 returns 0xffc6ce20 and (long) 32767 * -2 returns
0x00004038.

The tie is visible in the -mlra reload dump (trunk -O2), where the input is
byte 2 of the output pseudo, i.e. r1 of r0:r1:

  (insn 18 (set (reg:HI 1 r1 [orig:28+2 ] [28]) (mem/c:HI (plus:HI (reg/f:HI 6
sp) (const_int 2)))))
  (insn 12 (set (reg:SI 0 r0 [28])
          (mult:SI (sign_extend:SI (reg:HI 1 r1 [orig:28+2 ] [28]))
                   (sign_extend:SI (mem/c:HI (plus:HI (reg/f:HI 6 sp)
(const_int 4)))))))

With reload, one reload register serves both (reload_reg_rtx (reg:SI 2 r2));
the input goes to r3 and the MUL names r2.

MUL src,Rn with Rn even reads the multiplicand from Rn and writes the product
to Rn:Rn+1.
mulhisi3 (pdp11.md:2077 and :2091) ties the HImode input to the SImode output
with "0".
On a REG_WORDS_BIG_ENDIAN target the matching rule puts a narrower operand in
the last register of the wider one (operands_match_p in reload.cc: "(reg:DI 0)
and (reg:SI 1) will be considered the same register"; LRA does the same through
lowpart_subreg).
So the input lands in Rn+1 and "mul %2,%0" multiplies the stale Rn.

A proof-of-concept fix is attached: stop tying operand 1, earlyclobber the
output, and emit "mov %1,%0; mul %2,%0".
With it, the testcase compiles to the expected code above.
Our multiply checks, including the three signed edge cases above, pass at all
-m40/-m45 flag combinations, with reload and with -mlra, on trunk and on 13.3.0
(edge cases checked on trunk only).
A 3,740-check multiply/divide test shows all multiply mismatches gone.
It has not been through the testsuite.

GCC 8.5.0 also miscompiles this testcase (at -O2 it returns 0), so this does
not look like a regression.
```

Reply via email to