This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Transform abs pattern using multiplication to abs intrinsic (PR45691)
ClosedPublic

Authored by xbolva00 on Jan 17 2021, 6:48 AM.

Details

Summary
unsigned r(int v)
{
    return (1 | -(v < 0)) * v;
}

`r` is equivalent to `abs(v)`.
define <4 x i8> @src(<4 x i8> %0) {
%1:
  %2 = ashr <4 x i8> %0, { 31, undef, 31, 31 }
  %3 = or <4 x i8> %2, { 1, 1, 1, undef }
  %4 = mul nsw <4 x i8> %3, %0
  ret <4 x i8> %4
}
=>
define <4 x i8> @tgt(<4 x i8> %0) {
%1:
  %2 = icmp slt <4 x i8> %0, { 0, 0, 0, 0 }
  %3 = sub nsw <4 x i8> { 0, 0, 0, 0 }, %0
  %4 = select <4 x i1> %2, <4 x i8> %3, <4 x i8> %0
  ret <4 x i8> %4
}
Transformation seems to be correct!

Diff Detail

Event Timeline

xbolva00 created this revision.Jan 17 2021, 6:48 AM
xbolva00 requested review of this revision.Jan 17 2021, 6:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 17 2021, 6:48 AM
xbolva00 edited the summary of this revision. (Show Details)Jan 17 2021, 6:48 AM
xbolva00 edited the summary of this revision. (Show Details)
xbolva00 added a reviewer: nikic.
nikic added inline comments.Jan 17 2021, 7:12 AM
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
375

Doesn't just return Builder.CreateBinaryIntrinsic work here?

llvm/test/Transforms/InstCombine/ashr-or-mul-abs.ll
37

git grep thwart llvm/test/Transforms/InstCombine. Your current test is effectively equivalent to the previous one due to complexity canonicalization.

100

Missing a test where the two %X are not the same.

xbolva00 updated this revision to Diff 317219.Jan 17 2021, 7:52 AM

Addressed review comments

xbolva00 marked 2 inline comments as done.Jan 17 2021, 7:52 AM
xbolva00 added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
375

void llvm::SymbolTableListTraits<ValueSubClass>::addNodeToList(ValueSubClass*) [with ValueSubClass = llvm::Instruction]: Assertion `!V->getParent() && "Value already in a container!!"' failed.

xbolva00 updated this revision to Diff 317222.Jan 17 2021, 7:55 AM
nikic accepted this revision.Jan 17 2021, 7:55 AM

LGTM

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
375

Oh right. Thanks for checking.

This revision is now accepted and ready to land.Jan 17 2021, 7:55 AM