title: Constant combining in optimizing compilers
body: <p>I have a header file containing a lot of small inline functions. Most of them happen to have constant data. Since these functions are performance critical, the way they handle constants becomes important. AFAIK there are two ways to refer to constants:</p>
  
  <p>1) Define them in a separate source file that is later linked with the application. </p>
  
  <p>2) Define the constants in-place. </p>
  
  <p>I would choose the latter way because it's more maintainable. However, the it might be slower if the compiler doesn't optimize thousands of equal constants that are created by inlining.</p>
  
  <p><strong>The question:</strong></p>
  
  <p>Will the compiler combine these equal constants? In particular, which of the following methods will be utilized?</p>
  
  <p>1) Combining of equal constants across the compilation unit.<br>
  2) Combining of equal constants across the linking module (whole program or library)<br>
  3) Combining the constants with <em>any</em> static constant data that happens to have the same bit pattern and fulfills the alignment requirements across the compilation unit or whole program.</p>
  
  <p>I use a modern compiler (GCC4.5).</p>
  
  <p>I'm not an expert in assembler, thus I couldn't answer this question myself using several simple tests :)</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The constants are quite big (most of them at least 16 bytes), so the compiler can't make them immediate values.</p>
  
  <p><strong>EDIT2:</strong></p>
  
  <p><strong>EXAMPLE of the code</strong></p>
  
  <p>This one uses the constant in-place:</p>
  
  <pre><code>float_4 sign(float_4 a)
  {
      const __attribute__((aligned(16))) float mask[4] = { //I use a macro for this line
          0x80000000, 0x80000000, 0x80000000, 0x80000000};
      const int128 mask = load(mask);
      return b_and(a, mask);
  }
  </code></pre>