[llvm-bugs] [Bug 37611] New: nullptr check after pointer dereference not removed

via llvm-bugs llvm-bugs at lists.llvm.org
Mon May 28 06:47:27 PDT 2018


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

            Bug ID: 37611
           Summary: nullptr check after pointer dereference not removed
           Product: libraries
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: dave at znu.io
                CC: llvm-bugs at lists.llvm.org

The following code is the distillation of C++ code expansion. This also
represents a decent performance opportunity for LLVM itself, given that this
bug affects LLVM's casting machinery.

The following code should generate the same assembly, regardless of whether it
is '#if 0' or '#if 1'. The correct code assembly code gen is the '#if 0' case.
The "bad" code generation includes needless nullptr preservation logic *after*
the pointer was dereferenced, thus proving the pointer is not null and need not
be preserved.

struct A { long a; };
struct B { long b; };
struct C : public A, public B { long c; };
struct Other { B *b; };

#if 1
C *cast_wrapper(B *b) {
        return static_cast<C*>(b);
}
#else
#define cast_wrapper(b) static_cast<C*>(b)
#endif

long example(Other *o) {
        auto b = o->b;
        if (b->b != 42)
                __builtin_trap();
        return cast_wrapper(b)->a;
}

Here is a diff of the IR output. Negative is "bad" and positive is "good":

@@ -31,21 +20,16 @@
   unreachable

 ; <label>:8:                                      ; preds = %1
-  %9 = icmp eq %struct.B* %3, null
-  %10 = getelementptr inbounds %struct.B, %struct.B* %3, i64 -1
-  %11 = bitcast %struct.B* %10 to %struct.C*
-  %12 = select i1 %9, %struct.C* null, %struct.C* %11
-  %13 = getelementptr inbounds %struct.C, %struct.C* %12, i64 0, i32 0, i32 0
-  %14 = load i64, i64* %13, align 8, !tbaa !10
-  ret i64 %14
+  %9 = getelementptr inbounds %struct.B, %struct.B* %3, i64 -1, i32 0
+  %10 = load i64, i64* %9, align 8, !tbaa !10
+  ret i64 %10
 }

 ; Function Attrs: noreturn nounwind

-- 
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/20180528/82e428e8/attachment.html>


More information about the llvm-bugs mailing list