Skip to content

Commit 51a0609

Browse files
committed
[BOLT] Enchance LowerAnnotations pass. NFCI.
After llvm#70147, all primary annotation types are stored directly in the instruction and hence there's no need for the temporary storage we've used previously for repopulating preserved annotations.
1 parent 67d29fb commit 51a0609

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -574,65 +574,47 @@ bool CheckLargeFunctions::shouldOptimize(const BinaryFunction &BF) const {
574574
}
575575

576576
void LowerAnnotations::runOnFunctions(BinaryContext &BC) {
577-
std::vector<std::pair<MCInst *, uint32_t>> PreservedOffsetAnnotations;
578-
std::vector<std::pair<MCInst *, MCSymbol *>> PreservedLabelAnnotations;
579-
580-
for (auto &It : BC.getBinaryFunctions()) {
581-
BinaryFunction &BF = It.second;
582-
583-
for (FunctionFragment &FF : BF.getLayout().fragments()) {
577+
for (BinaryFunction *BF : BC.getAllBinaryFunctions()) {
578+
for (FunctionFragment &FF : BF->getLayout().fragments()) {
579+
// Reset at the start of the new fragment.
584580
int64_t CurrentGnuArgsSize = 0;
585581

586582
for (BinaryBasicBlock *const BB : FF) {
587-
// First convert GnuArgsSize annotations into CFIs. This may change
588-
// instr pointers, so do it before recording ptrs for preserved
589-
// annotations
590-
if (BF.usesGnuArgsSize()) {
591-
for (auto II = BB->begin(); II != BB->end(); ++II) {
592-
if (!BC.MIB->isInvoke(*II))
593-
continue;
583+
for (auto II = BB->begin(); II != BB->end(); ++II) {
584+
585+
// Convert GnuArgsSize annotations into CFIs.
586+
if (BF->usesGnuArgsSize() && BC.MIB->isInvoke(*II)) {
594587
const int64_t NewGnuArgsSize = BC.MIB->getGnuArgsSize(*II);
595588
assert(NewGnuArgsSize >= 0 &&
596-
"expected non-negative GNU_args_size");
589+
"Expected non-negative GNU_args_size.");
597590
if (NewGnuArgsSize != CurrentGnuArgsSize) {
598-
auto InsertII = BF.addCFIInstruction(
591+
auto InsertII = BF->addCFIInstruction(
599592
BB, II,
600593
MCCFIInstruction::createGnuArgsSize(nullptr, NewGnuArgsSize));
601594
CurrentGnuArgsSize = NewGnuArgsSize;
602595
II = std::next(InsertII);
603596
}
604597
}
605-
}
606598

607-
// Now record preserved annotations separately and then strip
608-
// annotations.
609-
for (auto II = BB->begin(); II != BB->end(); ++II) {
610-
if (BF.requiresAddressTranslation() && BC.MIB->getOffset(*II))
611-
PreservedOffsetAnnotations.emplace_back(&(*II),
612-
*BC.MIB->getOffset(*II));
613-
if (MCSymbol *Label = BC.MIB->getLabel(*II))
614-
PreservedLabelAnnotations.emplace_back(&*II, Label);
599+
// Preserve selected annotations and strip the rest.
600+
std::optional<uint32_t> Offset = BF->requiresAddressTranslation()
601+
? BC.MIB->getOffset(*II)
602+
: std::nullopt;
603+
MCSymbol *Label = BC.MIB->getLabel(*II);
604+
615605
BC.MIB->stripAnnotations(*II);
606+
607+
if (Offset)
608+
BC.MIB->setOffset(*II, *Offset);
609+
if (Label)
610+
BC.MIB->setLabel(*II, Label);
616611
}
617612
}
618613
}
619614
}
620-
for (BinaryFunction *BF : BC.getInjectedBinaryFunctions())
621-
for (BinaryBasicBlock &BB : *BF)
622-
for (MCInst &Instruction : BB) {
623-
if (MCSymbol *Label = BC.MIB->getLabel(Instruction))
624-
PreservedLabelAnnotations.emplace_back(&Instruction, Label);
625-
BC.MIB->stripAnnotations(Instruction);
626-
}
627615

628616
// Release all memory taken by annotations
629617
BC.MIB->freeAnnotations();
630-
631-
// Reinsert preserved annotations we need during code emission.
632-
for (const std::pair<MCInst *, uint32_t> &Item : PreservedOffsetAnnotations)
633-
BC.MIB->setOffset(*Item.first, Item.second);
634-
for (auto [Instr, Label] : PreservedLabelAnnotations)
635-
BC.MIB->setLabel(*Instr, Label);
636618
}
637619

638620
// Check for dirty state in MCSymbol objects that might be a consequence

0 commit comments

Comments
 (0)