title: Avoiding gcc function prologue overhead?
body: <p>I've lately encountered a lot of functions where gcc generates really bad code on x86. They all fit a pattern of:</p>
  
  <pre><code>if (some_condition) {
      /* do something really simple and return */
  } else {
      /* something complex that needs lots of registers */
  }
  </code></pre>
  
  <p>Think of simple case as something so small that half or more of the work is spent pushing and popping registers that won't be modified at all. If I were writing the asm by hand, I would save and restore the saved-across-calls registers inside the complex case, and avoid touching the stack pointer at all in the simple case.</p>
  
  <p>Is there any way to get gcc to be a little bit smarter and do this itself? Preferably with command line options rather than ugly hacks in the source...</p>
  
  <p><strong>Edit:</strong> To make it concrete, here's something very close to some of the functions I'm dealing with:</p>
  
  <pre><code>if (buf-&gt;pos &lt; buf-&gt;end) {
      return *buf-&gt;pos++;
  } else {
      /* fill buffer */
  }
  </code></pre>
  
  <p>and another one:</p>
  
  <pre><code>if (!initialized) {
      /* complex initialization procedure */
  }
  return &amp;initialized_object;
  </code></pre>
  
  <p>and another:</p>
  
  <pre><code>if (mutex-&gt;type == SIMPLE) {
      return atomic_swap(&amp;mutex-&gt;lock, 1);
  } else {
      /* deal with ownership, etc. */
  }
  </code></pre>
  
  <p><strong>Edit 2:</strong> I should have mentioned to begin with: these functions cannot be inlined. They have external linkage and they're library code. Allowing them to be inlined in the application would result in all kinds of problems.</p>