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"
|
2022-05-17 15:12:03 -04:00
|
|
|
#include "BuiltinTargetFeatures.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;
|
|
|
|
|
2023-01-17 10:34:59 +01:00
|
|
|
const char *HeaderDesc::getName() const {
|
|
|
|
switch (ID) {
|
|
|
|
#define HEADER(ID, NAME) \
|
|
|
|
case ID: \
|
|
|
|
return NAME;
|
|
|
|
#include "clang/Basic/BuiltinHeaders.def"
|
|
|
|
#undef HEADER
|
|
|
|
};
|
2023-01-23 14:08:20 +00:00
|
|
|
llvm_unreachable("Unknown HeaderDesc::HeaderID enum");
|
2023-01-17 10:34:59 +01:00
|
|
|
}
|
|
|
|
|
2022-12-26 09:19:09 +01:00
|
|
|
static constexpr Builtin::Info BuiltinInfo[] = {
|
2023-01-17 10:34:59 +01:00
|
|
|
{"not a builtin function", nullptr, nullptr, nullptr, HeaderDesc::NO_HEADER,
|
|
|
|
ALL_LANGUAGES},
|
2015-08-07 05:14:44 +00:00
|
|
|
#define BUILTIN(ID, TYPE, ATTRS) \
|
2023-01-17 10:34:59 +01:00
|
|
|
{#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
|
2015-08-05 21:04:28 +00:00
|
|
|
#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
|
2023-01-17 10:34:59 +01:00
|
|
|
{#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, LANGS},
|
2015-08-05 21:04:28 +00:00
|
|
|
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
|
2023-01-17 10:34:59 +01:00
|
|
|
{#ID, TYPE, ATTRS, nullptr, HeaderDesc::HEADER, LANGS},
|
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,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
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.
2022-04-20 17:13:56 -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,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
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.
2022-04-20 17:13:56 -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,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
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.
2022-04-20 17:13:56 -07:00
|
|
|
/// Is this builtin supported according to the given language options?
|
|
|
|
static bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
|
|
|
|
const LangOptions &LangOpts) {
|
2023-01-23 23:11:05 +01:00
|
|
|
/* Builtins Unsupported */
|
|
|
|
if (LangOpts.NoBuiltin && strchr(BuiltinInfo.Attributes, 'f') != nullptr)
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* CorBuiltins Unsupported */
|
|
|
|
if (!LangOpts.Coroutines && (BuiltinInfo.Langs & COR_LANG))
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* MathBuiltins Unsupported */
|
|
|
|
if (LangOpts.NoMathBuiltin && BuiltinInfo.Header.ID == HeaderDesc::MATH_H)
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* GnuMode Unsupported */
|
|
|
|
if (!LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG))
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* MSMode Unsupported */
|
|
|
|
if (!LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG))
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* ObjC Unsupported */
|
|
|
|
if (!LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG)
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* OpenCLC Unsupported */
|
|
|
|
if (!LangOpts.OpenCL && (BuiltinInfo.Langs & ALL_OCL_LANGUAGES))
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* OopenCL GAS Unsupported */
|
|
|
|
if (!LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.Langs & OCL_GAS))
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* OpenCL Pipe Unsupported */
|
|
|
|
if (!LangOpts.OpenCLPipes && (BuiltinInfo.Langs & OCL_PIPE))
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
|
|
|
|
2022-02-07 15:45:42 +03:00
|
|
|
// Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher
|
|
|
|
// support is indicated with language option for blocks.
|
2023-01-23 23:11:05 +01:00
|
|
|
|
|
|
|
/* OpenCL DSE Unsupported */
|
|
|
|
if ((LangOpts.getOpenCLCompatibleVersion() < 200 || !LangOpts.Blocks) &&
|
|
|
|
(BuiltinInfo.Langs & OCL_DSE))
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* OpenMP Unsupported */
|
|
|
|
if (!LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG)
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* CUDA Unsupported */
|
|
|
|
if (!LangOpts.CUDA && BuiltinInfo.Langs == CUDA_LANG)
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
2023-01-23 23:11:05 +01:00
|
|
|
/* CPlusPlus Unsupported */
|
|
|
|
if (!LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG)
|
2023-01-20 11:33:25 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
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,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
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.
2022-04-20 17:13:56 -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,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
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.
2022-04-20 17:13:56 -07:00
|
|
|
return ID == Builtin::NotBuiltin || ID == Builtin::BI__va_start ||
|
2022-09-20 12:35:18 -04:00
|
|
|
ID == Builtin::BI__builtin_assume_aligned ||
|
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,
64c045e25b8471bbb572bd29159c294a82a86a2, and
de6ddaeef3aaa8a9ae3663c12cdb57d9afc0f906,
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.
2022-04-20 17:13:56 -07:00
|
|
|
(!hasReferenceArgsOrResult(ID) && !hasCustomTypechecking(ID)) ||
|
|
|
|
isInStdNamespace(ID);
|
2018-04-16 21:30:08 +00:00
|
|
|
}
|
2022-05-17 15:12:03 -04:00
|
|
|
|
|
|
|
bool Builtin::evaluateRequiredTargetFeatures(
|
|
|
|
StringRef RequiredFeatures, const llvm::StringMap<bool> &TargetFetureMap) {
|
|
|
|
// Return true if the builtin doesn't have any required features.
|
|
|
|
if (RequiredFeatures.empty())
|
|
|
|
return true;
|
|
|
|
assert(!RequiredFeatures.contains(' ') && "Space in feature list");
|
|
|
|
|
|
|
|
TargetFeatures TF(TargetFetureMap);
|
|
|
|
return TF.hasRequiredFeatures(RequiredFeatures);
|
|
|
|
}
|