-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[MC] Optimize loops in MC. #98114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MC] Optimize loops in MC. #98114
Conversation
@llvm/pr-subscribers-mc Author: Dmitriy Chestnykh (chestnykh) Changeshttps://llvm.org/docs/CodingStandards.html tells us that we should avoid evaluating Full diff: https://github.com/llvm/llvm-project/pull/98114.diff 3 Files Affected:
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index f3caa90eedfb1..dba56b15424d5 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -6116,8 +6116,8 @@ bool AsmParser::parseMSInlineAsm(
const char *AsmStart = ASMString.begin();
const char *AsmEnd = ASMString.end();
array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
- for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
- const AsmRewrite &AR = *it;
+ for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
+ const AsmRewrite &AR = *I;
// Check if this has already been covered by another rewrite...
if (AR.Done)
continue;
@@ -6160,7 +6160,7 @@ bool AsmParser::parseMSInlineAsm(
SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
size_t OffsetLen = OffsetName.size();
auto rewrite_it = std::find_if(
- it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
+ I, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
(FusingAR.Kind == AOK_Input ||
FusingAR.Kind == AOK_CallInput);
diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index 653cc64b4c36a..f64b7f62d61d0 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -7474,8 +7474,8 @@ bool MasmParser::parseMSInlineAsm(
const char *AsmStart = ASMString.begin();
const char *AsmEnd = ASMString.end();
array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
- for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
- const AsmRewrite &AR = *it;
+ for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
+ const AsmRewrite &AR = *I;
// Check if this has already been covered by another rewrite...
if (AR.Done)
continue;
@@ -7518,7 +7518,7 @@ bool MasmParser::parseMSInlineAsm(
SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
size_t OffsetLen = OffsetName.size();
auto rewrite_it = std::find_if(
- it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
+ I, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
(FusingAR.Kind == AOK_Input ||
FusingAR.Kind == AOK_CallInput);
diff --git a/llvm/lib/MC/XCOFFObjectWriter.cpp b/llvm/lib/MC/XCOFFObjectWriter.cpp
index 03a4ee831d895..122f19367ae7b 100644
--- a/llvm/lib/MC/XCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/XCOFFObjectWriter.cpp
@@ -1385,11 +1385,12 @@ void XCOFFObjectWriter::addExceptionEntry(
unsigned XCOFFObjectWriter::getExceptionSectionSize() {
unsigned EntryNum = 0;
- for (auto it = ExceptionSection.ExceptionTable.begin();
- it != ExceptionSection.ExceptionTable.end(); ++it)
+ for (auto I = ExceptionSection.ExceptionTable.begin(),
+ E = ExceptionSection.ExceptionTable.end();
+ I != E; ++I)
// The size() gets +1 to account for the initial entry containing the
// symbol table index.
- EntryNum += it->second.Entries.size() + 1;
+ EntryNum += I->second.Entries.size() + 1;
return EntryNum * (is64Bit() ? XCOFF::ExceptionSectionEntrySize64
: XCOFF::ExceptionSectionEntrySize32);
@@ -1397,11 +1398,12 @@ unsigned XCOFFObjectWriter::getExceptionSectionSize() {
unsigned XCOFFObjectWriter::getExceptionOffset(const MCSymbol *Symbol) {
unsigned EntryNum = 0;
- for (auto it = ExceptionSection.ExceptionTable.begin();
- it != ExceptionSection.ExceptionTable.end(); ++it) {
- if (Symbol == it->second.FunctionSymbol)
+ for (auto I = ExceptionSection.ExceptionTable.begin(),
+ E = ExceptionSection.ExceptionTable.end();
+ I != E; ++I) {
+ if (Symbol == I->second.FunctionSymbol)
break;
- EntryNum += it->second.Entries.size() + 1;
+ EntryNum += I->second.Entries.size() + 1;
}
return EntryNum * (is64Bit() ? XCOFF::ExceptionSectionEntrySize64
: XCOFF::ExceptionSectionEntrySize32);
@@ -1667,17 +1669,18 @@ void XCOFFObjectWriter::writeSectionForDwarfSectionEntry(
void XCOFFObjectWriter::writeSectionForExceptionSectionEntry(
const MCAssembler &Asm, ExceptionSectionEntry &ExceptionEntry,
uint64_t &CurrentAddressLocation) {
- for (auto it = ExceptionEntry.ExceptionTable.begin();
- it != ExceptionEntry.ExceptionTable.end(); it++) {
+ for (auto I = ExceptionEntry.ExceptionTable.begin(),
+ E = ExceptionEntry.ExceptionTable.end();
+ I != E; ++I) {
// For every symbol that has exception entries, you must start the entries
// with an initial symbol table index entry
- W.write<uint32_t>(SymbolIndexMap[it->second.FunctionSymbol]);
+ W.write<uint32_t>(SymbolIndexMap[I->second.FunctionSymbol]);
if (is64Bit()) {
// 4-byte padding on 64-bit.
W.OS.write_zeros(4);
}
W.OS.write_zeros(2);
- for (auto &TrapEntry : it->second.Entries) {
+ for (auto &TrapEntry : I->second.Entries) {
writeWord(TrapEntry.TrapAddress);
W.write<uint8_t>(TrapEntry.Lang);
W.write<uint8_t>(TrapEntry.Reason);
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
d2d3977
to
13bafeb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) We don't usually end the subject line of a commit message with a period.
13bafeb
to
20032d3
Compare
llvm/lib/MC/XCOFFObjectWriter.cpp
Outdated
@@ -1385,23 +1385,25 @@ void XCOFFObjectWriter::addExceptionEntry( | |||
unsigned XCOFFObjectWriter::getExceptionSectionSize() { | |||
unsigned EntryNum = 0; | |||
|
|||
for (auto it = ExceptionSection.ExceptionTable.begin(); | |||
it != ExceptionSection.ExceptionTable.end(); ++it) | |||
for (auto I = ExceptionSection.ExceptionTable.begin(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can use for each
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rewritten to for_each
Got it |
b7b980e
to
fdf9be2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't some/all of these be range based for loops instead?
Is range based loop preferable?
I left comments where the loop can be transformed into range-based. Other loops use iterator in their body so i think we can leave them without changes. |
fdf9be2
to
6d4ae81
Compare
Yes :) From the same guide: https://llvm.org/docs/CodingStandards.html#use-range-based-for-loops-wherever-possible |
6d4ae81
to
659626a
Compare
659626a
to
4b9c1ef
Compare
In accordance with https://llvm.org/docs/CodingStandards.html we shouldn't do this. Use range-based loops where possible
In accordance with https://llvm.org/docs/CodingStandards.html we shouldn't do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
A note for the future: amending commits and force pushing are discouraged, see
https://llvm.org/docs/GitHub.html#updating-pull-requests
FYI i don't have commit access so please commit my PR when it is ready |
https://llvm.org/docs/CodingStandards.html tells us that we should avoid evaluating `.end()` each time if possible.
https://llvm.org/docs/CodingStandards.html tells us that we should avoid evaluating
.end()
each time if possible.