Skip to content

Commit 6859685

Browse files
authored
[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.
1 parent b18bf8f commit 6859685

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

llvm/include/llvm/MC/MCContext.h

+6
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,12 @@ class MCContext {
455455
MCSymbol *createNamedTempSymbol();
456456
MCSymbol *createNamedTempSymbol(const Twine &Name);
457457

458+
/// Get or create a symbol for a basic block. For non-always-emit symbols,
459+
/// this behaves like createTempSymbol, except that it uses the
460+
/// PrivateLabelPrefix instead of the PrivateGlobalPrefix. When AlwaysEmit is
461+
/// true, behaves like getOrCreateSymbol, prefixed with PrivateLabelPrefix.
462+
MCSymbol *createBlockSymbol(const Twine &Name, bool AlwaysEmit = false);
463+
458464
/// Create the definition of a directional local symbol for numbered label
459465
/// (used for "1:" definitions).
460466
MCSymbol *createDirectionalLocalSymbol(unsigned LocalLabelVal);

llvm/lib/CodeGen/MachineBasicBlock.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ MCSymbol *MachineBasicBlock::getSymbol() const {
8080
}
8181
CachedMCSymbol = Ctx.getOrCreateSymbol(MF->getName() + Suffix);
8282
} else {
83-
const StringRef Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix();
84-
CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" +
85-
Twine(MF->getFunctionNumber()) +
86-
"_" + Twine(getNumber()));
83+
// If the block occurs as label in inline assembly, parsing the assembly
84+
// needs an actual label name => set AlwaysEmit in these cases.
85+
CachedMCSymbol = Ctx.createBlockSymbol(
86+
"BB" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()),
87+
/*AlwaysEmit=*/hasLabelMustBeEmitted());
8788
}
8889
}
8990
return CachedMCSymbol;
@@ -104,10 +105,9 @@ MCSymbol *MachineBasicBlock::getEndSymbol() const {
104105
if (!CachedEndMCSymbol) {
105106
const MachineFunction *MF = getParent();
106107
MCContext &Ctx = MF->getContext();
107-
auto Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix();
108-
CachedEndMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB_END" +
109-
Twine(MF->getFunctionNumber()) +
110-
"_" + Twine(getNumber()));
108+
CachedEndMCSymbol = Ctx.createBlockSymbol(
109+
"BB_END" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()),
110+
/*AlwaysEmit=*/false);
111111
}
112112
return CachedEndMCSymbol;
113113
}

llvm/lib/MC/MCContext.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ MCSymbol *MCContext::createNamedTempSymbol(const Twine &Name) {
318318
/*IsTemporary=*/!SaveTempLabels);
319319
}
320320

321+
MCSymbol *MCContext::createBlockSymbol(const Twine &Name, bool AlwaysEmit) {
322+
if (AlwaysEmit)
323+
return getOrCreateSymbol(MAI->getPrivateLabelPrefix() + Name);
324+
325+
bool IsTemporary = !SaveTempLabels;
326+
if (IsTemporary && !UseNamesOnTempLabels)
327+
return createSymbolImpl(nullptr, IsTemporary);
328+
return createRenamableSymbol(MAI->getPrivateLabelPrefix() << Name,
329+
/*AlwaysAddSuffix=*/false, IsTemporary);
330+
}
331+
321332
MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
322333
return createLinkerPrivateSymbol("tmp");
323334
}

llvm/test/CodeGen/AArch64/branch-relax-cross-section.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ body: |
473473
; INDIRECT-NEXT: successors: %bb.1
474474
; INDIRECT-NEXT: liveins: $x16
475475
; INDIRECT-NEXT: {{ $}}
476-
; INDIRECT-NEXT: $[[SCAVENGED_REGISTER:x[0-9]+]] = ADRP target-flags(aarch64-page) <mcsymbol .LBB5_1>
477-
; INDIRECT-NEXT: $[[SCAVENGED_REGISTER]] = ADDXri $[[SCAVENGED_REGISTER]], target-flags(aarch64-pageoff, aarch64-nc) <mcsymbol .LBB5_1>, 0
476+
; INDIRECT-NEXT: $[[SCAVENGED_REGISTER:x[0-9]+]] = ADRP target-flags(aarch64-page) <mcsymbol >
477+
; INDIRECT-NEXT: $[[SCAVENGED_REGISTER]] = ADDXri $[[SCAVENGED_REGISTER]], target-flags(aarch64-pageoff, aarch64-nc) <mcsymbol >, 0
478478
; INDIRECT-NEXT: BR $[[SCAVENGED_REGISTER]]
479479
480480
bb.0.entry:

