https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129216
Bug ID: 129216
Summary: [17 Regression] wrong code from combine substituting a
constant comparison into its CC user since r17-6822
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: ishevlyakov at gcc dot gnu.org
CC: ishevlyakov at gcc dot gnu.org, pinskia at gcc dot gnu.org
Target Milestone: ---
Since r17-6822-g032b844694e5 (PR126556), when simplify_set in combine
finds that the comparison setting a CC register has a constant result, it
substitutes the constant into the CC user (other_insn) with subst. That
runs combine's whole simplifier on other_insn (combine_simplify_rtx,
simplify_set, force_to_mode, nonzero_bits through reg_stat), but reg_stat
describes the state at i3, not at other_insn: sets in i3 and in the insns
between i3 and other_insn have not been recorded yet. So a register that
is set to 0 before i2 and set again between i3 and the CC user is folded
to 0 in the CC user.
The attached x86_64 RTL frontend testcase aborts at -O2: f (1, 7) returns
0 instead of 7. combine dump:
modifying other_insn 16: r104:SI=0
GCC 16 gives "16: r104:SI=r102:SI" and the right result.
The same nesting can also go wrong for a PARALLEL other_insn that uses the
CC register in one element and sets it with a COMPARE in a later one: subst
walks the PARALLEL backwards, so the nested simplify_set of the later
element finds the not yet replaced CC use in the same insn (find_single_use
starts at subst_insn, i.e. i3) and rewrites it with its own comparison.
Both were found by the Sashiko review of the PR126556 patch. No C
testcase is known.
The PR126556 ICE only needs the extension of the comparison folded while
the mode of its operand is still known, which simplify_replace_rtx does
without combine's simplifier; I'm testing a patch that uses it instead of
subst.