This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] fold select X, (ext X), C
ClosedPublic

Authored by spatel on Sep 30 2016, 2:02 PM.

Details

Summary

If we're going to canonicalize IR towards select of constants, try harder to create those.
Also, don't lose the metadata.

Diff Detail

Repository
rL LLVM

Event Timeline

spatel updated this revision to Diff 73134.Sep 30 2016, 2:02 PM
spatel retitled this revision from to [InstCombine] fold select X, (ext X), C.
spatel updated this object.
spatel added reviewers: davidxl, efriedma, majnemer.
spatel added a subscriber: llvm-commits.
majnemer accepted this revision.Oct 7 2016, 10:19 AM
majnemer edited edge metadata.

LGTM

This revision is now accepted and ready to land.Oct 7 2016, 10:19 AM
bjope added a reviewer: bjope.Oct 7 2016, 10:27 AM
bjope added a subscriber: bjope.
bjope added inline comments.
lib/Transforms/InstCombine/InstCombineSelect.cpp
966 ↗(On Diff #73134)

Why aren't you using the Builder here? Is it to avoid an implicit fold by the CreateSelect?
To me it looks like you are using a different style compared to the optimization above involving the trunc.

And I guess that if you use the Builder, then you won't need to explicitly copyMetadata? Or is that missing from the optimization involving the trunc. There was not a single explicit copyMetadata in this file until now.

majnemer added inline comments.Oct 7 2016, 10:35 AM
lib/Transforms/InstCombine/InstCombineSelect.cpp
966 ↗(On Diff #73134)

The code added is in the style of the rest of InstCombine. The builder is used for intermediate instructions but not the final instruction which is RAUW. This has to do with the particular behavior InstCombine has augmented the builder with.

I'm pretty sure the copyMetadata is around to make sure we don't discard the !prof. Other select combines probably drop this information.

spatel added inline comments.Oct 7 2016, 10:50 AM
lib/Transforms/InstCombine/InstCombineSelect.cpp
966 ↗(On Diff #73134)

Correct about the !prof - although hopefully we're not dropping that anymore. In the trunc transform above, it is copied with:

Builder->CreateSelect(Cond, X, TruncCVal, "narrow", &Sel)  <-- last param means copy metadata from Sel.
bjope added inline comments.Oct 7 2016, 10:57 AM
lib/Transforms/InstCombine/InstCombineSelect.cpp
966 ↗(On Diff #73134)

Ok. Nothing wrong with the style then. Sorry about that comment.

And yes, the Builder->CreateSelect is copying metadata if the original select is provided. So in many situations the metadata will survive. That is kind of why I wondered why there was an explicit copy of metadata instead of using the Builder in this partiular case.

(I won't learn if I don't ask questions. Thanks for you answers!)

This revision was automatically updated to reflect the committed changes.