[PATCH] D55961: [InstCombine] canonicalize MUL with NEG operand

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 20 15:16:04 PST 2018


shchenz created this revision.
shchenz added reviewers: hfinkel, spatel, lebedev.ri, jsji, nemanjai, steven.zhang.
Herald added a subscriber: hiraditya.

There is an opportunity in instCombine for following instruction pattern:

  %mul = mul nsw i32 %b, %a
  %cmp = icmp sgt i32 %mul, -1
  %sub = sub i32 0, %a
  %mul2 = mul nsw i32 %sub, %b
  %cond = select i1 %cmp, i32 %mul, i32 %mul2

Source code for above pattern:

  return (a*b) >=0 ? (a*b) : -a*b;

Currently, llvm(-O3) can not recognize this as abs(a*b).

Got help from @spatel in llvm-dev:
"
it looks like we are missing an IR canonicalization for 'mul' like this:
https://rise4fun.com/Alive/ARs

  %neg = sub i8 0, %x
  %r = mul i8 %neg, %y

-->

  %mul2 = mul i8 %x, %y
  %r = sub i8 0, %mul2

"

Testcases are already add in https://reviews.llvm.org/rL349847.
Thanks @jsji, i have updated the testcases according to his comment to the above nfc patch.


https://reviews.llvm.org/D55961

Files:
  llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  llvm/test/Transforms/InstCombine/mul.ll
  llvm/test/Transforms/InstCombine/operand-complexity.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55961.179171.patch
Type: text/x-patch
Size: 5097 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181220/3b256090/attachment.bin>


More information about the llvm-commits mailing list