This is an archive of the discontinued LLVM Phabricator instance.

[IRCE] Fix buggy behavior in Clamp
ClosedPublic

Authored by mkazantsev on Aug 18 2017, 5:39 AM.

Details

Summary

Clamp function was too optimistic when choosing signed or unsigned min/max function for calculations.
In fact, !IsSignedPredicate guarantees us that Smallest and Greatest can be compared safely using unsigned
predicates, but we did not check this for S which can in theory be negative.

This patch makes Clamp use signed min/max for cases when it fails to prove S being non-negative,
and it adds a test where such situation may lead to incorrect conditions calculation.

Diff Detail

Repository
rL LLVM

Event Timeline

mkazantsev created this revision.Aug 18 2017, 5:39 AM
anna accepted this revision.Aug 18 2017, 6:12 AM

LGTM w/comment.

lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
1088 ↗(On Diff #111657)

I think there is just one missed case of checking isKnownNonNegative right?

Just looked through IRCE code and we correctly check it for choosing the right predicate: ULT or SLT.

test/Transforms/IRCE/clamp.ll
9 ↗(On Diff #111657)

Please give canonical names to the basic blocks (i.e. entry, header etc).

This revision is now accepted and ready to land.Aug 18 2017, 6:12 AM
mkazantsev marked an inline comment as done.

Renamed test's blocks.

lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
1088 ↗(On Diff #111657)

Yes, for other values we know that they are non-negative because they are start and final values of the IV which is checked by unsigned condition. For S, we didn't check it.

This revision was automatically updated to reflect the committed changes.