Skip to content

Commit 7a3dec4

Browse files
[Clang] Mark this pointer in destructors dead_on_return
This helps to clean up any dead stores that come up at the end of the destructor. The motivating example was a refactoring in libc++'s basic_string implementation in 8dae17b that added a zeroing store into the destructor, causing a large performance regression on an internal workload.
1 parent 46c34be commit 7a3dec4

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2767,7 +2767,8 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
27672767
}
27682768

27692769
// Apply `nonnull`, `dereferenceable(N)` and `align N` to the `this` argument,
2770-
// unless this is a thunk function.
2770+
// unless this is a thunk function. Add dead_on_return to the `this` argument
2771+
// in base class destructors to aid in DSE.
27712772
// FIXME: fix this properly, https://reviews.llvm.org/D100388
27722773
if (FI.isInstanceMethod() && !IRFunctionArgs.hasInallocaArg() &&
27732774
!FI.arg_begin()->type->isVoidPointerType() && !IsThunk) {
@@ -2800,6 +2801,15 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
28002801
.getAsAlign();
28012802
Attrs.addAlignmentAttr(Alignment);
28022803

2804+
if (isa_and_nonnull<CXXDestructorDecl>(
2805+
CalleeInfo.getCalleeDecl().getDecl())) {
2806+
auto *ClassDecl = dyn_cast<CXXRecordDecl>(
2807+
CalleeInfo.getCalleeDecl().getDecl()->getDeclContext());
2808+
if (ClassDecl->getNumBases() == 0 && ClassDecl->getNumVBases() == 0) {
2809+
Attrs.addAttribute(llvm::Attribute::DeadOnReturn);
2810+
}
2811+
}
2812+
28032813
ArgAttrs[IRArgs.first] = llvm::AttributeSet::get(getLLVMContext(), Attrs);
28042814
}
28052815

0 commit comments

Comments
 (0)