[PATCH] D43430: Omit nullptr check for sufficiently simple delete-expressions

Andrew Hunter via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 17 00:22:18 PST 2018


ahh created this revision.
ahh added a reviewer: rsmith.
Herald added a subscriber: cfe-commits.

[expr.delete] is pretty crystal clear that it's OK to invoke a
deallocation-function on a nullptr delete-expression:

"If the value of the operand of the delete-expression is a null
pointer value, it is unspecified whether a deallocation function will
be called as described above."

Even so, we currently check against nullptr unconditionally. This is
wasteful for anything with a trivial destructor; deleting nullptr is
very rare so it's not worth saving the call to ::operator delete, and
this is a useless branch (and waste of code size) in the common case.

Condition emission of the branch on us needing to actually look
through the pointer for a vtable, size cookie, or nontrivial
destructor.  (In principle a nontrivial destructor with no side
effects that didn't touch the object would also be safe to run
unconditionally, but I don't know how to test we have one of those and
who in the world would write one?)

On an important and very large (~500 MiB) Google search binary, this
saves approximately 32 KiB of text. Okay, it's not impressive in a
relative sense, but it's still the right thing to do.

A note on optimization: we still successfully elide delete-expressions of
(literal) nullptr.  Where before they were stuck behind a never-taken branch,
now they reduce (effectively) to calls to __builtin_operator_delete(nullptr),
which EarlyCSE is capable of optimizing out. So this has no cost in the
already well-optimized case.


Repository:
  rC Clang

https://reviews.llvm.org/D43430

Files:
  lib/CodeGen/CGCXXABI.h
  lib/CodeGen/CGExprCXX.cpp
  test/CodeGenCXX/cxx2a-destroying-delete.cpp
  test/CodeGenCXX/delete-two-arg.cpp
  test/CodeGenCXX/delete.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43430.134788.patch
Type: text/x-patch
Size: 6771 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180217/1bcb3754/attachment-0001.bin>


More information about the cfe-commits mailing list