Skip to content

Commit f15266e

Browse files
authored
[MC][ELF] Emit instructions directly into fragment (#94950)
Avoid needless copying of instructions and fixups and directly emit into the fragment small vectors. This (optionally, second commit) also removes the single use of the MCCompactEncodedInstFragment to simplify code.
1 parent 5db6eac commit f15266e

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

llvm/lib/MC/MCELFStreamer.cpp

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,6 @@ static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
511511
void MCELFStreamer::emitInstToData(const MCInst &Inst,
512512
const MCSubtargetInfo &STI) {
513513
MCAssembler &Assembler = getAssembler();
514-
SmallVector<MCFixup, 4> Fixups;
515-
SmallString<256> Code;
516-
Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);
517-
518-
for (auto &Fixup : Fixups)
519-
fixSymbolsInTLSFixups(Fixup.getValue());
520514

521515
// There are several possibilities here:
522516
//
@@ -526,9 +520,7 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
526520
//
527521
// If bundling is enabled:
528522
// - If we're not in a bundle-locked group, emit the instruction into a
529-
// fragment of its own. If there are no fixups registered for the
530-
// instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
531-
// MCDataFragment.
523+
// fragment of its own.
532524
// - If we're in a bundle-locked group, append the instruction to the current
533525
// data fragment because we want all the instructions in a group to get into
534526
// the same fragment. Be careful not to do that for the first instruction in
@@ -542,16 +534,6 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
542534
// The bundle-locking directive ensures this is a new data fragment.
543535
DF = cast<MCDataFragment>(getCurrentFragment());
544536
CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
545-
} else if (!isBundleLocked() && Fixups.size() == 0) {
546-
// Optimize memory usage by emitting the instruction to a
547-
// MCCompactEncodedInstFragment when not in a bundle-locked group and
548-
// there are no fixups registered.
549-
MCCompactEncodedInstFragment *CEIF =
550-
getContext().allocFragment<MCCompactEncodedInstFragment>();
551-
insert(CEIF);
552-
CEIF->getContents().append(Code.begin(), Code.end());
553-
CEIF->setHasInstructions(STI);
554-
return;
555537
} else {
556538
DF = getContext().allocFragment<MCDataFragment>();
557539
insert(DF);
@@ -571,17 +553,22 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
571553
DF = getOrCreateDataFragment(&STI);
572554
}
573555

574-
// Add the fixups and data.
556+
// Emit instruction directly into data fragment.
557+
size_t FixupStartIndex = DF->getFixups().size();
558+
size_t CodeOffset = DF->getContents().size();
559+
Assembler.getEmitter().encodeInstruction(Inst, DF->getContents(),
560+
DF->getFixups(), STI);
561+
562+
auto Fixups = MutableArrayRef(DF->getFixups()).slice(FixupStartIndex);
575563
for (auto &Fixup : Fixups) {
576-
Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
577-
DF->getFixups().push_back(Fixup);
564+
Fixup.setOffset(Fixup.getOffset() + CodeOffset);
565+
fixSymbolsInTLSFixups(Fixup.getValue());
578566
}
579567

580568
DF->setHasInstructions(STI);
581569
if (!Fixups.empty() && Fixups.back().getTargetKind() ==
582570
getAssembler().getBackend().RelaxFixupKind)
583571
DF->setLinkerRelaxable();
584-
DF->getContents().append(Code.begin(), Code.end());
585572
}
586573

587574
void MCELFStreamer::emitBundleAlignMode(Align Alignment) {

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,8 @@ void MCObjectStreamer::emitInstToFragment(const MCInst &Inst,
393393
getContext().allocFragment<MCRelaxableFragment>(Inst, STI);
394394
insert(IF);
395395

396-
SmallString<128> Code;
397-
getAssembler().getEmitter().encodeInstruction(Inst, Code, IF->getFixups(),
398-
STI);
399-
IF->getContents().append(Code.begin(), Code.end());
396+
getAssembler().getEmitter().encodeInstruction(Inst, IF->getContents(),
397+
IF->getFixups(), STI);
400398
}
401399

402400
#ifndef NDEBUG

llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCCodeEmitter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ uint64_t SystemZMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNum,
172172
uint32_t BitOffset = MIBitSize - RawBitOffset - OpBitSize;
173173
Fixups.push_back(MCFixup::create(BitOffset >> 3, MO.getExpr(),
174174
(MCFixupKind)Kind, MI.getLoc()));
175-
assert(Fixups.size() <= 2 && "More than two memory operands in MI?");
176175
return 0;
177176
}
178177
llvm_unreachable("Unexpected operand type!");

0 commit comments

Comments
 (0)