<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Clang uses __cxa_guard even in trivial cases where it isn't needed"
   href="https://llvm.org/bugs/show_bug.cgi?id=23053">23053</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang uses __cxa_guard even in trivial cases where it isn't needed
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ed@80386.nl
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following piece of code:

// ------------------
class Bunny {};

Bunny& GetBunny() {
  static Bunny bunny;
  return bunny;
}
// ------------------

When compiled, Clang is smart enough to just emit a static copy of a Bunny
object and implement GetBunny() as follows:

0000000000000000 <_Z8GetBunnyv>:
   0:   b8 00 00 00 00          mov    $0x0,%eax
   5:   c3                      retq   

Now consider the case where we add the following trivial constructor to Bunny:

// ----------
class Bunny {
 public:
  Bunny() {}
};
// ----------

Suddenly the GetBunny() function starts to look like this:

0000000000000000 <_Z8GetBunnyv>:
   0:   50                      push   %rax
   1:   8a 05 00 00 00 00       mov    0x0(%rip),%al        # 7
<_Z8GetBunnyv+0x7>
   7:   84 c0                   test   %al,%al
   9:   75 18                   jne    23 <_Z8GetBunnyv+0x23>
   b:   bf 00 00 00 00          mov    $0x0,%edi
  10:   e8 00 00 00 00          callq  15 <_Z8GetBunnyv+0x15>
  15:   85 c0                   test   %eax,%eax
  17:   74 0a                   je     23 <_Z8GetBunnyv+0x23>
  19:   bf 00 00 00 00          mov    $0x0,%edi
  1e:   e8 00 00 00 00          callq  23 <_Z8GetBunnyv+0x23>
  23:   b8 00 00 00 00          mov    $0x0,%eax
  28:   5a                      pop    %rdx
  29:   c3                      retq   

This is of course not needed, as the constructor is trivial. There is no need
to explicitly invoke it. In fact, if I make the constructor constexpr, Clang is
smart enough to reduce GetBunny() to the simple implementation shown above.
Clang should be able to use some smartness even if constexpr is not used.

Can reproduce this with the following versions of Clang:

FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512
clang version 3.7.0 (trunk 233268)</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>