@@ -58,6 +58,11 @@ class StaticDataSplitter : public MachineFunctionPass {
5858 // .data.rel.ro} sections.
5959 bool inStaticDataSection (const GlobalVariable *GV, const TargetMachine &TM);
6060
61+ // Returns the constant if the operand refers to a global variable or constant
62+ // that gets lowered to static data sections. Otherwise, return nullptr.
63+ const Constant *getConstant (const MachineOperand &Op,
64+ const TargetMachine &TM);
65+
6166 // Use profiles to partition static data.
6267 bool partitionStaticDataWithProfiles (MachineFunction &MF);
6368
@@ -84,6 +89,8 @@ class StaticDataSplitter : public MachineFunctionPass {
8489 AU.addRequired <MachineBlockFrequencyInfoWrapperPass>();
8590 AU.addRequired <ProfileSummaryInfoWrapperPass>();
8691 AU.addRequired <StaticDataProfileInfoWrapperPass>();
92+ // This pass does not modify the CFG.
93+ AU.setPreservesCFG ();
8794 }
8895
8996 bool runOnMachineFunction (MachineFunction &MF) override ;
@@ -112,6 +119,20 @@ bool StaticDataSplitter::runOnMachineFunction(MachineFunction &MF) {
112119 return Changed;
113120}
114121
122+ const Constant *StaticDataSplitter::getConstant (const MachineOperand &Op,
123+ const TargetMachine &TM) {
124+ if (!Op.isGlobal ())
125+ return nullptr ;
126+
127+ // Find global variables with local linkage.
128+ const GlobalVariable *GV = getLocalLinkageGlobalVariable (Op.getGlobal ());
129+ // Skip 'llvm.'-prefixed global variables conservatively because they are
130+ // often handled specially, and skip those not in static data sections.
131+ if (!GV || GV->getName ().starts_with (" llvm." ) || !inStaticDataSection (GV, TM))
132+ return nullptr ;
133+ return GV;
134+ }
135+
115136bool StaticDataSplitter::partitionStaticDataWithProfiles (MachineFunction &MF) {
116137 int NumChangedJumpTables = 0 ;
117138
@@ -148,17 +169,8 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
148169
149170 if (MJTI->updateJumpTableEntryHotness (JTI, Hotness))
150171 ++NumChangedJumpTables;
151- } else {
152- // Find global variables with local linkage.
153- const GlobalVariable *GV =
154- getLocalLinkageGlobalVariable (Op.getGlobal ());
155- // Skip 'special' global variables conservatively because they are
156- // often handled specially, and skip those not in static data
157- // sections.
158- if (!GV || GV->getName ().starts_with (" llvm." ) ||
159- !inStaticDataSection (GV, TM))
160- continue ;
161- SDPI->addConstantProfileCount (GV, Count);
172+ } else if (const Constant *C = getConstant (Op, TM)) {
173+ SDPI->addConstantProfileCount (C, Count);
162174 }
163175 }
164176 }
@@ -203,20 +215,11 @@ void StaticDataSplitter::updateStatsWithProfiles(const MachineFunction &MF) {
203215
204216void StaticDataSplitter::annotateStaticDataWithoutProfiles (
205217 const MachineFunction &MF) {
206- for (const auto &MBB : MF) {
207- for (const MachineInstr &I : MBB) {
208- for (const MachineOperand &Op : I.operands ()) {
209- if (!Op.isGlobal ())
210- continue ;
211- const GlobalVariable *GV =
212- getLocalLinkageGlobalVariable (Op.getGlobal ());
213- if (!GV || GV->getName ().starts_with (" llvm." ) ||
214- !inStaticDataSection (GV, MF.getTarget ()))
215- continue ;
216- SDPI->addConstantProfileCount (GV, std::nullopt );
217- }
218- }
219- }
218+ for (const auto &MBB : MF)
219+ for (const MachineInstr &I : MBB)
220+ for (const MachineOperand &Op : I.operands ())
221+ if (const Constant *C = getConstant (Op, MF.getTarget ()))
222+ SDPI->addConstantProfileCount (C, std::nullopt );
220223}
221224
222225void StaticDataSplitter::updateStatsWithoutProfiles (const MachineFunction &MF) {
0 commit comments