@@ -2880,8 +2880,10 @@ void AsmPrinter::emitJumpTableInfo() {
28802880 if (!TM.Options .EnableStaticDataPartitioning ) {
28812881 for (unsigned JTI = 0 , JTSize = JT.size (); JTI < JTSize; ++JTI)
28822882 JumpTableIndices.push_back (JTI);
2883- emitJumpTables (JumpTableIndices, TLOF.getSectionForJumpTable (F, TM),
2884- JTInDiffSection, *MJTI);
2883+ emitJumpTableImpl (
2884+ *MJTI,
2885+ llvm::make_range (JumpTableIndices.begin (), JumpTableIndices.end ()),
2886+ JTInDiffSection);
28852887 return ;
28862888 }
28872889
@@ -2891,47 +2893,56 @@ void AsmPrinter::emitJumpTableInfo() {
28912893 //
28922894 // Iterate all jump tables, put hot jump table indices towards the beginning
28932895 // of the vector, and cold jump table indices towards the end. Meanwhile
2894- // retain the relative orders of original jump tables within a hot or unlikely
2895- // section by reversing the cold jump table indices.
2896- int NextHotJumpTableIndex = 0 , NextColdJumpTableIndex = JT.size () - 1 ;
2896+ // retain the relative orders of original jump tables.
2897+ int NumHotJumpTables = 0 , NextColdJumpTableIndex = JT.size () - 1 ;
28972898 JumpTableIndices.resize (JT.size ());
28982899 for (unsigned JTI = 0 , JTSize = JT.size (); JTI < JTSize; ++JTI) {
28992900 if (JT[JTI].Hotness == MachineFunctionDataHotness::Cold) {
29002901 JumpTableIndices[NextColdJumpTableIndex--] = JTI;
29012902 } else {
2902- JumpTableIndices[NextHotJumpTableIndex ++] = JTI;
2903+ JumpTableIndices[NumHotJumpTables ++] = JTI;
29032904 }
29042905 }
29052906
2906- if (NextHotJumpTableIndex != 0 ) {
2907- emitJumpTables (
2908- ArrayRef<unsigned >(JumpTableIndices).take_front (NextHotJumpTableIndex),
2909- TLOF.getSectionForJumpTable (F, TM, &JT[0 ]), JTInDiffSection, *MJTI);
2910- }
2907+ emitJumpTableImpl (
2908+ *MJTI,
2909+ llvm::make_range (JumpTableIndices.begin (),
2910+ JumpTableIndices.begin () + NumHotJumpTables),
29112911
2912- if (NextHotJumpTableIndex < (int )JT.size ()) {
2913- // Reverse the order of cold jump tables indices.
2914- for (int L = NextHotJumpTableIndex, R = JT.size () - 1 ; L < R; ++L, --R)
2915- std::swap (JumpTableIndices[L], JumpTableIndices[R]);
2916-
2917- emitJumpTables (
2918- ArrayRef<unsigned >(JumpTableIndices)
2919- .take_back (JT.size () - NextHotJumpTableIndex),
2920- TLOF.getSectionForJumpTable (
2921- F, TM, &JT[JumpTableIndices[NextHotJumpTableIndex]]),
2922- JTInDiffSection, *MJTI);
2923- }
2912+ JTInDiffSection);
2913+
2914+ const int NumColdJumpTables = JT.size () - NumHotJumpTables;
2915+ assert (NumColdJumpTables >= 0 && " Invalid number of cold jump tables." );
2916+
2917+ // Reverse iterating cold jump table indices to emit in the original order.
2918+ emitJumpTableImpl (
2919+ *MJTI,
2920+ llvm::make_range (JumpTableIndices.rbegin (),
2921+ JumpTableIndices.rbegin () + NumColdJumpTables),
2922+ JTInDiffSection);
29242923
29252924 return ;
29262925}
29272926
2928- void AsmPrinter::emitJumpTables (ArrayRef<unsigned > JumpTableIndices,
2929- MCSection *JumpTableSection,
2930- bool JTInDiffSection,
2931- const MachineJumpTableInfo &MJTI) {
2927+ template <typename Iterator>
2928+ void AsmPrinter::emitJumpTableImpl (
2929+ const MachineJumpTableInfo &MJTI,
2930+ const llvm::iterator_range<Iterator> &JumpTableIndices,
2931+ bool JTInDiffSection) {
29322932 if (JumpTableIndices.empty ())
29332933 return ;
29342934
2935+ const TargetLoweringObjectFile &TLOF = getObjFileLowering ();
2936+ const Function &F = MF->getFunction ();
2937+ const std::vector<MachineJumpTableEntry> &JT = MJTI.getJumpTables ();
2938+ MCSection *JumpTableSection = nullptr ;
2939+ if (TM.Options .EnableStaticDataPartitioning ) {
2940+ JumpTableSection =
2941+ TLOF.getSectionForJumpTable (F, TM, &JT[*JumpTableIndices.begin ()]);
2942+ } else {
2943+ JumpTableSection = TLOF.getSectionForJumpTable (F, TM);
2944+ }
2945+
29352946 const DataLayout &DL = MF->getDataLayout ();
29362947 if (JTInDiffSection) {
29372948 OutStreamer->switchSection (JumpTableSection);
@@ -2944,10 +2955,8 @@ void AsmPrinter::emitJumpTables(ArrayRef<unsigned> JumpTableIndices,
29442955 if (!JTInDiffSection)
29452956 OutStreamer->emitDataRegion (MCDR_DataRegionJT32);
29462957
2947- const auto &JT = MJTI.getJumpTables ();
2948- for (unsigned Index = 0 , e = JumpTableIndices.size (); Index != e; ++Index) {
2949- ArrayRef<MachineBasicBlock *> JTBBs =
2950- JT[JumpTableIndices[Index]].MBBs ;
2958+ for (const unsigned JumpTableIndex : JumpTableIndices) {
2959+ ArrayRef<MachineBasicBlock *> JTBBs = JT[JumpTableIndex].MBBs ;
29512960
29522961 // If this jump table was deleted, ignore it.
29532962 if (JTBBs.empty ())
@@ -2959,8 +2968,8 @@ void AsmPrinter::emitJumpTables(ArrayRef<unsigned> JumpTableIndices,
29592968 MAI->doesSetDirectiveSuppressReloc ()) {
29602969 SmallPtrSet<const MachineBasicBlock *, 16 > EmittedSets;
29612970 const TargetLowering *TLI = MF->getSubtarget ().getTargetLowering ();
2962- const MCExpr *Base = TLI-> getPICJumpTableRelocBaseExpr (
2963- MF, JumpTableIndices[Index] , OutContext);
2971+ const MCExpr *Base =
2972+ TLI-> getPICJumpTableRelocBaseExpr ( MF, JumpTableIndex , OutContext);
29642973 for (const MachineBasicBlock *MBB : JTBBs) {
29652974 if (!EmittedSets.insert (MBB).second )
29662975 continue ;
@@ -2969,7 +2978,7 @@ void AsmPrinter::emitJumpTables(ArrayRef<unsigned> JumpTableIndices,
29692978 const MCExpr *LHS =
29702979 MCSymbolRefExpr::create (MBB->getSymbol (), OutContext);
29712980 OutStreamer->emitAssignment (
2972- GetJTSetSymbol (JumpTableIndices[Index] , MBB->getNumber ()),
2981+ GetJTSetSymbol (JumpTableIndex , MBB->getNumber ()),
29732982 MCBinaryExpr::createSub (LHS, Base, OutContext));
29742983 }
29752984 }
@@ -2982,15 +2991,15 @@ void AsmPrinter::emitJumpTables(ArrayRef<unsigned> JumpTableIndices,
29822991 // FIXME: This doesn't have to have any specific name, just any randomly
29832992 // named and numbered local label started with 'l' would work. Simplify
29842993 // GetJTISymbol.
2985- OutStreamer->emitLabel (GetJTISymbol (JumpTableIndices[Index] , true ));
2994+ OutStreamer->emitLabel (GetJTISymbol (JumpTableIndex , true ));
29862995
2987- MCSymbol *JTISymbol = GetJTISymbol (JumpTableIndices[Index] );
2996+ MCSymbol *JTISymbol = GetJTISymbol (JumpTableIndex );
29882997 OutStreamer->emitLabel (JTISymbol);
29892998
29902999 // Defer MCAssembler based constant folding due to a performance issue. The
29913000 // label differences will be evaluated at write time.
29923001 for (const MachineBasicBlock *MBB : JTBBs)
2993- emitJumpTableEntry (MJTI, MBB, JumpTableIndices[Index] );
3002+ emitJumpTableEntry (MJTI, MBB, JumpTableIndex );
29943003 }
29953004
29963005 if (EmitJumpTableSizesSection)
0 commit comments