This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Use maximum shift count in `shl` when determining if `shl` can be zero.
ClosedPublic

Authored by goldstein.w.n on Apr 9 2023, 5:14 PM.

Details

Summary

Previously only return shl non-zero if the shift value was 1. We
can expand this if we have some bounds on the shift count.

For example:

%cnt = and %c, 16 ; Max cnt == 16
%val = or %v, 4 ; val[2] is known one
%shl = shl %val, %cnt ; (val.known.one << cnt.maxval) != 0

Diff Detail

Event Timeline

goldstein.w.n created this revision.Apr 9 2023, 5:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 9 2023, 5:14 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
goldstein.w.n requested review of this revision.Apr 9 2023, 5:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 9 2023, 5:14 PM
nikic added inline comments.Apr 14 2023, 9:49 AM
llvm/lib/Analysis/ValueTracking.cpp
2688

Isn't this going to assert if the max value is wider than the bit width?

nikic added inline comments.Apr 14 2023, 9:52 AM
llvm/lib/Analysis/ValueTracking.cpp
2688

Should probably just use KnownBits::shl() here.

goldstein.w.n added inline comments.Apr 14 2023, 10:04 AM
llvm/lib/Analysis/ValueTracking.cpp
2688

Should probably just use KnownBits::shl() here.

Will do, does using KnownBits::shl fix the issue of failing the assert if max value is too wide, or do I need to add a check for that? Adding a few tests for that case and not seeing any assertions.

goldstein.w.n added inline comments.Apr 14 2023, 10:12 AM
llvm/lib/Analysis/ValueTracking.cpp
2688

Should probably just use KnownBits::shl() here.

Using KnownBits::shl(Known, KnownCnt).isNonZero() causes regressions. How about just adding a checking KnownCnt.getMaxValue().ult(Known.getBitWidth())?

goldstein.w.n added inline comments.Apr 14 2023, 10:23 AM
llvm/lib/Analysis/ValueTracking.cpp
2688

Should probably just use KnownBits::shl() here.

2688

Should probably just use KnownBits::shl() here.

Using KnownBits::shl(Known, KnownCnt).isNonZero() causes regressions. How about just adding a checking KnownCnt.getMaxValue().ult(Known.getBitWidth())?

Yeah KnownBits::shl isn't ideal here b.c its computing something more precision than we need. We only need to know if the maximum shift will clear the result. We don't need the precision of where the bits end up. For example with the 1 << (cnt & 16) case, KnowBits::shl cant set any bits for the output. But the current logic figures out that can't be zero.

Check that shift count isn't invalid

goldstein.w.n marked an inline comment as done.Apr 14 2023, 10:26 AM
goldstein.w.n added inline comments.
llvm/lib/Analysis/ValueTracking.cpp
2688

Isn't this going to assert if the max value is wider than the bit width?

Added a check.

nikic accepted this revision.Apr 14 2023, 12:09 PM

LGTM

This revision is now accepted and ready to land.Apr 14 2023, 12:09 PM