Skip to content

Commit 79ba323

Browse files
authored
[Debuginfo][GVNHoist] Fix #86227: update the debug location of the hoisted GEP (#86236)
This PR fixes #86227.
1 parent 8a7f021 commit 79ba323

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

llvm/lib/Transforms/Scalar/GVNHoist.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,14 @@ void GVNHoist::makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt,
951951
OtherGep = cast<GetElementPtrInst>(
952952
cast<StoreInst>(OtherInst)->getPointerOperand());
953953
ClonedGep->andIRFlags(OtherGep);
954+
955+
// Merge debug locations of GEPs, because the hoisted GEP replaces those
956+
// in branches. When cloning, ClonedGep preserves the debug location of
957+
// Gepd, so Gep is skipped to avoid merging it twice.
958+
if (OtherGep != Gep) {
959+
ClonedGep->applyMergedLocation(ClonedGep->getDebugLoc(),
960+
OtherGep->getDebugLoc());
961+
}
954962
}
955963

956964
// Replace uses of Gep with ClonedGep in Repl.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
; RUN: opt -S -passes=gvn-hoist < %s | FileCheck %s
2+
3+
define dso_local void @func(i32 noundef %a, ptr noundef %b) !dbg !10 {
4+
; Check the merged debug location of hoisted GEP
5+
; CHECK: entry
6+
; CHECK: %{{[a-zA-Z0-9_]*}} = getelementptr {{.*}} !dbg [[MERGED_DL:![0-9]+]]
7+
; CHECK: [[MERGED_DL]] = !DILocation(line: 0, scope: !{{[0-9]+}})
8+
entry:
9+
tail call void @llvm.dbg.value(metadata i32 %a, metadata !16, metadata !DIExpression()), !dbg !17
10+
tail call void @llvm.dbg.value(metadata ptr %b, metadata !18, metadata !DIExpression()), !dbg !17
11+
%tobool = icmp ne i32 %a, 0, !dbg !19
12+
br i1 %tobool, label %if.then, label %if.else, !dbg !21
13+
14+
if.then: ; preds = %entry
15+
%arrayidx = getelementptr inbounds i32, ptr %b, i64 1, !dbg !22
16+
store i32 1, ptr %arrayidx, align 4, !dbg !24
17+
br label %if.end, !dbg !25
18+
19+
if.else: ; preds = %entry
20+
%arrayidx1 = getelementptr inbounds i32, ptr %b, i64 1, !dbg !26
21+
store i32 1, ptr %arrayidx1, align 4, !dbg !28
22+
br label %if.end
23+
24+
if.end: ; preds = %if.else, %if.then
25+
ret void, !dbg !29
26+
}
27+
28+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
29+
30+
declare void @llvm.dbg.value(metadata, metadata, metadata)
31+
32+
!llvm.dbg.cu = !{!0}
33+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
34+
35+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 19.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
36+
!1 = !DIFile(filename: "main.c", directory: "/root/llvm-test/GVNHoist")
37+
!2 = !{i32 7, !"Dwarf Version", i32 5}
38+
!3 = !{i32 2, !"Debug Info Version", i32 3}
39+
!4 = !{i32 1, !"wchar_size", i32 4}
40+
!5 = !{i32 8, !"PIC Level", i32 2}
41+
!6 = !{i32 7, !"PIE Level", i32 2}
42+
!7 = !{i32 7, !"uwtable", i32 2}
43+
!8 = !{i32 7, !"frame-pointer", i32 2}
44+
!10 = distinct !DISubprogram(name: "func", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !15)
45+
!11 = !DISubroutineType(types: !12)
46+
!12 = !{null, !13, !14}
47+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
48+
!14 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64)
49+
!15 = !{}
50+
!16 = !DILocalVariable(name: "a", arg: 1, scope: !10, file: !1, line: 1, type: !13)
51+
!17 = !DILocation(line: 0, scope: !10)
52+
!18 = !DILocalVariable(name: "b", arg: 2, scope: !10, file: !1, line: 1, type: !14)
53+
!19 = !DILocation(line: 2, column: 9, scope: !20)
54+
!20 = distinct !DILexicalBlock(scope: !10, file: !1, line: 2, column: 9)
55+
!21 = !DILocation(line: 2, column: 9, scope: !10)
56+
!22 = !DILocation(line: 3, column: 9, scope: !23)
57+
!23 = distinct !DILexicalBlock(scope: !20, file: !1, line: 2, column: 12)
58+
!24 = !DILocation(line: 3, column: 14, scope: !23)
59+
!25 = !DILocation(line: 4, column: 5, scope: !23)
60+
!26 = !DILocation(line: 5, column: 9, scope: !27)
61+
!27 = distinct !DILexicalBlock(scope: !20, file: !1, line: 4, column: 12)
62+
!28 = !DILocation(line: 5, column: 14, scope: !27)
63+
!29 = !DILocation(line: 7, column: 1, scope: !10)

0 commit comments

Comments
 (0)