This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Reduce absolute diff from min+max+sub
ClosedPublic

Authored by junaire on Mar 7 2023, 7:34 PM.

Details

Summary

This patch implement fold: max(a,b) nsw/nuw - min(a,b) --> abs(a nsw - b)

Alive2: https://alive2.llvm.org/ce/z/4yLp7D
Fixes: https://github.com/llvm/llvm-project/issues/61228

Signed-off-by: Jun Zhang <jun@junz.org>

Diff Detail

Event Timeline

junaire created this revision.Mar 7 2023, 7:34 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 7 2023, 7:34 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
junaire requested review of this revision.Mar 7 2023, 7:34 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 7 2023, 7:34 PM
junaire edited the summary of this revision. (Show Details)Mar 7 2023, 7:35 PM
junaire updated this revision to Diff 503217.Mar 7 2023, 8:20 PM

Apply clang-format.

junaire updated this revision to Diff 503346.Mar 8 2023, 6:16 AM

Make clang-format happy

RKSimon added inline comments.Mar 8 2023, 6:52 AM
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
2413

What about cases where we have smin(Y,X) instead?

llvm/test/Transforms/InstCombine/sub-minmax.ll
1051

multi use negative tests?

RKSimon added inline comments.Mar 8 2023, 6:54 AM
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
2413

Also, don't we have m_SMax / m_SMin ?

junaire updated this revision to Diff 503359.Mar 8 2023, 7:18 AM

Update commute tests and negative tests

junaire marked 3 inline comments as done.Mar 8 2023, 7:18 AM
RKSimon added inline comments.Mar 8 2023, 7:38 AM
llvm/test/Transforms/InstCombine/sub-minmax.ll
1035

shouldn't this be:

%max = call i8 @llvm.smax.i8(i8 %a, i8 %b)
junaire added inline comments.Mar 8 2023, 7:47 AM
llvm/test/Transforms/InstCombine/sub-minmax.ll
1035

shouldn't this be:

%max = call i8 @llvm.smax.i8(i8 %a, i8 %b)

IIUC, we should test all the following cases?

  1. max(a, b) - min(a, b)
  2. max(b, a) - min(b, a)
  3. max(a, b) - min(b, a)
  4. max(b, a) - min(a, b)

However, I'm not certain it is worth doing so. I'll pick case 1 and case 3 then.

junaire updated this revision to Diff 503372.Mar 8 2023, 7:49 AM

Update tests.

RKSimon accepted this revision.Mar 9 2023, 10:07 AM

LGTM - cheers

This revision is now accepted and ready to land.Mar 9 2023, 10:07 AM
This revision was landed with ongoing or failed builds.Mar 9 2023, 4:01 PM
This revision was automatically updated to reflect the committed changes.

LGTM - cheers

Thank you, Simon!