This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] narrow abs with sign-extended input
ClosedPublic

Authored by spatel on Jan 22 2021, 7:16 AM.

Details

Summary

In the motivating cases from https://llvm.org/PR48816 , we have a trailing trunc. But that is not required to reduce the abs width:
https://alive2.llvm.org/ce/z/ECaz-p
...as long as we clear the int-min-is-poison bit (nsw).

We have some existing tests that are affected, and I'm not sure what the overall implications are, but in general I think we favor narrowing operations over preserving nsw/nuw. If not, we could restrict this transform based on type (shouldChangeType() and/or vector vs. scalar).

Diff Detail

Event Timeline

spatel created this revision.Jan 22 2021, 7:16 AM
spatel requested review of this revision.Jan 22 2021, 7:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 22 2021, 7:16 AM
lebedev.ri accepted this revision.Jan 22 2021, 8:12 AM

LGTM
I think narrowing ops is a win.

This revision is now accepted and ready to land.Jan 22 2021, 8:12 AM
xbolva00 accepted this revision.Jan 22 2021, 9:05 AM
RKSimon accepted this revision.Jan 22 2021, 9:07 AM

Cheers - please can you add the PR48816 test case

define i8 @transform_abs_epi8(i8 %0) {
  %2 = sext i8 %0 to i32
  %3 = tail call i32 @llvm.abs.i32(i32 %2, i1 true)
  %4 = trunc i32 %3 to i8
  ret i8 %4
}
declare i32 @llvm.abs.i32(i32, i1 immarg)

(and vector equivalent)

Cheers - please can you add the PR48816 test case

define i8 @transform_abs_epi8(i8 %0) {
  %2 = sext i8 %0 to i32
  %3 = tail call i32 @llvm.abs.i32(i32 %2, i1 true)
  %4 = trunc i32 %3 to i8
  ret i8 %4
}
declare i32 @llvm.abs.i32(i32, i1 immarg)

(and vector equivalent)

Sure - will add tests with trunc on pre-commit of tests and this patch.

This revision was automatically updated to reflect the committed changes.