<div dir="ltr"><div>This should be handled in:<br><a href="https://github.com/llvm-mirror/llvm/blob/master/lib/Transforms/Scalar/LoopStrengthReduce.cpp">https://github.com/llvm-mirror/llvm/blob/master/lib/Transforms/Scalar/LoopStrengthReduce.cpp</a><br><br></div>And it seems to work for both of your examples (x86-64 target):<br>$ ./clang -Os lsr.c -S -o - | grep addq<br>    addq    (%rsi), %rax<br>    addq    $96, %rsi   ; 12 * 8 bytes<br>    addq    $44, %rbx  ; 11 * 4 bytes<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 4, 2016 at 3:27 AM, Will via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Here is a simple loop:<br>
<br>
long foo(int len, long* s) {<br>
    long sum = 0;<br>
    for (int i=0; i<len; i++)<br>
        sum += s[i*12];<br>
    return sum;<br>
}<br>
<br>
There is a multiplication in each loop iteration.  Can this be turned<br>
into addition, and is there already a pass that does?<br>
<br>
(<a href="https://en.wikipedia.org/wiki/Strength_reduction" rel="noreferrer" target="_blank">https://en.wikipedia.org/wiki/Strength_reduction</a> uses this very<br>
situation as an example in the opening paragraph:<br>
<br>
"In software engineering, strength reduction is a compiler optimization<br>
where expensive operations are replaced with equivalent but less<br>
expensive operations. The classic example of strength reduction converts<br>
"strong" multiplications inside a loop into "weaker" additions –<br>
something that frequently occurs in array addressing." :) )<br>
<br>
And here is another loop:<br>
<br>
extern void foo(int);<br>
<br>
typedef struct {int i; int a[10];} S;<br>
<br>
void bar(S* A) {<br>
    for(int i = 50; i < 400;i++)<br>
        foo(A[i].i);<br>
}<br>
<br>
In this case, there is a multiplication in each loop iteration 'hidden'<br>
in the GEP.  Can this be turned into addition too?<br>
<br>
I was hoping the loop-reduce pass would do this kind of thing; should it?<br>
<br>
Thx<br>
Will<br>
<br>
<br>
<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br></div>