[llvm-dev] should we do this time-consuming transform in InstCombine?

Zheng CZ Chen via llvm-dev llvm-dev at lists.llvm.org
Mon Dec 17 23:18:31 PST 2018


Hi,

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).

I initially think we could do this in instCombine phase in opt. Below is
what I think:

%res = OP i32 %b, %a
%sub = sub i32 0, %b
%res2 = OP i32 %sub, %a

We could do the transform:
%res2 = OP i32 %sub, %a   ==>  %res2 = sub i32 0, %res

Then we can get the advantage:
1: if %res2 is the only user of %sub, %sub can be eliminated;
2: if %res2 is not the only user of %sub, we could change some heavy
instruction like div to sub;
3: expose more abs opportunity for later pass.

But my concern is finding %res is a little compiling time-consuming.
At least we need MIN(user_count(%b), user_count(%a)) times to check if
instruction with same opcode and same operands exists.

Could you guys give some comment? Is there any better idea for this
transform?

Thanks.

BRS//
Chen Zheng
Power Compiler Backend Developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20181218/9b051227/attachment.html>


More information about the llvm-dev mailing list