<div dir="ltr">Hi Will, <div><br></div><div>I believe the reason you still see multiplies with "clang -emit-llvm" is that LoopStrengthReduction is not in the target-independent optimization pipeline. It runs in the target-specific IR optimizer. <div><br></div><div>If you take the IR emitted with "clang -emit-llvm" and run "llc foo.ll -print-after-all", you should be able to see the IR right after applying LSR. The x86 backend adds LoopStrengthReduce to its target-specific pipeline <a href="https://github.com/llvm-mirror/llvm/blob/b1415e7ebadc135ddad990a19d30aed8757587da/lib/CodeGen/Passes.cpp#L412">here</a>. If you want LLVM to run LSR for your backend, make sure your TargetPassConfig adds LSR or calls the generic TargetPassConfig::addIRPasses (e.g. see <a href="https://github.com/llvm-mirror/llvm/blob/b1415e7ebadc135ddad990a19d30aed8757587da/lib/Target/NVPTX/NVPTXTargetMachine.cpp#L169">how the NVPTX backend adds LSR</a>). </div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 4, 2016 at 1:30 PM, 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">
  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    Thx for trying, Sanjay.<br>
    <br>
    Now that I try and compile x86_64, I too see addq: clang -Os lsr.c
    -S -o -<br>
    <br>
    But when I compile clang -Os lsr.c -S -emit-llvm -o -, I see
    multiplies :(<br>
    <br>
    I even tried clang -Os hello48.c -S -emit-llvm -loop-reduce
    -loop-strength-reduce -o - for good measure.  Still multiples :(<br></div></blockquote><div><br></div><div>I guess Clang doesn't consume per-optimization flags such as -loop-reduce. opt takes them. So, try "opt -loop-reduce unopt.ll -o opt.ll". </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div text="#000000" bgcolor="#FFFFFF">
    <br>
    I am using a variety of LLVM versions; you are presumably using the
    bleeding edge.  Do you also get multiplies in the IR?<br>
    <br>
    (I am working on a backend which is not x86_64)<br>
    <br>
    thousand thanks,<br>
    Will<div><div class="h5"><br>
    <br>
    <div>On 04/01/16 17:31, Sanjay Patel wrote:<br>
    </div>
    <blockquote type="cite">
      <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" target="_blank">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>
    </blockquote>
    <br>
  </div></div></div>

<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">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>
<br></blockquote></div><br></div></div></div>