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

            Bug ID: 117927
           Summary: Invalid rotate optimization
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
  Target Milestone: ---

The text about rotation in tree.def is not entirely clear, but the conclusion
in PR108440 is that it has the same restriction as shift: it is undefined
behavior to rotate more than the bit width.

    "Now, it is unclear if we in the middle-end treat rotates with rotate count
    equal or larger than precision as UB or not, unlike shifts there are less
    reasons to do so, but e.g. expansion of X r<< Y if there is no rotate optab
    for the mode is emitted as (X << Y) | (((unsigned) X) >> ((-Y) & (B - 1)))
    and so with UB on Y == B."

This implies that the optimization implemented in PR109906 is invalid because
(for a 32-bit type)

  x r<< (32 - y)

is valid for y = 32, while the optimized

  x r>> y

is UB.

Reply via email to