This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Allow removal of PHI cycles which only contain PHIs
Needs ReviewPublic

Authored by sbaranga on May 11 2016, 9:40 AM.

Details

Reviewers
majnemer
jmolloy
Summary

This teaches instcombine to be able to remove PHI cycles which
only contain PHIs.

When a PHI doesn't have a non-PHI incoming value, we will now
try to resolve the PHI to each of the incoming values.

Diff Detail

Event Timeline

sbaranga updated this revision to Diff 56931.May 11 2016, 9:40 AM
sbaranga retitled this revision from to [InstCombine] Allow removal of PHI cycles which only contain PHIs.
sbaranga updated this object.
sbaranga added reviewers: majnemer, jmolloy.
sbaranga added a subscriber: llvm-commits.
reames added a subscriber: reames.May 11 2016, 5:25 PM
reames added inline comments.
lib/Transforms/InstCombine/InstCombinePHI.cpp
972

Repeating this for each incoming value seems likely to be expensive. Can we do better? As one filter, we know that for the phi to be equal to some value, that value must dominate the phi or be a non-instruction value right? If so, can we use that?

Another option on how to approach this would be an inductive proof over the loop. On iteration 1, we know that %p2 = %p0 and thus that on every future iteration all inputes to the phis equal %p0. Framing this using a loop pass (possibly indvarsimplify?) might be another approach.

To be clear, I'm okay with the patch in it's current form if we can't find a better approach. I'm just asking to make sure we've explored all the options before eating the compile time.

sbaranga added inline comments.May 12 2016, 5:45 AM
lib/Transforms/InstCombine/InstCombinePHI.cpp
972

Correct, this might be a bit expensive when triggered. But the problem only occurs when all incoming values are phis and the phi that we processing has lots of incoming values. This should mean that the really expensive wouldn't trigger that often.

Filtering with DT sounds like a very good idea. Do you think that this transformation should only be done when DT is available, or should we only filter when DT is available?

I think this case can be triggered without needing the a loop, but I don't have an example for that.

sbaranga updated this revision to Diff 57335.May 16 2016, 4:24 AM

Only test incoming values if they dominate PN.

Note that all incoming values need to be PHIs in order for us to do this test,
so there's no need to test for non-instructions (since PHIs are instructions).

majnemer added inline comments.May 16 2016, 9:17 AM
lib/Transforms/InstCombine/InstCombinePHI.cpp
969–983

Seeing as how you are not inventing a new instruction, couldn't this live in InstructionSimplify?

sbaranga added inline comments.May 16 2016, 9:41 AM
lib/Transforms/InstCombine/InstCombinePHI.cpp
969–983

I suppose we could. Would we also move the logic that tries to find an incoming value that is not a PHI?

In that case this would get triggered a lot more often (and might have an impact on compile time).

Any thoughts on the compile time impact of doing this in InstructionSimplify?
I have a version of this that does the simplification in InstructionSimplify, and if there are no concerns about compile time we can use that.

Cheers,
Silviu

majnemer added inline comments.May 23 2016, 8:42 AM
lib/Transforms/InstCombine/InstCombinePHI.cpp
969–983

We already have similar logic in InstructionSimplify, this would make it more powerful. [1]

Would you mind benchmarking this between trunk vs trunk w/ InstructionSimplify amended?

[1] https://github.com/llvm-mirror/llvm/blob/master/lib/Analysis/InstructionSimplify.cpp#L3684

sbaranga added inline comments.May 23 2016, 9:09 AM
lib/Transforms/InstCombine/InstCombinePHI.cpp
969–983

Sure! I'll post the data once I have it.

Cheers,
Silviu