[LLVMdev] Missed optimization opportunity

Lup Gratian lgratian at gmail.com
Tue Dec 28 09:39:36 PST 2010


I recently downloaded LLVM 2.8 and started playing with the optimizations a
bit.
I saw something curious while trying the following function:

int g(unsigned int a) {
unsigned int c[100];
c[10] = a;
c[11] = a;
unsigned int b = c[10] + c[11];
 if(b > a*2) a = 4;
else a = 8;
return a + 7;
}

The generated code, with -O3 activated, is

define i32 @g(i32 a) nounwind readnone {
       %add = shl i32 %a, 1
       %mul = shl i32 %a, 1
       %cmp = icmp ugt i32 %add, %mul
       %a.addr.0 = select i1 %cmp, i32 11, i32 15
       ret i32 %a.addr.0
}

I find it strange that it hasn't found that %add and %mul have the same
value, %cmp would be then false, selecting and returning 15. If 'a' is
replaced by a constant it works.

I'm also curios which pass detects that c[10] and c[11] are 'a' in 'b =
c[10] + c[11]' (it isn't instcombine, at -O1 getelementptr/load are still
there).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20101228/bbbcfddd/attachment.html>


More information about the llvm-dev mailing list