This is an archive of the discontinued LLVM Phabricator instance.

[TypePromotion] Fix another case for sext vs zext in promoted constant.
ClosedPublic

Authored by craig.topper on May 15 2022, 5:55 PM.

Details

Summary

If the SafeWrap operation is a subtract, we negated the constant
to treat the subtract as an addition. The sext was based on the
operation being addition. So we really need to do (neg (sext (neg C)))
when promoting the constant. This is equivalent to (sext C) for
every value of C except the min signed value. For min signed value
we need to do (zext C) instead.

It turns out that the (neg C) is always less than or equal to 0. So the
original C had to be 0, positive, or min signed value. We know we need
to zext min signed value, but we can also zext 0 or positive and get the
same result as sign extending them. So we can always use a zero extend
for subtract.

Fixes PR55490.

Diff Detail

Event Timeline

craig.topper created this revision.May 15 2022, 5:55 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 15 2022, 5:55 PM
craig.topper requested review of this revision.May 15 2022, 5:55 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 15 2022, 5:55 PM

If I haven't got this wrong, this says we could just always be zexting the constant from a sub:
https://alive2.llvm.org/ce/z/kTAyt2
https://alive2.llvm.org/ce/z/a6HHq4
That sounds like it would make simpler constants too, although I'm not sure there are any tests that show it.

What do you think? Otherwise I believe this is correct.

If I haven't got this wrong, this says we could just always be zexting the constant from a sub:
https://alive2.llvm.org/ce/z/kTAyt2
https://alive2.llvm.org/ce/z/a6HHq4
That sounds like it would make simpler constants too, although I'm not sure there are any tests that show it.

What do you think? Otherwise I believe this is correct.

Since the pre-condition is -C1 <=s 0, then for all values of C1 except 0x80, the sign bit of C1 is 0. And the 0x80 case is the bug that requires zext. 0x80 can only occur for one of the two proofs since the first proof requires -C1 >s C2 which is never true for C1==0x80. I think all this means that yes we could use zext for sub, but I don't think it will generate better constants.

Use zext for Subtract always.

craig.topper edited the summary of this revision. (Show Details)May 19 2022, 9:54 PM
dmgreen accepted this revision.May 20 2022, 12:10 AM

OK thanks. LGTM

This revision is now accepted and ready to land.May 20 2022, 12:10 AM
This revision was landed with ongoing or failed builds.May 20 2022, 9:30 AM
This revision was automatically updated to reflect the committed changes.