Skip to content

Commit 22dace5

Browse files
committed
Store the length of the strings to avoid call to strlen().
1 parent b20c203 commit 22dace5

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

llvm/include/llvm/Analysis/TargetLibraryInfo.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ class TargetLibraryInfoImpl {
174174
/// Forces a function to be marked as available and provide an alternate name
175175
/// that must be used.
176176
void setAvailableWithName(LibFunc F, StringRef Name) {
177-
if (StandardNamesStrTable.getCString(StandardNamesOffsets[F]) != Name) {
177+
if (StringRef(StandardNamesStrTable.getCString(StandardNamesOffsets[F]),
178+
StandardNamesSizeTable[F]) != Name) {
178179
setState(F, CustomName);
179180
CustomNames[F] = std::string(Name);
180181
assert(CustomNames.contains(F));
@@ -452,17 +453,18 @@ class TargetLibraryInfo {
452453
/// Return the canonical name for a LibFunc. This should not be used for
453454
/// semantic purposes, use getName instead.
454455
static StringRef getStandardName(LibFunc F) {
455-
return TargetLibraryInfoImpl::StandardNamesStrTable.getCString(
456-
TargetLibraryInfoImpl::StandardNamesOffsets[F]);
456+
return StringRef(TargetLibraryInfoImpl::StandardNamesStrTable.getCString(
457+
TargetLibraryInfoImpl::StandardNamesOffsets[F]),
458+
TargetLibraryInfoImpl::StandardNamesSizeTable[F]);
457459
}
458460

459461
StringRef getName(LibFunc F) const {
460462
auto State = getState(F);
461463
if (State == TargetLibraryInfoImpl::Unavailable)
462464
return StringRef();
463465
if (State == TargetLibraryInfoImpl::StandardName)
464-
return Impl->StandardNamesStrTable.getCString(
465-
Impl->StandardNamesOffsets[F]);
466+
return StringRef(Impl->StandardNamesStrTable.getCString(
467+
Impl->StandardNamesOffsets[F]), Impl->StandardNamesSizeTable[F]);
466468
assert(State == TargetLibraryInfoImpl::CustomName);
467469
return Impl->CustomNames.find(F)->second;
468470
}

llvm/test/TableGen/TargetLibraryInfo.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,19 @@ def printf : TargetLibCall< "printf", [Int, Ptr, Ellip]>;
5252
// CHECK-NEXT: 11, // fmaxf
5353
// CHECK-NEXT: 17, // printf
5454
// CHECK-NEXT: };
55+
// CHECK-NEXT: const uint8_t TargetLibraryInfoImpl::StandardNamesSizeTable[] = { 0,
56+
// CHECK-NEXT: 4,
57+
// CHECK-NEXT: 4,
58+
// CHECK-NEXT: 5,
59+
// CHECK-NEXT: 6,
60+
// CHECK-NEXT: };
5561
// CHECK-NEXT: #endif
5662

5763
// CHECK: #ifdef GET_TARGET_LIBRARY_INFO_IMPL_DECL
5864
// CHECK-NEXT: #undef GET_TARGET_LIBRARY_INFO_IMPL_DECL
5965
// CHECK-NEXT: LLVM_ABI static const llvm::StringTable StandardNamesStrTable;
6066
// CHECK-NEXT: LLVM_ABI static const llvm::StringTable::Offset StandardNamesOffsets[5];
67+
// CHECK-NEXT: LLVM_ABI static const uint8_t StandardNamesSizeTable[5];
6168
// CHECK-NEXT: #endif
6269

6370
// CHECK: #ifdef GET_TARGET_LIBRARY_INFO_SIGNATURE_TABLE
@@ -91,7 +98,7 @@ def printf : TargetLibCall< "printf", [Int, Ptr, Ellip]>;
9198
// CHECK-NEXT: /* 7 */ Floating, Same, Same, NoFuncArgType,
9299
// CHECK-NEXT: /* 11 */ Void, NoFuncArgType,
93100
// CHECK-NEXT: };
94-
// CHECK-NEXT: static const unsigned short SignatureOffset[] = {
101+
// CHECK-NEXT: static const uint16_t SignatureOffset[] = {
95102
// CHECK-NEXT: 11, //
96103
// CHECK-NEXT: 4, // cosf
97104
// CHECK-NEXT: 4, // sinf

llvm/utils/TableGen/Basic/TargetLibraryInfoEmitter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,19 @@ void TargetLibraryInfoEmitter::emitTargetLibraryInfoStringTable(
9797
OS.indent(2) << Table.GetStringOffset(Str) << ", // " << Str << "\n";
9898
}
9999
OS << "};\n";
100+
OS << "const uint8_t TargetLibraryInfoImpl::StandardNamesSizeTable[] = {";
101+
OS << " 0,\n";
102+
for (const auto *R : AllTargetLibcalls)
103+
OS.indent(2) << R->getValueAsString("String").size() << ",\n";
104+
OS << "};\n";
100105
OS << "#endif\n\n";
101106
OS << "#ifdef GET_TARGET_LIBRARY_INFO_IMPL_DECL\n";
102107
OS << "#undef GET_TARGET_LIBRARY_INFO_IMPL_DECL\n";
103108
OS << "LLVM_ABI static const llvm::StringTable StandardNamesStrTable;\n";
104109
OS << "LLVM_ABI static const llvm::StringTable::Offset StandardNamesOffsets["
105110
<< NumEl << "];\n";
111+
OS << "LLVM_ABI static const uint8_t StandardNamesSizeTable[" << NumEl
112+
<< "];\n";
106113
OS << "#endif\n\n";
107114
}
108115

@@ -144,7 +151,7 @@ void TargetLibraryInfoEmitter::emitTargetLibraryInfoSignatureTable(
144151
OS << "static const FuncArgTypeID SignatureTable[] = {\n";
145152
SignatureTable.emit(OS, [](raw_ostream &OS, StringRef E) { OS << E; });
146153
OS << "};\n";
147-
OS << "static const unsigned short SignatureOffset[] = {\n";
154+
OS << "static const uint16_t SignatureOffset[] = {\n";
148155
OS.indent(2) << SignatureTable.get(NoFuncSig) << ", //\n";
149156
for (const auto *R : AllTargetLibcalls) {
150157
OS.indent(2) << SignatureTable.get(GetSignature(R)) << ", // "

0 commit comments

Comments
 (0)