This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Optimize (add (shl x, c0), (shl y, c1)) with SH*ADD
ClosedPublic

Authored by benshi001 on Aug 30 2021, 6:23 AM.

Details

Summary

Optimize (add (shl x, c0), (shl y, c1)) ->

(SLLI (SH*ADD x, y), c1), if c0-c1 == 1/2/3.

Diff Detail

Event Timeline

benshi001 created this revision.Aug 30 2021, 6:23 AM
benshi001 requested review of this revision.Aug 30 2021, 6:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2021, 6:23 AM
craig.topper added inline comments.Aug 30 2021, 9:00 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5790

You can't depend on this. I believe the add node will be visited before the shift nodes are visited to replace any negative shift amounts with undef. So if the IR contains a negative shift amount I think you will fail this assertion.

6233

You can just check

if (NA)

SDValue operator bool() checks getNode() != nullptr.

benshi001 updated this revision to Diff 369604.Aug 30 2021, 7:31 PM
benshi001 marked 2 inline comments as done.
benshi001 updated this revision to Diff 369607.Aug 30 2021, 7:37 PM
benshi001 updated this revision to Diff 369614.Aug 30 2021, 8:19 PM

ping...

Can this patch be revriewed and accepted? It is safe and does improve code quality.

luismarques accepted this revision.Sep 16 2021, 4:06 AM

LGTM.

llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5771

Nit: vector

5793–5794

Is this just abs(C1 - C0)?

This revision is now accepted and ready to land.Sep 16 2021, 4:06 AM
luismarques requested changes to this revision.Sep 16 2021, 5:15 AM

I had started reviewing this before D109729, now I'm very confused about the point of the two patches...

llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5773–5774

See craig's comment in D109729: The VT.getSizeInBits() > Subtarget->getXLen() should never fail. If the VT is scalar it must be XLenVT.

This revision now requires changes to proceed.Sep 16 2021, 5:15 AM
benshi001 updated this revision to Diff 372927.Sep 16 2021, 7:03 AM
benshi001 updated this revision to Diff 372928.Sep 16 2021, 7:06 AM
benshi001 marked 3 inline comments as done.
benshi001 added a comment.EditedSep 16 2021, 7:10 AM

I had started reviewing this before D109729, now I'm very confused about the point of the two patches...

The reason I created https://reviews.llvm.org/D109729 is that current patch has been pending for weeks and I thought you disagree with it.

So I created https://reviews.llvm.org/D109729, which initial version was the same optimization but implemented in TD rules. However TD rules were in bad form so I changed it to DAG2GAG.

However I think both patches are OK. If current one can be accepted, then I will close https://reviews.llvm.org/D109729.

The build failure x64 debian > libomp.api::omp_get_wtime.c seems not related to my patch.

craig.topper added inline comments.Sep 16 2021, 8:46 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5773–5774

This comment was not correct for this patch. The XLen check is needed here.

6232

Move this into performADDCombine

luismarques added inline comments.Sep 16 2021, 8:55 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5773–5774

This comment was not correct for this patch. The XLen check is needed here.

Yup, my bad. Sorry!

benshi001 updated this revision to Diff 373106.Sep 16 2021, 4:57 PM
benshi001 marked 2 inline comments as done.

Also I have moved transformAddShlImm into performADDCombine

benshi001 updated this revision to Diff 373119.Sep 16 2021, 6:07 PM
benshi001 edited the summary of this revision. (Show Details)
benshi001 marked an inline comment as done.
craig.topper added inline comments.Sep 18 2021, 3:31 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5784

NC0 -> N0C as I think that's the style DAGCombiner.cpp uses.

5917

Don't N0 for the name here. N0 usually means N->getOperand(0)

Use

if (SDValue V = transformAddShlImm(N, DAG, Subtarget))
  return V;

V is pretty generic and putting it in the if limits the scope.

benshi001 updated this revision to Diff 373438.Sep 18 2021, 5:33 PM
benshi001 edited the summary of this revision. (Show Details)
benshi001 marked 2 inline comments as done.
This revision was not accepted when it landed; it landed in state Needs Review.Sep 19 2021, 1:35 AM
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.