1; RUN: opt -passes=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
26; This salvage can't currently occur safely (PR40628), however if/when that's
27; ever fixed, then this is definitely a piece of test coverage that should
28; be maintained.
29define void @salvage_load(%struct.entry** %queue) local_unnamed_addr #0 !dbg !14 {
30entry:
31  %im_not_dead = alloca %struct.entry*
32  %0 = load %struct.entry*, %struct.entry** %queue, align 8, !dbg !19
33  %1 = load %struct.entry*, %struct.entry** %queue, align 8, !dbg !19
34  call void @llvm.dbg.value(metadata %struct.entry* %1, metadata !18, metadata !20), !dbg !19
35; CHECK: define void @salvage_load
36; CHECK-NEXT: entry:
37; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* undef
38  store %struct.entry* %1, %struct.entry** %im_not_dead, align 8
39  ret void, !dbg !21
40}
41
42define void @salvage_bitcast(%struct.entry* %queue) local_unnamed_addr #0 !dbg !22 {
43entry:
44  %im_not_dead = alloca i8*
45  %0 = bitcast %struct.entry* %queue to i8*, !dbg !23
46  %1 = bitcast %struct.entry* %queue to i8*, !dbg !23
47  call void @llvm.dbg.value(metadata i8* %1, metadata !24, metadata !20), !dbg !23
48; CHECK: define void @salvage_bitcast
49; CHECK-NEXT: entry:
50; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* %queue,
51; CHECK-SAME:                           metadata !DIExpression(DW_OP_plus_uconst, 0))
52  store i8* %1, i8** %im_not_dead, align 8
53  ret void, !dbg !23
54}
55
56define void @salvage_gep0(%struct.entry* %queue, %struct.entry* %end) local_unnamed_addr #0 !dbg !25 {
57entry:
58  %im_not_dead = alloca %struct.entry**
59  %0 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !26
60  %1 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !26
61  call void @llvm.dbg.value(metadata %struct.entry** %1, metadata !27, metadata !20), !dbg !26
62; CHECK: define void @salvage_gep0
63; CHECK-NEXT: entry:
64; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* %queue,
65; CHECK-SAME:                           metadata !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_plus_uconst, 0, DW_OP_stack_value))
66  store %struct.entry** %1, %struct.entry*** %im_not_dead, align 8
67  ret void, !dbg !26
68}
69
70define void @salvage_gep1(%struct.entry* %queue, %struct.entry* %end) local_unnamed_addr #0 !dbg !28 {
71entry:
72  %im_not_dead = alloca %struct.entry**
73  %0 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !29
74  %1 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !29
75  call void @llvm.dbg.value(metadata %struct.entry** %1, metadata !30, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 32)), !dbg !29
76; CHECK: define void @salvage_gep1
77; CHECK-NEXT: entry:
78; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* %queue,
79; CHECK-SAME:     metadata !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32))
80  store %struct.entry** %1, %struct.entry*** %im_not_dead, align 8
81  ret void, !dbg !29
82}
83
84define void @salvage_gep2(%struct.entry* %queue, %struct.entry* %end) local_unnamed_addr #0 !dbg !31 {
85entry:
86  %im_not_dead = alloca %struct.entry**
87  %0 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !32
88  %1 = getelementptr inbounds %struct.entry, %struct.entry* %queue, i32 -1, i32 0, !dbg !32
89  call void @llvm.dbg.value(metadata %struct.entry** %1, metadata !33, metadata !DIExpression(DW_OP_stack_value)), !dbg !32
90; CHECK: define void @salvage_gep2
91; CHECK-NEXT: entry:
92; CHECK-NEXT: call void @llvm.dbg.value(metadata %struct.entry* %queue,
93; CHECK-SAME:     metadata !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_stack_value))
94  store %struct.entry** %1, %struct.entry*** %im_not_dead, align 8
95  ret void, !dbg !32
96}
97
98; Function Attrs: nounwind readnone
99declare void @llvm.dbg.value(metadata, metadata, metadata) #1
100
101attributes #0 = { nounwind ssp uwtable }
102attributes #1 = { nounwind readnone }
103
104!llvm.dbg.cu = !{!0}
105!llvm.module.flags = !{!10, !11, !12}
106!llvm.ident = !{!13}
107
108!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)
109!1 = !DIFile(filename: "test.c", directory: "/")
110!2 = !{}
111!3 = !{!4, !8}
112!4 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64)
113!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "entry", file: !1, line: 1, size: 64, elements: !6)
114!6 = !{!7}
115!7 = !DIDerivedType(tag: DW_TAG_member, name: "next", scope: !5, file: !1, line: 2, baseType: !4, size: 64)
116!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64)
117!9 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
118!10 = !{i32 2, !"Dwarf Version", i32 4}
119!11 = !{i32 2, !"Debug Info Version", i32 3}
120!12 = !{i32 1, !"PIC Level", i32 2}
121!13 = !{!"clang version 5.0.0 (trunk 297628) (llvm/trunk 297643)"}
122!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)
123!15 = !DISubroutineType(types: !16)
124!16 = !{null, !4, !4}
125!17 = !{!18}
126!18 = !DILocalVariable(name: "entry", scope: !14, file: !1, line: 6, type: !4)
127!19 = !DILocation(line: 6, column: 17, scope: !14)
128!20 = !DIExpression(DW_OP_plus_uconst, 0)
129!21 = !DILocation(line: 11, column: 1, scope: !14)
130!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)
131!23 = !DILocation(line: 6, column: 17, scope: !22)
132!24 = !DILocalVariable(name: "entry", scope: !22, file: !1, line: 6, type: !4)
133!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)
134!26 = !DILocation(line: 6, column: 17, scope: !25)
135!27 = !DILocalVariable(name: "entry", scope: !25, file: !1, line: 6, type: !4)
136!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)
137!29 = !DILocation(line: 6, column: 17, scope: !28)
138!30 = !DILocalVariable(name: "entry", scope: !28, file: !1, line: 6, type: !4)
139!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)
140!32 = !DILocation(line: 6, column: 17, scope: !31)
141!33 = !DILocalVariable(name: "entry", scope: !31, file: !1, line: 6, type: !4)
142