[LLVMbugs] [Bug 23053] New: Clang uses __cxa_guard even in trivial cases where it isn't needed

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Mar 28 02:48:40 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=23053

            Bug ID: 23053
           Summary: Clang uses __cxa_guard even in trivial cases where it
                    isn't needed
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ed at 80386.nl
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150328/3865da9a/attachment.html>


More information about the llvm-bugs mailing list