public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug inline-asm/67944] New: GCC emits unnecessary push/pop for callee-save reads.
@ 2015-10-12 20:48 alex.reinking at gmail dot com
  2015-10-12 21:39 ` [Bug inline-asm/67944] " segher at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: alex.reinking at gmail dot com @ 2015-10-12 20:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67944

            Bug ID: 67944
           Summary: GCC emits unnecessary push/pop for callee-save reads.
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: inline-asm
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alex.reinking at gmail dot com
  Target Milestone: ---

When trying to return the current value of a register in a function via a local
register variable, the compiler will emit a useless push/pop instruction as in 
the following example:

C code:

    unsigned int readEBX(void) {
        register unsigned int reg asm("ebx");
        return reg;
    }

Assembly:

    readEBX():
        mov  eax, ebx
        push ebx
        pop  ebx
        ret

This same pattern occurs for all callee-save registers: ebx, esi, edi, ebp
The code is properly-optimized for: eax, ecx, edx, esp

See also my StackOverflow question: 
See also this Godbolt* result: https://goo.gl/bzgt4P

  * Try substituting different registers to compare


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug inline-asm/67944] GCC emits unnecessary push/pop for callee-save reads.
  2015-10-12 20:48 [Bug inline-asm/67944] New: GCC emits unnecessary push/pop for callee-save reads alex.reinking at gmail dot com
@ 2015-10-12 21:39 ` segher at gcc dot gnu.org
  2015-10-12 23:34 ` alex.reinking at gmail dot com
  2015-10-13 13:09 ` segher at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: segher at gcc dot gnu.org @ 2015-10-12 21:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67944

Segher Boessenkool <segher at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |segher at gcc dot gnu.org

--- Comment #1 from Segher Boessenkool <segher at gcc dot gnu.org> ---
You're returning an uninitialised variable.  What do you expect this
code to do?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug inline-asm/67944] GCC emits unnecessary push/pop for callee-save reads.
  2015-10-12 20:48 [Bug inline-asm/67944] New: GCC emits unnecessary push/pop for callee-save reads alex.reinking at gmail dot com
  2015-10-12 21:39 ` [Bug inline-asm/67944] " segher at gcc dot gnu.org
@ 2015-10-12 23:34 ` alex.reinking at gmail dot com
  2015-10-13 13:09 ` segher at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: alex.reinking at gmail dot com @ 2015-10-12 23:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67944

--- Comment #2 from Alex Reinking <alex.reinking at gmail dot com> ---
I expect it to return whatever is currently stored in ebx. Which it does, but
suboptimally. In my particular case, ebx is populated by a system call. I
wanted to put the inline assembly for retrieving that value in its own function
so I wouldn't have to remember the local register variable syntax.

The code I posted is simply a somehow minimal example of the anomalous assembly
generated. The question here is more "Why does gcc emit a push ebx; pop ebx?"
than "How should I retrieve whatever is currently in ebx?"


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug inline-asm/67944] GCC emits unnecessary push/pop for callee-save reads.
  2015-10-12 20:48 [Bug inline-asm/67944] New: GCC emits unnecessary push/pop for callee-save reads alex.reinking at gmail dot com
  2015-10-12 21:39 ` [Bug inline-asm/67944] " segher at gcc dot gnu.org
  2015-10-12 23:34 ` alex.reinking at gmail dot com
@ 2015-10-13 13:09 ` segher at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: segher at gcc dot gnu.org @ 2015-10-13 13:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67944

--- Comment #3 from Segher Boessenkool <segher at gcc dot gnu.org> ---
Your expectation is not in line with what the compiler actually promises
you: the only guarantee is that the local register variable will be in
(in your case) ebx _where used in an asm statement_.

Other than that, it is just a local variable.

You get a save/restore sequence in the prologue and epilogue because ebx
is a non-volatile register and this function uses that register.  Scheduling
later moves the push later in the code.

If you want to simply grab what is in ebx, you can use asm, like

long reg; asm("mov %%ebx,%0" : "=r"(reg));


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-10-13 13:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-12 20:48 [Bug inline-asm/67944] New: GCC emits unnecessary push/pop for callee-save reads alex.reinking at gmail dot com
2015-10-12 21:39 ` [Bug inline-asm/67944] " segher at gcc dot gnu.org
2015-10-12 23:34 ` alex.reinking at gmail dot com
2015-10-13 13:09 ` segher at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).