Skip to content

Commit 6ce03ff

Browse files
Revert "[IR] Use range-based for loops (NFC)"
This reverts commit e851278. This revert is done because llvm::drop_begin over an empty ArrayRef doesn't return an empty range, and therefore can lead to an invalid address returned instead. See discussion in #80737 for more context.
1 parent a7bc9cb commit 6ce03ff

File tree

6 files changed

+16
-14
lines changed

6 files changed

+16
-14
lines changed

llvm/lib/IR/AsmWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,8 @@ void SlotTracker::CreateMetadataSlot(const MDNode *N) {
13091309
++mdnNext;
13101310

13111311
// Recursively add any MDNodes referenced by operands.
1312-
for (const MDOperand &MDO : N->operands())
1313-
if (const MDNode *Op = dyn_cast_or_null<MDNode>(MDO))
1312+
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1313+
if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
13141314
CreateMetadataSlot(Op);
13151315
}
13161316

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5209,8 +5209,8 @@ static Metadata *upgradeLoopArgument(Metadata *MD) {
52095209
SmallVector<Metadata *, 8> Ops;
52105210
Ops.reserve(T->getNumOperands());
52115211
Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
5212-
for (const MDOperand &MDO : llvm::drop_begin(T->operands()))
5213-
Ops.push_back(MDO);
5212+
for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
5213+
Ops.push_back(T->getOperand(I));
52145214

52155215
return MDTuple::get(T->getContext(), Ops);
52165216
}

llvm/lib/IR/DebugInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ static MDNode *updateLoopMetadataDebugLocationsImpl(
400400
// Save space for the self-referential LoopID.
401401
SmallVector<Metadata *, 4> MDs = {nullptr};
402402

403-
for (const MDOperand &MDO : llvm::drop_begin(OrigLoopID->operands())) {
404-
Metadata *MD = MDO;
403+
for (unsigned i = 1; i < OrigLoopID->getNumOperands(); ++i) {
404+
Metadata *MD = OrigLoopID->getOperand(i);
405405
if (!MD)
406406
MDs.push_back(nullptr);
407407
else if (Metadata *NewMD = Updater(MD))

llvm/lib/IR/Function.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,9 +1976,10 @@ DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
19761976
if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
19771977
if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
19781978
if (MDS->getString().equals("function_entry_count"))
1979-
for (const MDOperand &MDO : llvm::drop_begin(MD->operands(), 2))
1980-
R.insert(
1981-
mdconst::extract<ConstantInt>(MDO)->getValue().getZExtValue());
1979+
for (unsigned i = 2; i < MD->getNumOperands(); i++)
1980+
R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
1981+
->getValue()
1982+
.getZExtValue());
19821983
return R;
19831984
}
19841985

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
162162
return false;
163163

164164
if (ProfDataName->getString().equals("branch_weights")) {
165-
for (const MDOperand &MDO : llvm::drop_begin(ProfileData->operands())) {
166-
auto *V = mdconst::dyn_extract<ConstantInt>(MDO);
165+
for (unsigned Idx = 1; Idx < ProfileData->getNumOperands(); Idx++) {
166+
auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
167167
assert(V && "Malformed branch_weight in MD_prof node");
168168
TotalVal += V->getValue().getZExtValue();
169169
}

llvm/lib/IR/Verifier.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,8 +2913,8 @@ void Verifier::visitFunction(const Function &F) {
29132913
VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
29142914
// The llvm.loop annotations also contain two DILocations.
29152915
if (auto MD = I.getMetadata(LLVMContext::MD_loop))
2916-
for (const MDOperand &MDO : llvm::drop_begin(MD->operands()))
2917-
VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MDO));
2916+
for (unsigned i = 1; i < MD->getNumOperands(); ++i)
2917+
VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
29182918
if (BrokenDebugInfo)
29192919
return;
29202920
}
@@ -4713,7 +4713,8 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
47134713
Check(MD->getNumOperands() == 1 + ExpectedNumOperands,
47144714
"Wrong number of operands", MD);
47154715
}
4716-
for (const MDOperand &MDO : llvm::drop_begin(MD->operands())) {
4716+
for (unsigned i = 1; i < MD->getNumOperands(); ++i) {
4717+
auto &MDO = MD->getOperand(i);
47174718
Check(MDO, "second operand should not be null", MD);
47184719
Check(mdconst::dyn_extract<ConstantInt>(MDO),
47194720
"!prof brunch_weights operand is not a const int");

0 commit comments

Comments
 (0)