[all-commits] [llvm/llvm-project] 72315d: Treat `std::move`, `forward`, etc. as builtins.

Richard Smith via All-commits all-commits at lists.llvm.org
Wed Apr 20 17:59:17 PDT 2022

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 72315d02c432a0fe0acae9c96c69eac8d8e1a9f6
  Author: Richard Smith <richard at metafoo.co.uk>
  Date:   2022-04-20 (Wed, 20 Apr 2022)

  Changed paths:
    M clang/docs/CommandGuide/clang.rst
    M clang/docs/ReleaseNotes.rst
    M clang/include/clang/Basic/Builtins.def
    M clang/include/clang/Basic/Builtins.h
    M clang/include/clang/Basic/DiagnosticSemaKinds.td
    M clang/lib/AST/ExprConstant.cpp
    M clang/lib/Analysis/BodyFarm.cpp
    M clang/lib/Basic/Builtins.cpp
    M clang/lib/CodeGen/CGBuiltin.cpp
    M clang/lib/CodeGen/CGCall.cpp
    M clang/lib/CodeGen/CGExpr.cpp
    M clang/lib/Sema/SemaChecking.cpp
    M clang/lib/Sema/SemaDecl.cpp
    M clang/lib/Sema/SemaExpr.cpp
    M clang/lib/Sema/SemaExprCXX.cpp
    M clang/lib/Sema/SemaInit.cpp
    M clang/lib/Sema/SemaOverload.cpp
    M clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
    M clang/test/Analysis/inner-pointer.cpp
    M clang/test/Analysis/use-after-move.cpp
    A clang/test/CodeGenCXX/builtin-std-move.cpp
    M clang/test/CodeGenCXX/builtins.cpp
    M clang/test/CodeGenCXX/microsoft-abi-throw.cpp
    A clang/test/SemaCXX/builtin-std-move-nobuiltin.cpp
    A clang/test/SemaCXX/builtin-std-move.cpp
    A clang/test/SemaCXX/libcxx_move_hack.cpp
    M clang/test/SemaCXX/unqualified-std-call-fixits.cpp
    M clang/test/SemaCXX/unqualified-std-call.cpp
    M clang/test/SemaCXX/warn-consumed-analysis.cpp
    M clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
    M libcxx/test/std/utilities/utility/forward/forward.fail.cpp

  Log Message:
  Treat `std::move`, `forward`, etc. as builtins.

This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.

We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.

This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.

We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.

In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.

The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.

This is a re-commit of
  64c045e25b8471bbb572bd29159c294a82a86a2, and
and reverts aa643f455a5362de7189eac630050d2c8aefe8f2.
This change also includes a workaround for users using libc++ 3.1 and
earlier (!!), as apparently happens on AIX, where std::move sometimes
returns by value.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D123345

Revert "Fixup D123950 to address revert of D123345"

This reverts commit aa643f455a5362de7189eac630050d2c8aefe8f2.

More information about the All-commits mailing list