This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Improve ValueTracking on left shift with nsw flag
ClosedPublic

Authored by lihuang on Aug 8 2016, 10:44 PM.

Details

Summary

Improve ValueTracking on left shift with nsw flag

If a left shift has nsw flag, it produces a poison value if it shifts out any bits that disagree with the resultant sign bit. So we can assume the result has the same sign bit as the first operand.

This patch is separated from D18777.

Diff Detail

Event Timeline

lihuang updated this revision to Diff 67285.Aug 8 2016, 10:44 PM
lihuang retitled this revision from to [ValueTracking] Improve ValueTracking on left shift with nsw flag.
lihuang updated this object.
lihuang added reviewers: hfinkel, sanjoy.
lihuang added a subscriber: llvm-commits.
sanjoy accepted this revision.Aug 8 2016, 11:14 PM
sanjoy edited edge metadata.

lgtm with some minor comments inline.

lib/Analysis/ValueTracking.cpp
1078

Use APInt::setBit here and below (otherwise we'll unnecessarily allocate a new APInt if the bitwidth is larger than 64).

test/Analysis/ValueTracking/known-signbit-shift.ll
21

Why do you need to and with 7 here? Why not just shl directly with %b?

This revision is now accepted and ready to land.Aug 8 2016, 11:14 PM
lihuang added inline comments.Aug 9 2016, 11:10 AM
test/Analysis/ValueTracking/known-signbit-shift.ll
21

It just didn't work without the "and", it seems like this optimization doesn't recognize the case when the second operand of shift is totally unknown. But this should be some other problem, not the problem with this change. I will investigate.

lihuang updated this revision to Diff 67392.Aug 9 2016, 12:23 PM
lihuang edited edge metadata.

Use APInt::setBit, add comment

lihuang closed this revision.Aug 10 2016, 11:02 AM

This review was not closed automatically. Committed as r278172

lihuang reopened this revision.Aug 16 2016, 11:26 AM

reopen this review because the committed change has been reverted

This revision is now accepted and ready to land.Aug 16 2016, 11:26 AM
lihuang updated this revision to Diff 68234.Aug 16 2016, 11:31 AM

Hi Sanjoy,

This change is mostly same as before, but with the fix for PR28946 added in computeKnownBitsFromShiftOperator.

lihuang requested a review of this revision.Aug 16 2016, 11:35 AM
lihuang edited edge metadata.
lihuang edited reviewers, added: majnemer; removed: hfinkel.Aug 17 2016, 2:07 PM
sanjoy requested changes to this revision.Aug 22 2016, 10:19 AM
sanjoy edited edge metadata.

Comments inline.

lib/Analysis/ValueTracking.cpp
803

This looks excessively pessimistic -- why can't we KnownOne &= ~KnownZero or KnownZero &= ~KnownOne instead? Both seem better than what we have here.

1077

Indent is off.

This revision now requires changes to proceed.Aug 22 2016, 10:19 AM
lihuang updated this revision to Diff 68904.Aug 22 2016, 1:59 PM
lihuang edited edge metadata.
lihuang added inline comments.Aug 22 2016, 2:03 PM
lib/Analysis/ValueTracking.cpp
803

You are right that's too pessimistic. I changed it to KnownOne &= ~KnownZero.

I just copied the logic at the end of this function, looks like we could also change that.

majnemer requested changes to this revision.Aug 22 2016, 2:05 PM
majnemer edited edge metadata.
majnemer added inline comments.
lib/Analysis/ValueTracking.cpp
798–802

This goes against the ethos of ComputeKnownValues. Conflicts should return "I don't know" so that we can transform stuff into undef.

This revision now requires changes to proceed.Aug 22 2016, 2:05 PM
lihuang added inline comments.Aug 22 2016, 2:50 PM
lib/Analysis/ValueTracking.cpp
798–802

Hi David, I'm not familiar with this part but can we transform "i don't know" to undef? What if we just cannot infer anything from this value?

lihuang added inline comments.Aug 22 2016, 4:06 PM
lib/Analysis/ValueTracking.cpp
798–802

Sorry, I think I misunderstood what you meant. I agree that this should return "I don't know" so it won't prevent transforming stuff to undef.

lihuang updated this revision to Diff 68927.Aug 22 2016, 4:26 PM
lihuang edited edge metadata.

Should clear KnownZero and KnownOne bits when there is conflicts to allow propagation of undef. Changed the code in computeKnownBitsFromShiftOperator to have the same logic as that at the end of this function.

majnemer added inline comments.Aug 22 2016, 5:06 PM
lib/Analysis/ValueTracking.cpp
799–800

Please remove "We can return arbitrary values."

lihuang updated this revision to Diff 68940.Aug 22 2016, 5:16 PM
lihuang edited edge metadata.
lihuang marked an inline comment as done.Aug 23 2016, 1:36 PM
majnemer accepted this revision.Aug 23 2016, 1:48 PM
majnemer edited edge metadata.

LGTM, thanks!

Committed r279684.

sanjoy accepted this revision.Aug 30 2016, 9:33 PM
sanjoy edited edge metadata.

Removing from queue (since this has been committed).

This revision is now accepted and ready to land.Aug 30 2016, 9:33 PM
lihuang closed this revision.Sep 2 2016, 2:51 PM