Skip to content

Commit 9f85001

Browse files
SLTozerIanWood1
authored andcommitted
[DebugInfo][GlobalOpt] Preserve source locs for optimized loads (llvm#134828)
Some optimizations in globalopt simplify uses of a global value to uses of a generated global bool value; in some cases where this happens, the newly-generated instructions would not have the original source location(s) of the instructions they replaced propagated to them; this patch properly preserves those source locations. Found using llvm#107279.
1 parent 982f29f commit 9f85001

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -965,11 +965,12 @@ OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI,
965965
if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
966966
// The global is initialized when the store to it occurs. If the stored
967967
// value is null value, the global bool is set to false, otherwise true.
968-
new StoreInst(ConstantInt::getBool(
969-
GV->getContext(),
970-
!isa<ConstantPointerNull>(SI->getValueOperand())),
971-
InitBool, false, Align(1), SI->getOrdering(),
972-
SI->getSyncScopeID(), SI->getIterator());
968+
auto *NewSI = new StoreInst(
969+
ConstantInt::getBool(GV->getContext(), !isa<ConstantPointerNull>(
970+
SI->getValueOperand())),
971+
InitBool, false, Align(1), SI->getOrdering(), SI->getSyncScopeID(),
972+
SI->getIterator());
973+
NewSI->setDebugLoc(SI->getDebugLoc());
973974
SI->eraseFromParent();
974975
continue;
975976
}
@@ -988,6 +989,11 @@ OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI,
988989
InitBool->getName() + ".val", false, Align(1),
989990
LI->getOrdering(), LI->getSyncScopeID(),
990991
LI->getIterator());
992+
// FIXME: Should we use the DebugLoc of the load used by the predicate, or
993+
// the predicate? The load seems most appropriate, but there's an argument
994+
// that the new load does not represent the old load, but is simply a
995+
// component of recomputing the predicate.
996+
cast<LoadInst>(LV)->setDebugLoc(LI->getDebugLoc());
991997
InitBoolUsed = true;
992998
switch (ICI->getPredicate()) {
993999
default: llvm_unreachable("Unknown ICmp Predicate!");
@@ -1000,6 +1006,7 @@ OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI,
10001006
case ICmpInst::ICMP_ULE:
10011007
case ICmpInst::ICMP_EQ:
10021008
LV = BinaryOperator::CreateNot(LV, "notinit", ICI->getIterator());
1009+
cast<BinaryOperator>(LV)->setDebugLoc(ICI->getDebugLoc());
10031010
break;
10041011
case ICmpInst::ICMP_NE:
10051012
case ICmpInst::ICMP_UGT:
@@ -1276,6 +1283,7 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
12761283
new LoadInst(NewGV->getValueType(), NewGV, LI->getName() + ".b",
12771284
false, Align(1), LI->getOrdering(),
12781285
LI->getSyncScopeID(), LI->getIterator());
1286+
cast<LoadInst>(StoreVal)->setDebugLoc(LI->getDebugLoc());
12791287
} else {
12801288
assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
12811289
"This is not a form that we understand!");
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -p=globalopt -S | FileCheck %s
3+
4+
;; Test that when we are able to simplify uses of global variables with loads of
5+
;; newly generated bool values, we transfer debuglocs over correctly.
6+
7+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
8+
target triple = "x86_64-unknown-linux-gnu"
9+
10+
@global = internal global i32 0
11+
@global.1 = internal unnamed_addr global ptr null, align 8
12+
13+
define void @ham() !dbg !7 {
14+
; CHECK-LABEL: define void @ham(
15+
; CHECK-SAME: ) local_unnamed_addr !dbg [[DBG4:![0-9]+]] {
16+
; CHECK-NEXT: [[BB:.*:]]
17+
; CHECK-NEXT: [[LOAD_B:%.*]] = load i1, ptr @global, align 1, !dbg [[DBG6:![0-9]+]]
18+
; CHECK-NEXT: [[LOAD_B1:%.*]] = load i1, ptr @global, align 1, !dbg [[DBG6]]
19+
; CHECK-NEXT: [[LOAD:%.*]] = zext i1 [[LOAD_B1]] to i32, !dbg [[DBG6]]
20+
; CHECK-NEXT: store i1 [[LOAD_B]], ptr @global, align 1
21+
; CHECK-NEXT: ret void
22+
;
23+
bb:
24+
%load = load i32, ptr @global, align 4, !dbg !4
25+
store i32 %load, ptr @global, align 4
26+
ret void
27+
}
28+
29+
define void @hoge() {
30+
; CHECK-LABEL: define void @hoge() local_unnamed_addr {
31+
; CHECK-NEXT: [[BB:.*:]]
32+
; CHECK-NEXT: store i1 true, ptr @global, align 1
33+
; CHECK-NEXT: ret void
34+
;
35+
bb:
36+
store i32 1, ptr @global, align 4
37+
ret void
38+
}
39+
40+
define void @bar() !dbg !13 {
41+
; CHECK-LABEL: define void @bar(
42+
; CHECK-SAME: ) local_unnamed_addr !dbg [[DBG8:![0-9]+]] {
43+
; CHECK-NEXT: [[ENTRY:.*:]]
44+
; CHECK-NEXT: store i1 true, ptr @global.1.init, align 1, !dbg [[DBG9:![0-9]+]]
45+
; CHECK-NEXT: ret void
46+
;
47+
entry:
48+
%call = tail call noalias nonnull dereferenceable(48) ptr @_Znwm(i64 48)
49+
store ptr %call, ptr @global.1, align 8, !dbg !14
50+
ret void
51+
}
52+
53+
define void @pluto() !dbg !10 {
54+
; CHECK-LABEL: define void @pluto(
55+
; CHECK-SAME: ) local_unnamed_addr !dbg [[DBG10:![0-9]+]] {
56+
; CHECK-NEXT: [[ENTRY:.*:]]
57+
; CHECK-NEXT: [[GLOBAL_1_INIT_VAL:%.*]] = load i1, ptr @global.1.init, align 1, !dbg [[DBG11:![0-9]+]]
58+
; CHECK-NEXT: [[NOTINIT:%.*]] = xor i1 [[GLOBAL_1_INIT_VAL]], true, !dbg [[DBG12:![0-9]+]]
59+
; CHECK-NEXT: unreachable
60+
;
61+
entry:
62+
%0 = load ptr, ptr @global.1, align 8, !dbg !11
63+
%.not = icmp eq ptr %0, null, !dbg !12
64+
unreachable
65+
}
66+
67+
declare ptr @_Znwm(i64)
68+
69+
!llvm.dbg.cu = !{!0}
70+
!llvm.module.flags = !{!3}
71+
72+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2, splitDebugInlining: false, nameTableKind: None)
73+
!1 = !DIFile(filename: "preserve-load-dbgloc.c", directory: "/tmp")
74+
!2 = !{}
75+
!3 = !{i32 2, !"Debug Info Version", i32 3}
76+
!4 = !DILocation(line: 10, column: 1, scope: !5)
77+
!5 = distinct !DILexicalBlock(scope: !7, file: !6, line: 1524, column: 3)
78+
!6 = !DIFile(filename: "preserve-load-dbgloc.c", directory: "/tmp")
79+
!7 = distinct !DISubprogram(name: "ham", scope: !6, file: !6, line: 10, type: !8, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
80+
!8 = distinct !DISubroutineType(types: !2)
81+
!10 = distinct !DISubprogram(name: "pluto", scope: !6, file: !6, line: 20, type: !8, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
82+
!11 = !DILocation(line: 20, column: 2, scope: !10)
83+
!12 = !DILocation(line: 21, column: 3, scope: !10)
84+
!13 = distinct !DISubprogram(name: "bar", scope: !6, file: !6, line: 230, type: !8, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
85+
!14 = !DILocation(line: 11, column: 4, scope: !13)
86+
;.
87+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: [[META2:![0-9]+]], retainedTypes: [[META2]], globals: [[META2]], splitDebugInlining: false, nameTableKind: None)
88+
; CHECK: [[META1]] = !DIFile(filename: "preserve-load-dbgloc.c", directory: {{.*}})
89+
; CHECK: [[META2]] = !{}
90+
; CHECK: [[DBG4]] = distinct !DISubprogram(name: "ham", scope: [[META1]], file: [[META1]], line: 10, type: [[META5:![0-9]+]], flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META2]])
91+
; CHECK: [[META5]] = distinct !DISubroutineType(types: [[META2]])
92+
; CHECK: [[DBG6]] = !DILocation(line: 10, column: 1, scope: [[META7:![0-9]+]])
93+
; CHECK: [[META7]] = distinct !DILexicalBlock(scope: [[DBG4]], file: [[META1]], line: 1524, column: 3)
94+
; CHECK: [[DBG8]] = distinct !DISubprogram(name: "bar", scope: [[META1]], file: [[META1]], line: 230, type: [[META5]], flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META2]])
95+
; CHECK: [[DBG9]] = !DILocation(line: 11, column: 4, scope: [[DBG8]])
96+
; CHECK: [[DBG10]] = distinct !DISubprogram(name: "pluto", scope: [[META1]], file: [[META1]], line: 20, type: [[META5]], flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META2]])
97+
; CHECK: [[DBG11]] = !DILocation(line: 20, column: 2, scope: [[DBG10]])
98+
; CHECK: [[DBG12]] = !DILocation(line: 21, column: 3, scope: [[DBG10]])
99+
;.

0 commit comments

Comments
 (0)