Skip to content

Commit 3fa5b52

Browse files
committed
[MC] Decrease direct access to getContents()
to make it easier to store fragment contents out-of-line. Loosely based on Alexis Engelke's prototype.
1 parent 9b0c8ef commit 3fa5b52

File tree

10 files changed

+28
-24
lines changed

10 files changed

+28
-24
lines changed

llvm/include/llvm/MC/MCFragment.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ class MCEncodedFragmentWithFixups : public MCEncodedFragment {
197197
SmallVectorImpl<char> &getContents() { return Contents; }
198198
const SmallVectorImpl<char> &getContents() const { return Contents; }
199199

200+
void appendContents(ArrayRef<char> C) { Contents.append(C.begin(), C.end()); }
201+
void appendContents(size_t Num, char Elt) { Contents.append(Num, Elt); }
202+
void setContents(ArrayRef<char> C) { Contents.assign(C.begin(), C.end()); }
203+
200204
SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
201205
const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
202206

llvm/lib/MC/MCCodeView.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ MCDataFragment *CodeViewContext::getStringTableFragment() {
137137
if (!StrTabFragment) {
138138
StrTabFragment = MCCtx->allocFragment<MCDataFragment>();
139139
// Start a new string table out with a null byte.
140-
StrTabFragment->getContents().push_back('\0');
140+
StrTabFragment->appendContents(1, '\0');
141141
}
142142
return StrTabFragment;
143143
}
@@ -151,7 +151,8 @@ std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) {
151151
std::make_pair(Insertion.first->first(), Insertion.first->second);
152152
if (Insertion.second) {
153153
// The string map key is always null terminated.
154-
Contents.append(Ret.first.begin(), Ret.first.end() + 1);
154+
StrTabFragment->appendContents(
155+
ArrayRef(Ret.first.begin(), Ret.first.end() + 1));
155156
}
156157
return Ret;
157158
}

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ void MCMachOStreamer::emitInstToData(const MCInst &Inst,
456456
DF->getFixups().push_back(Fixup);
457457
}
458458
DF->setHasInstructions(STI);
459-
DF->getContents().append(Code.begin(), Code.end());
459+
DF->appendContents(Code);
460460
}
461461

462462
void MCMachOStreamer::finishImpl() {
@@ -523,8 +523,7 @@ void MCMachOStreamer::finalizeCGProfile() {
523523
size_t SectionBytes =
524524
W.getCGProfile().size() * (2 * sizeof(uint32_t) + sizeof(uint64_t));
525525
cast<MCDataFragment>(*CGProfileSection->begin())
526-
.getContents()
527-
.resize(SectionBytes);
526+
.appendContents(SectionBytes, 0);
528527
}
529528

530529
MCStreamer *llvm::createMachOStreamer(MCContext &Context,
@@ -559,5 +558,5 @@ void MCMachOStreamer::createAddrSigSection() {
559558
// (instead of emitting a zero-sized section) so these relocations are
560559
// technically valid, even though we don't expect these relocations to
561560
// actually be applied by the linker.
562-
Frag->getContents().resize(8);
561+
Frag->appendContents(8, 0);
563562
}

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
202202
DF->getFixups().push_back(
203203
MCFixup::create(DF->getContents().size(), Value,
204204
MCFixup::getKindForSize(Size, false), Loc));
205-
DF->getContents().resize(DF->getContents().size() + Size, 0);
205+
DF->appendContents(Size, 0);
206206
}
207207

208208
MCSymbol *MCObjectStreamer::emitCFILabel() {
@@ -552,7 +552,7 @@ void MCObjectStreamer::emitCVFileChecksumOffsetDirective(unsigned FileNo) {
552552
void MCObjectStreamer::emitBytes(StringRef Data) {
553553
MCDwarfLineEntry::make(this, getCurrentSectionOnly());
554554
MCDataFragment *DF = getOrCreateDataFragment();
555-
DF->getContents().append(Data.begin(), Data.end());
555+
DF->appendContents(ArrayRef(Data.data(), Data.size()));
556556
}
557557

558558
void MCObjectStreamer::emitValueToAlignment(Align Alignment, int64_t Value,
@@ -586,47 +586,47 @@ void MCObjectStreamer::emitDTPRel32Value(const MCExpr *Value) {
586586
MCDataFragment *DF = getOrCreateDataFragment();
587587
DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
588588
Value, FK_DTPRel_4));
589-
DF->getContents().resize(DF->getContents().size() + 4, 0);
589+
DF->appendContents(4, 0);
590590
}
591591

592592
// Associate DTPRel64 fixup with data and resize data area
593593
void MCObjectStreamer::emitDTPRel64Value(const MCExpr *Value) {
594594
MCDataFragment *DF = getOrCreateDataFragment();
595595
DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
596596
Value, FK_DTPRel_8));
597-
DF->getContents().resize(DF->getContents().size() + 8, 0);
597+
DF->appendContents(8, 0);
598598
}
599599

600600
// Associate TPRel32 fixup with data and resize data area
601601
void MCObjectStreamer::emitTPRel32Value(const MCExpr *Value) {
602602
MCDataFragment *DF = getOrCreateDataFragment();
603603
DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
604604
Value, FK_TPRel_4));
605-
DF->getContents().resize(DF->getContents().size() + 4, 0);
605+
DF->appendContents(4, 0);
606606
}
607607

