Skip to content

Commit 6f1f6d1

Browse files
authored
[InstCombine][DebugInfo] Update debug value uses in freelyInvertAllUsersOf (#137013)
This patch updates all debug value uses in `freelyInvertAllUsersOf` by inserting `DW_OP_not` at the front of the DIExpression. Related issue: #71065
1 parent f2351d9 commit 6f1f6d1

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,26 @@ void InstCombinerImpl::freelyInvertAllUsersOf(Value *I, Value *IgnoredUser) {
13961396
"canFreelyInvertAllUsersOf() ?");
13971397
}
13981398
}
1399+
1400+
// Update pre-existing debug value uses.
1401+
SmallVector<DbgValueInst *, 4> DbgValues;
1402+
SmallVector<DbgVariableRecord *, 4> DbgVariableRecords;
1403+
llvm::findDbgValues(DbgValues, I, &DbgVariableRecords);
1404+
1405+
auto InvertDbgValueUse = [&](auto *DbgVal) {
1406+
SmallVector<uint64_t, 1> Ops = {dwarf::DW_OP_not};
1407+
for (unsigned Idx = 0, End = DbgVal->getNumVariableLocationOps();
1408+
Idx != End; ++Idx)
1409+
if (DbgVal->getVariableLocationOp(Idx) == I)
1410+
DbgVal->setExpression(
1411+
DIExpression::appendOpsToArg(DbgVal->getExpression(), Ops, Idx));
1412+
};
1413+
1414+
for (DbgValueInst *DVI : DbgValues)
1415+
InvertDbgValueUse(DVI);
1416+
1417+
for (DbgVariableRecord *DVR : DbgVariableRecords)
1418+
InvertDbgValueUse(DVR);
13991419
}
14001420

14011421
/// Given a 'sub' instruction, return the RHS of the instruction if the LHS is a
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes=instcombine -S %s -o - | FileCheck %s
3+
4+
; Make sure that the DIExpression is updated correctly after InstCombinerImpl::freelyInvertAllUsersOf.
5+
6+
define i32 @test(i32 noundef %x, i32 noundef %y) !dbg !10 {
7+
; CHECK-LABEL: define i32 @test(
8+
; CHECK-SAME: i32 noundef [[X:%.*]], i32 noundef [[Y:%.*]]) !dbg [[DBG10:![0-9]+]] {
9+
; CHECK-NEXT: [[ENTRY:.*:]]
10+
; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq i32 [[X]], 0
11+
; CHECK-NEXT: #dbg_value(i1 [[CMP_NOT]], [[META15:![0-9]+]], !DIExpression(DW_OP_not, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_stack_value), [[META16:![0-9]+]])
12+
; CHECK-NEXT: #dbg_value(!DIArgList(i1 false, i1 [[CMP_NOT]]), [[META17:![0-9]+]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_not, DW_OP_or, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value), [[META16]])
13+
; CHECK-NEXT: [[TMP0:%.*]] = and i32 [[Y]], 1
14+
; CHECK-NEXT: [[AND:%.*]] = select i1 [[CMP_NOT]], i32 0, i32 [[TMP0]]
15+
; CHECK-NEXT: ret i32 [[AND]]
16+
;
17+
entry:
18+
%cmp = icmp ne i32 %x, 0
19+
%conv = zext i1 %cmp to i32
20+
#dbg_value(i32 %conv, !15, !DIExpression(), !16)
21+
#dbg_value(!DIArgList(i1 false, i1 %cmp), !17, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_or, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value), !18)
22+
%and = and i32 %conv, %y
23+
ret i32 %and
24+
}
25+
26+
!llvm.dbg.cu = !{!0}
27+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
28+
!llvm.ident = !{!9}
29+
30+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 21.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
31+
!1 = !DIFile(filename: "test.c", directory: "/", checksumkind: CSK_MD5, checksum: "b2d9ffc7905684d8b7c3b52a3136e57c")
32+
!2 = !{i32 7, !"Dwarf Version", i32 5}
33+
!3 = !{i32 2, !"Debug Info Version", i32 3}
34+
!4 = !{i32 1, !"wchar_size", i32 4}
35+
!5 = !{i32 8, !"PIC Level", i32 2}
36+
!6 = !{i32 7, !"PIE Level", i32 2}
37+
!7 = !{i32 7, !"uwtable", i32 2}
38+
!8 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
39+
!9 = !{!"clang version 21.0.0git"}
40+
!10 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !14)
41+
!11 = !DISubroutineType(types: !12)
42+
!12 = !{!13, !13, !13}
43+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
44+
!14 = !{!15}
45+
!15 = !DILocalVariable(name: "z", scope: !10, file: !1, line: 2, type: !13)
46+
!16 = !DILocation(line: 0, scope: !10)
47+
!17 = !DILocalVariable(name: "w", scope: !10, file: !1, line: 3, type: !13)
48+
!18 = !DILocation(line: 0, scope: !10)
49+
;.
50+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
51+
; CHECK: [[META1]] = !DIFile(filename: "{{.*}}test.c", directory: {{.*}})
52+
; CHECK: [[DBG10]] = distinct !DISubprogram(name: "test", scope: [[META1]], file: [[META1]], line: 1, type: [[META11:![0-9]+]], scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META14:![0-9]+]])
53+
; CHECK: [[META11]] = !DISubroutineType(types: [[META12:![0-9]+]])
54+
; CHECK: [[META12]] = !{[[META13:![0-9]+]], [[META13]], [[META13]]}
55+
; CHECK: [[META13]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
56+
; CHECK: [[META14]] = !{[[META15]]}
57+
; CHECK: [[META15]] = !DILocalVariable(name: "z", scope: [[DBG10]], file: [[META1]], line: 2, type: [[META13]])
58+
; CHECK: [[META16]] = !DILocation(line: 0, scope: [[DBG10]])
59+
; CHECK: [[META17]] = !DILocalVariable(name: "w", scope: [[DBG10]], file: [[META1]], line: 3, type: [[META13]])
60+
;.

0 commit comments

Comments
 (0)