This is an archive of the discontinued LLVM Phabricator instance.

[DAG] computeKnownBits - add ISD::AVGCEILU handling
ClosedPublic

Authored by RKSimon on Feb 12 2022, 6:28 AM.

Details

Summary

Expand the ISD::AVGCEILU to determine the known bits of the result.

I can move this inside KnownBits if you would prefer - we don't have any use for it outside this method yet so wasn't sure if it was worth it.

First part of PR53622

Diff Detail

Event Timeline

RKSimon created this revision.Feb 12 2022, 6:28 AM
RKSimon requested review of this revision.Feb 12 2022, 6:28 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 12 2022, 6:28 AM
foad added a comment.Feb 13 2022, 5:27 AM

I guess this method can calculate some low bits of the result, but is that actually useful? If you're only interested in the high bits you can just say that the result is known to be >= the smaller operand and <= the larger one, which would work for all four avg variants (floor/ceil, signed/unsigned).

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3135

BitWidth+1 is enough.

dmgreen added inline comments.Feb 13 2022, 7:01 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3135

Yeah - I went back and forth on BW+1 and BW+2, but I believe +1 is enough. The docs may still say +2 because I was being too safe.

Somewhat relatedly, 0xf+0xf+1 = 0x1f. So avgflooru and avgceilu may both only require one computeForAddSub call, and could possible share the same code.

(This seems to says that 1 bit is enough for any +C: https://alive2.llvm.org/ce/z/e6PMYq. Not sure how to prove known bits more generally).

RKSimon updated this revision to Diff 408262.Feb 13 2022, 7:22 AM

reduce zext bitwidth

foad added inline comments.Feb 13 2022, 8:08 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3138–3139

I think you could do all of this with a single call to computeForAddCarry with carry-in known to be 1.

RKSimon added inline comments.Feb 13 2022, 8:29 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3138–3139

Nice catch!

RKSimon updated this revision to Diff 408268.Feb 13 2022, 8:32 AM

Use KnownBits::computeForAddCarry

@dmgreen I'm intending to add coverage for the other avg nodes once D119559 has landed

foad accepted this revision.Feb 13 2022, 11:32 PM

LGTM. I'd prefer not to have this inside KnownBits since it is a rather specific operation.

This revision is now accepted and ready to land.Feb 13 2022, 11:32 PM
dmgreen accepted this revision.Feb 14 2022, 12:16 AM

@dmgreen I'm intending to add coverage for the other avg nodes once D119559 has landed

Sounds good. I'll try and get that in now. I found the llvm.aarch64.neon.hadd intrinsics useful for testing, as they will be transformed directly to avg nodes.

This revision was landed with ongoing or failed builds.Feb 16 2022, 5:14 AM
This revision was automatically updated to reflect the committed changes.