608608
// Associate TPRel64 fixup with data and resize data area
609609
void MCObjectStreamer::emitTPRel64Value(const MCExpr *Value) {
610610
MCDataFragment *DF = getOrCreateDataFragment();
611611
DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
612612
Value, FK_TPRel_8));
613-
DF->getContents().resize(DF->getContents().size() + 8, 0);
613+
DF->appendContents(8, 0);
614614
}
615615

616616
// Associate GPRel32 fixup with data and resize data area
617617
void MCObjectStreamer::emitGPRel32Value(const MCExpr *Value) {
618618
MCDataFragment *DF = getOrCreateDataFragment();
619619
DF->getFixups().push_back(
620620
MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
621-
DF->getContents().resize(DF->getContents().size() + 4, 0);
621+
DF->appendContents(4, 0);
622622
}
623623

624624
// Associate GPRel64 fixup with data and resize data area
625625
void MCObjectStreamer::emitGPRel64Value(const MCExpr *Value) {
626626
MCDataFragment *DF = getOrCreateDataFragment();
627627
DF->getFixups().push_back(
628628
MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
629-
DF->getContents().resize(DF->getContents().size() + 8, 0);
629+
DF->appendContents(8, 0);
630630
}
631631

632632
static std::optional<std::pair<bool, std::string>>

llvm/lib/MC/MCSPIRVStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void MCSPIRVStreamer::emitInstToData(const MCInst &Inst,
2828
MCDataFragment *DF = getOrCreateDataFragment();
2929

3030
DF->setHasInstructions(STI);
31-
DF->getContents().append(Code.begin(), Code.end());
31+
DF->appendContents(Code);
3232
}
3333

3434
MCStreamer *llvm::createSPIRVStreamer(MCContext &Context,

llvm/lib/MC/MCWasmStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void MCWasmStreamer::emitInstToData(const MCInst &Inst,
196196
DF->getFixups().push_back(Fixups[I]);
197197
}
198198
DF->setHasInstructions(STI);
199-
DF->getContents().append(Code.begin(), Code.end());
199+
DF->appendContents(Code);
200200
}
201201

202202
void MCWasmStreamer::finishImpl() {

llvm/lib/MC/MCWinCOFFStreamer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void MCWinCOFFStreamer::emitInstToData(const MCInst &Inst,
7171
DF->getFixups().push_back(Fixups[i]);
7272
}
7373
DF->setHasInstructions(STI);
74-
DF->getContents().append(Code.begin(), Code.end());
74+
DF->appendContents(Code);
7575
}
7676

7777
void MCWinCOFFStreamer::initSections(bool NoExecStack,
@@ -239,7 +239,7 @@ void MCWinCOFFStreamer::emitCOFFSectionIndex(const MCSymbol *Symbol) {
239239
const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
240240
MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
241241
DF->getFixups().push_back(Fixup);
242-
DF->getContents().resize(DF->getContents().size() + 2, 0);
242+
DF->appendContents(2, 0);
243243
}
244244

245245
void MCWinCOFFStreamer::emitCOFFSecRel32(const MCSymbol *Symbol,
@@ -257,7 +257,7 @@ void MCWinCOFFStreamer::emitCOFFSecRel32(const MCSymbol *Symbol,
257257
// Record the relocation.
258258
DF->getFixups().push_back(Fixup);
259259
// Emit 4 bytes (zeros) to the object file.
260-
DF->getContents().resize(DF->getContents().size() + 4, 0);
260+
DF->appendContents(4, 0);
261261
}
262262

263263
void MCWinCOFFStreamer::emitCOFFImgRel32(const MCSymbol *Symbol,
@@ -276,7 +276,7 @@ void MCWinCOFFStreamer::emitCOFFImgRel32(const MCSymbol *Symbol,
276276
// Record the relocation.
277277
DF->getFixups().push_back(Fixup);
278278
// Emit 4 bytes (zeros) to the object file.
279-
DF->getContents().resize(DF->getContents().size() + 4, 0);
279+
DF->appendContents(4, 0);
280280
}
281281

282282
void MCWinCOFFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,

llvm/lib/MC/MCXCOFFStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void MCXCOFFStreamer::emitInstToData(const MCInst &Inst,
161161
}
162162

163163
DF->setHasInstructions(STI);
164-
DF->getContents().append(Code.begin(), Code.end());
164+
DF->appendContents(Code);
165165
}
166166

167167
void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,

llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class HexagonAsmBackend : public MCAsmBackend {
5454

5555
// Update the fragment.
5656
RF.setInst(HMB);
57-
RF.getContents() = Code;
57+
RF.setContents(Code);
5858
RF.getFixups() = Fixups;
5959
}
6060

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ bool X86AsmBackend::padInstructionViaPrefix(MCRelaxableFragment &RF,
811811
SmallString<256> Code;
812812
Code.append(PrefixBytesToAdd, Prefix);
813813
Code.append(RF.getContents().begin(), RF.getContents().end());
814-
RF.getContents() = Code;
814+
RF.setContents(Code);
815815

816816
// Adjust the fixups for the change in offsets
817817
for (auto &F : RF.getFixups()) {
@@ -843,7 +843,7 @@ bool X86AsmBackend::padInstructionViaRelaxation(MCRelaxableFragment &RF,
843843
if (Delta > RemainingSize)
844844
return false;
845845
RF.setInst(Relaxed);
846-
RF.getContents() = Code;
846+
RF.setContents(Code);
847847
RF.getFixups() = Fixups;
848848
RemainingSize -= Delta;
849849
return true;

0 commit comments

Comments
 (0)