<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 - nullptr check after pointer dereference not removed"
href="https://bugs.llvm.org/show_bug.cgi?id=37611">37611</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>nullptr check after pointer dereference not removed
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</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>Scalar Optimizations
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@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>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</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>