This is an archive of the discontinued LLVM Phabricator instance.

[SelectionDAG] Improve codegen for udiv by constant if any divisors are 1.
ClosedPublic

Authored by craig.topper on Jan 4 2023, 6:35 PM.

Details

Summary

If the divisor is 1, the magic algorithm does not return a correct
result and we end up using a select to pick the numerator for those
elements at the end.

Therefore we can use undef for that element of the earlier operations
when the divisor is 1. We sometimes get this through SimplifyDemandedVectorElts,
but not always. Definitely seems like we don't if the NPQ fixup is used.

Unfortunately, DAGCombiner is unable to fold srl X, <0, undef> to X so
I had to add flags to avoid emitting the srl unless one of the shift
amounts is non-zero.

Diff Detail

Event Timeline

craig.topper created this revision.Jan 4 2023, 6:35 PM
craig.topper requested review of this revision.Jan 4 2023, 6:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2023, 6:35 PM
lebedev.ri accepted this revision.Jan 5 2023, 6:32 AM

LG, thanks.
There may be similar opportunity at least in the urem==C expansion.

DAGCombiner is unable to fold srl X, <0, undef> to X

This seems like a somewhat straight-forward missing optimization?
Although, then the second element becomes non-poison in the result.

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
6067

Not clang-formatted

This revision is now accepted and ready to land.Jan 5 2023, 6:32 AM
This revision was landed with ongoing or failed builds.Jan 5 2023, 8:42 AM
This revision was automatically updated to reflect the committed changes.