1; RUN: opt -passes=declare-to-assign -S %s -o - | FileCheck %s
2; RUN: opt --try-experimental-debuginfo-iterators -passes=declare-to-assign -S %s -o - | FileCheck %s
3
4;; The variable doesn't fill the whole alloca which has a range of different
5;; sized stores to it, overlapping (or not) the variable in various ways. Check
6;; the fragment is truncated to represent the intersect between the store and
7;; the variable. If that intersect has exactly the same offset and size as the
8;; variable then a fragment should not be produced (the whole variable is
9;; covered by the store).
10;;
11;; Check directives written inline.
12
13%struct.two = type { i32, i32 }
14
15define dso_local noundef i32 @_Z3funv() #0 !dbg !10 {
16entry:
17  %0 = alloca [4 x i16], align 4
18  call void @llvm.dbg.declare(metadata ptr %0, metadata !15, metadata !DIExpression()), !dbg !16
19; CHECK: %0 = alloca [4 x i16], align 4, !DIAssignID ![[ID1:[0-9]+]]
20; CHECK-NEXT: #dbg_assign(i1 undef, ![[#]], !DIExpression(), ![[ID1]], ptr %0, !DIExpression(),
21  %a = getelementptr inbounds [4 x i16], ptr %0, i32 0, i32 0
22  %a.5 = getelementptr inbounds [4 x i16], ptr %0, i32 0, i32 1
23  %b = getelementptr inbounds [4 x i16], ptr %0, i32 0, i32 2
24  store i64 1, ptr %a, align 4
25; CHECK: store i64 1, ptr %a, align 4, !DIAssignID ![[ID2:[0-9]+]]
26; CHECK-NEXT: #dbg_assign(i64 1, ![[#]], !DIExpression(), ![[ID2]], ptr %a, !DIExpression(),
27  store i64 2, ptr %b, align 4
28;; %b is outside the variable bounds, no debug intrinsic needed.
29  store i16 3, ptr %a.5, align 4
30; CHECK: store i16 3, ptr %a.5, align 4, !DIAssignID ![[ID3:[0-9]+]]
31; CHECK-NEXT: #dbg_assign(i16 3, ![[#]], !DIExpression(DW_OP_LLVM_fragment, 16, 16), ![[ID3]], ptr %a.5, !DIExpression(),
32  store i32 4, ptr %a.5, align 4
33; CHECK: store i32 4, ptr %a.5, align 4, !DIAssignID ![[ID4:[0-9]+]]
34; CHECK-NEXT: #dbg_assign(i32 4, ![[#]], !DIExpression(DW_OP_LLVM_fragment, 16, 16), ![[ID4]], ptr %a.5, !DIExpression(),
35  store i32 5, ptr %a, align 4
36; CHECK: store i32 5, ptr %a, align 4, !DIAssignID ![[ID5:[0-9]+]]
37; CHECK-NEXT: #dbg_assign(i32 5, ![[#]], !DIExpression(), ![[ID5]], ptr %a, !DIExpression(),
38  ret i32 0
39}
40
41declare void @llvm.dbg.declare(metadata, metadata, metadata)
42declare i64 @_Z3getv()
43
44!llvm.dbg.cu = !{!0}
45!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
46!llvm.ident = !{!9}
47
48!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 17.0.0)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
49!1 = !DIFile(filename: "test.cpp", directory: "/")
50!2 = !{i32 7, !"Dwarf Version", i32 5}
51!3 = !{i32 2, !"Debug Info Version", i32 3}
52!4 = !{i32 1, !"wchar_size", i32 4}
53!5 = !{i32 8, !"PIC Level", i32 2}
54!6 = !{i32 7, !"PIE Level", i32 2}
55!7 = !{i32 7, !"uwtable", i32 2}
56!8 = !{i32 7, !"frame-pointer", i32 2}
57!9 = !{!"clang version 17.0.0)"}
58!10 = distinct !DISubprogram(name: "fun", linkageName: "_Z3funv", scope: !1, file: !1, line: 3, type: !11, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14)
59!11 = !DISubroutineType(types: !12)
60!12 = !{!13}
61!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
62!14 = !{}
63!15 = !DILocalVariable(name: "a", scope: !10, file: !1, line: 4, type: !13)
64!16 = !DILocation(line: 4, column: 9, scope: !10)
65!17 = !DILocalVariable(name: "b", scope: !10, file: !1, line: 4, type: !13)
66!18 = !DILocation(line: 4, column: 11, scope: !10)
67!19 = !DILocalVariable(scope: !10, file: !1, line: 4, type: !20)
68!20 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "two", file: !1, line: 1, size: 64, flags: DIFlagTypePassByValue, elements: !21, identifier: "_ZTS3two")
69!21 = !{!22, !23}
70!22 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !20, file: !1, line: 1, baseType: !13, size: 32)
71!23 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !20, file: !1, line: 1, baseType: !13, size: 32, offset: 32)
72!25 = !DILocation(line: 4, column: 16, scope: !10)
73