From 6859685a87ad093d60c8bed60b116143c0a684c7 Mon Sep 17 00:00:00 2001 From: Alexis Engelke Date: Thu, 20 Jun 2024 13:18:41 +0200 Subject: [CodeGen] Use temp symbol for MBBs (#95031) Internal label names never occur in the symbol table, so when using an object streamer, there's no point in constructing these names and then adding them to hash tables -- they are never visible in the output. It's not possible to reuse createTempSymbol, because on BPF has a different prefix for globals and basic blocks right now. --- llvm/include/llvm/MC/MCContext.h | 6 ++++++ llvm/lib/CodeGen/MachineBasicBlock.cpp | 16 ++++++++-------- llvm/lib/MC/MCContext.cpp | 11 +++++++++++ llvm/test/CodeGen/AArch64/branch-relax-cross-section.mir | 4 ++-- llvm/test/CodeGen/BPF/objdump_cond_op.ll | 9 +++------ llvm/test/CodeGen/BPF/objdump_cond_op_2.ll | 6 ++---- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h index e32a532..6c977a5 100644 --- a/llvm/include/llvm/MC/MCContext.h +++ b/llvm/include/llvm/MC/MCContext.h @@ -455,6 +455,12 @@ public: MCSymbol *createNamedTempSymbol(); MCSymbol *createNamedTempSymbol(const Twine &Name); + /// Get or create a symbol for a basic block. For non-always-emit symbols, + /// this behaves like createTempSymbol, except that it uses the + /// PrivateLabelPrefix instead of the PrivateGlobalPrefix. When AlwaysEmit is + /// true, behaves like getOrCreateSymbol, prefixed with PrivateLabelPrefix. + MCSymbol *createBlockSymbol(const Twine &Name, bool AlwaysEmit = false); + /// Create the definition of a directional local symbol for numbered label /// (used for "1:" definitions). MCSymbol *createDirectionalLocalSymbol(unsigned LocalLabelVal); diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp index 16505f2..abf43e3 100644 --- a/llvm/lib/CodeGen/MachineBasicBlock.cpp +++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp @@ -80,10 +80,11 @@ MCSymbol *MachineBasicBlock::getSymbol() const { } CachedMCSymbol = Ctx.getOrCreateSymbol(MF->getName() + Suffix); } else { - const StringRef Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix(); - CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" + - Twine(MF->getFunctionNumber()) + - "_" + Twine(getNumber())); + // If the block occurs as label in inline assembly, parsing the assembly + // needs an actual label name => set AlwaysEmit in these cases. + CachedMCSymbol = Ctx.createBlockSymbol( + "BB" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()), + /*AlwaysEmit=*/hasLabelMustBeEmitted()); } } return CachedMCSymbol; @@ -104,10 +105,9 @@ 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())); + CachedEndMCSymbol = Ctx.createBlockSymbol( + "BB_END" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()), + /*AlwaysEmit=*/false); } return CachedEndMCSymbol; } diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp index 9c7cb74..729e8da 100644 --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -318,6 +318,17 @@ MCSymbol *MCContext::createNamedTempSymbol(const Twine &Name) { /*IsTemporary=*/!SaveTempLabels); } +MCSymbol *MCContext::createBlockSymbol(const Twine &Name, bool AlwaysEmit) { + if (AlwaysEmit) + return getOrCreateSymbol(MAI->getPrivateLabelPrefix() + Name); + + bool IsTemporary = !SaveTempLabels; + if (IsTemporary && !UseNamesOnTempLabels) + return createSymbolImpl(nullptr, IsTemporary); + return createRenamableSymbol(MAI->getPrivateLabelPrefix() << Name, + /*AlwaysAddSuffix=*/false, IsTemporary); +} + MCSymbol *MCContext::createLinkerPrivateTempSymbol() { return createLinkerPrivateSymbol("tmp"); } diff --git a/llvm/test/CodeGen/AArch64/branch-relax-cross-section.mir b/llvm/test/CodeGen/AArch64/branch-relax-cross-section.mir index f8f0b76..db88bf0 100644 --- a/llvm/test/CodeGen/AArch64/branch-relax-cross-section.mir +++ b/llvm/test/CodeGen/AArch64/branch-relax-cross-section.mir @@ -473,8 +473,8 @@ body: | ; INDIRECT-NEXT: successors: %bb.1 ; INDIRECT-NEXT: liveins: $x16 ; INDIRECT-NEXT: {{ $}} - ; INDIRECT-NEXT: $[[SCAVENGED_REGISTER:x[0-9]+]] = ADRP target-flags(aarch64-page) - ; INDIRECT-NEXT: $[[SCAVENGED_REGISTER]] = ADDXri $[[SCAVENGED_REGISTER]], target-flags(aarch64-pageoff, aarch64-nc) , 0 + ; INDIRECT-NEXT: $[[SCAVENGED_REGISTER:x[0-9]+]] = ADRP target-flags(aarch64-page) + ; INDIRECT-NEXT: $[[SCAVENGED_REGISTER]] = ADDXri $[[SCAVENGED_REGISTER]], target-flags(aarch64-pageoff, aarch64-nc) , 0 ; INDIRECT-NEXT: BR $[[SCAVENGED_REGISTER]] bb.0.entry: diff --git a/llvm/test/CodeGen/BPF/objdump_cond_op.ll b/llvm/test/CodeGen/BPF/objdump_cond_op.ll index 4a4fa84..3b2e6c1 100644 --- a/llvm/test/CodeGen/BPF/objdump_cond_op.ll +++ b/llvm/test/CodeGen/BPF/objdump_cond_op.ll @@ -27,7 +27,7 @@ define i32 @test(i32, i32) local_unnamed_addr #0 { br label %13 ; CHECK: r1 <<= 32 ; CHECK: r1 >>= 32 -; CHECK: if r1 != 2 goto +6 +; CHECK: if r1 != 2 goto +6 ;