1010// for the following types of static data:
1111// - Jump tables
1212// - Module-internal global variables
13- // - Constant pools (TODO)
13+ // - Constant pools
1414//
1515// For the original RFC of this pass please see
1616// https://discourse.llvm.org/t/rfc-profile-guided-static-data-partitioning/83744
@@ -117,16 +117,17 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
117117
118118 const TargetMachine &TM = MF.getTarget ();
119119 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo ();
120+ const MachineConstantPool *MCP = MF.getConstantPool ();
120121
121122 // Jump table could be used by either terminating instructions or
122123 // non-terminating ones, so we walk all instructions and use
123124 // `MachineOperand::isJTI()` to identify jump table operands.
124- // Similarly, `MachineOperand::isCPI()` can identify constant pool usages
125- // in the same loop.
125+ // Similarly, `MachineOperand::isCPI()` is used to identify constant pool
126+ // usages in the same loop.
126127 for (const auto &MBB : MF) {
127128 for (const MachineInstr &I : MBB) {
128129 for (const MachineOperand &Op : I.operands ()) {
129- if (!Op.isJTI () && !Op.isGlobal ())
130+ if (!Op.isJTI () && !Op.isGlobal () && !Op. isCPI () )
130131 continue ;
131132
132133 std::optional<uint64_t > Count = MBFI->getBlockProfileCount (&MBB);
@@ -148,7 +149,7 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
148149
149150 if (MJTI->updateJumpTableEntryHotness (JTI, Hotness))
150151 ++NumChangedJumpTables;
151- } else {
152+ } else if (Op. isGlobal ()) {
152153 // Find global variables with local linkage.
153154 const GlobalVariable *GV =
154155 getLocalLinkageGlobalVariable (Op.getGlobal ());
@@ -159,6 +160,20 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
159160 !inStaticDataSection (GV, TM))
160161 continue ;
161162 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 ;
176+ SDPI->addConstantProfileCount (C, Count);
162177 }
163178 }
164179 }
@@ -203,17 +218,34 @@ void StaticDataSplitter::updateStatsWithProfiles(const MachineFunction &MF) {
203218
204219void StaticDataSplitter::annotateStaticDataWithoutProfiles (
205220 const MachineFunction &MF) {
221+ const MachineConstantPool *MCP = MF.getConstantPool ();
206222 for (const auto &MBB : MF) {
207223 for (const MachineInstr &I : MBB) {
208224 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 ()))
225+ if (!Op.isGlobal () && !Op.isCPI ())
215226 continue ;
216- SDPI->addConstantProfileCount (GV, std::nullopt );
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 ;
247+ SDPI->addConstantProfileCount (C, std::nullopt );
248+ }
217249 }
218250 }
219251 }
0 commit comments