@@ -69,6 +69,11 @@ class StaticDataSplitter : public MachineFunctionPass {
6969
7070 void annotateStaticDataWithoutProfiles (const MachineFunction &MF);
7171
72+ // Returns the constant if the operand refers to a global variable or constant
73+ // that gets lowered to static data sections. Otherwise, return nullptr.
74+ const Constant *getConstant (const MachineOperand &Op, const TargetMachine &TM,
75+ const MachineConstantPool *MCP);
76+
7277public:
7378 static char ID;
7479
@@ -112,12 +117,42 @@ bool StaticDataSplitter::runOnMachineFunction(MachineFunction &MF) {
112117 return Changed;
113118}
114119
120+ const Constant *
121+ StaticDataSplitter::getConstant (const MachineOperand &Op,
122+ const TargetMachine &TM,
123+ const MachineConstantPool *MCP) {
124+ if (!Op.isGlobal () && !Op.isCPI ())
125+ return nullptr ;
126+
127+ if (Op.isGlobal ()) {
128+ // Find global variables with local linkage.
129+ const GlobalVariable *GV = getLocalLinkageGlobalVariable (Op.getGlobal ());
130+ // Skip 'special' global variables conservatively because they are
131+ // often handled specially, and skip those not in static data
132+ // sections.
133+ if (!GV || GV->getName ().starts_with (" llvm." ) ||
134+ !inStaticDataSection (GV, TM))
135+ return nullptr ;
136+ return GV;
137+ }
138+ assert (Op.isCPI () && " Op must be constant pool index in this branch" );
139+ int CPI = Op.getIndex ();
140+ if (CPI == -1 )
141+ return nullptr ;
142+
143+ assert (MCP != nullptr && " Constant pool info is not available." );
144+ const MachineConstantPoolEntry &CPE = MCP->getConstants ()[CPI];
145+
146+ if (CPE.isMachineConstantPoolEntry ())
147+ return nullptr ;
148+
149+ return CPE.Val .ConstVal ;
150+ }
151+
115152bool StaticDataSplitter::partitionStaticDataWithProfiles (MachineFunction &MF) {
116153 int NumChangedJumpTables = 0 ;
117154
118- const TargetMachine &TM = MF.getTarget ();
119155 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo ();
120- const MachineConstantPool *MCP = MF.getConstantPool ();
121156
122157 // Jump table could be used by either terminating instructions or
123158 // non-terminating ones, so we walk all instructions and use
@@ -149,30 +184,8 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
149184
150185 if (MJTI->updateJumpTableEntryHotness (JTI, Hotness))
151186 ++NumChangedJumpTables;
152- } else if (Op.isGlobal ()) {
153- // Find global variables with local linkage.
154- const GlobalVariable *GV =
155- getLocalLinkageGlobalVariable (Op.getGlobal ());
156- // Skip 'special' global variables conservatively because they are
157- // often handled specially, and skip those not in static data
158- // sections.
159- if (!GV || GV->getName ().starts_with (" llvm." ) ||
160- !inStaticDataSection (GV, TM))
161- continue ;
162- SDPI->addConstantProfileCount (GV, Count);
163- } else {
164- assert (Op.isCPI () && " Op must be constant pool index in this branch" );
165- int CPI = Op.getIndex ();
166- if (CPI == -1 )
167- continue ;
168-
169- assert (MCP != nullptr && " Constant pool info is not available." );
170- const MachineConstantPoolEntry &CPE = MCP->getConstants ()[CPI];
171-
172- if (CPE.isMachineConstantPoolEntry ())
173- continue ;
174-
175- const Constant *C = CPE.Val .ConstVal ;
187+ } else if (const Constant *C =
188+ getConstant (Op, MF.getTarget (), MF.getConstantPool ())) {
176189 SDPI->addConstantProfileCount (C, Count);
177190 }
178191 }
@@ -218,34 +231,13 @@ void StaticDataSplitter::updateStatsWithProfiles(const MachineFunction &MF) {
218231
219232void StaticDataSplitter::annotateStaticDataWithoutProfiles (
220233 const MachineFunction &MF) {
221- const MachineConstantPool *MCP = MF.getConstantPool ();
222234 for (const auto &MBB : MF) {
223235 for (const MachineInstr &I : MBB) {
224236 for (const MachineOperand &Op : I.operands ()) {
225- if (!Op.isGlobal () && !Op.isCPI ())
226- continue ;
227- if (Op.isGlobal ()) {
228- const GlobalVariable *GV =
229- getLocalLinkageGlobalVariable (Op.getGlobal ());
230- if (!GV || GV->getName ().starts_with (" llvm." ) ||
231- !inStaticDataSection (GV, MF.getTarget ()))
232- continue ;
233- SDPI->addConstantProfileCount (GV, std::nullopt );
234- } else {
235- assert (Op.isCPI () && " Op must be constant pool index in this branch" );
236- int CPI = Op.getIndex ();
237- if (CPI == -1 )
238- continue ;
239-
240- assert (MCP != nullptr && " Constant pool info is not available." );
241- const MachineConstantPoolEntry &CPE = MCP->getConstants ()[CPI];
242-
243- if (CPE.isMachineConstantPoolEntry ())
244- continue ;
245-
246- const Constant *C = CPE.Val .ConstVal ;
237+ const Constant *C =
238+ getConstant (Op, MF.getTarget (), MF.getConstantPool ());
239+ if (C)
247240 SDPI->addConstantProfileCount (C, std::nullopt );
248- }
249241 }
250242 }
251243 }
0 commit comments