Skip to content

Commit 2941d39

Browse files
SLTozerGroverkss
authored andcommitted
[DebugInfo][Reassociate] Preserve DebugLocs when reassociating subs (llvm#114226)
In NegateValue in Reassociate, we return the negation of an existing value in order to break a subtract into an negate + add, potentially creating a new instruction to perform the negation, but we neglect to propagate the DebugLoc of the sub being replaced to the negate instruction if one is created. This patch adds that propagation. Found using llvm#107279.
1 parent 0ffaf40 commit 2941d39

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

llvm/lib/Transforms/Scalar/Reassociate.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,8 @@ static Value *NegateValue(Value *V, Instruction *BI,
874874
// negation.
875875
Instruction *NewNeg =
876876
CreateNeg(V, V->getName() + ".neg", BI->getIterator(), BI);
877+
// NewNeg is generated to potentially replace BI, so use its DebugLoc.
878+
NewNeg->setDebugLoc(BI->getDebugLoc());
877879
ToRedo.insert(NewNeg);
878880
return NewNeg;
879881
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
;; Tests that we preserve DebugLocs through reassociation of sub instructions.
3+
; RUN: opt < %s -passes=reassociate -S | FileCheck %s
4+
5+
define void @foo(i64 %0) {
6+
; CHECK-LABEL: define void @foo(
7+
; CHECK-SAME: i64 [[TMP0:%.*]]) {
8+
; CHECK-NEXT: [[ENTRY:.*:]]
9+
; CHECK-NEXT: [[DOTNEG:%.*]] = sub i64 0, [[TMP0]], !dbg [[DBG3:![0-9]+]]
10+
; CHECK-NEXT: [[ADD_I_I:%.*]] = add i64 [[DOTNEG]], 1
11+
; CHECK-NEXT: store i64 [[ADD_I_I]], ptr null, align 8
12+
; CHECK-NEXT: ret void
13+
;
14+
entry:
15+
%sub5.i.i = sub i64 1, %0, !dbg !4
16+
%add.i.i = add i64 %sub5.i.i, 0
17+
store i64 %add.i.i, ptr null, align 8
18+
ret void
19+
}
20+
21+
!llvm.dbg.cu = !{!0}
22+
!llvm.module.flags = !{!3}
23+
24+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 20.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
25+
!1 = !DIFile(filename: "test.cpp", directory: "/tmp")
26+
!2 = !{}
27+
!3 = !{i32 2, !"Debug Info Version", i32 3}
28+
!4 = !DILocation(line: 212, column: 25, scope: !5)
29+
!5 = distinct !DISubprogram(name: "foo", scope: !0, file: !1, line: 161, type: !6, scopeLine: 162, unit: !0, retainedNodes: !2)
30+
!6 = distinct !DISubroutineType(types: !2)
31+
;.
32+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
33+
; CHECK: [[META1]] = !DIFile(filename: "test.cpp", directory: {{.*}})
34+
; CHECK: [[DBG3]] = !DILocation(line: 212, column: 25, scope: [[META4:![0-9]+]])
35+
; CHECK: [[META4]] = distinct !DISubprogram(name: "foo", scope: [[META0]], file: [[META1]], line: 161, type: [[META5:![0-9]+]], scopeLine: 162, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META6:![0-9]+]])
36+
; CHECK: [[META5]] = distinct !DISubroutineType(types: [[META6]])
37+
; CHECK: [[META6]] = !{}
38+
;.

0 commit comments

Comments
 (0)