[LLVMdev] Missed optimization opportunity

Chris Lattner clattner at apple.com
Tue Dec 28 10:48:55 PST 2010


On Dec 28, 2010, at 9:39 AM, Lup Gratian wrote:

> 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.

You're right, that is a missed optimization.  I added it to the missed optimization notes in r122603.  Did this come from a larger example, or was this just a test?

> 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).


There are several capable of picking this up, but GVN+MemDep is probably what you want.

-Chris






More information about the llvm-dev mailing list