@@ -1097,6 +1097,14 @@ void AsmPrinter::emitFunctionBody() {
1097
1097
// Print out code for the function.
1098
1098
bool HasAnyRealCode = false ;
1099
1099
int NumInstsInFunction = 0 ;
1100
+ bool emitBBSections = MF->hasBBSections ();
1101
+ MachineBasicBlock *EndOfRegularSectionMBB = nullptr ;
1102
+ if (emitBBSections) {
1103
+ EndOfRegularSectionMBB =
1104
+ const_cast <MachineBasicBlock *>(MF->front ().getSectionEndMBB ());
1105
+ assert (EndOfRegularSectionMBB->isEndSection () &&
1106
+ " The MBB at the end of the regular section must end a section" );
1107
+ }
1100
1108
1101
1109
for (auto &MBB : *MF) {
1102
1110
// Print a label for the basic block.
@@ -1177,41 +1185,17 @@ void AsmPrinter::emitFunctionBody() {
1177
1185
}
1178
1186
}
1179
1187
}
1180
-
1181
- // We need a temporary symbol for the end of this basic block, if either we
1182
- // have BBLabels enabled and we want to emit size directive for the BBs, or
1183
- // if this basic blocks marks the end of a section (except the section
1184
- // containing the entry basic block as the end symbol for that section is
1185
- // CurrentFnEnd).
1186
- MCSymbol *CurrentBBEnd = nullptr ;
1187
- if ((MAI->hasDotTypeDotSizeDirective () && MF->hasBBLabels ()) ||
1188
- (MBB.isEndSection () && !MBB.sameSection (&MF->front ()))) {
1189
- CurrentBBEnd = OutContext.createTempSymbol ();
1190
- OutStreamer->emitLabel (CurrentBBEnd);
1191
- }
1192
-
1193
- // Helper for emitting the size directive associated with a basic block
1194
- // symbol.
1195
- auto emitELFSizeDirective = [&](MCSymbol *SymForSize) {
1196
- assert (CurrentBBEnd && " Basicblock end symbol not set!" );
1188
+ if (&MBB != EndOfRegularSectionMBB &&
1189
+ (MF->hasBBLabels () || MBB.isEndSection ())) {
1190
+ // Emit size directive for the size of this basic block. Create a symbol
1191
+ // for the end of the basic block.
1192
+ MCSymbol *CurrentBBEnd = OutContext.createTempSymbol ();
1197
1193
const MCExpr *SizeExp = MCBinaryExpr::createSub (
1198
1194
MCSymbolRefExpr::create (CurrentBBEnd, OutContext),
1199
- MCSymbolRefExpr::create (SymForSize, OutContext), OutContext);
1200
- OutStreamer->emitELFSize (SymForSize, SizeExp);
1201
- };
1202
-
1203
- // Emit size directive for the size of each basic block, if BBLabels is
1204
- // enabled.
1205
- if (MAI->hasDotTypeDotSizeDirective () && MF->hasBBLabels ())
1206
- emitELFSizeDirective (MBB.getSymbol ());
1207
-
1208
- // Emit size directive for the size of each basic block section once we
1209
- // get to the end of that section.
1210
- if (MBB.isEndSection ()) {
1211
- if (!MBB.sameSection (&MF->front ())) {
1212
- if (MAI->hasDotTypeDotSizeDirective ())
1213
- emitELFSizeDirective (CurrentSectionBeginSym);
1214
- }
1195
+ MCSymbolRefExpr::create (MBB.getSymbol (), OutContext), OutContext);
1196
+ OutStreamer->emitLabel (CurrentBBEnd);
1197
+ MBB.setEndMCSymbol (CurrentBBEnd);
1198
+ OutStreamer->emitELFSize (MBB.getSymbol (), SizeExp);
1215
1199
}
1216
1200
emitBasicBlockEnd (MBB);
1217
1201
}
@@ -1246,8 +1230,9 @@ void AsmPrinter::emitFunctionBody() {
1246
1230
}
1247
1231
}
1248
1232
1249
- // Switch to the original section in case basic block sections was used.
1250
- OutStreamer->SwitchSection (MF->getSection ());
1233
+ // Switch to the original section if basic block sections was used.
1234
+ if (emitBBSections)
1235
+ OutStreamer->SwitchSection (MF->getSection ());
1251
1236
1252
1237
const Function &F = MF->getFunction ();
1253
1238
for (const auto &BB : F) {
@@ -1264,7 +1249,7 @@ void AsmPrinter::emitFunctionBody() {
1264
1249
emitFunctionBodyEnd ();
1265
1250
1266
1251
if (needFuncLabelsForEHOrDebugInfo (*MF, MMI) ||
1267
- MAI->hasDotTypeDotSizeDirective ()) {
1252
+ MAI->hasDotTypeDotSizeDirective () || emitBBSections ) {
1268
1253
// Create a symbol for the end of function.
1269
1254
CurrentFnEnd = createTempSymbol (" func_end" );
1270
1255
OutStreamer->emitLabel (CurrentFnEnd);
@@ -1287,6 +1272,8 @@ void AsmPrinter::emitFunctionBody() {
1287
1272
HI.Handler ->markFunctionEnd ();
1288
1273
}
1289
1274
1275
+ if (emitBBSections)
1276
+ EndOfRegularSectionMBB->setEndMCSymbol (CurrentFnEnd);
1290
1277
1291
1278
// Print out jump tables referenced by the function.
1292
1279
emitJumpTableInfo ();
@@ -1766,7 +1753,6 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
1766
1753
1767
1754
CurrentFnSymForSize = CurrentFnSym;
1768
1755
CurrentFnBegin = nullptr ;
1769
- CurrentSectionBeginSym = nullptr ;
1770
1756
CurExceptionSym = nullptr ;
1771
1757
bool NeedsLocalForSize = MAI->needsLocalForSize ();
1772
1758
if (F.hasFnAttribute (" patchable-function-entry" ) ||
@@ -2995,6 +2981,7 @@ static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
2995
2981
// / MachineBasicBlock, an alignment (if present) and a comment describing
2996
2982
// / it if appropriate.
2997
2983
void AsmPrinter::emitBasicBlockStart (const MachineBasicBlock &MBB) {
2984
+ bool BBSections = MF->hasBBSections ();
2998
2985
// End the previous funclet and start a new one.
2999
2986
if (MBB.isEHFuncletEntry ()) {
3000
2987
for (const HandlerInfo &HI : Handlers) {
@@ -3004,9 +2991,11 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
3004
2991
}
3005
2992
3006
2993
// Emit an alignment directive for this block, if needed.
3007
- const Align Alignment = MBB.getAlignment ();
3008
- if (Alignment != Align (1 ))
3009
- emitAlignment (Alignment);
2994
+ if (MBB.pred_empty () || !BBSections) {
2995
+ const Align Alignment = MBB.getAlignment ();
2996
+ if (Alignment != Align (1 ))
2997
+ emitAlignment (Alignment);
2998
+ }
3010
2999
3011
3000
// If the block has its address taken, emit any labels that were used to
3012
3001
// reference the block. It is possible that there is more than one label
@@ -3038,8 +3027,9 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
3038
3027
emitBasicBlockLoopComments (MBB, MLI, *this );
3039
3028
}
3040
3029
3030
+ bool emitBBLabels = BBSections || MF->hasBBLabels ();
3041
3031
if (MBB.pred_empty () ||
3042
- (!MF-> hasBBLabels () && isBlockOnlyReachableByFallthrough (&MBB) &&
3032
+ (!emitBBLabels && isBlockOnlyReachableByFallthrough (&MBB) &&
3043
3033
!MBB.isEHFuncletEntry () && !MBB.hasLabelMustBeEmitted ())) {
3044
3034
if (isVerbose ()) {
3045
3035
// NOTE: Want this comment at start of line, don't emit with AddComment.
@@ -3050,12 +3040,23 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
3050
3040
if (isVerbose () && MBB.hasLabelMustBeEmitted ()) {
3051
3041
OutStreamer->AddComment (" Label of block must be emitted" );
3052
3042
}
3053
- // Switch to a new section if this basic block must begin a section.
3054
- if (MBB.isBeginSection ()) {
3043
+ // With -fbasicblock-sections, a basic block can start a new section.
3044
+ if (MBB.getSectionType () == MachineBasicBlockSection::MBBS_Exception) {
3045
+ // Create the exception section for this function.
3046
+ OutStreamer->SwitchSection (
3047
+ getObjFileLowering ().getNamedSectionForMachineBasicBlock (
3048
+ MF->getFunction (), MBB, TM, " .eh" ));
3049
+ } else if (MBB.getSectionType () == MachineBasicBlockSection::MBBS_Cold) {
3050
+ // Create the cold section here.
3051
+ OutStreamer->SwitchSection (
3052
+ getObjFileLowering ().getNamedSectionForMachineBasicBlock (
3053
+ MF->getFunction (), MBB, TM, " .unlikely" ));
3054
+ } else if (MBB.isBeginSection () && MBB.isEndSection ()) {
3055
3055
OutStreamer->SwitchSection (
3056
3056
getObjFileLowering ().getSectionForMachineBasicBlock (MF->getFunction (),
3057
3057
MBB, TM));
3058
- CurrentSectionBeginSym = MBB.getSymbol ();
3058
+ } else if (BBSections) {
3059
+ OutStreamer->SwitchSection (MF->getSection ());
3059
3060
}
3060
3061
OutStreamer->emitLabel (MBB.getSymbol ());
3061
3062
}
@@ -3089,7 +3090,7 @@ void AsmPrinter::emitVisibility(MCSymbol *Sym, unsigned Visibility,
3089
3090
// / the predecessor and this block is a fall-through.
3090
3091
bool AsmPrinter::
3091
3092
isBlockOnlyReachableByFallthrough (const MachineBasicBlock *MBB) const {
3092
- // With BasicBlock Sections, beginning of the section is not a fallthrough .
3093
+ // With BasicBlock Sections, no block is a fall through .
3093
3094
if (MBB->isBeginSection ())
3094
3095
return false ;
3095
3096
0 commit comments