This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold bitcasts into loads across PHINodes
Needs ReviewPublic

Authored by philip.pfaffe on May 5 2015, 8:46 AM.

Details

Reviewers
majnemer
Summary

Due to a combination of canonicalization of induction vars and subsequent inlining/simplification, we are sometimes left with a PHINode which only uses a load and bitcasts, and whose only user is also a bitcast. If the bitcasts are noop and tautological when concatenated, we can instead fold the desired type through the PHI into the load and get rid of some cast clutter.

Diff Detail

Event Timeline

philip.pfaffe retitled this revision from to [InstCombine] Fold bitcasts into loads across PHINodes.
philip.pfaffe updated this object.
philip.pfaffe edited the test plan for this revision. (Show Details)
philip.pfaffe added reviewers: majnemer, chandlerc.
philip.pfaffe set the repository for this revision to rL LLVM.
philip.pfaffe added a subscriber: Unknown Object (MLST).
chandlerc edited edge metadata.May 28 2015, 2:32 PM

The idea here makes total sense to me, but I don't think this is the right way to do it.

I think you should be combining the PHI or cast, not the load. The combine should be to hoist a cast of a PHI around the PHI if all of the PHI inputs are casts and loads. You might also need to enqueue the loads in the combiner afterward so we revisit them and recognize that we can fold the cast into the load.

philip.pfaffe edited edge metadata.
philip.pfaffe removed rL LLVM as the repository for this revision.

I adopted Chandler's suggestion of combining the PHI instead of the Load and updated the patch accordingly. I also added a missing RUN line to the test case.

The idea here is to detect a PHI whose only user is a Noop CastInst and then fold the cast back through the phi if the phi only depends on loads and matching casts. This produces a new cast for an incoming load (which should be folded into the loop later on), and skips incoming casts.