Store OptTable::Info::Name as a StringRef

This avoids implicit conversion to StringRef at several points, which in
turns avoid redundant calls to strlen.

As a side effect, this greatly simplifies the implementation of
StrCmpOptionNameIgnoreCase.

It also eventually gives a consistent, humble speedup in compilation
time.

https://llvm-compile-time-tracker.com/compare.php?from=5f5b942823474e98e43a27d515a87ce140396c53&to=60e13b778119fc32d50dc38ff1a564a87146e9c6&stat=instructions:u

Differential Revision: https://reviews.llvm.org/D139274
This commit is contained in:
serge-sans-paille 2022-12-04 09:33:14 +01:00
parent ba5c26da7c
commit 8ae18303f9
No known key found for this signature in database
GPG Key ID: 7B24DA8C9551659F
5 changed files with 28 additions and 37 deletions

View File

@ -331,8 +331,8 @@ static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
if (HasStaticPIE && Args.hasArg(options::OPT_nopie)) {
const Driver &D = TC.getDriver();
const llvm::opt::OptTable &Opts = D.getOpts();
const char *StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
const char *NoPIEName = Opts.getOptionName(options::OPT_nopie);
StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
}
return HasStaticPIE;

View File

@ -44,7 +44,7 @@ public:
/// A null terminated array of prefix strings to apply to name while
/// matching.
const char *const *Prefixes;
const char *Name;
StringRef Name;
const char *HelpText;
const char *MetaVar;
unsigned ID;
@ -102,9 +102,7 @@ public:
const Option getOption(OptSpecifier Opt) const;
/// Lookup the name of the given option.
const char *getOptionName(OptSpecifier id) const {
return getInfo(id).Name;
}
StringRef getOptionName(OptSpecifier id) const { return getInfo(id).Name; }
/// Get the kind of the given option.
unsigned getOptionKind(OptSpecifier id) const {
@ -184,7 +182,7 @@ public:
/// takes
///
/// \return true in success, and false in fail.
bool addValues(const char *Option, const char *Values);
bool addValues(StringRef Option, const char *Values);
/// Parse a single argument; returning the new argument and
/// updating Index.

View File

@ -36,31 +36,23 @@ namespace opt {
// Ordering on Info. The ordering is *almost* case-insensitive lexicographic,
// with an exception. '\0' comes at the end of the alphabet instead of the
// beginning (thus options precede any other options which prefix them).
static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) {
const char *X = A, *Y = B;
char a = tolower(*A), b = tolower(*B);
while (a == b) {
if (a == '\0')
return 0;
static int StrCmpOptionNameIgnoreCase(StringRef A, StringRef B) {
size_t MinSize = std::min(A.size(), B.size());
if (int Res = A.substr(0, MinSize).compare_insensitive(B.substr(0, MinSize)))
return Res;
a = tolower(*++X);
b = tolower(*++Y);
}
if (A.size() == B.size())
return 0;
if (a == '\0') // A is a prefix of B.
return 1;
if (b == '\0') // B is a prefix of A.
return -1;
// Otherwise lexicographic.
return (a < b) ? -1 : 1;
return (A.size() == MinSize) ? 1 /* A is a prefix of B. */
: -1 /* B is a prefix of A */;
}
#ifndef NDEBUG
static int StrCmpOptionName(const char *A, const char *B) {
static int StrCmpOptionName(StringRef A, StringRef B) {
if (int N = StrCmpOptionNameIgnoreCase(A, B))
return N;
return strcmp(A, B);
return A.compare(B);
}
static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
@ -86,7 +78,7 @@ static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
#endif
// Support lower_bound between info and an option name.
static inline bool operator<(const OptTable::Info &I, const char *Name) {
static inline bool operator<(const OptTable::Info &I, StringRef Name) {
return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
}
@ -321,7 +313,7 @@ unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
return BestDistance;
}
bool OptTable::addValues(const char *Option, const char *Values) {
bool OptTable::addValues(StringRef Option, const char *Values) {
for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
Info &In = OptionInfos[I];
if (optionMatches(In, Option)) {
@ -347,8 +339,8 @@ std::unique_ptr<Arg> OptTable::parseOneArgGrouped(InputArgList &Args,
const Info *End = OptionInfos.data() + OptionInfos.size();
StringRef Name = Str.ltrim(PrefixChars);
const Info *Start = std::lower_bound(
OptionInfos.data() + FirstSearchableIndex, End, Name.data());
const Info *Start =
std::lower_bound(OptionInfos.data() + FirstSearchableIndex, End, Name);
const Info *Fallback = nullptr;
unsigned Prev = Index;
@ -415,7 +407,7 @@ std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
StringRef Name = StringRef(Str).ltrim(PrefixChars);
// Search for the first next option which could be a prefix.
Start = std::lower_bound(Start, End, Name.data());
Start = std::lower_bound(Start, End, Name);
// Options are stored in sorted order, with '\0' at the end of the
// alphabet. Since the only options which can accept a string must

View File

@ -6,10 +6,11 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringRef.h"
#include "gtest/gtest.h"
struct OptionWithMarshallingInfo {
const char *Name;
llvm::StringRef Name;
const char *KeyPath;
const char *ImpliedCheck;
const char *ImpliedValue;
@ -27,10 +28,10 @@ static const OptionWithMarshallingInfo MarshallingTable[] = {
};
TEST(OptionMarshalling, EmittedOrderSameAsDefinitionOrder) {
ASSERT_STREQ(MarshallingTable[0].Name, "marshalled-flag-d");
ASSERT_STREQ(MarshallingTable[1].Name, "marshalled-flag-c");
ASSERT_STREQ(MarshallingTable[2].Name, "marshalled-flag-b");
ASSERT_STREQ(MarshallingTable[3].Name, "marshalled-flag-a");
ASSERT_STREQ(MarshallingTable[0].Name.data(), "marshalled-flag-d");
ASSERT_STREQ(MarshallingTable[1].Name.data(), "marshalled-flag-c");
ASSERT_STREQ(MarshallingTable[2].Name.data(), "marshalled-flag-b");
ASSERT_STREQ(MarshallingTable[3].Name.data(), "marshalled-flag-a");
}
TEST(OptionMarshalling, EmittedSpecifiedKeyPath) {

View File

@ -54,9 +54,9 @@ static std::string getOptionSpelling(const Record &R) {
static void emitNameUsingSpelling(raw_ostream &OS, const Record &R) {
size_t PrefixLength;
OS << "&";
OS << "llvm::StringRef(";
write_cstring(OS, StringRef(getOptionSpelling(R, PrefixLength)));
OS << "[" << PrefixLength << "]";
OS << ").substr(" << PrefixLength << ")";
}
class MarshallingInfo {