llvm/test/CodeGen/BPF/objdump_cond_op.ll

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ define i32 @test(i32, i32) local_unnamed_addr #0 {
2727
br label %13
2828
; CHECK: r1 <<= 32
2929
; CHECK: r1 >>= 32
30-
; CHECK: if r1 != 2 goto +6 <LBB0_2>
30+
; CHECK: if r1 != 2 goto +6 <test+0x48>
3131

3232
; <label>:8: ; preds = %2
3333
%9 = icmp eq i32 %0, %1
@@ -38,32 +38,29 @@ define i32 @test(i32, i32) local_unnamed_addr #0 {
3838
; CHECK: r0 = *(u32 *)(r1 + 0)
3939
; CHECK: r0 *= r0
4040
; CHECK: r0 <<= 1
41-
; CHECK: goto +7 <LBB0_4>
41+
; CHECK: goto +7 <test+0x80>
4242

4343
; <label>:11: ; preds = %8
4444
%12 = shl nsw i32 %10, 2
4545
br label %13
4646

47-
; CHECK-LABEL: <LBB0_2>:
4847
; CHECK: r3 = 0 ll
4948
; CHECK: r0 = *(u32 *)(r3 + 0)
5049
; CHECK: r2 <<= 32
5150
; CHECK: r2 >>= 32
52-
; CHECK: if r1 == r2 goto +4 <LBB0_5>
51+
; CHECK: if r1 == r2 goto +4 <test+0x98>
5352
; CHECK: r0 <<= 2
5453

5554
; <label>:13: ; preds = %4, %11
5655
%14 = phi i32 [ %12, %11 ], [ %7, %4 ]
5756
store i32 %14, ptr @gbl, align 4
5857
br label %15
59-
; CHECK-LABEL: <LBB0_4>:
6058
; CHECK: r1 = 0 ll
6159
; CHECK: *(u32 *)(r1 + 0) = r0
6260

6361
; <label>:15: ; preds = %8, %13
6462
%16 = phi i32 [ %14, %13 ], [ %10, %8 ]
6563
ret i32 %16
66-
; CHECK-LABEL: <LBB0_5>:
6764
; CHECK: exit
6865
}
6966
attributes #0 = { norecurse nounwind }

llvm/test/CodeGen/BPF/objdump_cond_op_2.ll

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ define i32 @test(i32, i32) local_unnamed_addr #0 {
1414

1515
; <label>:4: ; preds = %2
1616
br label %5
17-
; CHECK: if r4 s>= r3 goto +10 <LBB0_2>
18-
; CHECK-LABEL: <LBB0_1>:
17+
; CHECK: if r4 s>= r3 goto +10 <test+0x90>
1918

2019
; <label>:5: ; preds = %4, %5
2120
%6 = phi i32 [ %9, %5 ], [ 0, %4 ]
@@ -27,12 +26,11 @@ define i32 @test(i32, i32) local_unnamed_addr #0 {
2726
%12 = icmp slt i32 %10, %11
2827
br i1 %12, label %5, label %13
2928
; CHECK: r1 = r3
30-
; CHECK: if r2 s> r3 goto -10 <LBB0_1>
29+
; CHECK: if r2 s> r3 goto -10 <test+0x40>
3130

3231
; <label>:13: ; preds = %5, %2
3332
%14 = phi i32 [ 0, %2 ], [ %9, %5 ]
3433
ret i32 %14
35-
; CHECK-LABEL: <LBB0_2>:
3634
; CHECK: exit
3735
}
3836
attributes #0 = { norecurse nounwind readnone }

0 commit comments

Comments
 (0)