17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/MathExtras.h"
21#include "llvm/Support/xxhash.h"
27#define HEADER(ID, NAME) \
30#include "clang/Basic/BuiltinHeaders.def"
33 llvm_unreachable(
"Unknown HeaderDesc::HeaderID enum");
38#define GET_BUILTIN_STR_TABLE
39#include "clang/Basic/Builtins.inc"
40#undef GET_BUILTIN_STR_TABLE
44#define GET_BUILTIN_INFOS
45#include "clang/Basic/Builtins.inc"
46#undef GET_BUILTIN_INFOS
54std::pair<const Builtin::InfosShard &, const Builtin::Info &>
55Builtin::Context::getShardAndInfo(
unsigned ID)
const {
57 return Builtins->getShardAndInfo(
ID);
70 Buf.append(Name.begin(), Name.end());
71 return StringRef(Buf.data(), Buf.size());
82 const auto &[Shard, I] = getShardAndInfo(
ID);
83 return I.getName(Shard);
87 const auto &[Shard, I] = getShardAndInfo(
ID);
89 Result += Shard.NamePrefix;
90 Result += (*Shard.Strings)[I.Offsets.Name];
96 const auto &[Shard, I] = getShardAndInfo(
ID);
97 return (*Shard.Strings)[I.Offsets.Type].data();
101 const auto &[Shard, I] = getShardAndInfo(
ID);
102 return (*Shard.Strings)[I.Offsets.Attributes].data();
106 const auto &[Shard, I] = getShardAndInfo(
ID);
107 return (*Shard.Strings)[I.Offsets.Features].data();
116 : Builtins(
std::move(Builtins)),
117 NumTargetBuiltins(this->Builtins->getNumTargetBuiltins()) {}
120 bool InStdNamespace = FuncName.consume_front(
"std-");
122 if (llvm::StringRef FuncNameSuffix = FuncName;
123 FuncNameSuffix.consume_front(Shard.NamePrefix))
124 for (
const auto &I : Shard.Infos)
125 if (FuncNameSuffix == (*Shard.Strings)[I.Offsets.Name] &&
126 (
bool)strchr((*Shard.Strings)[I.Offsets.Attributes].data(),
'z') ==
128 return strchr((*Shard.Strings)[I.Offsets.Attributes].data(),
'f') !=
141 if (LangOpts.NoBuiltin && strchr(AttributesStr.data(),
'f') !=
nullptr)
147 if (LangOpts.NoMathBuiltin && BuiltinInfo.
Header.
ID == HeaderDesc::MATH_H)
153 if (!LangOpts.MicrosoftExt && (BuiltinInfo.
Langs &
MS_LANG))
165 if (!LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.
Langs &
OCL_GAS))
188 if (!LangOpts.CPlusPlus20 && strchr(AttributesStr.data(),
'G') !=
nullptr)
209 case Builtin::BI__builtin_fma:
210 case Builtin::BI__builtin_fmaf:
211 case Builtin::BI__builtin_fmal:
212 case Builtin::BI__builtin_fmaf16:
214 case Builtin::BIfmaf:
215 case Builtin::BIfmal: {
216 if (Trip.isGNUEnvironment() || Trip.isOSMSVCRT())
228 unsigned BuiltinID, llvm::Triple Trip, std::optional<bool> ErrnoOverwritten,
229 bool MathErrnoEnabled,
bool HasOptNoneAttr,
230 bool IsOptimizationEnabled)
const {
235 bool ErrnoOverridenToFalseWithOpt = ErrnoOverwritten.has_value() &&
236 !ErrnoOverwritten.value() &&
237 !HasOptNoneAttr && IsOptimizationEnabled;
249 bool ConstWithoutErrnoAndExceptions =
268 bool ConstWithoutErrnoOrExceptions =
269 ConstWithoutErrnoAndExceptions || ConstWithoutExceptions;
270 bool GenerateIntrinsics =
271 (ConstAlways && !HasOptNoneAttr) ||
272 (!MathErrnoEnabled &&
273 !(ErrnoOverwritten.has_value() && ErrnoOverwritten.value()) &&
275 if (!GenerateIntrinsics) {
277 ConstWithoutErrnoOrExceptions && !ConstWithoutErrnoAndExceptions;
278 if (!GenerateIntrinsics)
280 ConstWithoutErrnoOrExceptions &&
281 (!MathErrnoEnabled &&
282 !(ErrnoOverwritten.has_value() && ErrnoOverwritten.value()) &&
284 if (!GenerateIntrinsics)
286 ConstWithoutErrnoOrExceptions && ErrnoOverridenToFalseWithOpt;
289 return GenerateIntrinsics;
293 return static_cast<uint32_t>(llvm::xxh3_64bits(Name));
299 llvm::append_range(Shards,
Target.getTargetBuiltins());
300 NumTargetShards = Shards.size() - 1;
304 NumTargetBuiltins += Shard.Infos.size();
306 NumAuxTargetBuiltins += Shard.Infos.size();
310 NumNames += Shard.Infos.size();
312 size_t NumSlots = llvm::PowerOf2Ceil(NumNames * 2);
313 Slots = std::make_unique<uint64_t[]>(NumSlots);
318 for (
const Info &I : Shard.Infos) {
322 size_t Slot = Hash & Mask;
324 Slot = (Slot + 1) & Mask;
332std::pair<const Builtin::InfosShard &, const Builtin::Info &>
335 "Invalid builtin ID!");
348 if (
ID < Shard.Infos.size())
349 return {Shard, Shard.Infos[
ID]};
350 ID -= Shard.Infos.size();
352 llvm_unreachable(
"Invalid target builtin shard structure!");
355bool Builtin::TargetBuiltins::hasName(
unsigned ID, StringRef Name)
const {
356 const auto &[Shard, I] = getShardAndInfo(
ID);
357 return Name.consume_front(Shard.NamePrefix) &&
358 Name == (*Shard.Strings)[I.Offsets.Name];
364 for (
size_t Slot = Hash & Mask; uint64_t Entry = Slots[Slot];
365 Slot = (Slot + 1) & Mask) {
366 unsigned ID =
static_cast<uint32_t
>(Entry);
367 if (Entry >> 32 == Hash &&
hasName(
ID, Name))
382 for (
unsigned ID : Builtins->lookup(Name)) {
385 const auto &[Shard, I] = getShardAndInfo(
ID);
397 if (NoBuiltin.consume_front(
"std-") == InStdNamespace &&
405 const LangOptions &LangOpts, llvm::function_ref<
void(StringRef)> Fn)
const {
411 for (
const auto &I : Shard.Infos.drop_front())
414 for (
const auto &Shard : Builtins->getTargetShards())
415 for (
const auto &I : Shard.Infos)
418 for (
const auto &Shard : Builtins->getAuxTargetShards())
419 for (
const auto &I : Shard.Infos)
429 assert(*WidthPos ==
':' &&
430 "Vector width specifier must be followed by a ':'");
434 unsigned Width = ::strtol(WidthPos, &EndPos, 10);
435 assert(*EndPos ==
':' &&
"Vector width specific must end with a ':'");
439bool Builtin::Context::isLike(
unsigned ID,
unsigned &FormatIdx,
440 bool &HasVAListArg,
const char *Fmt)
const {
441 assert(Fmt &&
"Not passed a format string");
442 assert(::strlen(Fmt) == 2 &&
443 "Format string needs to be two characters long");
444 assert(::toupper(Fmt[0]) == Fmt[1] &&
445 "Format string is not in the form \"xX\"");
447 const char *Like = ::strpbrk(getAttributesString(
ID), Fmt);
451 HasVAListArg = (*Like == Fmt[1]);
454 assert(*Like ==
':' &&
"Format specifier must be followed by a ':'");
457 assert(::strchr(Like,
':') &&
"Format specifier must end with a ':'");
458 FormatIdx = ::strtol(Like,
nullptr, 10);
463 bool &HasVAListArg) {
464 return isLike(
ID, FormatIdx, HasVAListArg,
"pP");
468 bool &HasVAListArg) {
469 return isLike(
ID, FormatIdx, HasVAListArg,
"sS");
474 assert(*CurrPos ==
'<' &&
"Expected '<' to start index list");
478 int PosIdx = ::strtol(CurrPos, &EndPos, 10);
479 assert(PosIdx >= 0 &&
"Index is supposed to be positive!");
480 Indxs.push_back(PosIdx);
482 while (*EndPos ==
',') {
483 const char *PayloadPos = EndPos + 1;
485 int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
486 Indxs.push_back(PayloadIdx);
489 assert(*EndPos ==
'>' &&
"Index list must end with '>'");
500 assert(*AttrPos ==
':' &&
"Format specifier must be followed by a ':'");
504 else if (*AttrPos ==
'1')
507 llvm_unreachable(
"Unrecognized NonNull optimization mode");
509 assert(*AttrPos ==
':' &&
"Mode must be followed by a ':'");
520 size_t Pos = Attrs.find(
'R');
521 if (Pos == StringRef::npos)
524 StringRef Str = Attrs.drop_front(Pos + 1);
525 if (!Str.consume_front(
"<"))
526 llvm_unreachable(
"Expected '<' to start range list");
530 if (Str.consumeInteger(10, Range.Index) || !Str.consume_front(
":") ||
531 Str.consumeInteger(10, Range.Lower) || !Str.consume_front(
":") ||
532 Str.consumeInteger(10, Range.Upper))
533 llvm_unreachable(
"Malformed argument range");
534 Ranges.push_back(Range);
535 }
while (Str.consume_front(
","));
537 assert(Str.starts_with(
'>') &&
"Range list must end with '>'");
554 ID == Builtin::BI__builtin_assume_aligned ||
560 StringRef RequiredFeatures,
const llvm::StringMap<bool> &TargetFetureMap) {
562 if (RequiredFeatures.empty())
564 assert(!RequiredFeatures.contains(
' ') &&
"Space in feature list");
567 return TF.hasRequiredFeatures(RequiredFeatures);
static constexpr llvm::StringTable BuiltinStrings
static StringRef getBuiltinNameInto(const Builtin::InfosShard &Shard, const Builtin::Info &BuiltinInfo, SmallVectorImpl< char > &Buf)
Return a non-owning StringRef of the builtin's name, reconstructed into Buf.
static constexpr Builtin::Info BuiltinInfos[]
static bool builtinIsSupported(const llvm::StringTable &Strings, const Builtin::Info &BuiltinInfo, const LangOptions &LangOpts)
Is this builtin supported according to the given language options?
static constexpr unsigned NumBuiltins
static void parseCommaSeparatedIndices(const char *CurrPos, llvm::SmallVectorImpl< int > &Indxs)
static bool isBuiltinConstForTriple(unsigned BuiltinID, llvm::Triple Trip)
static const Builtin::InfosShard TargetIndependentShards[]
The shard of the target-independent builtins, which every context has.
static uint32_t hashBuiltinName(StringRef Name)
Defines enum values for all the target-independent builtin functions.
Defines the clang::LangOptions interface.
bool shouldGenerateFPMathIntrinsic(unsigned BuiltinID, llvm::Triple Trip, std::optional< bool > ErrnoOverwritten, bool MathErrnoEnabled, bool HasOptNoneAttr, bool IsOptimizationEnabled) const
Determine whether we can generate LLVM intrinsics for the given builtin ID, based on whether it has s...
llvm::SmallString< 64 > getQuotedName(unsigned ID) const
Return the identifier name for the specified builtin inside single quotes for a diagnostic,...
bool hasReferenceArgsOrResult(unsigned ID) const
Return true if this builtin has a result or any arguments which are reference types.
bool performsCallback(unsigned ID, llvm::SmallVectorImpl< int > &Encoding) const
Determine whether this builtin has callback behavior (see llvm::AbstractCallSites for details).
bool isAuxBuiltinID(unsigned ID) const
Return true if the builtin ID belongs exclusively to the AuxTarget, and false if it belongs to both p...
bool isNonNull(unsigned ID, llvm::SmallVectorImpl< int > &Indxs, Info::NonNullMode &Mode) const
Return true if this builtin has parameters that must be non-null.
unsigned lookupBuiltinID(llvm::StringRef Name, const LangOptions &LangOpts) const
Return the ID of the builtin named Name, or NotBuiltin if there's none, LangOpts doesn't support it,...
bool isConstWithoutErrnoAndExceptions(unsigned ID) const
Return true if this function has no side effects and doesn't read memory, except for possibly errno o...
static bool isBuiltinFunc(llvm::StringRef Name)
Returns true if this is a libc/libm function without the '__builtin_' prefix.
unsigned getRequiredVectorWidth(unsigned ID) const
bool hasCustomTypechecking(unsigned ID) const
Determines whether this builtin has custom typechecking.
std::string getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like scanf in its formatting rules and, if so, set the index to the...
bool isInStdNamespace(unsigned ID) const
Determines whether this builtin is a C++ standard library function that lives in (possibly-versioned)...
bool canBeRedeclared(unsigned ID) const
Returns true if this is a builtin that can be redeclared.
const char * getAttributesString(unsigned ID) const
Get the attributes descriptor string for the specified builtin.
void forEachBuiltinName(const LangOptions &LangOpts, llvm::function_ref< void(llvm::StringRef)> Fn) const
Call Fn with the name of every builtin supported by LangOpts, whether or not the identifier table con...
Context()
Create a context with only the target-independent builtins.
bool isPredefinedLibFunction(unsigned ID) const
Determines whether this builtin is a predefined libc/libm function, such as "malloc",...
bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like printf in its formatting rules and, if so, set the index to th...
const char * getTypeString(unsigned ID) const
Get the type descriptor string for the specified builtin.
const char * getRequiredFeatures(unsigned ID) const
bool isConstWithoutExceptions(unsigned ID) const
void getArgRanges(unsigned ID, llvm::SmallVectorImpl< ArgRange > &Ranges) const
Append the constant argument ranges of this builtin into Ranges.
bool isConst(unsigned ID) const
Return true if this function has no side effects and doesn't read memory.
The builtins of a target and an optional aux target: their shards, and a hash table from their names ...
TargetBuiltins(const TargetInfo &Target, const TargetInfo *AuxTarget)
Collect the builtins of Target and AuxTarget, which may be null.
llvm::ArrayRef< InfosShard > getAuxTargetShards() const
The shards of the aux target's builtins.
std::pair< const InfosShard &, const Info & > getShardAndInfo(unsigned ID) const
Return the shard of the builtin with ID and its info.
llvm::ArrayRef< InfosShard > getTargetShards() const
The shards of the target's builtins.
llvm::SmallVector< unsigned, 2 > lookup(llvm::StringRef Name) const
Return the IDs of all builtins named Name, in no particular order.
TargetFeatures - This class is used to check whether the builtin function has the required target spe...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
std::vector< std::string > NoBuiltinFuncs
A list of all -fno-builtin-* function names (e.g., memset).
unsigned getOpenCLCompatibleVersion() const
Return the OpenCL version that kernel language is compatible with.
Exposes information about the current target.
virtual llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const =0
Return information about target-specific builtins for the current primary target, and info about whic...
Defines the clang::TargetInfo interface.
bool evaluateRequiredTargetFeatures(llvm::StringRef RequiredFatures, const llvm::StringMap< bool > &TargetFetureMap)
Returns true if the required target features of a builtin function are enabled.
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
Top level wrappers for InstallAPI frontend operations.
@ Result
The result type of a method or function.
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
A constant argument that must be in the half-open range [Lower, Upper).
llvm::StringTable::Offset Name
llvm::StringTable::Offset Attributes
The info used to represent each builtin.
struct clang::Builtin::Info::StrOffsets Offsets
std::string getName(const InfosShard &Shard) const
Get the name for the builtin represented by this Info object.
A shard of a target's builtins string table and info.
const llvm::StringTable * Strings
llvm::StringLiteral NamePrefix