I would like to understand how LLVM decides what size memory regions allocated with alloca
should have. As can be seen in this example, when generating code for x86-64 it appears to always select a size that is an odd multiple of 8 bytes:
%0 = alloca [ 88 x i8] ; Allocates 88 bytes
%1 = alloca [ 96 x i8] ;
%2 = alloca [100 x i8] ; All allocate 104 bytes
%3 = alloca [104 x i8] ;
%4 = alloca [105 x i8] ; Allocates 120 bytes
So is this really the pattern that the code generator follows? And if so, why? Why not prefer even multiples of 8 or any multiple?
I tripped over this seemingly odd behaviour while trying to answer this question. It feels however as though I'm missing something regarding the size of the allocations.
lea rdi, [rsp+4]
for some), since all the space is getting touched anyway. In a leaf function with-mno-red-zone
you could observe placement by storing the address (likevolatile char*
) – Barracudacall
pushes an 8-byte return address. As explained in those two Q&As I linked,RSP % 16 == 8
is guaranteed on function entry becauseRSP % 16 == 0
is required before acall
. (Both AMD64 SysV and Windows x64 require that.) – Barracuda