[LLVMbugs] [Bug 9181] New: Missed optimization on empty virtual destructors

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Feb 9 14:01:47 PST 2011


http://llvm.org/bugs/show_bug.cgi?id=9181

           Summary: Missed optimization on empty virtual destructors
           Product: clang
           Version: trunk
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: jyasskin at google.com
                CC: llvmbugs at cs.uiuc.edu


At the beginning of every virtual destructor, clang writes the class's vtable
into the objects vptr. If the base class is empty, though, it doesn't need to
do this, since after the return or delete call, the object is dead, and any
read of the vptr is undefined behavior.

$ cat test.cc
struct Base {
  virtual ~Base() {}
};

struct Derived : Base {
  ~Derived();
};
Derived::~Derived() {}
$ clang++ -O3 -S -fverbose-asm test.cc -o -
...
_ZN7DerivedD0Ev:                        # @_ZN7DerivedD0Ev
# BB#0:                                 # %invoke.cont
        movq    $_ZTV4Base+16, (%rdi)
        jmp     _ZdlPv                  # TAILCALL
...
_ZN7DerivedD2Ev:                        # @_ZN7DerivedD2Ev
# BB#0:                                 # %entry
        movq    $_ZTV4Base+16, (%rdi)
        ret


-emit-llvm shows:

define void @_ZN7DerivedD2Ev(%struct.Derived* %this) unnamed_addr align 2 {
entry:
  %this.addr = alloca %struct.Derived*, align 8
  store %struct.Derived* %this, %struct.Derived** %this.addr, align 8
  %this1 = load %struct.Derived** %this.addr
  %0 = bitcast %struct.Derived* %this1 to i8***
  store i8** getelementptr inbounds ([4 x i8*]* @_ZTV7Derived, i64 0, i64 2),
i8*** %0
  %1 = bitcast %struct.Derived* %this1 to %struct.Base*
  call void @_ZN4BaseD2Ev(%struct.Base* %1)
  ret void
}
define linkonce_odr void @_ZN4BaseD2Ev(%struct.Base* %this) unnamed_addr
nounwind align 2 {
entry:
  %this.addr = alloca %struct.Base*, align 8
  store %struct.Base* %this, %struct.Base** %this.addr, align 8
  %this1 = load %struct.Base** %this.addr
  %0 = bitcast %struct.Base* %this1 to i8***
  store i8** getelementptr inbounds ([4 x i8*]* @_ZTV4Base, i64 0, i64 2),
i8*** %0
  ret void
}


I think this could be fixed by calling 
@llvm.lifetime.end(sizeof(Base), %this1) at the end of @_ZN4BaseD2Ev and
@llvm.lifetime.end(sizeof(Derived)-sizeof(Base), %this1+sizeof(Base)) in
@_ZN7DerivedD2Ev before the call to @_ZN4BaseD2Ev.

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list