This is an archive of the discontinued LLVM Phabricator instance.

[GuardWidening] Remove nuw/nsw flags for hoisted instructions
ClosedPublic

Authored by skatkov on May 25 2022, 12:49 AM.

Details

Summary

When we hoist instructions over guard we must clear flags due to these flags
might be implied using this guard, so they make sense only after the guard.

As an example of the bug due to current behavior.
L is known to be in range say [0, 100)
c1 = x u< L
guard (c1)
x1 = add x, 1
c2 = x1 u< L
guard(c2)

basing on guard(c1) we can say that x1 = add nuw nsw x, 1
after guard widening we get
c1 = x u< L
x1 = add nuw nsw x, 1
c2 = x1 u< L
c = and c1, c2
guard(c)

now, basing on fact that x + 1 < L and x >= 0 due to x + 1 is nuw
we can prove that x + 1 u< L implies that x u< L, so we can just remove c1
x1 = add nuw nsw x, 1
c2 = x1 u< L
guard(c2)

But that is not correct due to we will pass x == -1 value.

Diff Detail

Event Timeline

skatkov created this revision.May 25 2022, 12:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 25 2022, 12:49 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
skatkov requested review of this revision.May 25 2022, 12:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 25 2022, 12:49 AM
mkazantsev accepted this revision.May 25 2022, 4:47 AM

Looks good, but please regenerate checks here. It's not clear at all what this test is trying to ensure (e.g. what is wide.chk).

llvm/test/Transforms/GuardWidening/range-check-merging.ll
342

Can you please regenerate all checks in this file with update_test_checks.py and then rebase on top of it? I can't say what it's actually trying to check (and same applies to other tests here).

This revision is now accepted and ready to land.May 25 2022, 4:47 AM
nikic added a subscriber: nikic.May 25 2022, 5:55 AM

Not familiar with this pass, but I suspect the right fix for this might be to use CreateLogicalAnd instead of CreateAnd.

Not familiar with this pass, but I suspect the right fix for this might be to use CreateLogicalAnd instead of CreateAnd.

Could you please elaborate on this. I do not follow what you mean.

skatkov updated this revision to Diff 432198.May 25 2022, 11:20 PM

Looks good, but please regenerate checks here. It's not clear at all what this test is trying to ensure (e.g. what is wide.chk).

Done.

This revision was landed with ongoing or failed builds.May 25 2022, 11:32 PM
This revision was automatically updated to reflect the committed changes.