This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold sub(Y,and(lshr(X,C),1)) --> add(ashr(shl(X,(BW-1)-C),BW-1),Y) (PR53610)
ClosedPublic

Authored by RKSimon on Feb 14 2022, 6:32 AM.

Details

Summary

As noted on PR53610, we can fold a 'bit splat' negation of a shifted bitmask pattern into a pair of shifts.

https://alive2.llvm.org/ce/z/eGrsoN

Diff Detail

Event Timeline

RKSimon created this revision.Feb 14 2022, 6:32 AM
RKSimon requested review of this revision.Feb 14 2022, 6:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 14 2022, 6:32 AM

Please add tests w/ subtraction from a value, and then double the amount of tests by adding all the missing extra-use tests.

spatel added inline comments.Feb 14 2022, 7:37 AM
llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
193

Use m_ImmConstant unless there's a test/need to match a constant expression.

RKSimon updated this revision to Diff 408489.Feb 14 2022, 10:14 AM

Use m_ImmConstant and oneUse checks

Hm, please also add an assortment of tests where there is a trunc inbetween, we really should handle that.

llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
192

Since one-use check is required, this should go into the next switch which is use-constrained.

193

I think just plain m_c_And() would make more sense here.

194

SplattingShAmt

RKSimon updated this revision to Diff 408818.Feb 15 2022, 4:43 AM

Add optional and(trunc(lshr(x,c)),1) support

lebedev.ri accepted this revision.Feb 15 2022, 5:10 AM
lebedev.ri edited the summary of this revision. (Show Details)

LGTM, thank you.

llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
253
This revision is now accepted and ready to land.Feb 15 2022, 5:12 AM
RKSimon retitled this revision from [InstCombine] Fold sub(0,and(lshr(X,C),1)) --> ashr(shl(X,(BW-1)-C),BW-1) (PR53610) to [InstCombine] Fold sub(Y,and(lshr(X,C),1)) --> add(ashr(shl(X,(BW-1)-C),BW-1),Y) (PR53610).Feb 15 2022, 5:16 AM
This revision was landed with ongoing or failed builds.Feb 15 2022, 5:24 AM
This revision was automatically updated to reflect the committed changes.