Skip to content

Commit 1bcb760

Browse files
ApochensAlexisPerry
authored andcommitted
[DebugInfo][DivRemPairs] Fix missing debug location updates (llvm#96045)
Fix llvm#96014 .
1 parent 1582654 commit 1bcb760

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

llvm/lib/Transforms/Scalar/DivRemPairs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
215215
RemInst = RealRem;
216216
// And replace the original instruction with the new one.
217217
OrigRemInst->replaceAllUsesWith(RealRem);
218+
RealRem->setDebugLoc(OrigRemInst->getDebugLoc());
218219
OrigRemInst->eraseFromParent();
219220
NumRecomposed++;
220221
// Note that we have left ((X / Y) * Y) around.
@@ -366,7 +367,9 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
366367
if (!DivDominates)
367368
DivInst->moveBefore(RemInst);
368369
Mul->insertAfter(RemInst);
370+
Mul->setDebugLoc(RemInst->getDebugLoc());
369371
Sub->insertAfter(Mul);
372+
Sub->setDebugLoc(RemInst->getDebugLoc());
370373

371374
// If DivInst has the exact flag, remove it. Otherwise this optimization
372375
// may replace a well-defined value 'X % Y' with poison.
@@ -384,6 +387,7 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
384387
if (!isGuaranteedNotToBeUndef(X, nullptr, DivInst, &DT)) {
385388
auto *FrX =
386389
new FreezeInst(X, X->getName() + ".frozen", DivInst->getIterator());
390+
FrX->setDebugLoc(DivInst->getDebugLoc());
387391
DivInst->setOperand(0, FrX);
388392
Sub->setOperand(0, FrX);
389393
}
@@ -392,6 +396,7 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
392396
if (!isGuaranteedNotToBeUndef(Y, nullptr, DivInst, &DT)) {
393397
auto *FrY =
394398
new FreezeInst(Y, Y->getName() + ".frozen", DivInst->getIterator());
399+
FrY->setDebugLoc(DivInst->getDebugLoc());
395400
DivInst->setOperand(1, FrY);
396401
Mul->setOperand(1, FrY);
397402
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
; RUN: opt -S -passes=div-rem-pairs -mtriple=x86_64-unknown-unknown < %s | FileCheck %s
2+
3+
; Check that DivRemPairs's optimizeDivRem() correctly propagates debug locations
4+
; of `%div` and `%rem` to new freeze, mul and sub instructions.
5+
6+
define i128 @dont_hoist_urem(i128 %a, i128 %b) !dbg !5 {
7+
; CHECK-LABEL: define i128 @dont_hoist_urem(
8+
; CHECK-SAME: i128 [[A:%.*]], i128 [[B:%.*]])
9+
; CHECK: entry:
10+
; CHECK-NEXT: [[A_FROZEN:%.*]] = freeze i128 [[A]], !dbg [[DBG8:![0-9]+]]
11+
; CHECK-NEXT: [[B_FROZEN:%.*]] = freeze i128 [[B]], !dbg [[DBG8]]
12+
; CHECK: if:
13+
; CHECK-NEXT: [[TMP0:%.*]] = mul i128 [[DIV:%.*]], [[B_FROZEN]], !dbg [[DBG11:![0-9]+]]
14+
; CHECK-NEXT: [[REM_DECOMPOSED:%.*]] = sub i128 [[A_FROZEN]], [[TMP0:%.*]], !dbg [[DBG11]]
15+
; CHECK: end:
16+
entry:
17+
%div = udiv i128 %a, %b, !dbg !8
18+
%cmp = icmp eq i128 %div, 42, !dbg !9
19+
br i1 %cmp, label %if, label %end, !dbg !10
20+
21+
if: ; preds = %entry
22+
%rem = urem i128 %a, %b, !dbg !11
23+
br label %end, !dbg !12
24+
25+
end: ; preds = %if, %entry
26+
%ret = phi i128 [ %rem, %if ], [ 3, %entry ], !dbg !13
27+
ret i128 %ret, !dbg !14
28+
}
29+
30+
!llvm.dbg.cu = !{!0}
31+
!llvm.debugify = !{!2, !3}
32+
!llvm.module.flags = !{!4}
33+
34+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
35+
!1 = !DIFile(filename: "frx_fry_.preserve.ll", directory: "/")
36+
!2 = !{i32 7}
37+
!3 = !{i32 0}
38+
!4 = !{i32 2, !"Debug Info Version", i32 3}
39+
!5 = distinct !DISubprogram(name: "dont_hoist_urem", linkageName: "dont_hoist_urem", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
40+
!6 = !DISubroutineType(types: !7)
41+
!7 = !{}
42+
!8 = !DILocation(line: 1, column: 1, scope: !5)
43+
!9 = !DILocation(line: 2, column: 1, scope: !5)
44+
!10 = !DILocation(line: 3, column: 1, scope: !5)
45+
!11 = !DILocation(line: 4, column: 1, scope: !5)
46+
!12 = !DILocation(line: 5, column: 1, scope: !5)
47+
!13 = !DILocation(line: 6, column: 1, scope: !5)
48+
!14 = !DILocation(line: 7, column: 1, scope: !5)
49+
; CHECK: [[DBG8]] = !DILocation(line: 1,
50+
; CHECK: [[DBG11]] = !DILocation(line: 4,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; RUN: opt -S -passes=div-rem-pairs -mtriple=x86_64-unknown-unknown < %s | FileCheck %s
2+
3+
; Check that DivRemPairs's optimizeDivRem() propagates the debug location of
4+
; replaced `%rem` to the new srem or urem instruction.
5+
; @decompose_illegal_srem_same_block tests it when the operands of `%rem` is signed.
6+
; @decompose_illegal_urem_same_block tests it when the operands of `%rem` is unsigned.
7+
8+
define void @decompose_illegal_srem_same_block(i32 %a, i32 %b) !dbg !5 {
9+
; CHECK-LABEL: define void @decompose_illegal_srem_same_block(
10+
; CHECK: %rem.recomposed = srem i32 [[A:%.*]], [[B:%.*]], !dbg [[DBG10:![0-9]+]]
11+
%div = sdiv i32 %a, %b, !dbg !8
12+
%t0 = mul i32 %div, %b, !dbg !9
13+
%rem = sub i32 %a, %t0, !dbg !10
14+
call void @foo(i32 %rem, i32 %div), !dbg !11
15+
ret void, !dbg !12
16+
}
17+
18+
define void @decompose_illegal_urem_same_block(i32 %a, i32 %b) !dbg !13 {
19+
; CHECK-LABEL: define void @decompose_illegal_urem_same_block(
20+
; CHECK: %rem.recomposed = urem i32 [[A:%.*]], [[B:%.*]], !dbg [[DBG16:![0-9]+]]
21+
%div = udiv i32 %a, %b, !dbg !14
22+
%t0 = mul i32 %div, %b, !dbg !15
23+
%rem = sub i32 %a, %t0, !dbg !16
24+
call void @foo(i32 %rem, i32 %div), !dbg !17
25+
ret void, !dbg !18
26+
}
27+
28+
declare void @foo(i32, i32)
29+
30+
!llvm.dbg.cu = !{!0}
31+
!llvm.debugify = !{!2, !3}
32+
!llvm.module.flags = !{!4}
33+
34+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
35+
!1 = !DIFile(filename: "realrem_preverse.ll", directory: "/")
36+
!2 = !{i32 10}
37+
!3 = !{i32 0}
38+
!4 = !{i32 2, !"Debug Info Version", i32 3}
39+
!5 = distinct !DISubprogram(name: "decompose_illegal_srem_same_block", linkageName: "decompose_illegal_srem_same_block", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
40+
!6 = !DISubroutineType(types: !7)
41+
!7 = !{}
42+
!8 = !DILocation(line: 1, column: 1, scope: !5)
43+
!9 = !DILocation(line: 2, column: 1, scope: !5)
44+
!10 = !DILocation(line: 3, column: 1, scope: !5)
45+
!11 = !DILocation(line: 4, column: 1, scope: !5)
46+
!12 = !DILocation(line: 5, column: 1, scope: !5)
47+
!13 = distinct !DISubprogram(name: "decompose_illegal_urem_same_block", linkageName: "decompose_illegal_urem_same_block", scope: null, file: !1, line: 6, type: !6, scopeLine: 6, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
48+
!14 = !DILocation(line: 6, column: 1, scope: !13)
49+
!15 = !DILocation(line: 7, column: 1, scope: !13)
50+
!16 = !DILocation(line: 8, column: 1, scope: !13)
51+
!17 = !DILocation(line: 9, column: 1, scope: !13)
52+
!18 = !DILocation(line: 10, column: 1, scope: !13)
53+
54+
; CHECK: [[DBG10]] = !DILocation(line: 3,
55+
; CHECK: [[DBG16]] = !DILocation(line: 8,
56+

0 commit comments

Comments
 (0)