[cfe-dev] Loop invariant and strength reduction

Damien Vincent damien.llvm at gmail.com
Thu Feb 24 15:25:08 PST 2011


When I compile to llvm (using -emit-llvm -O2 -S) the following piece of
code:

int f(int *ptr, int incr, int n)
{
  int r = n+1;

  do
  {
    if(*ptr!=0)
      r = n;
    ptr += incr;
    n--;
  } while(n!=0);
  return r;
}

clang notices the index can be generated using a multiplication ( ptr[incr *
loopindex] ) and generates some code which is at least as complicated (a
multiplication is more or at least as complex as an addition + the generated
code uses indirect addressing instead of direct addressing which is
sometimes more complex).
I am new to clang but I was thinking a compiler should do some strength
reduction (and not the other way !).

Could you explain me why clang replaces this addition+direct addressing by a
multiplication + indirect addressing ?

  Thank you !

  Damien
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110224/d7b1f83b/attachment.html>


More information about the cfe-dev mailing list