This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold (X << Z) / (X * Y) -> (1 << Z) / Y
ClosedPublic

Authored by Chenbing.Zheng on Dec 7 2022, 6:00 PM.

Details

Summary

Alive2: https://alive2.llvm.org/ce/z/CBJLeP

Note that both shl and mul require nuw and already
have negtive test cases in div-shift.ll for it.

Diff Detail

Event Timeline

Chenbing.Zheng created this revision.Dec 7 2022, 6:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 7 2022, 6:00 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
Chenbing.Zheng requested review of this revision.Dec 7 2022, 6:00 PM
arsenm added a subscriber: arsenm.Dec 7 2022, 6:37 PM
arsenm added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1066

Can you preserve NUW and exact?

llvm/test/Transforms/InstCombine/div-shift.ll
556–629

Vector test? Tests with flag preservation?

RKSimon added inline comments.Dec 8 2022, 4:55 AM
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1066

Move the !IsSigned check outside to avoid the cost of match calls?

  1. rebase after add more tests
  2. add oneuse check
  3. keep exact flag
Chenbing.Zheng marked 2 inline comments as done.Dec 8 2022, 7:29 PM
Chenbing.Zheng added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1066

I preserve exact, but it seems that NUW is unnecessary when Op0 of SHL is 1 ?

nikic added a subscriber: nikic.Dec 14 2022, 12:25 AM
nikic added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1066

You're right that we can infer nuw for 1 << x, but based on your tests it doesn't look like we actually do this...

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1066

Yer, I think 'shl nuw 1 << x' is equivalent to 'shl 1 << x', so I tend to don't keep 'NUW'. Or what is the benefit of keeping 'NUW' ?

spatel added inline comments.Dec 15 2022, 9:26 AM
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1066

It could enable some other transform. Some tests actually improve with that:
d4493dd1ed58

So assuming that patch is good, it won't matter much now if it isn't included here, but it's still more efficient to create the 'nuw' directly in this patch instead of relying on another fold to do it.

spatel added inline comments.Dec 15 2022, 9:34 AM
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1061

Do you plan to handle the signed case too?

I'm not sure exactly what no-wrap is required, but there is some combination that works, so add a TODO comment:
https://alive2.llvm.org/ce/z/WrXmth

add nuw for 1<<Z

Chenbing.Zheng marked 4 inline comments as done.Dec 15 2022, 6:57 PM
spatel added inline comments.Dec 16 2022, 4:38 AM
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1062

Would this still be a good transform even if the shift has another use (is there a test for that pattern)?
If we are trading a multiply for a shift, it's probably beneficial.

address comment

spatel accepted this revision.Dec 28 2022, 5:34 AM

LGTM

This revision is now accepted and ready to land.Dec 28 2022, 5:34 AM