A question about nonnull value of auto_ptr

Jiangning Liu liujiangning1 at gmail.com
Tue Nov 4 18:23:13 PST 2014


For the following small test case,

#include <iostream>
#include <memory>

class C {
  std::auto_ptr<const C> pc;

  int f(void);

int C::f(void) {
  return (&*this->pc == 0);

clang/llvm -O2 optimizes (&*this->pc == 0) to be false statically, while
gcc doesn't.

So my question is if this optimization is really safe?

Front-end result shows the auto pointer is nonnull, i.e.
dereferenceable(8), and this is why llvm back-end can optimize the
comparison away, but the problem is can we really assume &*this->pc is not
0? Why?

; Function Attrs: nounwind uwtable
define i32 @_ZN1C1fEv(%class.C* %this) #2 align 2 {
  %this.addr = alloca %class.C*, align 8
  store %class.C* %this, %class.C** %this.addr, align 8
  %this1 = load %class.C** %this.addr
  %pc = getelementptr inbounds %class.C* %this1, i32 0, i32 0
  %call = call dereferenceable(8) %class.C*
@_ZNKSt8auto_ptrIK1CEdeEv(%"class.std::auto_ptr"* %pc) #1
  %cmp = icmp eq %class.C* %call, null
  %conv = zext i1 %cmp to i32
  ret i32 %conv

BTW, this issue triggered a run-time crash for one of SPEC benchmarks.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141105/177b92ef/attachment.html>

More information about the cfe-commits mailing list