This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] recognize sub X, (X % Y) as not overflowing
ClosedPublic

Authored by spatel on May 12 2022, 1:29 PM.

Details

Summary

I fixed some poison-safety violations on related patterns in InstCombine and noticed that we missed adding nsw/nuw on them, so this adds clauses to the underlying analysis for that.
We need the undef input restriction to make this safe according to Alive2:
https://alive2.llvm.org/ce/z/48g9K8

Diff Detail

Event Timeline

spatel created this revision.May 12 2022, 1:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2022, 1:29 PM
spatel requested review of this revision.May 12 2022, 1:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2022, 1:29 PM
nikic accepted this revision.May 13 2022, 2:07 AM

LGTM

There are a lot more cases we could handle here (at least for the unsigned case) -- in particular a lot of the cases starting at https://github.com/llvm/llvm-project/blob/6d53d35efd3b876d2cc70a776fb168d6debfeba5/llvm/lib/Analysis/InstructionSimplify.cpp#L3023 are relevant. We have quite a few operations (urem, udiv, shr, and) that are non-increasing functions, often of both operands. Though I'm also not sure whether its worthwhile to try particularly hard to infer nuw here.

This revision is now accepted and ready to land.May 13 2022, 2:07 AM

LGTM

There are a lot more cases we could handle here (at least for the unsigned case) -- in particular a lot of the cases starting at https://github.com/llvm/llvm-project/blob/6d53d35efd3b876d2cc70a776fb168d6debfeba5/llvm/lib/Analysis/InstructionSimplify.cpp#L3023 are relevant. We have quite a few operations (urem, udiv, shr, and) that are non-increasing functions, often of both operands. Though I'm also not sure whether its worthwhile to try particularly hard to infer nuw here.

Yes, we also miss cases where an existing nuw could be used to simplify patterns with those ops:
https://alive2.llvm.org/ce/z/UFHE5_

I'll put a TODO comment in there.

This revision was landed with ongoing or failed builds.May 13 2022, 7:15 AM
This revision was automatically updated to reflect the committed changes.