1; RUN: opt < %s -passes=gvn-sink -S | FileCheck %s 2 3; Test that GVNSink correctly performs the sink optimization in the presence of debug information 4; Test that GVNSink correctly merges the debug locations of sinked instruction, eg, propagating 5; the merged debug location of `%add` and `%add1` to the sinked add instruction. 6 7; Function Attrs: noinline nounwind uwtable 8define dso_local i32 @fun(i32 noundef %a, i32 noundef %b) #0 !dbg !10 { 9; CHECK-LABEL: define dso_local i32 @fun( 10; CHECK-SAME: i32 noundef [[A:%.*]], i32 noundef [[B:%.*]]) 11; CHECK: if.end: 12; CHECK: [[B_SINK:%.*]] = phi i32 [ [[B]], %if.else ], [ [[A]], %if.then ] 13; CHECK: [[ADD1:%.*]] = add nsw i32 [[B_SINK]], 1, !dbg [[DBG:![0-9]+]] 14; CHECK: [[XOR2:%.*]] = xor i32 [[ADD1]], 1, !dbg [[DBG:![0-9]+]] 15; CHECK: [[DBG]] = !DILocation(line: 0, 16; 17entry: 18 tail call void @llvm.dbg.value(metadata i32 %a, metadata !15, metadata !DIExpression()), !dbg !16 19 tail call void @llvm.dbg.value(metadata i32 %b, metadata !17, metadata !DIExpression()), !dbg !16 20 %cmp = icmp sgt i32 %b, 10, !dbg !18 21 br i1 %cmp, label %if.then, label %if.else, !dbg !20 22 23if.then: ; preds = %entry 24 %add = add nsw i32 %a, 1, !dbg !21 25 tail call void @llvm.dbg.value(metadata i32 %add, metadata !23, metadata !DIExpression()), !dbg !24 26 %xor = xor i32 %add, 1, !dbg !25 27 tail call void @llvm.dbg.value(metadata i32 %xor, metadata !26, metadata !DIExpression()), !dbg !24 28 tail call void @llvm.dbg.value(metadata i32 %xor, metadata !27, metadata !DIExpression()), !dbg !16 29 br label %if.end, !dbg !28 30 31if.else: ; preds = %entry 32 %add1 = add nsw i32 %b, 1, !dbg !29 33 tail call void @llvm.dbg.value(metadata i32 %add1, metadata !31, metadata !DIExpression()), !dbg !32 34 %xor2 = xor i32 %add1, 1, !dbg !33 35 tail call void @llvm.dbg.value(metadata i32 %xor2, metadata !34, metadata !DIExpression()), !dbg !32 36 tail call void @llvm.dbg.value(metadata i32 %xor2, metadata !27, metadata !DIExpression()), !dbg !16 37 br label %if.end 38 39if.end: ; preds = %if.else, %if.then 40 %ret.0 = phi i32 [ %xor, %if.then ], [ %xor2, %if.else ], !dbg !35 41 tail call void @llvm.dbg.value(metadata i32 %ret.0, metadata !27, metadata !DIExpression()), !dbg !16 42 ret i32 %ret.0, !dbg !36 43} 44 45; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none) 46declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 47 48; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none) 49declare void @llvm.dbg.value(metadata, metadata, metadata) #1 50 51attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } 52attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } 53 54!llvm.dbg.cu = !{!0} 55!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8} 56 57!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 18.0.0git (https://github.com/llvm/llvm-project.git 5dfcb3e5d1d16bb4f8fce52b3c089119ed977e7f)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) 58!1 = !DIFile(filename: "main.c", directory: "/") 59!2 = !{i32 7, !"Dwarf Version", i32 5} 60!3 = !{i32 2, !"Debug Info Version", i32 3} 61!4 = !{i32 1, !"wchar_size", i32 4} 62!5 = !{i32 8, !"PIC Level", i32 2} 63!6 = !{i32 7, !"PIE Level", i32 2} 64!7 = !{i32 7, !"uwtable", i32 2} 65!8 = !{i32 7, !"frame-pointer", i32 2} 66!10 = distinct !DISubprogram(name: "fun", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14) 67!11 = !DISubroutineType(types: !12) 68!12 = !{!13, !13, !13} 69!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) 70!14 = !{} 71!15 = !DILocalVariable(name: "a", arg: 1, scope: !10, file: !1, line: 1, type: !13) 72!16 = !DILocation(line: 0, scope: !10) 73!17 = !DILocalVariable(name: "b", arg: 2, scope: !10, file: !1, line: 1, type: !13) 74!18 = !DILocation(line: 3, column: 11, scope: !19) 75!19 = distinct !DILexicalBlock(scope: !10, file: !1, line: 3, column: 9) 76!20 = !DILocation(line: 3, column: 9, scope: !10) 77!21 = !DILocation(line: 4, column: 20, scope: !22) 78!22 = distinct !DILexicalBlock(scope: !19, file: !1, line: 3, column: 17) 79!23 = !DILocalVariable(name: "a1", scope: !22, file: !1, line: 4, type: !13) 80!24 = !DILocation(line: 0, scope: !22) 81!25 = !DILocation(line: 5, column: 21, scope: !22) 82!26 = !DILocalVariable(name: "a2", scope: !22, file: !1, line: 5, type: !13) 83!27 = !DILocalVariable(name: "ret", scope: !10, file: !1, line: 2, type: !13) 84!28 = !DILocation(line: 7, column: 5, scope: !22) 85!29 = !DILocation(line: 8, column: 20, scope: !30) 86!30 = distinct !DILexicalBlock(scope: !19, file: !1, line: 7, column: 12) 87!31 = !DILocalVariable(name: "b1", scope: !30, file: !1, line: 8, type: !13) 88!32 = !DILocation(line: 0, scope: !30) 89!33 = !DILocation(line: 9, column: 21, scope: !30) 90!34 = !DILocalVariable(name: "b2", scope: !30, file: !1, line: 9, type: !13) 91!35 = !DILocation(line: 0, scope: !19) 92!36 = !DILocation(line: 12, column: 5, scope: !10) 93