This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Apply the isKnownNonZero techniques in `ashr`/`lshl` to `shl` and vice-versa
ClosedPublic

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

Details

Summary

For all shifts we can apply the same two optimizations.

  1. ShiftOp(KnownVal.One, Max(KnownCnt)) != 0 -> result is non-zero
  2. If already known Val != 0 and we only shift out zeros (based on Max(KnownCnt)) -> result is non-zero

The former exists for shl and the latter (for constant Cnt) exists
for ashr/lshr.

This patch improves the latter to use Max(KnownCnt) instead of
relying on a constant shift Cnt and applies both techniques for all
shift ops.

Diff Detail

Event Timeline

goldstein.w.n created this revision.Apr 14 2023, 5:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2023, 5:22 PM
goldstein.w.n requested review of this revision.Apr 14 2023, 5:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2023, 5:22 PM

Remove extraneous changes

nikic added inline comments.Apr 15 2023, 1:17 AM
llvm/lib/Analysis/ValueTracking.cpp
2518

const APInt &

2543

clang-format

2735–2736

This and below should directly return, because we don't want to fall through to the generic known bits logic anymore.

goldstein.w.n marked 2 inline comments as done.

Make args const ref. Make new helper unconditional return

goldstein.w.n marked an inline comment as done.Apr 16 2023, 5:39 PM
nikic accepted this revision.Apr 17 2023, 11:54 AM

LGTM, though I suspect you'd need less code if you didn't try to factor this into one function and just handled left and right shifts separately.

This revision is now accepted and ready to land.Apr 17 2023, 11:54 AM
goldstein.w.n reopened this revision.Apr 18 2023, 1:32 PM

This doesn't appear to have been related to PR62175 so repushing.

This revision is now accepted and ready to land.Apr 18 2023, 1:32 PM
goldstein.w.n closed this revision.Apr 19 2023, 5:32 PM