This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombine] fold (urem x, (lshr pow2, y)) -> (and x, (add (lshr pow2, y), -1))
ClosedPublic

Authored by reames on Jul 12 2022, 5:16 PM.

Details

Summary

We have the same fold in InstCombine - though implemented via OrZero flag on isKnownToBePowerOfTwo. The reasoning here is that either a) the result of the lshr is a power-of-two, or b) we have a div-by-zero triggering UB which we can ignore.

Diff Detail

Event Timeline

reames created this revision.Jul 12 2022, 5:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 12 2022, 5:16 PM
reames requested review of this revision.Jul 12 2022, 5:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 12 2022, 5:16 PM
craig.topper added inline comments.Jul 12 2022, 5:53 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
4596

nit: Knownto -> KnownTo

4608

Is this code identical to the SHL code? Could we rewrite the if to

if ((N1.getOpcode() == ISD::SHL || N1.getOpcode() == ISD::SRL) &&
     DAG.isKnownToBeAPowerOfTwo(N1.getOperand(0))) {

And put the
// fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) and // fold (urem x, (lshr pow2, y)) -> (and x, (add (lshr pow2, y), -1)) comments above the if.

reames updated this revision to Diff 444134.Jul 12 2022, 6:38 PM

Address review comments

This revision is now accepted and ready to land.Jul 12 2022, 10:39 PM
This revision was landed with ongoing or failed builds.Jul 13 2022, 8:35 AM
This revision was automatically updated to reflect the committed changes.