2007-01-28 08:20:04 +00:00
|
|
|
//===--- Builtins.cpp - Builtin function implementation -------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-01-28 08:20:04 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements various things for builtin functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-14 01:05:48 +00:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2007-10-07 08:58:51 +00:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2010-11-30 17:35:24 +00:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2013-07-23 00:13:01 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2007-01-28 08:20:04 +00:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static const Builtin::Info BuiltinInfo[] = {
|
2015-08-07 05:14:44 +00:00
|
|
|
{ "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
|
|
|
|
#define BUILTIN(ID, TYPE, ATTRS) \
|
|
|
|
{ #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
|
2015-08-05 21:04:28 +00:00
|
|
|
#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
|
2015-08-07 05:14:44 +00:00
|
|
|
{ #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
|
2015-08-05 21:04:28 +00:00
|
|
|
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
|
2015-08-07 05:14:44 +00:00
|
|
|
{ #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
|
2009-06-14 01:05:48 +00:00
|
|
|
#include "clang/Basic/Builtins.def"
|
2007-01-28 08:20:04 +00:00
|
|
|
};
|
|
|
|
|
2015-08-06 01:01:12 +00:00
|
|
|
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
|
2007-01-29 05:24:35 +00:00
|
|
|
if (ID < Builtin::FirstTSBuiltin)
|
|
|
|
return BuiltinInfo[ID];
|
2015-10-19 04:51:35 +00:00
|
|
|
assert(((ID - Builtin::FirstTSBuiltin) <
|
|
|
|
(TSRecords.size() + AuxTSRecords.size())) &&
|
2015-09-22 17:23:22 +00:00
|
|
|
"Invalid builtin ID!");
|
|
|
|
if (isAuxBuiltinID(ID))
|
|
|
|
return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
|
2007-01-29 05:24:35 +00:00
|
|
|
return TSRecords[ID - Builtin::FirstTSBuiltin];
|
2007-01-28 08:20:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:23:22 +00:00
|
|
|
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
|
|
|
|
const TargetInfo *AuxTarget) {
|
2015-10-19 04:51:35 +00:00
|
|
|
assert(TSRecords.empty() && "Already initialized target?");
|
|
|
|
TSRecords = Target.getTargetBuiltins();
|
2015-09-22 17:23:22 +00:00
|
|
|
if (AuxTarget)
|
2015-10-19 04:51:35 +00:00
|
|
|
AuxTSRecords = AuxTarget->getTargetBuiltins();
|
2009-06-16 16:18:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 17:28:34 +01:00
|
|
|
bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
|
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
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c, and
64c045e25b8471bbb572bd29159c294a82a86a25
which were reverted in
e75d8b70370435b0ad10388afba0df45fcf9bfcc
due to a crasher bug where CodeGen would emit a builtin glvalue as an
rvalue if it constant-folds.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
2022-04-17 13:09:42 -07:00
|
|
|
bool InStdNamespace = FuncName.consume_front("std-");
|
|
|
|
for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin;
|
|
|
|
++i) {
|
|
|
|
if (FuncName.equals(BuiltinInfo[i].Name) &&
|
|
|
|
(bool)strchr(BuiltinInfo[i].Attributes, 'z') == InStdNamespace)
|
2016-01-06 14:35:46 +00:00
|
|
|
return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
|
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
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c, and
64c045e25b8471bbb572bd29159c294a82a86a25
which were reverted in
e75d8b70370435b0ad10388afba0df45fcf9bfcc
due to a crasher bug where CodeGen would emit a builtin glvalue as an
rvalue if it constant-folds.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
2022-04-17 13:09:42 -07:00
|
|
|
}
|
2016-01-06 14:35:46 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c, and
64c045e25b8471bbb572bd29159c294a82a86a25
which were reverted in
e75d8b70370435b0ad10388afba0df45fcf9bfcc
due to a crasher bug where CodeGen would emit a builtin glvalue as an
rvalue if it constant-folds.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
2022-04-17 13:09:42 -07:00
|
|
|
/// Is this builtin supported according to the given language options?
|
|
|
|
static bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
|
|
|
|
const LangOptions &LangOpts) {
|
2016-01-06 14:35:46 +00:00
|
|
|
bool BuiltinsUnsupported =
|
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
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c, and
64c045e25b8471bbb572bd29159c294a82a86a25
which were reverted in
e75d8b70370435b0ad10388afba0df45fcf9bfcc
due to a crasher bug where CodeGen would emit a builtin glvalue as an
rvalue if it constant-folds.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
2022-04-17 13:09:42 -07:00
|
|
|
LangOpts.NoBuiltin && strchr(BuiltinInfo.Attributes, 'f') != nullptr;
|
2021-05-20 12:37:26 -07:00
|
|
|
bool CorBuiltinsUnsupported =
|
|
|
|
!LangOpts.Coroutines && (BuiltinInfo.Langs & COR_LANG);
|
2013-07-23 00:13:01 +00:00
|
|
|
bool MathBuiltinsUnsupported =
|
2015-09-14 14:08:18 +00:00
|
|
|
LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
|
2013-07-23 00:13:01 +00:00
|
|
|
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
|
2015-08-05 21:04:28 +00:00
|
|
|
bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
|
|
|
|
bool MSModeUnsupported =
|
|
|
|
!LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
|
2018-10-30 20:31:30 +00:00
|
|
|
bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
|
2022-02-07 15:45:42 +03:00
|
|
|
bool OclCUnsupported =
|
|
|
|
!LangOpts.OpenCL && (BuiltinInfo.Langs & ALL_OCL_LANGUAGES);
|
|
|
|
bool OclGASUnsupported =
|
|
|
|
!LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.Langs & OCL_GAS);
|
|
|
|
bool OclPipeUnsupported =
|
|
|
|
!LangOpts.OpenCLPipes && (BuiltinInfo.Langs & OCL_PIPE);
|
|
|
|
// Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher
|
|
|
|
// support is indicated with language option for blocks.
|
|
|
|
bool OclDSEUnsupported =
|
|
|
|
(LangOpts.getOpenCLCompatibleVersion() < 200 || !LangOpts.Blocks) &&
|
|
|
|
(BuiltinInfo.Langs & OCL_DSE);
|
2017-10-17 14:28:14 +00:00
|
|
|
bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
|
2021-03-24 17:28:56 -04:00
|
|
|
bool CUDAUnsupported = !LangOpts.CUDA && BuiltinInfo.Langs == CUDA_LANG;
|
2019-04-24 02:23:30 +00:00
|
|
|
bool CPlusPlusUnsupported =
|
|
|
|
!LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG;
|
2021-05-20 12:37:26 -07:00
|
|
|
return !BuiltinsUnsupported && !CorBuiltinsUnsupported &&
|
2022-02-07 15:45:42 +03:00
|
|
|
!MathBuiltinsUnsupported && !OclCUnsupported && !OclGASUnsupported &&
|
|
|
|
!OclPipeUnsupported && !OclDSEUnsupported && !OpenMPUnsupported &&
|
|
|
|
!GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported &&
|
|
|
|
!CPlusPlusUnsupported && !CUDAUnsupported;
|
2013-07-23 00:13:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 09:53:14 +00:00
|
|
|
/// initializeBuiltins - Mark the identifiers for all the builtins with their
|
2007-01-28 08:20:04 +00:00
|
|
|
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
|
|
|
|
/// such.
|
2015-08-06 01:01:12 +00:00
|
|
|
void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
|
2010-11-30 17:35:24 +00:00
|
|
|
const LangOptions& LangOpts) {
|
2007-01-28 08:20:04 +00:00
|
|
|
// Step #1: mark all target-independent builtins with their ID's.
|
2007-01-29 05:24:35 +00:00
|
|
|
for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
|
2015-08-06 01:01:12 +00:00
|
|
|
if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
|
2013-05-31 16:29:28 +00:00
|
|
|
Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
|
2013-07-23 00:13:01 +00:00
|
|
|
}
|
2009-06-14 01:54:56 +00:00
|
|
|
|
2009-04-22 04:56:28 +00:00
|
|
|
// Step #2: Register target-specific builtins.
|
2015-10-19 04:51:35 +00:00
|
|
|
for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
|
2015-08-06 01:01:12 +00:00
|
|
|
if (builtinIsSupported(TSRecords[i], LangOpts))
|
2015-09-22 17:23:22 +00:00
|
|
|
Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
|
|
|
|
|
|
|
|
// Step #3: Register target-specific builtins for AuxTarget.
|
2015-10-19 04:51:35 +00:00
|
|
|
for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
|
2015-09-22 17:23:22 +00:00
|
|
|
Table.get(AuxTSRecords[i].Name)
|
2015-10-19 04:51:35 +00:00
|
|
|
.setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
|
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
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c, and
64c045e25b8471bbb572bd29159c294a82a86a25
which were reverted in
e75d8b70370435b0ad10388afba0df45fcf9bfcc
due to a crasher bug where CodeGen would emit a builtin glvalue as an
rvalue if it constant-folds.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
2022-04-17 13:09:42 -07:00
|
|
|
|
|
|
|
// Step #4: Unregister any builtins specified by -fno-builtin-foo.
|
|
|
|
for (llvm::StringRef Name : LangOpts.NoBuiltinFuncs) {
|
|
|
|
bool InStdNamespace = Name.consume_front("std-");
|
|
|
|
auto NameIt = Table.find(Name);
|
|
|
|
if (NameIt != Table.end()) {
|
|
|
|
unsigned ID = NameIt->second->getBuiltinID();
|
|
|
|
if (ID != Builtin::NotBuiltin && isPredefinedLibFunction(ID) &&
|
|
|
|
isInStdNamespace(ID) == InStdNamespace) {
|
|
|
|
Table.get(Name).setBuiltinID(Builtin::NotBuiltin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-28 08:20:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 19:00:16 +00:00
|
|
|
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
|
|
|
|
const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
|
|
|
|
if (!WidthPos)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
++WidthPos;
|
|
|
|
assert(*WidthPos == ':' &&
|
|
|
|
"Vector width specifier must be followed by a ':'");
|
|
|
|
++WidthPos;
|
|
|
|
|
|
|
|
char *EndPos;
|
|
|
|
unsigned Width = ::strtol(WidthPos, &EndPos, 10);
|
|
|
|
assert(*EndPos == ':' && "Vector width specific must end with a ':'");
|
|
|
|
return Width;
|
|
|
|
}
|
|
|
|
|
2014-01-03 20:10:54 +00:00
|
|
|
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg, const char *Fmt) const {
|
|
|
|
assert(Fmt && "Not passed a format string");
|
|
|
|
assert(::strlen(Fmt) == 2 &&
|
|
|
|
"Format string needs to be two characters long");
|
|
|
|
assert(::toupper(Fmt[0]) == Fmt[1] &&
|
|
|
|
"Format string is not in the form \"xX\"");
|
|
|
|
|
2015-08-06 01:01:12 +00:00
|
|
|
const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
|
2014-01-03 20:10:54 +00:00
|
|
|
if (!Like)
|
2009-02-14 00:32:47 +00:00
|
|
|
return false;
|
|
|
|
|
2014-01-03 20:10:54 +00:00
|
|
|
HasVAListArg = (*Like == Fmt[1]);
|
2009-02-14 00:32:47 +00:00
|
|
|
|
2014-01-03 20:10:54 +00:00
|
|
|
++Like;
|
|
|
|
assert(*Like == ':' && "Format specifier must be followed by a ':'");
|
|
|
|
++Like;
|
2009-02-14 00:32:47 +00:00
|
|
|
|
2014-01-03 20:10:54 +00:00
|
|
|
assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
|
2014-05-08 06:41:40 +00:00
|
|
|
FormatIdx = ::strtol(Like, nullptr, 10);
|
2009-02-14 00:32:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-03 20:10:54 +00:00
|
|
|
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg) {
|
|
|
|
return isLike(ID, FormatIdx, HasVAListArg, "pP");
|
2010-07-16 02:11:15 +00:00
|
|
|
}
|
|
|
|
|
2014-01-03 20:10:54 +00:00
|
|
|
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg) {
|
|
|
|
return isLike(ID, FormatIdx, HasVAListArg, "sS");
|
|
|
|
}
|
2018-04-16 21:30:08 +00:00
|
|
|
|
Emit !callback metadata and introduce the callback attribute
With commit r351627, LLVM gained the ability to apply (existing) IPO
optimizations on indirections through callbacks, or transitive calls.
The general idea is that we use an abstraction to hide the middle man
and represent the callback call in the context of the initial caller.
It is described in more detail in the commit message of the LLVM patch
r351627, the llvm::AbstractCallSite class description, and the
language reference section on callback-metadata.
This commit enables clang to emit !callback metadata that is
understood by LLVM. It does so in three different cases:
1) For known broker functions declarations that are directly
generated, e.g., __kmpc_fork_call for the OpenMP pragma parallel.
2) For known broker functions that are identified by their name and
source location through the builtin detection, e.g.,
pthread_create from the POSIX thread API.
3) For user annotated functions that carry the "callback(callee, ...)"
attribute. The attribute has to include the name, or index, of
the callback callee and how the passed arguments can be
identified (as many as the callback callee has). See the callback
attribute documentation for detailed information.
Differential Revision: https://reviews.llvm.org/D55483
llvm-svn: 351629
2019-01-19 05:36:54 +00:00
|
|
|
bool Builtin::Context::performsCallback(unsigned ID,
|
|
|
|
SmallVectorImpl<int> &Encoding) const {
|
|
|
|
const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
|
|
|
|
if (!CalleePos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++CalleePos;
|
|
|
|
assert(*CalleePos == '<' &&
|
|
|
|
"Callback callee specifier must be followed by a '<'");
|
|
|
|
++CalleePos;
|
|
|
|
|
|
|
|
char *EndPos;
|
|
|
|
int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
|
|
|
|
assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
|
|
|
|
Encoding.push_back(CalleeIdx);
|
|
|
|
|
|
|
|
while (*EndPos == ',') {
|
|
|
|
const char *PayloadPos = EndPos + 1;
|
|
|
|
|
|
|
|
int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
|
|
|
|
Encoding.push_back(PayloadIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:30:08 +00:00
|
|
|
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
|
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
fc3090109643af8d2da9822d0f99c84742b9c877,
a571f82a50416b767fd3cce0fb5027bb5dfec58c, and
64c045e25b8471bbb572bd29159c294a82a86a25
which were reverted in
e75d8b70370435b0ad10388afba0df45fcf9bfcc
due to a crasher bug where CodeGen would emit a builtin glvalue as an
rvalue if it constant-folds.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D123345
2022-04-17 13:09:42 -07:00
|
|
|
return ID == Builtin::NotBuiltin || ID == Builtin::BI__va_start ||
|
|
|
|
(!hasReferenceArgsOrResult(ID) && !hasCustomTypechecking(ID)) ||
|
|
|
|
isInStdNamespace(ID);
|
2018-04-16 21:30:08 +00:00
|
|
|
}
|