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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=120434
                 CC|                            |jakub at redhat dot com
           Assignee|unassigned at gcc dot gnu.org      |law at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #8 from Jeffrey A. Law <law at gcc dot gnu.org> ---
So my tester started flagging bootstrap comparison failures on the SH port as
well a while ago.  I'd been hoping it'd magically go away, but I couldn't
ignore anymore as my worry was it was a sign of a generic bug...  So I finally
dove in.



First the SH port is being a bit naughty.  If we look in prepare_operands:

            {
              rtx r0_rtx = gen_rtx_REG (GET_MODE (XEXP (adr, 1)), R0_REG);
              emit_move_insn (r0_rtx, XEXP (adr, 1));
              XEXP (adr, 1) = r0_rtx;
            }

Not a fan of that.  THough we know we're dealing a PLUS as the toplevel code in
ADR, so it's not going to be shared.  Or at least it shouldn't be.  So I'd
consider the SH port OK here.

Now if we look at this code in expr.cc:

          /* If argument is known to be positive when interpreted
             as signed, we can expand it as both sign and zero
             extension.  Choose the cheaper sequence in that case.  */
          bool speed_p = optimize_insn_for_speed_p ();
          rtx uns_ret = NULL_RTX, sgn_ret = NULL_RTX;
          do_pending_stack_adjust ();
          start_sequence ();
          if (target == NULL_RTX)
            uns_ret = convert_to_mode (mode, op0, 1);
          else
            convert_move (target, op0, 1);
          rtx_insn *uns_insns = end_sequence ();
          start_sequence ();
          if (target == NULL_RTX)
            sgn_ret = convert_to_mode (mode, op0, 0);
          else
            convert_move (target, op0, 0);
          rtx_insn *sgn_insns = end_sequence ();

The warning lights should start flashing now.

We generate two sequences and select one based on costs.

op0 looks like:

(mem/u:QI (plus:SI (reg/f:SI 167)
        (subreg:SI (reg:DI 169 [ _2 ]) 0)) [0 ashiftrt_insns[_2]+0 S1 A8])

During generation of the unsigned sequence we run through that SH code noted
above and op0 magically turns into:

(mem/u:QI (plus:SI (reg/f:SI 167)
        (reg:SI 0 r0)) [0 ashiftrt_insns[_2]+0 S1 A8])


uns_insns looks reasonable:

(insn 11 0 12 (set (reg:SI 0 r0)
        (subreg:SI (reg:DI 169 [ _2 ]) 0)) "t.ii":8:58 -1
     (nil))

(insn 12 11 13 (set (reg:QI 173)
        (mem/u:QI (plus:SI (reg/f:SI 167)
                (reg:SI 0 r0)) [0 ashiftrt_insns[_2]+0 S1 A8])) "t.ii":8:58 -1
     (nil))

(insn 13 12 0 (set (reg:SI 172)
        (zero_extend:SI (reg:QI 173))) "t.ii":8:58 -1
     (nil))


So far so good, but of course we take the modified op0 and build a signed
sequence too....  And since op0 has been modified we get a different sequence
that looks like this:


(insn 14 0 0 (set (reg:SI 174)
        (sign_extend:SI (mem/u:QI (plus:SI (reg/f:SI 167)
                    (reg:SI 0 r0)) [0 ashiftrt_insns[_2]+0 S1 A8])))
"t.ii":8:58 -1
     (nil))



Naturally that sequence is going to be lower cost.  But note because the SH
backend modified that chunk of RTL we've got a use of r0 without it being
defined in the sequence.

With that sequence being lower cost, we select it and now we've got
pseudo-random behavior based on whatever value is lying around in R0.

In the case I chased down this happens in the right shift emission/costing code
in sh.cc causing the stage2 compiler to generate a different shift sequence
than the stage1 compiler.  Boom, comparison failures.


The code in question in expr.cc comes from:

commit 2e4688a7202d73baeb4de18ca4591e6b0985f4a4
Author: Jakub Jelinek <[email protected]>
Date:   Tue Jun 10 20:06:14 2025 +0200

    expand: Use less costly from sign and zero extensions for values where
value range says they don't have MSB set [PR120434]

    On top of the just posted patch, the following patch attempts to
    use value range to see if MSB is known to be false and for scalar integral
    extension in that case tries to expand both sign and zero extension and
    chooses based on RTX costs the cheaper one (if the costs are the same
    uses what it used before, TYPE_UNSIGNED (TREE_TYPE (treeop0)) based.
    [ ... ]

A well-placed copy_rtx call in this expr.cc code will fix this instance of the
problem.  My concern is this kind of goof could be common and we could be
playing wack-a-mole on them.

We could be somewhat defensive here and make the SH port more robust against
this kind of problem with an additional copy_rtx.  But the SH is relying on
well defined structure sharing assumptions and the defensive copy_rtx call
would likely generate a fair amount of garbage RTL.  So I'm not going to
propose that.

On a humorous note, Claude actually got really close to nailing this problem
when analyzing the dumps after compiling the two key functions in sh.cc.    It
pointed out the sh.cc code above as wrong and likely would have shoved in a
copy_rtx call in the SH backend.  That would have looked plausible, but would
have ultimately been wrong.

I had mentally dismissed Claude's guess because PLUS isn't shared, so the port
wasn't doing anything wrong.  Had a listened to claude more closely I probably
wouldn't have saved meaningful time though.  It's unlikely I would have
stumbled into the true source of the sharing problem without a massively
reduced testcase which made it painfully obvious after about 15 minutes in the
debugger.  Overall the debug time was on the order of 2 days.

Reply via email to