[LLVMdev] Optimization opportunity

Jeff Cohen jeffc at jolt-lang.org
Thu Aug 26 23:16:43 PDT 2004


There seems to be a disadvantage to the approach of allocating all
locals on the stack using alloca.  Consider the following code:

extern void func(int*);
extern int xyz();

void abc()
{
	int a = xyz();
	int i;

	{
		int b[10];
		for (i = 0; i < 10; i++) b[i] = xyz();
		func(b);
	}

	{
		int c[10];
		for (i = 0; i < 10; i++) c[i] = xyz();
		func(c);
	}

	func(&a);
}

We have two arrays, b and c, only one of which can exist at any given
time.   Both g++ and Microsoft VC++ map both arrays to the same bytes in
the stack frame.  LLVM doesn't.  As there isn't anything in the LLVM
assembly code to mark the scope of locals, it will take some analysis to
determine which locals can share the same memory locations.

Also, the store into the arrays generates two x86 machine instructions:

	lea %ECX, DWORD PTR [%ESP + 16]
	mov DWORD PTR [%ECX + <element offset>], %EAX

These can be combined into a single instruction.  I'm tempted to work on
this one myself :)

On the plus side, only LLVM unrolled the loops.




More information about the llvm-dev mailing list