title: Do C compilers de-duplicate (merge) code?
body: <p>Loop unrolling is a common optimization, but is the reverse done too?<br><em>(to reduce size of the object file output, a smaller binary).</em></p>
  
  <p>I'm curious if it's a common technique for compilers to de-duplicate successive, identical blocks of code (or function calls) into a loop, or extract a duplicate block into a static function.</p>
  
  <p>I'm interested because there are header-only libraries<code>*</code> in C which can add a lot of duplicate code, so it would be useful to know if some C compilers are able to detect this and handle it more efficiently.<br></p>
  
  <p><code>*</code> <em>By header-only-library, I mean a header that defines code directly rather than function definitions.</em></p>
  
  <p>And if this is done, it would be useful to know under what conditions &amp; constraints apply to ensure it can be made use of.</p>
  
  <p>Note (For the purpose of the question - any popular C compiler is fine GCC/Clang/Intel/MSVC).</p>
  
  <p>A header-only library I found, called <strong>uthash</strong> uses some very large macros, and I wanted to know if there was some compiler trickery going on that could cleverly de-duplicate such huge blocks of code, see: eg <a href="https://github.com/troydhanson/uthash/blob/master/src/uthash.h" rel="nofollow">uthash.h</a>, another similar example is <a href="http://www.corpit.ru/mjt/qsort.html" rel="nofollow">inline qsort.h</a></p>
  
  <hr>
  
  <p>Example of a block that could be de-duplicated (it turns out <code>Py_DECREF</code> can expand into a fairly large block of code).</p>
  
  <pre><code>#define PY_ADD_TO_DICT(dict, i)  \
      do {
          PyDict_SetItemString(dict, names[i], item = PyUnicode_FromString(values[i])); \
          Py_DECREF(item); \
      } while (0)
  
  /* this could be made into a loop */
  PY_ADD_TO_DICT(d, 0);
  PY_ADD_TO_DICT(d, 1);
  PY_ADD_TO_DICT(d, 2);
  PY_ADD_TO_DICT(d, 3);
  </code></pre>
  
  <p><em>Note, this is contrived, but based on a real example.</em></p>
  
  <hr>
  
  <h2>Clarification</h2>
  
  <p>It seems the short answer to my question is <strong>no</strong> (or only in some limited / trivial cases), just to clarify why I was asking a bit further.</p>
  
  <p>Some replies in comments seem to assume anyone you would just refactor code into a function.<br>
  This is <strong>almost-always</strong> the best option of course, nevertheless there are times when blocks of very similar code can show up.</p>
  
  <ul>
  <li>boiler plate code created by a not-very-smart code generator.</li>
  <li>when using an external API which exposes some of its functionality as macros <em>(in this case wrapping locally in functions works in most cases of course, but this means your code gets its own quirks which aren't typical for uses of those API's)</em>.</li>
  <li>when you can't replace macros with functions, there are some rare cases where it just ends up being impractical to do this.</li>
  <li>when importing code from an external code-base, its not always ideal to go in and start cleaning up their code, when evaluating that code-base its useful to have an idea how smart the compiler will be at optimizing the code.</li>
  </ul>
  
  <p>In all these cases its possible to de-duplicate <em>(generate smarter code, wrap macros in functions, patch 3rd party libraries)</em>, but before going making an effort to do such things its worth knowing how much work the compiler is doing for us.</p>