From 994eb5adc40cd001d82d0f95d18d1827b57e496c Mon Sep 17 00:00:00 2001 From: Daniel Hoekwater Date: Mon, 21 Aug 2023 16:29:02 +0000 Subject: [CodeGen] Fix unconditional branch duplication issue in bbsections If an end section basic block ends in an unconditional branch to its fallthrough, BasicBlockSections will duplicate the unconditional branch. This doesn't break x86, but it is a (slight) size optimization and more importantly prevents AArch64 builds from breaking. Ex: ``` bb1 (bbsections Hot): jmp bb2 bb2 (bbsections Cold): /* do work... */ ``` After running sortBasicBlocksAndUpdateBranches(): ``` bb1 (bbsections Hot): jmp bb2 jmp bb2 bb2 (bbsections Cold): /* do work... */ ``` Differential Revision: https://reviews.llvm.org/D158674 --- llvm/lib/CodeGen/BasicBlockSections.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'llvm/lib/CodeGen/BasicBlockSections.cpp') diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp index 33e70b1..de7c170 100644 --- a/llvm/lib/CodeGen/BasicBlockSections.cpp +++ b/llvm/lib/CodeGen/BasicBlockSections.cpp @@ -258,7 +258,8 @@ void llvm::sortBasicBlocksAndUpdateBranches( [[maybe_unused]] const MachineBasicBlock *EntryBlock = &MF.front(); SmallVector PreLayoutFallThroughs(MF.getNumBlockIDs()); for (auto &MBB : MF) - PreLayoutFallThroughs[MBB.getNumber()] = MBB.getFallThrough(); + PreLayoutFallThroughs[MBB.getNumber()] = + MBB.getFallThrough(/*JumpToFallThrough=*/false); MF.sort(MBBCmp); assert(&MF.front() == EntryBlock && -- cgit v1.1