Skip to content

Commit 345861b

Browse files
authored
[MC] Optimize loops in MC (#98114)
https://llvm.org/docs/CodingStandards.html tells us that we should avoid evaluating `.end()` each time if possible.
1 parent 0998999 commit 345861b

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6120,8 +6120,8 @@ bool AsmParser::parseMSInlineAsm(
61206120
const char *AsmStart = ASMString.begin();
61216121
const char *AsmEnd = ASMString.end();
61226122
array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
6123-
for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
6124-
const AsmRewrite &AR = *it;
6123+
for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
6124+
const AsmRewrite &AR = *I;
61256125
// Check if this has already been covered by another rewrite...
61266126
if (AR.Done)
61276127
continue;
@@ -6164,7 +6164,7 @@ bool AsmParser::parseMSInlineAsm(
61646164
SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
61656165
size_t OffsetLen = OffsetName.size();
61666166
auto rewrite_it = std::find_if(
6167-
it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
6167+
I, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
61686168
return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
61696169
(FusingAR.Kind == AOK_Input ||
61706170
FusingAR.Kind == AOK_CallInput);

llvm/lib/MC/MCParser/MasmParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7474,8 +7474,8 @@ bool MasmParser::parseMSInlineAsm(
74747474
const char *AsmStart = ASMString.begin();
74757475
const char *AsmEnd = ASMString.end();
74767476
array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
7477-
for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
7478-
const AsmRewrite &AR = *it;
7477+
for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
7478+
const AsmRewrite &AR = *I;
74797479
// Check if this has already been covered by another rewrite...
74807480
if (AR.Done)
74817481
continue;
@@ -7518,7 +7518,7 @@ bool MasmParser::parseMSInlineAsm(
75187518
SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
75197519
size_t OffsetLen = OffsetName.size();
75207520
auto rewrite_it = std::find_if(
7521-
it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
7521+
I, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
75227522
return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
75237523
(FusingAR.Kind == AOK_Input ||
75247524
FusingAR.Kind == AOK_CallInput);

llvm/lib/MC/XCOFFObjectWriter.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,23 +1385,21 @@ void XCOFFObjectWriter::addExceptionEntry(
13851385
unsigned XCOFFObjectWriter::getExceptionSectionSize() {
13861386
unsigned EntryNum = 0;
13871387

1388-
for (auto it = ExceptionSection.ExceptionTable.begin();
1389-
it != ExceptionSection.ExceptionTable.end(); ++it)
1388+
for (const auto &TableEntry : ExceptionSection.ExceptionTable)
13901389
// The size() gets +1 to account for the initial entry containing the
13911390
// symbol table index.
1392-
EntryNum += it->second.Entries.size() + 1;
1391+
EntryNum += TableEntry.second.Entries.size() + 1;
13931392

13941393
return EntryNum * (is64Bit() ? XCOFF::ExceptionSectionEntrySize64
13951394
: XCOFF::ExceptionSectionEntrySize32);
13961395
}
13971396

13981397
unsigned XCOFFObjectWriter::getExceptionOffset(const MCSymbol *Symbol) {
13991398
unsigned EntryNum = 0;
1400-
for (auto it = ExceptionSection.ExceptionTable.begin();
1401-
it != ExceptionSection.ExceptionTable.end(); ++it) {
1402-
if (Symbol == it->second.FunctionSymbol)
1399+
for (const auto &TableEntry : ExceptionSection.ExceptionTable) {
1400+
if (Symbol == TableEntry.second.FunctionSymbol)
14031401
break;
1404-
EntryNum += it->second.Entries.size() + 1;
1402+
EntryNum += TableEntry.second.Entries.size() + 1;
14051403
}
14061404
return EntryNum * (is64Bit() ? XCOFF::ExceptionSectionEntrySize64
14071405
: XCOFF::ExceptionSectionEntrySize32);
@@ -1667,17 +1665,16 @@ void XCOFFObjectWriter::writeSectionForDwarfSectionEntry(
16671665
void XCOFFObjectWriter::writeSectionForExceptionSectionEntry(
16681666
const MCAssembler &Asm, ExceptionSectionEntry &ExceptionEntry,
16691667
uint64_t &CurrentAddressLocation) {
1670-
for (auto it = ExceptionEntry.ExceptionTable.begin();
1671-
it != ExceptionEntry.ExceptionTable.end(); it++) {
1668+
for (const auto &TableEntry : ExceptionEntry.ExceptionTable) {
16721669
// For every symbol that has exception entries, you must start the entries
16731670
// with an initial symbol table index entry
1674-
W.write<uint32_t>(SymbolIndexMap[it->second.FunctionSymbol]);
1671+
W.write<uint32_t>(SymbolIndexMap[TableEntry.second.FunctionSymbol]);
16751672
if (is64Bit()) {
16761673
// 4-byte padding on 64-bit.
16771674
W.OS.write_zeros(4);
16781675
}
16791676
W.OS.write_zeros(2);
1680-
for (auto &TrapEntry : it->second.Entries) {
1677+
for (auto &TrapEntry : TableEntry.second.Entries) {
16811678
writeWord(TrapEntry.TrapAddress);
16821679
W.write<uint8_t>(TrapEntry.Lang);
16831680
W.write<uint8_t>(TrapEntry.Reason);

0 commit comments

Comments
 (0)