This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Simplify select operand based on equality condition
ClosedPublic

Authored by nikic on Sep 10 2020, 1:24 PM.

Details

Summary

For selects of the type X == Y ? A : B, check if we can simplify A by using the X == Y equality and replace the operand if that's possible. We already try to do this in InstSimplify, but will only fold if the result of the simplification is the same as B, in which case the select can be dropped entirely. Here the select will be retained, just one operand simplified.

As we are performing an actual replacement here, we don't have problems with refinement / poison values.

Diff Detail

Event Timeline

nikic created this revision.Sep 10 2020, 1:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 10 2020, 1:24 PM
nikic added a comment.Sep 10 2020, 1:31 PM

This partially subsumes the foldSelectBinOpIdentity() fold at https://github.com/llvm/llvm-project/blob/cb19e8c6d192a108b72ab07362921864a9e244f9/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L57, which handles a special case of this (where the equality comparison is to the neutral element of a binary operator). However, that fold also handles floating point equalities, which are tricky due to signed zero, so it can't simply be dropped.

reames accepted this revision.Sep 14 2020, 8:26 AM
reames added a subscriber: reames.

LGTM

This revision is now accepted and ready to land.Sep 14 2020, 8:26 AM
spatel added inline comments.Sep 14 2020, 8:41 AM
llvm/test/Transforms/InstCombine/select-binop-cmp.ll
566–567

Remove stale comment and rename test since it's no longer "bad",

604–605

Rename test - it's not "bad" now.

This revision was landed with ongoing or failed builds.Sep 14 2020, 11:08 AM
This revision was automatically updated to reflect the committed changes.