@@ -1097,14 +1097,6 @@ void AsmPrinter::emitFunctionBody() {
10971097 // Print out code for the function.
10981098 bool HasAnyRealCode = false ;
10991099 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- }
11081100
11091101 for (auto &MBB : *MF) {
11101102 // Print a label for the basic block.
@@ -1185,17 +1177,41 @@ void AsmPrinter::emitFunctionBody() {
11851177 }
11861178 }
11871179 }
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 ();
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!" );
11931197 const MCExpr *SizeExp = MCBinaryExpr::createSub (
11941198 MCSymbolRefExpr::create (CurrentBBEnd, OutContext),
1195- MCSymbolRefExpr::create (MBB.getSymbol (), OutContext), OutContext);
1196- OutStreamer->emitLabel (CurrentBBEnd);
1197- MBB.setEndMCSymbol (CurrentBBEnd);
1198- OutStreamer->emitELFSize (MBB.getSymbol (), SizeExp);
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+ }
11991215 }
12001216 emitBasicBlockEnd (MBB);
12011217 }
@@ -1230,9 +1246,8 @@ void AsmPrinter::emitFunctionBody() {
12301246 }
12311247 }
12321248
1233- // Switch to the original section if basic block sections was used.
1234- if (emitBBSections)
1235- OutStreamer->SwitchSection (MF->getSection ());
1249+ // Switch to the original section in case basic block sections was used.
1250+ OutStreamer->SwitchSection (MF->getSection ());
12361251
12371252 const Function &F = MF->getFunction ();
12381253 for (const auto &BB : F) {
@@ -1249,7 +1264,7 @@ void AsmPrinter::emitFunctionBody() {
12491264 emitFunctionBodyEnd ();
12501265
12511266 if (needFuncLabelsForEHOrDebugInfo (*MF, MMI) ||
1252- MAI->hasDotTypeDotSizeDirective () || emitBBSections ) {
1267+ MAI->hasDotTypeDotSizeDirective ()) {
12531268 // Create a symbol for the end of function.
12541269 CurrentFnEnd = createTempSymbol (" func_end" );
12551270 OutStreamer->emitLabel (CurrentFnEnd);
@@ -1272,8 +1287,6 @@ void AsmPrinter::emitFunctionBody() {
12721287 HI.Handler ->markFunctionEnd ();
12731288 }
12741289
1275- if (emitBBSections)
1276- EndOfRegularSectionMBB->setEndMCSymbol (CurrentFnEnd);
12771290
12781291 // Print out jump tables referenced by the function.
12791292 emitJumpTableInfo ();
@@ -1753,6 +1766,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
17531766
17541767 CurrentFnSymForSize = CurrentFnSym;
17551768 CurrentFnBegin = nullptr ;
1769+ CurrentSectionBeginSym = nullptr ;
17561770 CurExceptionSym = nullptr ;
17571771 bool NeedsLocalForSize = MAI->needsLocalForSize ();
17581772 if (F.hasFnAttribute (" patchable-function-entry" ) ||
@@ -2981,7 +2995,6 @@ static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
29812995// / MachineBasicBlock, an alignment (if present) and a comment describing
29822996// / it if appropriate.
29832997void AsmPrinter::emitBasicBlockStart (const MachineBasicBlock &MBB) {
2984- bool BBSections = MF->hasBBSections ();
29852998 // End the previous funclet and start a new one.
29862999 if (MBB.isEHFuncletEntry ()) {
29873000 for (const HandlerInfo &HI : Handlers) {
@@ -2991,11 +3004,9 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
29913004 }
29923005
29933006 // Emit an alignment directive for this block, if needed.
2994- if (MBB.pred_empty () || !BBSections) {
2995- const Align Alignment = MBB.getAlignment ();
2996- if (Alignment != Align (1 ))
2997- emitAlignment (Alignment);
2998- }
3007+ const Align Alignment = MBB.getAlignment ();
3008+ if (Alignment != Align (1 ))
3009+ emitAlignment (Alignment);
29993010
30003011 // If the block has its address taken, emit any labels that were used to
30013012 // reference the block. It is possible that there is more than one label
@@ -3027,9 +3038,8 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
30273038 emitBasicBlockLoopComments (MBB, MLI, *this );
30283039 }
30293040
3030- bool emitBBLabels = BBSections || MF->hasBBLabels ();
30313041 if (MBB.pred_empty () ||
3032- (!emitBBLabels && isBlockOnlyReachableByFallthrough (&MBB) &&
3042+ (!MF-> hasBBLabels () && isBlockOnlyReachableByFallthrough (&MBB) &&
30333043 !MBB.isEHFuncletEntry () && !MBB.hasLabelMustBeEmitted ())) {
30343044 if (isVerbose ()) {
30353045 // NOTE: Want this comment at start of line, don't emit with AddComment.
@@ -3040,23 +3050,12 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
30403050 if (isVerbose () && MBB.hasLabelMustBeEmitted ()) {
30413051 OutStreamer->AddComment (" Label of block must be emitted" );
30423052 }
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 ()) {
3053+ // Switch to a new section if this basic block must begin a section.
3054+ if (MBB.isBeginSection ()) {
30553055 OutStreamer->SwitchSection (
30563056 getObjFileLowering ().getSectionForMachineBasicBlock (MF->getFunction (),
30573057 MBB, TM));
3058- } else if (BBSections) {
3059- OutStreamer->SwitchSection (MF->getSection ());
3058+ CurrentSectionBeginSym = MBB.getSymbol ();
30603059 }
30613060 OutStreamer->emitLabel (MBB.getSymbol ());
30623061 }
@@ -3090,7 +3089,7 @@ void AsmPrinter::emitVisibility(MCSymbol *Sym, unsigned Visibility,
30903089// / the predecessor and this block is a fall-through.
30913090bool AsmPrinter::
30923091isBlockOnlyReachableByFallthrough (const MachineBasicBlock *MBB) const {
3093- // With BasicBlock Sections, no block is a fall through .
3092+ // With BasicBlock Sections, beginning of the section is not a fallthrough .
30943093 if (MBB->isBeginSection ())
30953094 return false ;
30963095
0 commit comments