[llvm-bugs] [Bug 35909] New: static_cast<T*>(this) does not propagate non-null invariant of 'this'

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jan 11 07:37:10 PST 2018


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

            Bug ID: 35909
           Summary: static_cast<T*>(this) does not propagate non-null
                    invariant of 'this'
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: dave at znu.io
                CC: llvm-bugs at lists.llvm.org

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

-- 
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/20180111/e178a0d4/attachment.html>


More information about the llvm-bugs mailing list