1; RUN: opt -instcombine %s -S -o - | FileCheck %s
2; Verify that the eliminated instructions (bitcast, gep, load) are salvaged into
3; a DIExpression.
4;
5; Originally created from the following C source and then heavily isolated/reduced.
6;
7; struct entry {
8;   struct entry *next;
9; };
10; void scan(struct entry *queue, struct entry *end)
11; {
12;   struct entry *entry;
13;   for (entry = (struct entry *)((char *)(queue->next) - 8);
14;        &entry->next == end;
15;        entry = (struct entry *)((char *)(entry->next) - 8)) {
16;   }
17; }
18
19; ModuleID = '<stdin>'
20source_filename = "test.c"
21target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
22target triple = "x86_64-apple-macosx10.12.0"
23
24%struct.entry = type { %struct.entry* }
25
26define void @salvage_load(%struct.entry** %queue) local_unnamed_addr #0 !dbg !14 {
27entry:
28  %im_not_dead = alloca %struct.entry*
29  %0 = load %struct.entry*, %struct.entry** %queue, align 8, !dbg !19
30  %1 = load %struct.entry*, %struct.entry** %queue, align 8, !dbg !19
31  call void @llvm.dbg.value(metadata %struct.entry* %1, metadata !18, metadata !20), !dbg !19
32; CHECK: define void @salvage_load
33; CHECK-NEXT: entry:
34; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry** %queue,
35; CHECK-SAME:                           metadata !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 0))
36  store %struct.entry* %1, %struct.entry** %im_not_dead, align 8
37  ret void, !dbg !21
38}
39
40define void @salvage_bitcast(%struct.entry* %queue) local_unnamed_addr #0 !dbg !22 {
41entry:
42  %im_not_dead = alloca i8*
43  %0 = bitcast %struct.entry* %queue to i8*, !dbg !23
44  %1 = bitcast %struct.entry* %queue to i8*, !dbg !23
45  call void @llvm.dbg.value(metadata i8* %1, metadata !24, metadata !20), !dbg !23
46; CHECK: define void @salvage_bitcast
47; CHECK-NEXT: entry:
48; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* %queue,
49; CHECK-SAME:                           metadata !DIExpression(DW_OP_plus_uconst, 0))
50  store i8* %1, i8** %im_not_dead, align 8
51  ret void, !dbg !23
52}
53
54define void @salvage_gep0(%struct.entry* %queue, %struct.entry* %end) local_unnamed_addr #0 !dbg !25 {
55entry:
56  %im_not_dead = alloca %struct.entry**
57  %0 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !26
58  %1 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !26
59  call void @llvm.dbg.value(metadata %struct.entry** %1, metadata !27, metadata !20), !dbg !26
60; CHECK: define void @salvage_gep0
61; CHECK-NEXT: entry:
62; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* %queue,
63; CHECK-SAME:                           metadata !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_plus_uconst, 0, DW_OP_stack_value))
64  store %struct.entry** %1, %struct.entry*** %im_not_dead, align 8
65  ret void, !dbg !26
66}
67
68define void @salvage_gep1(%struct.entry* %queue, %struct.entry* %end) local_unnamed_addr #0 !dbg !28 {
69entry:
70  %im_not_dead = alloca %struct.entry**
71  %0 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !29
72  %1 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !29
73  call void @llvm.dbg.value(metadata %struct.entry** %1, metadata !30, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 32)), !dbg !29
74; CHECK: define void @salvage_gep1
75; CHECK-NEXT: entry:
76; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* %queue,
77; CHECK-SAME:     metadata !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32))
78  store %struct.entry** %1, %struct.entry*** %im_not_dead, align 8
79  ret void, !dbg !29
80}
81
82define void @salvage_gep2(%struct.entry* %queue, %struct.entry* %end) local_unnamed_addr #0 !dbg !31 {
83entry:
84  %im_not_dead = alloca %struct.entry**
85  %0 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !32
86  %1 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !32
87  call void @llvm.dbg.value(metadata %struct.entry** %1, metadata !33, metadata !DIExpression(DW_OP_stack_value)), !dbg !32
88; CHECK: define void @salvage_gep2
89; CHECK-NEXT: entry:
90; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* %queue,
91; CHECK-SAME:     metadata !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_stack_value))
92  store %struct.entry** %1, %struct.entry*** %im_not_dead, align 8
93  ret void, !dbg !32
94}
95
96; Function Attrs: nounwind readnone
97declare void @llvm.dbg.value(metadata, metadata, metadata) #1
98
99attributes #0 = { nounwind ssp uwtable }
100attributes #1 = { nounwind readnone }
101
102!llvm.dbg.cu = !{!0}
103!llvm.module.flags = !{!10, !11, !12}
104!llvm.ident = !{!13}
105
106!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 5.0.0 (trunk 297628) (llvm/trunk 297643)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3)
107!1 = !DIFile(filename: "test.c", directory: "/")
108!2 = !{}
109!3 = !{!4, !8}
110!4 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64)
111!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "entry", file: !1, line: 1, size: 64, elements: !6)
112!6 = !{!7}
113!7 = !DIDerivedType(tag: DW_TAG_member, name: "next", scope: !5, file: !1, line: 2, baseType: !4, size: 64)
114!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64)
115!9 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
116!10 = !{i32 2, !"Dwarf Version", i32 4}
117!11 = !{i32 2, !"Debug Info Version", i32 3}
118!12 = !{i32 1, !"PIC Level", i32 2}
119!13 = !{!"clang version 5.0.0 (trunk 297628) (llvm/trunk 297643)"}
120!14 = distinct !DISubprogram(name: "scan", scope: !1, file: !1, line: 4, type: !15, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !17)
121!15 = !DISubroutineType(types: !16)
122!16 = !{null, !4, !4}
123!17 = !{!18}
124!18 = !DILocalVariable(name: "entry", scope: !14, file: !1, line: 6, type: !4)
125!19 = !DILocation(line: 6, column: 17, scope: !14)
126!20 = !DIExpression(DW_OP_plus_uconst, 0)
127!21 = !DILocation(line: 11, column: 1, scope: !14)
128!22 = distinct !DISubprogram(name: "scan", scope: !1, file: !1, line: 4, type: !15, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !17)
129!23 = !DILocation(line: 6, column: 17, scope: !22)
130!24 = !DILocalVariable(name: "entry", scope: !22, file: !1, line: 6, type: !4)
131!25 = distinct !DISubprogram(name: "scan", scope: !1, file: !1, line: 4, type: !15, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !17)
132!26 = !DILocation(line: 6, column: 17, scope: !25)
133!27 = !DILocalVariable(name: "entry", scope: !25, file: !1, line: 6, type: !4)
134!28 = distinct !DISubprogram(name: "scan", scope: !1, file: !1, line: 4, type: !15, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !17)
135!29 = !DILocation(line: 6, column: 17, scope: !28)
136!30 = !DILocalVariable(name: "entry", scope: !28, file: !1, line: 6, type: !4)
137!31 = distinct !DISubprogram(name: "scan", scope: !1, file: !1, line: 4, type: !15, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !17)
138!32 = !DILocation(line: 6, column: 17, scope: !31)
139!33 = !DILocalVariable(name: "entry", scope: !31, file: !1, line: 6, type: !4)
140