This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Make SimplifyDemandedUseBits handle PhiNode
Needs ReviewPublic

Authored by xur on Feb 15 2018, 12:14 PM.

Details

Summary

This patch enables SimplifyDemandedUseBits to handle PhiNode so that some redundant instructions across the PhiNode can be eliminated.

Diff Detail

Event Timeline

xur created this revision.Feb 15 2018, 12:14 PM
craig.topper added inline comments.Feb 15 2018, 10:16 PM
lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
473

This seems unrelated or at least unexplained.

476

DemandedMaskIn isn't used after this.

516

This as well?

craig.topper added inline comments.Feb 15 2018, 10:24 PM
lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
680

Use "KnownZero &= PhiOperandKnown.Zero" it will save an allocation if the width is more than 64-bits.

xur marked an inline comment as done.Feb 16 2018, 10:49 AM
xur added inline comments.
lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
473

see the comments below.

476

I actually did not understand why DemandedMaskIn is reset here. So I moved it down (after processed the operand 0).

To me, since we are doing the do the shift right, ShiftAmt of the lowbits is not needed (whatever value it might be).
The original code seems to say these lowbits are needed.

If the lowbits in DemandMaskin are indeed needed, I would have to set the Known.zero and check Known.zero in AND instruction.

craig.topper added inline comments.Feb 16 2018, 11:00 AM
lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
476

If we dont' preserve the bits in the input to the shift, then the exact flag could become incorrect. I'm not sure why we would prefer to preserve the bits rather than clear the exact flag.

Interesting. NewGVN can already figure this out if it's important.
How often does stuff like this occur?

test/Transforms/InstCombine/simplify-demanded-bits-across-phi.ll
36

Remove the ss :)

xur updated this revision to Diff 134671.Feb 16 2018, 11:52 AM

Updated the patch to integrate comments from ctopper and dberlin.

xur added inline comments.Feb 16 2018, 12:00 PM
lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
476

You are right! Thanks for the explanation. If the operand marked as exact, it's unsafe the remove the AND operation.
This exact in lshr seems to be insert by instcombine (with the presence of AND).
It won's apply to my original c++ programs.

Changed the test case as suggested by dberlin.

xur added a subscriber: davidxl.Feb 21 2018, 11:47 AM
efriedma added inline comments.
lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
679

This call is going to be expensive in the case where the PHI node refers to itself (either directly or indirectly). It's not an infinite loop due to the depth limit, but we might want to limit the recursion some other way.