A question about nonnull value of auto_ptr

Tim Northover t.p.northover at gmail.com
Tue Nov 4 19:01:41 PST 2014


> Yes, the program here is not well defined. Calling operator*() on an auto_ptr, according to the standard, "Requires: get() != 0
> ". (see D.10.1.2). Generally speaking, binding a reference to a null pointer is undefined behavior. Changing the program to this->pc.get() != 0 should fix the problem.

There's quite a nasty interaction with alwaysinline here, which made
-emit-llvm results differ from single stage compilation and took me a
while to see through.

Clang initially produces (roughly):

define @C::f() {
  %ref = call dereferenceable(8) "auto_ptr::operator *"()
  icmp %ref, 0
}

at O0, the call gets inlined (it's alwaysinline) and all trace of
dereferenceable disappears. "opt -O3" will not resurrect it. At O3,
the comparison gets folded, and *then* the call gets inlined.

The only way I found it was with "-mllvm -print-after-all".

But I agree it's a valid optimization: auto_ptr::operator* returns a
reference, which means you have to know the pointer is good before
using it.

Cheers.

Tim.



More information about the llvm-commits mailing list