<div dir="ltr">Most of the optimizations for these things are handled after IR in SelectionDAG often with target specific knowledge. For IR we want to preserve the simplest operation as long as possible to make sure optimization passes can reason about it.<div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Mar 5, 2019 at 8:35 PM Peng Yu via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
$ clang -Wall -pedantic -O3 -S -emit-llvm -c -o - main.c<br>
<br>
When I compile the following .c file using the above clang command, I<br>
still get mul in the IR code. But isn't that 31 is just 32 - 1, so<br>
that the implementation can use left shift by 4 and subtract the<br>
original number?<br>
<br>
#include <stdio.h><br>
int f(int x) {<br>
    return x * 31;<br>
}<br>
int main() {<br>
    int x=1;<br>
    printf("%d\n", f(x));<br>
    return 0;<br>
}<br>
<br>
; Function Attrs: norecurse nounwind readnone ssp uwtable<br>
define i32 @f(i32) local_unnamed_addr #0 {<br>
  %2 = mul nsw i32 %0, 31<br>
  ret i32 %2<br>
}<br>
<br>
Also, I see this comment. But % is just translated to `srem` by clang.<br>
<br>
"Turns out if you do a modulo by a constant, the compiler knows a<br>
bunch of tricks to make this fast."<br>
<a href="https://probablydance.com/2017/02/26/i-wrote-the-fastest-hashtable/" rel="noreferrer" target="_blank">https://probablydance.com/2017/02/26/i-wrote-the-fastest-hashtable/</a><br>
<br>
Is clang able to produce optimized code for integer multiplication and<br>
modulo operations? Thanks.<br>
<br>
-- <br>
Regards,<br>
Peng<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>