69
69
#include " llvm/ADT/SmallVector.h"
70
70
#include " llvm/ADT/StringMap.h"
71
71
#include " llvm/ADT/StringRef.h"
72
+ #include " llvm/CodeGen/BasicBlockSectionUtils.h"
72
73
#include " llvm/CodeGen/MachineFunction.h"
73
74
#include " llvm/CodeGen/MachineFunctionPass.h"
74
75
#include " llvm/CodeGen/MachineModuleInfo.h"
@@ -226,9 +227,9 @@ static bool getBBClusterInfoForFunction(
226
227
// and "Cold" succeeding all other clusters.
227
228
// FuncBBClusterInfo represent the cluster information for basic blocks. If this
228
229
// is empty, it means unique sections for all basic blocks in the function.
229
- static bool assignSectionsAndSortBasicBlocks (
230
- MachineFunction &MF,
231
- const std::vector<Optional<BBClusterInfo>> &FuncBBClusterInfo) {
230
+ static void
231
+ assignSections ( MachineFunction &MF,
232
+ const std::vector<Optional<BBClusterInfo>> &FuncBBClusterInfo) {
232
233
assert (MF.hasBBSections () && " BB Sections is not set for function." );
233
234
// This variable stores the section ID of the cluster containing eh_pads (if
234
235
// all eh_pads are one cluster). If more than one cluster contain eh_pads, we
@@ -271,12 +272,51 @@ static bool assignSectionsAndSortBasicBlocks(
271
272
for (auto &MBB : MF)
272
273
if (MBB.isEHPad ())
273
274
MBB.setSectionID (EHPadsSectionID.getValue ());
275
+ }
274
276
277
+ void llvm::sortBasicBlocksAndUpdateBranches (
278
+ MachineFunction &MF, MachineBasicBlockComparator MBBCmp) {
275
279
SmallVector<MachineBasicBlock *, 4 > PreLayoutFallThroughs (
276
280
MF.getNumBlockIDs ());
277
281
for (auto &MBB : MF)
278
282
PreLayoutFallThroughs[MBB.getNumber ()] = MBB.getFallThrough ();
279
283
284
+ MF.sort (MBBCmp);
285
+
286
+ // Set IsBeginSection and IsEndSection according to the assigned section IDs.
287
+ MF.assignBeginEndSections ();
288
+
289
+ // After reordering basic blocks, we must update basic block branches to
290
+ // insert explicit fallthrough branches when required and optimize branches
291
+ // when possible.
292
+ updateBranches (MF, PreLayoutFallThroughs);
293
+ }
294
+
295
+ bool BasicBlockSections::runOnMachineFunction (MachineFunction &MF) {
296
+ auto BBSectionsType = MF.getTarget ().getBBSectionsType ();
297
+ assert (BBSectionsType != BasicBlockSection::None &&
298
+ " BB Sections not enabled!" );
299
+ // Renumber blocks before sorting them for basic block sections. This is
300
+ // useful during sorting, basic blocks in the same section will retain the
301
+ // default order. This renumbering should also be done for basic block
302
+ // labels to match the profiles with the correct blocks.
303
+ MF.RenumberBlocks ();
304
+
305
+ if (BBSectionsType == BasicBlockSection::Labels) {
306
+ MF.setBBSectionsType (BBSectionsType);
307
+ MF.createBBLabels ();
308
+ return true ;
309
+ }
310
+
311
+ std::vector<Optional<BBClusterInfo>> FuncBBClusterInfo;
312
+ if (BBSectionsType == BasicBlockSection::List &&
313
+ !getBBClusterInfoForFunction (MF, FuncAliasMap, ProgramBBClusterInfo,
314
+ FuncBBClusterInfo))
315
+ return true ;
316
+ MF.setBBSectionsType (BBSectionsType);
317
+ MF.createBBLabels ();
318
+ assignSections (MF, FuncBBClusterInfo);
319
+
280
320
// We make sure that the cluster including the entry basic block precedes all
281
321
// other clusters.
282
322
auto EntryBBSectionID = MF.front ().getSectionID ();
@@ -300,7 +340,8 @@ static bool assignSectionsAndSortBasicBlocks(
300
340
// contiguous and ordered accordingly. Furthermore, clusters are ordered in
301
341
// increasing order of their section IDs, with the exception and the
302
342
// cold section placed at the end of the function.
303
- MF.sort ([&](MachineBasicBlock &X, MachineBasicBlock &Y) {
343
+ auto Comparator = [&](const MachineBasicBlock &X,
344
+ const MachineBasicBlock &Y) {
304
345
auto XSectionID = X.getSectionID ();
305
346
auto YSectionID = Y.getSectionID ();
306
347
if (XSectionID != YSectionID)
@@ -311,43 +352,9 @@ static bool assignSectionsAndSortBasicBlocks(
311
352
return FuncBBClusterInfo[X.getNumber ()]->PositionInCluster <
312
353
FuncBBClusterInfo[Y.getNumber ()]->PositionInCluster ;
313
354
return X.getNumber () < Y.getNumber ();
314
- });
315
-
316
- // Set IsBeginSection and IsEndSection according to the assigned section IDs.
317
- MF.assignBeginEndSections ();
318
-
319
- // After reordering basic blocks, we must update basic block branches to
320
- // insert explicit fallthrough branches when required and optimize branches
321
- // when possible.
322
- updateBranches (MF, PreLayoutFallThroughs);
323
-
324
- return true ;
325
- }
326
-
327
- bool BasicBlockSections::runOnMachineFunction (MachineFunction &MF) {
328
- auto BBSectionsType = MF.getTarget ().getBBSectionsType ();
329
- assert (BBSectionsType != BasicBlockSection::None &&
330
- " BB Sections not enabled!" );
331
- // Renumber blocks before sorting them for basic block sections. This is
332
- // useful during sorting, basic blocks in the same section will retain the
333
- // default order. This renumbering should also be done for basic block
334
- // labels to match the profiles with the correct blocks.
335
- MF.RenumberBlocks ();
336
-
337
- if (BBSectionsType == BasicBlockSection::Labels) {
338
- MF.setBBSectionsType (BBSectionsType);
339
- MF.createBBLabels ();
340
- return true ;
341
- }
355
+ };
342
356
343
- std::vector<Optional<BBClusterInfo>> FuncBBClusterInfo;
344
- if (BBSectionsType == BasicBlockSection::List &&
345
- !getBBClusterInfoForFunction (MF, FuncAliasMap, ProgramBBClusterInfo,
346
- FuncBBClusterInfo))
347
- return true ;
348
- MF.setBBSectionsType (BBSectionsType);
349
- MF.createBBLabels ();
350
- assignSectionsAndSortBasicBlocks (MF, FuncBBClusterInfo);
357
+ sortBasicBlocksAndUpdateBranches (MF, Comparator);
351
358
return true ;
352
359
}
353
360
0 commit comments