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

--- Comment #8 from Uroš Bizjak <ubizjak at gmail dot com> ---
The combined v2 patch, with SUBREG issue, that Sashiko found in v1 is at [1].
Sashiko found possible ICE path that v3 fixed [2], but that just motivated
Sashiko to went down rabbit hole and found several deficiencies in the original
pass.

[1] https://gcc.gnu.org/pipermail/gcc-patches/2026-December/742311.html
[2] https://gcc.gnu.org/pipermail/gcc-patches/2026-December/742344.html
[3]
https://gcc.sashiko.dev/#/patchset/CAFULd4bv9O_665wEBrvossVipUuvD-K5ZF13LbdSz1T%2BeMG5%2BQ%40mail.gmail.com

Sashiko found several issues [3] in v3, but most of this is in the original
pass and two of the three hit code the patch only touches at the edges. v2
already exposed a pre-existing issue (but introduced a potential ICE instead of
wrong code, which v3 fixed by rewritten insn insertion approach).

*** This is the answer from Claude analysis: *** 

Follow-up notes on ix86_emit_tls_call (x86_cse) from PR129052

While fixing PR129052, an automated review (Sashiko) raised several issues in
the TLS placement code. I compared them with the original code, and four
weaknesses look to have been there before the PR129052 patch. They are not
fixed by it. I'm reporting them here so the pass's authors can decide what to
do. The reproducers below are untested guesses, and the paragraphs on each
issue say which parts rest on the review's claims and which I checked against
the code.

1. No valid insertion point at the entry block (reproduced)

ix86_emit_tls_call walks up the dominator tree until the live registers
clobbered by the TLS call are dead. Nothing handles the walk reaching the entry
block. In the original, a register defined in the entry block and still live at
its conditional jump gives the wrong placement. After the PR129052 fix it
reached gcc_assert (set_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)) at -O1. The patch
now checks placement before replace_tls_call rewrites anything and keeps the
original calls when no place exists, so this one is handled. The reproducer is
the -O1 -fPIC file with register long r9 asm ("r9"); register long r8 asm
("r8") = b;, where "+r" on r9 keeps it live from entry. It did ICE before the
fix.

2. Conditional-jump handling can loop, and ignores asm goto definitions (not
reproduced)

The original handles JUMP_P by restarting from ix86_get_dominator_for_reg
(FLAGS_REG, bb). That function counts definitions in blocks that dominate bb,
and bb dominates itself. So when FLAGS is set in bb, it returns bb and the loop
never ends. This is the hang behind this PR. The code also assumes any jump is
a conditional jump and never looks at registers an asm goto defines. With the
PR129052 patch the jump ends the scan before note_stores, so a register defined
by the jump itself is not recorded as a local def and the search can still
return bb. A possible reproducer, built from the review's description:

c
__thread int t;

long
f (int c)
{
  register long r8 asm ("r8");
  asm goto ("" : "+r" (r8) ::: out);
  if (c)
    {
      asm volatile ("# a %0" :: "r" (r8));
      return t;
    }
  asm volatile ("# b %0" :: "r" (r8));
  return t + 1;
out:
  return 0;
}

At -O1 -fPIC I'd expect a hang in the TLS placement if the review is right. I
haven't compiled it.

3. GNU2 handler clears FLAGS without checking for an empty live set (not
reproduced)

The updated_gnu2_tls_insns branch clears FLAGS_REG from the live set and does
continue. The set can be empty after that, and the code never checks. The
REG_DEAD loop then places the call after any later instruction with a note,
including notes on pseudos, and if no such instruction follows, the scan ends
at the gcc_assert (!bitmap_empty_p (...)). This is in the original. I couldn't
construct a reproducer, because FLAGS being live at a TLSDESC instruction is
rare in valid RTL. I'm listing it from reading the code.

4. ix86_get_dominator_for_reg can return an empty set (not reproduced)

The function counts only non-artificial, non-clobber definitions in blocks that
dominate the current block. For an incoming argument register, DRAP, or a
register defined only in the arms of an earlier diamond, no such definition
exists, so the set passed to nearest_common_dominator_for_set can be empty. I
haven't read how that function behaves on an empty bitmap, so that part of the
review's claim is unchecked. A possible reproducer:

c
__thread int t;

long
f (long a, long b, int c, int d)
{
  register long r8 asm ("r8");
  if (c)
    r8 = a;
  else
    r8 = b;
  if (d)
    {
      asm volatile ("# a %0" :: "r" (r8));
      return t;
    }
  asm volatile ("# b %0" :: "r" (r8));
  return t + 1;
}

At -O1 -fPIC, the merge block has r8 live-in and not defined, with neither arm
dominating it.

Status

None of 2, 3 or 4 is needed for PR129052. The patch doesn't try to fix them.
I'm raising them as findings for the authors of the original pass.

Reply via email to