I am reading Computer Systems: A Programmer's Perspective 3rd Edition by Randal E. Bryant and David R. O'Hallaron.
In chapter 3 and section 7.5, there is figure demonstrating how stack frames are allocated as follows:
I couldn't understand that why are line 4 and 12 needed. It seems that those lines are not required because the surplus 8 bytes of stack memory are not used at all.
As comment indicates, IMHO, it seems that it is inevitably allocated to align stack frame by 24 bytes:
pushq %rbp
and pushq %rbx
respectivelysubq $8, %rsp
So, my question can be summarized to "Why stack frames are aligned by 24 bytes?"
This is consistent with x64 ABI (both Microsoft and SystemV). Stack must be aligned on 16 bytes boundary before calling a function ("call Q" in your example). Let's assume that the stack is initially at a 16 bytes boundary. When the program reaches "P" label (due to a "call P" instruction), RSP points 8 byte below, because "call" subtracts 8 to RSP and saves RIP (8 bytes) at *RSP. Then, there are two "pushq" (line 2 and 3), each decreases RSP by 8, so RSP is still misaligned. That's why The compiler must subtract 8 to align RSP before executing "call Q" at line 8 and 10.
This article describes x86/x64 ABIs quite well: https://en.wikipedia.org/wiki/X86_calling_conventions