Skip to content

Commit 4e6f6fd

Browse files
[IR] Use StringRef::operator== instead of StringRef::equals (NFC) (#90550)
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator== outnumbers StringRef::equals by a factor of 22 under llvm/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
1 parent 9b07a03 commit 4e6f6fd

File tree

9 files changed

+25
-28
lines changed

9 files changed

+25
-28
lines changed

llvm/lib/IR/Attributes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,7 @@ struct StrBoolAttr {
22822282
static bool isSet(const Function &Fn,
22832283
StringRef Kind) {
22842284
auto A = Fn.getFnAttribute(Kind);
2285-
return A.getValueAsString().equals("true");
2285+
return A.getValueAsString() == "true";
22862286
}
22872287

22882288
static void set(Function &Fn,

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn,
10591059
}
10601060
}
10611061

1062-
if (F->arg_size() == 2 && Name.equals("coro.end")) {
1062+
if (F->arg_size() == 2 && Name == "coro.end") {
10631063
rename(F);
10641064
NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::coro_end);
10651065
return true;

llvm/lib/IR/BasicBlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ std::optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const {
682682
if (MDNode *MDIrrLoopHeader =
683683
TI->getMetadata(LLVMContext::MD_irr_loop)) {
684684
MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0));
685-
if (MDName->getString().equals("loop_header_weight")) {
685+
if (MDName->getString() == "loop_header_weight") {
686686
auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1));
687687
return std::optional<uint64_t>(CI->getValue().getZExtValue());
688688
}

llvm/lib/IR/Function.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,8 +1847,8 @@ bool Function::hasAddressTaken(const User **PutOffender,
18471847
if (llvm::all_of(FUU->users(), [](const User *U) {
18481848
if (const auto *GV = dyn_cast<GlobalVariable>(U))
18491849
return GV->hasName() &&
1850-
(GV->getName().equals("llvm.compiler.used") ||
1851-
GV->getName().equals("llvm.used"));
1850+
(GV->getName() == "llvm.compiler.used" ||
1851+
GV->getName() == "llvm.used");
18521852
return false;
18531853
}))
18541854
continue;
@@ -1993,7 +1993,7 @@ std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
19931993
MDNode *MD = getMetadata(LLVMContext::MD_prof);
19941994
if (MD && MD->getOperand(0))
19951995
if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
1996-
if (MDS->getString().equals("function_entry_count")) {
1996+
if (MDS->getString() == "function_entry_count") {
19971997
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
19981998
uint64_t Count = CI->getValue().getZExtValue();
19991999
// A value of -1 is used for SamplePGO when there were no samples.
@@ -2002,7 +2002,7 @@ std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
20022002
return std::nullopt;
20032003
return ProfileCount(Count, PCT_Real);
20042004
} else if (AllowSynthetic &&
2005-
MDS->getString().equals("synthetic_function_entry_count")) {
2005+
MDS->getString() == "synthetic_function_entry_count") {
20062006
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
20072007
uint64_t Count = CI->getValue().getZExtValue();
20082008
return ProfileCount(Count, PCT_Synthetic);
@@ -2015,7 +2015,7 @@ DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
20152015
DenseSet<GlobalValue::GUID> R;
20162016
if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
20172017
if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
2018-
if (MDS->getString().equals("function_entry_count"))
2018+
if (MDS->getString() == "function_entry_count")
20192019
for (unsigned i = 2; i < MD->getNumOperands(); i++)
20202020
R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
20212021
->getValue()
@@ -2031,9 +2031,8 @@ void Function::setSectionPrefix(StringRef Prefix) {
20312031

20322032
std::optional<StringRef> Function::getSectionPrefix() const {
20332033
if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
2034-
assert(cast<MDString>(MD->getOperand(0))
2035-
->getString()
2036-
.equals("function_section_prefix") &&
2034+
assert(cast<MDString>(MD->getOperand(0))->getString() ==
2035+
"function_section_prefix" &&
20372036
"Metadata not match");
20382037
return cast<MDString>(MD->getOperand(1))->getString();
20392038
}

llvm/lib/IR/Metadata.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,8 +1195,7 @@ MDNode *MDNode::mergeDirectCallProfMetadata(MDNode *A, MDNode *B,
11951195
"first operand should be a non-null MDString");
11961196
StringRef AProfName = AMDS->getString();
11971197
StringRef BProfName = BMDS->getString();
1198-
if (AProfName.equals("branch_weights") &&
1199-
BProfName.equals("branch_weights")) {
1198+
if (AProfName == "branch_weights" && BProfName == "branch_weights") {
12001199
ConstantInt *AInstrWeight =
12011200
mdconst::dyn_extract<ConstantInt>(A->getOperand(1));
12021201
ConstantInt *BInstrWeight =

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ bool isTargetMD(const MDNode *ProfData, const char *Name, unsigned MinOps) {
6262
if (!ProfDataName)
6363
return false;
6464

65-
return ProfDataName->getString().equals(Name);
65+
return ProfDataName->getString() == Name;
6666
}
6767

6868
} // namespace
@@ -161,7 +161,7 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
161161
if (!ProfDataName)
162162
return false;
163163

164-
if (ProfDataName->getString().equals("branch_weights")) {
164+
if (ProfDataName->getString() == "branch_weights") {
165165
for (unsigned Idx = 1; Idx < ProfileData->getNumOperands(); Idx++) {
166166
auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
167167
assert(V && "Malformed branch_weight in MD_prof node");
@@ -170,8 +170,7 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
170170
return true;
171171
}
172172

173-
if (ProfDataName->getString().equals("VP") &&
174-
ProfileData->getNumOperands() > 3) {
173+
if (ProfDataName->getString() == "VP" && ProfileData->getNumOperands() > 3) {
175174
TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
176175
->getValue()
177176
.getZExtValue();
@@ -197,8 +196,8 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
197196
return;
198197

199198
auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
200-
if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
201-
!ProfDataName->getString().equals("VP")))
199+
if (!ProfDataName || (ProfDataName->getString() != "branch_weights" &&
200+
ProfDataName->getString() != "VP"))
202201
return;
203202

204203
LLVMContext &C = I.getContext();
@@ -207,7 +206,7 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
207206
SmallVector<Metadata *, 3> Vals;
208207
Vals.push_back(ProfileData->getOperand(0));
209208
APInt APS(128, S), APT(128, T);
210-
if (ProfDataName->getString().equals("branch_weights") &&
209+
if (ProfDataName->getString() == "branch_weights" &&
211210
ProfileData->getNumOperands() > 0) {
212211
// Using APInt::div may be expensive, but most cases should fit 64 bits.
213212
APInt Val(128, mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1))
@@ -216,7 +215,7 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
216215
Val *= APS;
217216
Vals.push_back(MDB.createConstant(ConstantInt::get(
218217
Type::getInt32Ty(C), Val.udiv(APT).getLimitedValue(UINT32_MAX))));
219-
} else if (ProfDataName->getString().equals("VP"))
218+
} else if (ProfDataName->getString() == "VP")
220219
for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
221220
// The first value is the key of the value profile, which will not change.
222221
Vals.push_back(ProfileData->getOperand(i));

llvm/lib/IR/ProfileSummary.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static ConstantAsMetadata *getValMD(MDTuple *MD, const char *Key) {
110110
ConstantAsMetadata *ValMD = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
111111
if (!KeyMD || !ValMD)
112112
return nullptr;
113-
if (!KeyMD->getString().equals(Key))
113+
if (KeyMD->getString() != Key)
114114
return nullptr;
115115
return ValMD;
116116
}
@@ -140,7 +140,7 @@ static bool isKeyValuePair(MDTuple *MD, const char *Key, const char *Val) {
140140
MDString *ValMD = dyn_cast<MDString>(MD->getOperand(1));
141141
if (!KeyMD || !ValMD)
142142
return false;
143-
if (!KeyMD->getString().equals(Key) || !ValMD->getString().equals(Val))
143+
if (KeyMD->getString() != Key || ValMD->getString() != Val)
144144
return false;
145145
return true;
146146
}
@@ -150,7 +150,7 @@ static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary) {
150150
if (!MD || MD->getNumOperands() != 2)
151151
return false;
152152
MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
153-
if (!KeyMD || !KeyMD->getString().equals("DetailedSummary"))
153+
if (!KeyMD || KeyMD->getString() != "DetailedSummary")
154154
return false;
155155
MDTuple *EntriesMD = dyn_cast<MDTuple>(MD->getOperand(1));
156156
if (!EntriesMD)

llvm/lib/IR/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ struct TargetTypeInfo {
834834
static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
835835
LLVMContext &C = Ty->getContext();
836836
StringRef Name = Ty->getName();
837-
if (Name.equals("spirv.Image"))
837+
if (Name == "spirv.Image")
838838
return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal);
839839
if (Name.starts_with("spirv."))
840840
return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::HasZeroInit,

llvm/lib/IR/Verifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,8 +2379,8 @@ void Verifier::verifyFunctionMetadata(
23792379
"expected string with name of the !prof annotation", MD);
23802380
MDString *MDS = cast<MDString>(MD->getOperand(0));
23812381
StringRef ProfName = MDS->getString();
2382-
Check(ProfName.equals("function_entry_count") ||
2383-
ProfName.equals("synthetic_function_entry_count"),
2382+
Check(ProfName == "function_entry_count" ||
2383+
ProfName == "synthetic_function_entry_count",
23842384
"first operand should be 'function_entry_count'"
23852385
" or 'synthetic_function_entry_count'",
23862386
MD);
@@ -4785,7 +4785,7 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
47854785
StringRef ProfName = MDS->getString();
47864786

47874787
// Check consistency of !prof branch_weights metadata.
4788-
if (ProfName.equals("branch_weights")) {
4788+
if (ProfName == "branch_weights") {
47894789
if (isa<InvokeInst>(&I)) {
47904790
Check(MD->getNumOperands() == 2 || MD->getNumOperands() == 3,
47914791
"Wrong number of InvokeInst branch_weights operands", MD);

0 commit comments

Comments
 (0)