diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h index 5291369b3b9f1..3da63af5ba571 100644 --- a/llvm/include/llvm/CodeGen/AsmPrinter.h +++ b/llvm/include/llvm/CodeGen/AsmPrinter.h @@ -893,6 +893,9 @@ class AsmPrinter : public MachineFunctionPass { // Internal Implementation Details //===------------------------------------------------------------------===// + void emitJumpTableImpl(const MachineJumpTableInfo &MJTI, + ArrayRef JumpTableIndices, + bool JTInDiffSection); void emitJumpTableEntry(const MachineJumpTableInfo &MJTI, const MachineBasicBlock *MBB, unsigned uid) const; diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index b2a4721f37b26..c21915673f643 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2868,42 +2868,62 @@ void AsmPrinter::emitJumpTableInfo() { MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 || MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference64, F); + + SmallVector JumpTableIndices; + for (unsigned JTI = 0, JTSize = JT.size(); JTI < JTSize; ++JTI) { + JumpTableIndices.push_back(JTI); + } + emitJumpTableImpl(*MJTI, JumpTableIndices, JTInDiffSection); +} + +void AsmPrinter::emitJumpTableImpl(const MachineJumpTableInfo &MJTI, + ArrayRef JumpTableIndices, + bool JTInDiffSection) { + if (JumpTableIndices.empty()) + return; + + const TargetLoweringObjectFile &TLOF = getObjFileLowering(); + const Function &F = MF->getFunction(); + const std::vector &JT = MJTI.getJumpTables(); + MCSection *JumpTableSection = TLOF.getSectionForJumpTable(F, TM); + + const DataLayout &DL = MF->getDataLayout(); if (JTInDiffSection) { - // Drop it in the readonly section. - MCSection *ReadOnlySection = TLOF.getSectionForJumpTable(F, TM); - OutStreamer->switchSection(ReadOnlySection); + OutStreamer->switchSection(JumpTableSection); } - emitAlignment(Align(MJTI->getEntryAlignment(DL))); + emitAlignment(Align(MJTI.getEntryAlignment(MF->getDataLayout()))); // Jump tables in code sections are marked with a data_region directive // where that's supported. if (!JTInDiffSection) OutStreamer->emitDataRegion(MCDR_DataRegionJT32); - for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) { - const std::vector &JTBBs = JT[JTI].MBBs; + for (const unsigned JumpTableIndex : JumpTableIndices) { + ArrayRef JTBBs = JT[JumpTableIndex].MBBs; // If this jump table was deleted, ignore it. - if (JTBBs.empty()) continue; + if (JTBBs.empty()) + continue; // For the EK_LabelDifference32 entry, if using .set avoids a relocation, /// emit a .set directive for each unique entry. - if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 && + if (MJTI.getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 && MAI->doesSetDirectiveSuppressReloc()) { - SmallPtrSet EmittedSets; + SmallPtrSet EmittedSets; const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); - const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext); + const MCExpr *Base = + TLI->getPICJumpTableRelocBaseExpr(MF, JumpTableIndex, OutContext); for (const MachineBasicBlock *MBB : JTBBs) { if (!EmittedSets.insert(MBB).second) continue; // .set LJTSet, LBB32-base const MCExpr *LHS = - MCSymbolRefExpr::create(MBB->getSymbol(), OutContext); - OutStreamer->emitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()), - MCBinaryExpr::createSub(LHS, Base, - OutContext)); + MCSymbolRefExpr::create(MBB->getSymbol(), OutContext); + OutStreamer->emitAssignment( + GetJTSetSymbol(JumpTableIndex, MBB->getNumber()), + MCBinaryExpr::createSub(LHS, Base, OutContext)); } } @@ -2915,19 +2935,19 @@ void AsmPrinter::emitJumpTableInfo() { // FIXME: This doesn't have to have any specific name, just any randomly // named and numbered local label started with 'l' would work. Simplify // GetJTISymbol. - OutStreamer->emitLabel(GetJTISymbol(JTI, true)); + OutStreamer->emitLabel(GetJTISymbol(JumpTableIndex, true)); - MCSymbol* JTISymbol = GetJTISymbol(JTI); + MCSymbol *JTISymbol = GetJTISymbol(JumpTableIndex); OutStreamer->emitLabel(JTISymbol); // Defer MCAssembler based constant folding due to a performance issue. The // label differences will be evaluated at write time. for (const MachineBasicBlock *MBB : JTBBs) - emitJumpTableEntry(*MJTI, MBB, JTI); + emitJumpTableEntry(MJTI, MBB, JumpTableIndex); } if (EmitJumpTableSizesSection) - emitJumpTableSizesSection(*MJTI, F); + emitJumpTableSizesSection(MJTI, MF->getFunction()); if (!JTInDiffSection) OutStreamer->emitDataRegion(MCDR_DataRegionEnd);