diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -3046,6 +3046,16 @@ if (Alignment != Align(1)) emitAlignment(Alignment); + // Switch to a new section if this basic block must begin a section. The + // entry block is always placed in the function section and is not handled + // here. + if (MBB.isBeginSection() && !MBB.pred_empty()) { + OutStreamer->SwitchSection( + getObjFileLowering().getSectionForMachineBasicBlock(MF->getFunction(), + MBB, TM)); + CurrentSectionBeginSym = MBB.getSymbol(); + } + // If the block has its address taken, emit any labels that were used to // reference the block. It is possible that there is more than one label // here, because multiple LLVM BB's may have been RAUW'd to this block after @@ -3076,6 +3086,7 @@ emitBasicBlockLoopComments(MBB, MLI, *this); } + // Emit label of the basic block if needed. if (MBB.pred_empty() || (!MF->hasBBLabels() && isBlockOnlyReachableByFallthrough(&MBB) && !MBB.isEHFuncletEntry() && !MBB.hasLabelMustBeEmitted())) { @@ -3085,24 +3096,17 @@ false); } } else { - if (isVerbose() && MBB.hasLabelMustBeEmitted()) { + if (isVerbose() && MBB.hasLabelMustBeEmitted()) OutStreamer->AddComment("Label of block must be emitted"); - } - auto *BBSymbol = MBB.getSymbol(); - // Switch to a new section if this basic block must begin a section. - if (MBB.isBeginSection()) { - OutStreamer->SwitchSection( - getObjFileLowering().getSectionForMachineBasicBlock(MF->getFunction(), - MBB, TM)); - CurrentSectionBeginSym = BBSymbol; - } - OutStreamer->emitLabel(BBSymbol); - // With BB sections, each basic block must handle CFI information on its own - // if it begins a section. - if (MBB.isBeginSection()) - for (const HandlerInfo &HI : Handlers) - HI.Handler->beginBasicBlock(MBB); + OutStreamer->emitLabel(MBB.getSymbol()); } + + // With BB sections, each basic block must handle CFI information on its own + // if it begins a section (The entry block is handled separately by + // AsmPrinterHandler::beginFunction). + if (MBB.isBeginSection() && !MBB.pred_empty()) + for (const HandlerInfo &HI : Handlers) + HI.Handler->beginBasicBlock(MBB); } void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) { diff --git a/llvm/test/CodeGen/X86/machine-function-splitter-blockaddress.ll b/llvm/test/CodeGen/X86/machine-function-splitter-blockaddress.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/machine-function-splitter-blockaddress.ll @@ -0,0 +1,41 @@ +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -split-machine-functions | FileCheck %s + +define void @foo1(i1 zeroext %0) nounwind !prof !0 !section_prefix !1 { +; CHECK: .text +; CHECK-LABEL: foo1: +; CHECK: movl $.Ltmp0, %eax +; CHECK-NEXT: movl $.Ltmp1, %ecx +; CHECK-NEXT: cmovneq %rax, %rcx +; CHECK-NEXT: jmpq *%rcx +; CHECK-NEXT: .Ltmp0: +; CHECK-NEXT: .LBB0_1: +; CHECK-NEXT: callq bar +; CHECK: .section .text.unlikely.foo1 +;; Verify that the cold address-taken block and its labels are moved to .text.unlikely. +; CHECK-NEXT: .Ltmp1: +; CHECK-NEXT: foo1.cold: +; CHECK-NEXT: callq baz +entry: + %1 = select i1 %0, i8* blockaddress(@foo1, %bb1), i8* blockaddress(@foo1, %bb2) ; [#uses=1] + indirectbr i8* %1, [label %bb1, label %bb2], !prof !2 + +bb1: ; preds = %entry + %2 = call i32 @bar() + br label %bb3 + +bb2: ; preds = %entry + %3 = call i32 @baz() + br label %bb3 + +bb3: ; preds = %bb2, %bb1 + %4 = tail call i32 @qux() + ret void +} + +declare i32 @bar() +declare i32 @baz() +declare i32 @qux() + +!0 = !{!"function_entry_count", i64 7000} +!1 = !{!"function_section_prefix", !".hot"} +!2 = !{!"branch_weights", i32 7000, i32 0}