@@ -43,7 +43,8 @@ STATISTIC(NumUnknownJumpTables,
4343
4444static cl::opt<MachineFunctionDataHotness> StaticDataDefaultHotness (
4545 " static-data-default-hotness" , cl::Hidden,
46- cl::desc (" The hotness for static data with unknown hotness" ),
46+ cl::desc (" This option specifies the hotness of static data when profile "
47+ " information is unavailable" ),
4748 cl::init(MachineFunctionDataHotness::Hot),
4849 cl::values(clEnumValN(MachineFunctionDataHotness::Hot, " hot" , " Hot" ),
4950 clEnumValN(MachineFunctionDataHotness::Cold, " cold" , " Cold" )));
@@ -84,32 +85,38 @@ bool StaticDataSplitter::runOnMachineFunction(MachineFunction &MF) {
8485 MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
8586 PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI ();
8687
87- // Split jump tables based on profile information. Subsequent patches will
88- // handle other data types like constant pools, module-internal data, etc.
8988 return splitJumpTables (MF);
9089}
9190
9291bool StaticDataSplitter::splitJumpTablesWithProfiles (
9392 MachineFunction &MF, MachineJumpTableInfo &MJTI) {
9493 int NumChangedJumpTables = 0 ;
9594
95+ // Jump table could be used by either terminating instructions or
96+ // non-terminating ones, so we walk all instructions and use
97+ // `MachineOperand::isJTI()` to identify jump table operands.
98+ // Similarly, `MachineOperand::isCPI()` can identify constant pool usages
99+ // in the same loop.
96100 for (const auto &MBB : MF) {
97- // IMPORTANT, `getJumpTableIndex` is a thin wrapper around per-target
98- // interface `TargetInstrInfo::getjumpTableIndex`, and only X86 implements
99- // it so far.
100- const int JTI = MBB.getJumpTableIndex ();
101- // This is not a source block of jump table.
102- if (JTI == -1 )
103- continue ;
104-
105- auto Hotness = MachineFunctionDataHotness::Hot;
106-
107- // Hotness is based on source basic block hotness.
108- if (PSI->isColdBlock (&MBB, MBFI))
109- Hotness = MachineFunctionDataHotness::Cold;
110-
111- if (MF.getJumpTableInfo ()->updateJumpTableEntryHotness (JTI, Hotness))
112- ++NumChangedJumpTables;
101+ for (const MachineInstr &I : MBB) {
102+ for (const MachineOperand &Op : I.operands ()) {
103+ if (!Op.isJTI ())
104+ continue ;
105+ const int JTI = Op.getIndex ();
106+ // This is not a source block of jump table.
107+ if (JTI == -1 )
108+ continue ;
109+
110+ auto Hotness = MachineFunctionDataHotness::Hot;
111+
112+ // Hotness is based on source basic block hotness.
113+ if (PSI->isColdBlock (&MBB, MBFI))
114+ Hotness = MachineFunctionDataHotness::Cold;
115+
116+ if (MF.getJumpTableInfo ()->updateJumpTableEntryHotness (JTI, Hotness))
117+ ++NumChangedJumpTables;
118+ }
119+ }
113120 }
114121 return NumChangedJumpTables > 0 ;
115122}
@@ -136,7 +143,7 @@ bool StaticDataSplitter::splitJumpTables(MachineFunction &MF) {
136143 NumHotJumpTables++;
137144 else {
138145 assert (Hotness == MachineFunctionDataHotness::Cold &&
139- " A jump table is hot or cold when profile information is "
146+ " A jump table is either hot or cold when profile information is "
140147 " available." );
141148 NumColdJumpTables++;
142149 }
@@ -147,8 +154,9 @@ bool StaticDataSplitter::splitJumpTables(MachineFunction &MF) {
147154 if (ProfileAvailable)
148155 return splitJumpTablesWithProfiles (MF, *MJTI);
149156
150- // If function profile is unavailable, -static-data-default-hotness specifies
151- // the hotness.
157+ // If function profile is unavailable (e.g., module not instrumented, or new
158+ // code paths lacking samples), -static-data-default-hotness specifies the
159+ // hotness.
152160 for (size_t JTI = 0 ; JTI < MJTI->getJumpTables ().size (); JTI++)
153161 MF.getJumpTableInfo ()->updateJumpTableEntryHotness (
154162 JTI, StaticDataDefaultHotness);
0 commit comments