[llvm-bugs] [Bug 41344] New: [missed-optimization] compiler fails to de-virtualize within a single TU

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Apr 2 04:08:56 PDT 2019


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

            Bug ID: 41344
           Summary: [missed-optimization] compiler fails to de-virtualize
                    within a single TU
           Product: clang
           Version: 8.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: eyalroz at technion.ac.il
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Related StackOverflow question: https://stackoverflow.com/q/55464578/1593077
GodBolt example: https://godbolt.org/z/l0vdFG

In the following code:

  struct A {
      virtual A& operator+=(const A& other) noexcept = 0;
  };

  void foo_inner(int *p) noexcept { *p += *p; }
  void foo_virtual_inner(A *p) noexcept { *p += *p; }

  void foo(int *p) noexcept
  {
      return foo_inner(p);
  } 

  struct Aint : public A {
      int i;
      A& operator+=(const A& other) noexcept override final
      { 
          i += dynamic_cast<const Aint&>(other).i; 
  //      i += reinterpret_cast<const Aint&>(other).i; 
          return *this;
      }
  };

   void foo_virtual(Aint *p) noexcept
   {
       return foo_virtual_inner(p);
   }

Both functions, `foo()` and `foo_virtual()`, should compile to the same thing.
But clang++ 8.0.0 (and whatever GodBolt has as trunk), produces:
```
foo(int*):                               # @foo(int*)
        shl     dword ptr [rdi]
        ret
foo_virtual(Aint*):                  # @foo_virtual(Aint*)
        mov     rax, qword ptr [rdi]
        mov     rax, qword ptr [rax]
        mov     rsi, rdi
        jmp     rax     
```
i.e. clang++ doesn't manage to de-virtualize `Aint::operator+=` - although it
really should. It has all the necessary information AFAICT.

-- 
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/20190402/81d8fffc/attachment.html>


More information about the llvm-bugs mailing list