[llvm-dev] alloca combining, not (yet) possible ?

Nat! via llvm-dev llvm-dev at lists.llvm.org
Mon Aug 31 04:49:22 PDT 2015


Hello

since my broad RFC request didn't catch any responses, let me get a bit 
more into the nitty-gritty:

I tried to get llvm (3.7) to optimize superflous allocas away, but so 
far I haven't figured out how. Is there no optimizer for this ? Should I 
adorn something with some attributes ? As far as I can tell no, but I am 
no llvm expert...

For what I want to do, i will probably have a few dozen combinable 
allocas in my produced code each function. So it would be worthwhile to 
reduce stack space needs.

Ciao
    Nat!

---
%struct.a_b = type { i32, i32 }

; Function Attrs: nounwind ssp uwtable
define void @g() #0 {
entry:
   %x = alloca %struct.a_b, align 4
   %y = alloca %struct.a_b, align 4
   %a = getelementptr inbounds %struct.a_b, %struct.a_b* %x, i32 0, i32 0
   store i32 1, i32* %a, align 4
   %b = getelementptr inbounds %struct.a_b, %struct.a_b* %x, i32 0, i32 1
   store i32 2, i32* %b, align 4
   call void @f(%struct.a_b* %x)
   %a1 = getelementptr inbounds %struct.a_b, %struct.a_b* %y, i32 0, i32 0
   store i32 1, i32* %a1, align 4
   %b2 = getelementptr inbounds %struct.a_b, %struct.a_b* %y, i32 0, i32 1
   store i32 3, i32* %b2, align 4
   call void @f(%struct.a_b* %y)
   ret void
}
---

produces the following x86_64-apple-macosx10.10.0 output (with -O4)

_g:
0000000000000000	pushq	%rbp
0000000000000001	movq	%rsp, %rbp
0000000000000004	subq	$0x10, %rsp
0000000000000008	movabsq	$0x200000001, %rax      ## imm = 0x200000001
0000000000000012	movq	%rax, -0x8(%rbp)

0000000000000016	leaq	-0x8(%rbp), %rdi
--------------------------------^^^^
000000000000001a	callq	0x1f
000000000000001f	movabsq	$0x300000001, %rax      ## imm = 0x300000001
0000000000000029	movq	%rax, -0x10(%rbp)

000000000000002d	leaq	-0x10(%rbp), %rdi
--------------------------------^^^^^

0000000000000031	callq	0x36
0000000000000036	addq	$0x10, %rsp
000000000000003a	popq	%rbp
000000000000003b	retq
000000000000003c	nopl	(%rax)


void g( void)
{
	struct a_b   x;
	struct a_b   y;

	x.a = 1;
	x.b = 2;
	f( &x);

	// x no longer needed
	// expect y to reuse x space
	y.a = 1;
	y.b = 3;
	f( &y);
}


More information about the llvm-dev mailing list