0

I figured out that the g++ compiler generates an assembly code hardly without any push/pop instructions. It only uses those when getting in/out a function. Everytime it emplaces bytes in the stack, it makes 2 or 3 instruction, for example:

movl    foo, %eax
subl    $4, %esp
movl    %eax, (%esp)`

Insted of just pushl foo. Is there a reason for that? Is it faster or something?

Thank you.

MathieuSnd
  • 23
  • 5
  • 1
    can you give a sample function that compiles to that? gcc has a known missed optimization of not pushing to create + initialize variables, because it wants to establish a stack frame once instead of emitting more `.eh_frame` unwind info every time ESP changes. [What C/C++ compiler can use push pop instructions for creating local variables, instead of just increasing esp once?](https://stackoverflow.com/q/49485395). But it does use `push` for function args in stack-args calling conventions, unless you use tuning options for old CPUs (without a stack engine). – Peter Cordes Dec 09 '18 at 12:52
  • Anyway no, it's not faster. `pushl foo` would save 1 fused-domain uop on Haswell, for example, and one back-end uop thanks to the stack engine. (Unless ESP was already in sync from an instruction that accessed it directly, in which case using push again will result in needing another stack-sync uop when you do use an ESP-relative addressing mode to access locals in a function with `-fomit-frame-pointer`). https://agner.org/optimize/ – Peter Cordes Dec 09 '18 at 12:55
  • 1
    Out of curiosity is this a G++ compiler targeting windows? Knowing your gCC version and platform would be interesting to know. – Michael Petch Dec 09 '18 at 13:16
  • This is a mingw g++, targeting win32. This code is used to send the parameter to the exit function, maybe it's related – MathieuSnd Dec 09 '18 at 13:33
  • Do you have a sample C++ file that generates something like this code? It would helpful to understand source code that would lead to this. Are you compiling with any special parameters? Optimizations on? – Michael Petch Dec 09 '18 at 15:38
  • [Why use push/pop instead of sub and mov?](https://stackoverflow.com/q/60872952) talks about more plausible code-gen with GCC tuning options that make it avoid `push`; it won't adjust ESP separately multiple times, it will move ESP once for the whole function, normally. (`-maccumulate-outgoing-args`) – Peter Cordes Nov 09 '22 at 21:31

0 Answers0