This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] Fold SSHLSAT/USHLSAT to SHL when no saturation will occur
ClosedPublic

Authored by bjope on Feb 2 2022, 2:37 AM.

Details

Summary

When the shift amount is known and a known sign bit analysis of
the shiftee indicates that no saturation will occur, then we can
replace SSHLSAT/USHLSAT by SHL.

Diff Detail

Event Timeline

bjope created this revision.Feb 2 2022, 2:37 AM
bjope requested review of this revision.Feb 2 2022, 2:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 2 2022, 2:37 AM
bjope added inline comments.Feb 2 2022, 5:08 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
9354

Maybe I need to check that the SHL is legal/custom (unless this is before legalization).

bjope updated this revision to Diff 405320.Feb 2 2022, 9:48 AM

Added check that it is "legal" to use SHL.

a few minors

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
9354

We've been hit in the past by fuzzers which think its funny to create constants greater than i64 - so maybe:

N1->getAPIntValue().ult(DAG.ComputeNumSignBits(N0))
9359
N1->getAPIntValue().ult(BitWidth) &&
9361

It might be easier to grok as this?

DAG.computeKnownBits(N0).getMaxValue().ult(N1C->getZExtValue())
bjope updated this revision to Diff 406248.Feb 6 2022, 7:04 AM

Updated after review feedback:

  • Now using APInt for comparisons.
  • For USHLSAT we now compare minimum number of known leading zeroes to the shift amount when checking if all shifted out bits are zero. That is hopefully easier to read/understand compared to using MaskedValueIsZero.
bjope marked 3 inline comments as done.Feb 6 2022, 7:09 AM
RKSimon accepted this revision.Feb 6 2022, 7:50 AM

LGTM

This revision is now accepted and ready to land.Feb 6 2022, 7:50 AM
This revision was landed with ongoing or failed builds.Feb 6 2022, 10:00 AM
This revision was automatically updated to reflect the committed changes.