<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - static_cast<T*>(this) does not propagate non-null invariant of 'this'"
   href="https://bugs.llvm.org/show_bug.cgi?id=35909">35909</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>static_cast<T*>(this) does not propagate non-null invariant of 'this'
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

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

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </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>dave@znu.io
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>static_cast<T*>(this) does not propagate non-null invariant of 'this'.

Consider the following code (found during code gen analysis of a LLVM
subproject):

class A {
    int a;
};
class B {
    int b;
public:
    A *getAsA();
};
class X : public A, public B {
    int x;
};

A *B::getAsA() {
    if (b == 42) {
        auto temp = static_cast<X*>(this);
        //__builtin_assume(temp != nullptr);
        return temp;
    }
    return nullptr;
}

void helper(A *);

void test(B *b) {
    auto temp = b->getAsA();
    if (temp)
        return helper(temp);
}

WITHOUT the __builtin_assume(), the following suboptimal x86 code is generated:

        movq    %rdi, %rax
        addq    $-4, %rdi
        je      .LBB1_2
        cmpl    $42, (%rax)
        jne     .LBB1_2
        jmp     _Z6helperP1A            # TAILCALL
.LBB1_2:
        retq


WITH the __builtin_assume(), the following optimal x86 code is generated:

        cmpl    $42, (%rdi)
        jne     .LBB1_1
        addq    $-4, %rdi
        jmp     _Z6helperP1A            # TAILCALL
.LBB1_1:
        retq</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>