<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 --- - Failure to exploit final modifier for pointer to member devirtualization"
   href="https://llvm.org/bugs/show_bug.cgi?id=23195">23195</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Failure to exploit final modifier for pointer to member devirtualization
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>listmail@philipreames.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>We appear to be failing to devirtualize a pointer-to-member call when we know
that the type of the receiver is a final class.

(NOTE: This bug report is based on data collected on a ToT build in early
March.  I have not confirmed that this still applies to ToT today.)

I originally noted this when writing a toy interpreter.  Here's a simplified
bit of code pulled from that project:
struct Interpreter final {
  typedef void (Interpreter::*BytecodeFuncType)(void *);
  BytecodeFuncType BytecodeFuncs[NumBytecodes] = { ...  };

  void dispatch_next(void* stack){
    BytecodeFuncType NextFunc = BytecodeFuncs[bytecode[current_index]];
    current_index++;
    (this->*NextFunc)(stack);
  }
};

The important part is this line:
(this->*NextFunc)(stack);

This is calling a pointer-to-member function where the receiver type is
statically known.  In particular, we know that "this" is a static type of
Interpreter and that, because of the final annotation, there are no other
possible dynamic types.

We emit LLVM IR that looks like this (after -O3):
  %13 = load { i64, i64 }* %arrayidx3.i, align 8, !tbaa !10
  %.fca.0.extract.i = extractvalue { i64, i64 } %13, 0
  %.fca.1.extract.i = extractvalue { i64, i64 } %13, 1
  %14 = bitcast %struct.Interpreter* %this to i8*
  %15 = getelementptr inbounds i8* %14, i64 %.fca.1.extract.i
  %this.adjusted.i = bitcast i8* %15 to %struct.Interpreter*
  %16 = and i64 %.fca.0.extract.i, 1
  %memptr.isvirtual.i = icmp eq i64 %16, 0
  br i1 %memptr.isvirtual.i, label %memptr.nonvirtual.i, label
%memptr.virtual.i

memptr.virtual.i:                                 ; preds =
%_ZNSt6vectorIlSaIlEE9push_backEOl.exit
  %17 = bitcast i8* %15 to i8**
  %vtable.i = load i8** %17, align 8, !tbaa !11
  %18 = add i64 %.fca.0.extract.i, -1
  %19 = getelementptr i8* %vtable.i, i64 %18
  %20 = bitcast i8* %19 to void (%struct.Interpreter*, %"class.std::vector"*)**
  %memptr.virtualfn.i = load void (%struct.Interpreter*,
%"class.std::vector"*)** %20, align 8
  br label %_ZN11Interpreter13dispatch_nextERSt6vectorIlSaIlEE.exit

memptr.nonvirtual.i:                              ; preds =
%_ZNSt6vectorIlSaIlEE9push_backEOl.exit
  %memptr.nonvirtualfn.i = inttoptr i64 %.fca.0.extract.i to void
(%struct.Interpreter*, %"class.std::vector"*)*
  br label %_ZN11Interpreter13dispatch_nextERSt6vectorIlSaIlEE.exit

_ZN11Interpreter13dispatch_nextERSt6vectorIlSaIlEE.exit: ; preds =
%memptr.virtual.i, %memptr.nonvirtual.i
  %21 = phi void (%struct.Interpreter*, %"class.std::vector"*)* [
%memptr.virtualfn.i, %memptr.virtual.i ], [ %memptr.nonvirtualfn.i,
%memptr.nonvirtual.i ]
  tail call void %21(%struct.Interpreter* %this.adjusted.i,
%"class.std::vector"* dereferenceable(24) %stack)
  ret void

The virtual dispatch path of this control flow is impossible. Similarly, all of
the this adjustment code is unnecessary.</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>