This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fix incorrect SimplifyWithOpReplaced transform (PR47322)
ClosedPublic

Authored by nikic on Sep 10 2020, 3:27 AM.

Details

Summary

This is a followup to D86834, which partially fixed this issue in InstSimplify. However, InstCombine repeats the same transform while dropping poison flags -- which does not cover cases where poison is introduced in some other way.

The fix here is a bit more comprehensive, because things are quite entangled, and it's hard to only partially address it without regressing optimization. There are really two changes here:

  • Export the SimplifyWithOpReplaced API from InstSimplify, with an added AllowRefinement flag. For replacements inside the TrueVal we don't actually care whether refinement occurs or not, the replacement is always legal. This part of the transform is now done in InstSimplify only. (It should be noted that the current AllowRefinement check is not sufficient -- that's an issue we need to address separately.)
  • Change the InstCombine fold to work by temporarily dropping poison generating flags, running the fold and then restoring the flags if it didn't work out. This will ensure that the InstCombine fold is correct as long as the InstSimplify fold is correct.

I hope this approach makes sense.

Diff Detail

Event Timeline

nikic created this revision.Sep 10 2020, 3:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 10 2020, 3:27 AM
nikic requested review of this revision.Sep 10 2020, 3:27 AM

The change makes sense to me.

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
1205

Is there reason why SimplifyWithOpReplaced(TrueVal, ....) == FalseVal is removed?

nikic added inline comments.Sep 11 2020, 1:20 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
1205

This case is now fully handled by InstSimplify, as it does the TrueVal replacement with AllowRefinement=true now. Previously this code here caught cases where TrueVal had poison flags (but would then unnecessarily drop poison flags on FalseVal, because the implementation was mixing two different transforms).

aqjune accepted this revision.Sep 12 2020, 3:55 AM

LGTM

This revision is now accepted and ready to land.Sep 12 2020, 3:55 AM
This revision was landed with ongoing or failed builds.Sep 12 2020, 5:45 AM
This revision was automatically updated to reflect the committed changes.