This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] dropRedundantMaskingOfLeftShiftInput(): pat. c/d/e with mask (PR42563)
ClosedPublic

Authored by lebedev.ri on Sep 18 2019, 11:40 AM.

Details

Summary

If we have a pattern (x & (-1 >> maskNbits)) << shiftNbits,
we already know (have a fold) that will drop the & (-1 >> maskNbits)
mask iff (shiftNbits-maskNbits) s>= 0 (i.e. shiftNbits u>= maskNbits).

So even if (shiftNbits-maskNbits) s< 0, we can still
fold, we will just need to apply a constant mask afterwards:

Name: c, normal+mask
  %t0 = lshr i32 -1, C1
  %t1 = and i32 %t0, %x
  %r = shl i32 %t1, C2
=>
  %n0 = shl i32 %x, C2
  %n1 = i32 ((-(C2-C1))+32)
  %n2 = zext i32 %n1 to i64 
  %n3 = lshr i64 -1, %n2
  %n4 = trunc i64 %n3 to i32
  %r = and i32 %n0, %n4

https://rise4fun.com/Alive/gslRa

Naturally, old %masked will have to be one-use.
This is not valid for pattern f - where "masking" is done via ashr.

https://bugs.llvm.org/show_bug.cgi?id=42563

Diff Detail

Repository
rL LLVM

Event Timeline

lebedev.ri created this revision.Sep 18 2019, 11:40 AM
lebedev.ri marked an inline comment as done.
lebedev.ri added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
216–220 ↗(On Diff #220713)

This is duplicated, will later hoist into Type itself.

spatel added inline comments.Sep 23 2019, 8:14 AM
llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
207 ↗(On Diff #220713)

Did we ensure that Masked is not a ConstantExpr? If so, add an assert that Masked must be an Instruction here or somewhere above here?

lebedev.ri marked 2 inline comments as done.

Avoid constantexpr pitfail.
I'm not adding a test - i tried to come up with it, but it does not match.

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
207 ↗(On Diff #220713)

*sigh*

Err, actually upload the correct patch.

spatel accepted this revision.Sep 23 2019, 9:56 AM

LGTM

This revision is now accepted and ready to land.Sep 23 2019, 9:56 AM

LGTM

Thank you for the review!

This revision was automatically updated to reflect the committed changes.