[llvm] 3fa5b52 - [MC] Decrease direct access to getContents()
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 21 17:14:32 PST 2024
Author: Fangrui Song
Date: 2024-12-21T17:14:26-08:00
New Revision: 3fa5b52714b3dadbc5fddfc4ae1e7c423a652962
URL: https://github.com/llvm/llvm-project/commit/3fa5b52714b3dadbc5fddfc4ae1e7c423a652962
DIFF: https://github.com/llvm/llvm-project/commit/3fa5b52714b3dadbc5fddfc4ae1e7c423a652962.diff
LOG: [MC] Decrease direct access to getContents()
to make it easier to store fragment contents out-of-line. Loosely based
on Alexis Engelke's prototype.
Added:
Modified:
llvm/include/llvm/MC/MCFragment.h
llvm/lib/MC/MCCodeView.cpp
llvm/lib/MC/MCMachOStreamer.cpp
llvm/lib/MC/MCObjectStreamer.cpp
llvm/lib/MC/MCSPIRVStreamer.cpp
llvm/lib/MC/MCWasmStreamer.cpp
llvm/lib/MC/MCWinCOFFStreamer.cpp
llvm/lib/MC/MCXCOFFStreamer.cpp
llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/MC/MCFragment.h b/llvm/include/llvm/MC/MCFragment.h
index 87465bcfa5d670..a1426c5f1c9e61 100644
--- a/llvm/include/llvm/MC/MCFragment.h
+++ b/llvm/include/llvm/MC/MCFragment.h
@@ -197,6 +197,10 @@ class MCEncodedFragmentWithFixups : public MCEncodedFragment {
SmallVectorImpl<char> &getContents() { return Contents; }
const SmallVectorImpl<char> &getContents() const { return Contents; }
+ void appendContents(ArrayRef<char> C) { Contents.append(C.begin(), C.end()); }
+ void appendContents(size_t Num, char Elt) { Contents.append(Num, Elt); }
+ void setContents(ArrayRef<char> C) { Contents.assign(C.begin(), C.end()); }
+
SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
diff --git a/llvm/lib/MC/MCCodeView.cpp b/llvm/lib/MC/MCCodeView.cpp
index 86f5016e86daa6..9e4a69ea18559c 100644
--- a/llvm/lib/MC/MCCodeView.cpp
+++ b/llvm/lib/MC/MCCodeView.cpp
@@ -137,7 +137,7 @@ MCDataFragment *CodeViewContext::getStringTableFragment() {
if (!StrTabFragment) {
StrTabFragment = MCCtx->allocFragment<MCDataFragment>();
// Start a new string table out with a null byte.
- StrTabFragment->getContents().push_back('\0');
+ StrTabFragment->appendContents(1, '\0');
}
return StrTabFragment;
}
@@ -151,7 +151,8 @@ std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) {
std::make_pair(Insertion.first->first(), Insertion.first->second);
if (Insertion.second) {
// The string map key is always null terminated.
- Contents.append(Ret.first.begin(), Ret.first.end() + 1);
+ StrTabFragment->appendContents(
+ ArrayRef(Ret.first.begin(), Ret.first.end() + 1));
}
return Ret;
}
diff --git a/llvm/lib/MC/MCMachOStreamer.cpp b/llvm/lib/MC/MCMachOStreamer.cpp
index 2e3b67eca08c1d..aa79d1bc5b869a 100644
--- a/llvm/lib/MC/MCMachOStreamer.cpp
+++ b/llvm/lib/MC/MCMachOStreamer.cpp
@@ -456,7 +456,7 @@ void MCMachOStreamer::emitInstToData(const MCInst &Inst,
DF->getFixups().push_back(Fixup);
}
DF->setHasInstructions(STI);
- DF->getContents().append(Code.begin(), Code.end());
+ DF->appendContents(Code);
}
void MCMachOStreamer::finishImpl() {
@@ -523,8 +523,7 @@ void MCMachOStreamer::finalizeCGProfile() {
size_t SectionBytes =
W.getCGProfile().size() * (2 * sizeof(uint32_t) + sizeof(uint64_t));
cast<MCDataFragment>(*CGProfileSection->begin())
- .getContents()
- .resize(SectionBytes);
+ .appendContents(SectionBytes, 0);
}
MCStreamer *llvm::createMachOStreamer(MCContext &Context,
@@ -559,5 +558,5 @@ void MCMachOStreamer::createAddrSigSection() {
// (instead of emitting a zero-sized section) so these relocations are
// technically valid, even though we don't expect these relocations to
// actually be applied by the linker.
- Frag->getContents().resize(8);
+ Frag->appendContents(8, 0);
}
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index b2b21435fa4af4..fff30955b25768 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -202,7 +202,7 @@ void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
DF->getFixups().push_back(
MCFixup::create(DF->getContents().size(), Value,
MCFixup::getKindForSize(Size, false), Loc));
- DF->getContents().resize(DF->getContents().size() + Size, 0);
+ DF->appendContents(Size, 0);
}
MCSymbol *MCObjectStreamer::emitCFILabel() {
@@ -552,7 +552,7 @@ void MCObjectStreamer::emitCVFileChecksumOffsetDirective(unsigned FileNo) {
void MCObjectStreamer::emitBytes(StringRef Data) {
MCDwarfLineEntry::make(this, getCurrentSectionOnly());
MCDataFragment *DF = getOrCreateDataFragment();
- DF->getContents().append(Data.begin(), Data.end());
+ DF->appendContents(ArrayRef(Data.data(), Data.size()));
}
void MCObjectStreamer::emitValueToAlignment(Align Alignment, int64_t Value,
@@ -586,7 +586,7 @@ void MCObjectStreamer::emitDTPRel32Value(const MCExpr *Value) {
MCDataFragment *DF = getOrCreateDataFragment();
DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Value, FK_DTPRel_4));
- DF->getContents().resize(DF->getContents().size() + 4, 0);
+ DF->appendContents(4, 0);
}
// Associate DTPRel64 fixup with data and resize data area
@@ -594,7 +594,7 @@ void MCObjectStreamer::emitDTPRel64Value(const MCExpr *Value) {
MCDataFragment *DF = getOrCreateDataFragment();
DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Value, FK_DTPRel_8));
- DF->getContents().resize(DF->getContents().size() + 8, 0);
+ DF->appendContents(8, 0);
}
// Associate TPRel32 fixup with data and resize data area
@@ -602,7 +602,7 @@ void MCObjectStreamer::emitTPRel32Value(const MCExpr *Value) {
MCDataFragment *DF = getOrCreateDataFragment();
DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Value, FK_TPRel_4));
- DF->getContents().resize(DF->getContents().size() + 4, 0);
+ DF->appendContents(4, 0);
}
// Associate TPRel64 fixup with data and resize data area
@@ -610,7 +610,7 @@ void MCObjectStreamer::emitTPRel64Value(const MCExpr *Value) {
MCDataFragment *DF = getOrCreateDataFragment();
DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Value, FK_TPRel_8));
- DF->getContents().resize(DF->getContents().size() + 8, 0);
+ DF->appendContents(8, 0);
}
// Associate GPRel32 fixup with data and resize data area
@@ -618,7 +618,7 @@ void MCObjectStreamer::emitGPRel32Value(const MCExpr *Value) {
MCDataFragment *DF = getOrCreateDataFragment();
DF->getFixups().push_back(
MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
- DF->getContents().resize(DF->getContents().size() + 4, 0);
+ DF->appendContents(4, 0);
}
// Associate GPRel64 fixup with data and resize data area
@@ -626,7 +626,7 @@ void MCObjectStreamer::emitGPRel64Value(const MCExpr *Value) {
MCDataFragment *DF = getOrCreateDataFragment();
DF->getFixups().push_back(
MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
- DF->getContents().resize(DF->getContents().size() + 8, 0);
+ DF->appendContents(8, 0);
}
static std::optional<std::pair<bool, std::string>>
diff --git a/llvm/lib/MC/MCSPIRVStreamer.cpp b/llvm/lib/MC/MCSPIRVStreamer.cpp
index 3b75a2e17a4a98..3b8cbee70d2091 100644
--- a/llvm/lib/MC/MCSPIRVStreamer.cpp
+++ b/llvm/lib/MC/MCSPIRVStreamer.cpp
@@ -28,7 +28,7 @@ void MCSPIRVStreamer::emitInstToData(const MCInst &Inst,
MCDataFragment *DF = getOrCreateDataFragment();
DF->setHasInstructions(STI);
- DF->getContents().append(Code.begin(), Code.end());
+ DF->appendContents(Code);
}
MCStreamer *llvm::createSPIRVStreamer(MCContext &Context,
diff --git a/llvm/lib/MC/MCWasmStreamer.cpp b/llvm/lib/MC/MCWasmStreamer.cpp
index 29b4cbd712d7fa..8560f0ebeb1d89 100644
--- a/llvm/lib/MC/MCWasmStreamer.cpp
+++ b/llvm/lib/MC/MCWasmStreamer.cpp
@@ -196,7 +196,7 @@ void MCWasmStreamer::emitInstToData(const MCInst &Inst,
DF->getFixups().push_back(Fixups[I]);
}
DF->setHasInstructions(STI);
- DF->getContents().append(Code.begin(), Code.end());
+ DF->appendContents(Code);
}
void MCWasmStreamer::finishImpl() {
diff --git a/llvm/lib/MC/MCWinCOFFStreamer.cpp b/llvm/lib/MC/MCWinCOFFStreamer.cpp
index 92329e079f93ad..fcd6c24d0778bd 100644
--- a/llvm/lib/MC/MCWinCOFFStreamer.cpp
+++ b/llvm/lib/MC/MCWinCOFFStreamer.cpp
@@ -71,7 +71,7 @@ void MCWinCOFFStreamer::emitInstToData(const MCInst &Inst,
DF->getFixups().push_back(Fixups[i]);
}
DF->setHasInstructions(STI);
- DF->getContents().append(Code.begin(), Code.end());
+ DF->appendContents(Code);
}
void MCWinCOFFStreamer::initSections(bool NoExecStack,
@@ -239,7 +239,7 @@ void MCWinCOFFStreamer::emitCOFFSectionIndex(const MCSymbol *Symbol) {
const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
DF->getFixups().push_back(Fixup);
- DF->getContents().resize(DF->getContents().size() + 2, 0);
+ DF->appendContents(2, 0);
}
void MCWinCOFFStreamer::emitCOFFSecRel32(const MCSymbol *Symbol,
@@ -257,7 +257,7 @@ void MCWinCOFFStreamer::emitCOFFSecRel32(const MCSymbol *Symbol,
// Record the relocation.
DF->getFixups().push_back(Fixup);
// Emit 4 bytes (zeros) to the object file.
- DF->getContents().resize(DF->getContents().size() + 4, 0);
+ DF->appendContents(4, 0);
}
void MCWinCOFFStreamer::emitCOFFImgRel32(const MCSymbol *Symbol,
@@ -276,7 +276,7 @@ void MCWinCOFFStreamer::emitCOFFImgRel32(const MCSymbol *Symbol,
// Record the relocation.
DF->getFixups().push_back(Fixup);
// Emit 4 bytes (zeros) to the object file.
- DF->getContents().resize(DF->getContents().size() + 4, 0);
+ DF->appendContents(4, 0);
}
void MCWinCOFFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
diff --git a/llvm/lib/MC/MCXCOFFStreamer.cpp b/llvm/lib/MC/MCXCOFFStreamer.cpp
index f5b83f29352ca7..0699d16047f4a9 100644
--- a/llvm/lib/MC/MCXCOFFStreamer.cpp
+++ b/llvm/lib/MC/MCXCOFFStreamer.cpp
@@ -161,7 +161,7 @@ void MCXCOFFStreamer::emitInstToData(const MCInst &Inst,
}
DF->setHasInstructions(STI);
- DF->getContents().append(Code.begin(), Code.end());
+ DF->appendContents(Code);
}
void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,
diff --git a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
index 6acc37e599f2ec..7864d45d594ac5 100644
--- a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
+++ b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
@@ -54,7 +54,7 @@ class HexagonAsmBackend : public MCAsmBackend {
// Update the fragment.
RF.setInst(HMB);
- RF.getContents() = Code;
+ RF.setContents(Code);
RF.getFixups() = Fixups;
}
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
index ef917568b5a50a..144a0c99fdf435 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
@@ -811,7 +811,7 @@ bool X86AsmBackend::padInstructionViaPrefix(MCRelaxableFragment &RF,
SmallString<256> Code;
Code.append(PrefixBytesToAdd, Prefix);
Code.append(RF.getContents().begin(), RF.getContents().end());
- RF.getContents() = Code;
+ RF.setContents(Code);
// Adjust the fixups for the change in offsets
for (auto &F : RF.getFixups()) {
@@ -843,7 +843,7 @@ bool X86AsmBackend::padInstructionViaRelaxation(MCRelaxableFragment &RF,
if (Delta > RemainingSize)
return false;
RF.setInst(Relaxed);
- RF.getContents() = Code;
+ RF.setContents(Code);
RF.getFixups() = Fixups;
RemainingSize -= Delta;
return true;
More information about the llvm-commits
mailing list