Index: llvm/include/llvm/CodeGen/MachineBasicBlock.h =================================================================== --- llvm/include/llvm/CodeGen/MachineBasicBlock.h +++ llvm/include/llvm/CodeGen/MachineBasicBlock.h @@ -174,8 +174,9 @@ /// is only computed once and is cached. mutable MCSymbol *CachedMCSymbol = nullptr; - /// Used during basic block sections to mark the end of a basic block. - MCSymbol *EndMCSymbol = nullptr; + /// Marks the end of the basic block. Used during basic block sections to + /// calculate the size of the basic block, or the BB section ending with it. + mutable MCSymbol *CachedEndMCSymbol = nullptr; // Intrusive list support MachineBasicBlock() = default; @@ -474,6 +475,11 @@ /// Sets the section ID for this basic block. void setSectionID(MBBSectionID V) { SectionID = V; } + MCSymbol *getEndSymbol() const; + + /// Returns the BB info metadata to be emitted in the bb_info section. + unsigned getBBInfoMetadata() const; + /// Returns true if this block may have an INLINEASM_BR (overestimate, by /// checking if any of the successors are indirect targets of any inlineasm_br /// in the function). Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1173,24 +1173,20 @@ } } - // We need a temporary symbol for the end of this basic block, if either we - // have BBLabels enabled and we want to emit size directive for the BBs, or - // if this basic blocks marks the end of a section (except the section + // We must emit temporary symbol for the end of this basic block, if either + // we have BBLabels enabled and we want to emit size directive for the BBs, + // or if this basic blocks marks the end of a section (except the section // containing the entry basic block as the end symbol for that section is // CurrentFnEnd). - MCSymbol *CurrentBBEnd = nullptr; if ((MAI->hasDotTypeDotSizeDirective() && MF->hasBBLabels()) || - (MBB.isEndSection() && !MBB.sameSection(&MF->front()))) { - CurrentBBEnd = OutContext.createTempSymbol(); - OutStreamer->emitLabel(CurrentBBEnd); - } + (MBB.isEndSection() && !MBB.sameSection(&MF->front()))) + OutStreamer->emitLabel(MBB.getEndSymbol()); // Helper for emitting the size directive associated with a basic block // symbol. auto emitELFSizeDirective = [&](MCSymbol *SymForSize) { - assert(CurrentBBEnd && "Basicblock end symbol not set!"); const MCExpr *SizeExp = MCBinaryExpr::createSub( - MCSymbolRefExpr::create(CurrentBBEnd, OutContext), + MCSymbolRefExpr::create(MBB.getEndSymbol(), OutContext), MCSymbolRefExpr::create(SymForSize, OutContext), OutContext); OutStreamer->emitELFSize(SymForSize, SizeExp); }; @@ -1207,7 +1203,7 @@ if (MAI->hasDotTypeDotSizeDirective()) emitELFSizeDirective(CurrentSectionBeginSym); MBBSectionRanges[MBB.getSectionIDNum()] = - MBBSectionRange{CurrentSectionBeginSym, CurrentBBEnd}; + MBBSectionRange{CurrentSectionBeginSym, MBB.getEndSymbol()}; } } emitBasicBlockEnd(MBB); Index: llvm/lib/CodeGen/MachineBasicBlock.cpp =================================================================== --- llvm/lib/CodeGen/MachineBasicBlock.cpp +++ llvm/lib/CodeGen/MachineBasicBlock.cpp @@ -100,6 +100,17 @@ return CachedMCSymbol; } +MCSymbol *MachineBasicBlock::getEndSymbol() const { + if (!CachedEndMCSymbol) { + const MachineFunction *MF = getParent(); + MCContext &Ctx = MF->getContext(); + auto Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix(); + CachedEndMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB_END" + + Twine(MF->getFunctionNumber()) + + "_" + Twine(getNumber())); + } + return CachedEndMCSymbol; +} raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { MBB.print(OS); Index: llvm/test/CodeGen/X86/basicblock-sections-clusters.ll =================================================================== --- llvm/test/CodeGen/X86/basicblock-sections-clusters.ll +++ llvm/test/CodeGen/X86/basicblock-sections-clusters.ll @@ -42,14 +42,18 @@ ; LINUX-SECTIONS1-NOT: .section ; LINUX-SECTIONS1-LABEL: foo: ; LINUX-SECTIONS1-NOT: .section +; LINUX-SECTIONS1-NOT: .LBB_END0_{{0-9}}+ ; LINUX-SECTIONS1-LABEL: # %bb.2: +; LINUX-SECTIONS1-NOT: .LBB_END0_{{0-9}}+ ; LINUX-SECTIONS1: .section .text.foo,"ax",@progbits,unique,1 ; LINUX-SECTIONS1-LABEL: foo.1: -; LINUX-SECTIONS1-LABEL: .Ltmp0: -; LINUX-SECTIONS1-NEXT: .size foo.1, .Ltmp0-foo.1 +; LINUX-SECTIONS1-LABEL: .LBB_END0_1: +; LINUX-SECTIONS1-NEXT: .size foo.1, .LBB_END0_1-foo.1 ; LINUX-SECTIONS1-NOT: .section ; LINUX-SECTIONS1: .section .text.unlikely.foo,"ax",@progbits ; LINUX-SECTIONS1-LABEL: foo.cold: +; LINUX-SECTIONS1-LABEL: .LBB_END0_3: +; LINUX-SECTIONS1-NEXT: .size foo.cold, .LBB_END0_3-foo.cold ; LINUX-SECTIONS1: .section .text.foo,"ax",@progbits ; LINUX-SECTIONS1-LABEL: .Lfunc_end0: ; LINUX-SECTIONS1-NEXT: .size foo, .Lfunc_end0-foo @@ -57,14 +61,18 @@ ; LINUX-SECTIONS2: .section .text.foo,"ax",@progbits ; LINUX-SECTIONS2-NOT: .section ; LINUX-SECTIONS2-LABEL: foo: +; LINUX-SECTIONS2-NOT: .LBB_END0_{{0-9}}+ ; LINUX-SECTIONS2-NOT: .section ; LINUX-SECTIONS2-LABEL: # %bb.2: +; LINUX-SECTIONS2-NOT: .LBB_END0_{{0-9}}+ ; LINUX-SECTIONS2: .section .text.foo,"ax",@progbits,unique,1 ; LINUX-SECTIONS2-NEXT: foo.0: +; LINUX-SECTIONS2-NOT: .LBB_END0_{{0-9}}+ ; LINUX-SECTIONS2-NOT: .section ; LINUX-SECTIONS2-LABEL: .LBB0_3: -; LINUX-SECTIONS2-LABEL: .Ltmp0: -; LINUX-SECTIONS2-NEXT: .size foo.0, .Ltmp0-foo.0 +; LINUX-SECTIONS2-LABEL: .LBB_END0_3: +; LINUX-SECTIONS2-NEXT: .size foo.0, .LBB_END0_3-foo.0 ; LINUX-SECTIONS2: .section .text.foo,"ax",@progbits +; LINUX-SECTIONS2-NOT: .LBB_END0_{{0-9}}+ ; LINUX-SECTIONS2-LABEL: .Lfunc_end0: ; LINUX-SECTIONS2-NEXT: .size foo, .Lfunc_end0-foo Index: llvm/test/CodeGen/X86/basicblock-sections-listbb.ll =================================================================== --- llvm/test/CodeGen/X86/basicblock-sections-listbb.ll +++ llvm/test/CodeGen/X86/basicblock-sections-listbb.ll @@ -36,8 +36,11 @@ ; LINUX-SECTIONS-NOT: .section .text._Z3bazb._Z3bazb.{{[0-9]+}},"ax",@progbits ; LINUX-SECTIONS-LABEL: # %bb.1: ; LINUX-SECTIONS-NEXT: callq _Z3barv -; LINUX-SECTIONS: .section .text._Z3bazb._Z3bazb.{{[0-9]+}},"ax",@progbits -; LINUX-SECTIONS-LABEL: _Z3bazb.{{[0-9]+}}: +; LINUX-SECTIONS: .section .text._Z3bazb.[[SECTION_LABEL:_Z3bazb.[0-9]+]],"ax",@progbits +; LINUX-SECTIONS-NEXT: [[SECTION_LABEL]]: ; LINUX-SECTIONS-NEXT: callq _Z3foov -; LINUX-SECTIONS: .Ltmp0: -; LINUX-SECTIONS-NEXT: .size _Z3bazb.{{[0-9]+}}, .Ltmp0-_Z3bazb.{{[0-9]+}} +; LINUX-SECTIONS: .LBB_END0_2: +; LINUX-SECTIONS-NEXT: .size [[SECTION_LABEL]], .LBB_END0_2-[[SECTION_LABEL]] +; LINUX-SECTIONS: .section .text._Z3bazb,"ax",@progbits +; LINUX-SECTIONS: .Lfunc_end0: +; LINUX-SECTIONS-NEXT: .size _Z3bazb, .Lfunc_end0-_Z3bazb Index: llvm/test/CodeGen/X86/basicblock-sections.ll =================================================================== --- llvm/test/CodeGen/X86/basicblock-sections.ll +++ llvm/test/CodeGen/X86/basicblock-sections.ll @@ -30,9 +30,9 @@ ; LINUX-SECTIONS: _Z3bazb: ; LINUX-SECTIONS: .section .text._Z3bazb._Z3bazb.1,"ax",@progbits ; LINUX-SECTIONS: _Z3bazb.1: -; LINUX-SECTIONS: .Ltmp0: -; LINUX-SECTIONS-NEXT: .size _Z3bazb.1, .Ltmp0-_Z3bazb.1 +; LINUX-SECTIONS: .LBB_END0_1: +; LINUX-SECTIONS-NEXT: .size _Z3bazb.1, .LBB_END0_1-_Z3bazb.1 ; LINUX-SECTIONS: .section .text._Z3bazb._Z3bazb.2,"ax",@progbits ; LINUX-SECTIONS: _Z3bazb.2: -; LINUX-SECTIONS: .Ltmp1: -; LINUX-SECTIONS-NEXT: .size _Z3bazb.2, .Ltmp1-_Z3bazb.2 +; LINUX-SECTIONS: .LBB_END0_2: +; LINUX-SECTIONS-NEXT: .size _Z3bazb.2, .LBB_END0_2-_Z3bazb.2 Index: llvm/test/DebugInfo/X86/basicblock-sections_1.ll =================================================================== --- llvm/test/DebugInfo/X86/basicblock-sections_1.ll +++ llvm/test/DebugInfo/X86/basicblock-sections_1.ll @@ -25,24 +25,24 @@ ; BB-SECTIONS-ASM: .Ltmp{{[0-9]+}}: ; BB-SECTIONS-ASM-NEXT: .loc 1 2 7 is_stmt ; BB-SECTIONS-ASM: _Z3fooi.1: -; BB-SECTIONS-ASM: .Ltmp{{[0-9]+}}: -; BB-SECTIONS-ASM: .size _Z3fooi.1, .Ltmp{{[0-9]+}}-_Z3fooi.1 +; BB-SECTIONS-ASM: .LBB_END0_{{[0-9]+}}: +; BB-SECTIONS-ASM: .size _Z3fooi.1, .LBB_END0_{{[0-9]+}}-_Z3fooi.1 ; BB-SECTIONS-ASM: _Z3fooi.2: ; BB-SECTIONS-ASM: .Ltmp{{[0-9]+}}: -; BB-SECTIONS-ASM-NEXT: .Ltmp{{[0-9]+}}: -; BB-SECTIONS-ASM: .size _Z3fooi.2, .Ltmp{{[0-9]+}}-_Z3fooi.2 +; BB-SECTIONS-ASM-NEXT: .LBB_END0_{{[0-9]+}}: +; BB-SECTIONS-ASM: .size _Z3fooi.2, .LBB_END0_{{[0-9]+}}-_Z3fooi.2 ; BB-SECTIONS-ASM: _Z3fooi.3: ; BB-SECTIONS-ASM: .Ltmp{{[0-9]+}}: -; BB-SECTIONS-ASM-NEXT: .Ltmp{{[0-9]+}}: -; BB-SECTIONS-ASM: .size _Z3fooi.3, .Ltmp{{[0-9]+}}-_Z3fooi.3 +; BB-SECTIONS-ASM-NEXT: .LBB_END0_{{[0-9]+}}: +; BB-SECTIONS-ASM: .size _Z3fooi.3, .LBB_END0_{{[0-9]+}}-_Z3fooi.3 ; BB-SECTIONS-ASM: .Lfunc_end0: ; BB-SECTIONS-ASM: .Ldebug_ranges0: ; BB-SECTIONS-ASM-NEXT: .quad _Z3fooi.1 -; BB-SECTIONS-ASM-NEXT: .quad .Ltmp{{[0-9]+}} +; BB-SECTIONS-ASM-NEXT: .quad .LBB_END0_{{[0-9]+}} ; BB-SECTIONS-ASM-NEXT: .quad _Z3fooi.2 -; BB-SECTIONS-ASM-NEXT: .quad .Ltmp{{[0-9]+}} +; BB-SECTIONS-ASM-NEXT: .quad .LBB_END0_{{[0-9]+}} ; BB-SECTIONS-ASM-NEXT: .quad _Z3fooi.3 -; BB-SECTIONS-ASM-NEXT: .quad .Ltmp{{[0-9]+}} +; BB-SECTIONS-ASM-NEXT: .quad .LBB_END0_{{[0-9]+}} ; BB-SECTIONS-ASM-NEXT: .quad .Lfunc_begin0 ; BB-SECTIONS-ASM-NEXT: .quad .Lfunc_end0 ; BB-SECTIONS-ASM-NEXT: .quad 0