1; RUN: opt -S -mtriple=x86_64 -codegenprepare %s | FileCheck %s
2;
3; Generated from the following source with:
4; clang -O2 -g -S -emit-llvm -mllvm -stop-after=indirectbr-expand test.cpp
5;
6; extern void use(int);
7; extern int foo(long long);
8;
9; void test(int p)
10; {
11;   int i = p + 4;
12;   (void)foo(i);  // sign extension of i
13;   if (p)
14;     return;
15;   use(i);        // use of original i
16; }
17;
18; PR39845: Check that CodeGenPrepare does not drop the reference to a local when it is
19;          sign-extended and used later.
20;
21; CHECK: define{{.*}}test
22; CHECK: call{{.*}}dbg.value(metadata i32 %p
23; CHECK: call{{.*}}dbg.value(metadata i32 %add
24;
25define dso_local void @_Z4testi(i32 %p) local_unnamed_addr !dbg !7 {
26entry:
27  call void @llvm.dbg.value(metadata i32 %p, metadata !12, metadata !DIExpression()), !dbg !14
28  %add = add nsw i32 %p, 4, !dbg !15
29  call void @llvm.dbg.value(metadata i32 %add, metadata !13, metadata !DIExpression()), !dbg !16
30  %conv = sext i32 %add to i64, !dbg !17
31  %call = tail call i32 @_Z3foox(i64 %conv), !dbg !18
32  %tobool = icmp eq i32 %p, 0, !dbg !19
33  br i1 %tobool, label %if.end, label %cleanup, !dbg !21
34
35if.end:
36  tail call void @_Z3usei(i32 %add), !dbg !22
37  br label %cleanup, !dbg !23
38
39cleanup:
40  ret void, !dbg !23
41}
42
43declare dso_local i32 @_Z3foox(i64) local_unnamed_addr
44declare dso_local void @_Z3usei(i32) local_unnamed_addr
45
46declare void @llvm.dbg.value(metadata, metadata, metadata)
47
48!llvm.dbg.cu = !{!0}
49!llvm.module.flags = !{!3, !4, !5}
50!llvm.ident = !{!6}
51
52!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 8.0.0 (trunk 348209)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
53!1 = !DIFile(filename: "test.cpp", directory: "/home/test/src")
54!2 = !{}
55!3 = !{i32 2, !"Dwarf Version", i32 4}
56!4 = !{i32 2, !"Debug Info Version", i32 3}
57!5 = !{i32 1, !"wchar_size", i32 4}
58!6 = !{!"clang version 8.0.0 (trunk 348209)"}
59!7 = distinct !DISubprogram(name: "test", linkageName: "_Z4testi", scope: !1, file: !1, line: 4, type: !8, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
60!8 = !DISubroutineType(types: !9)
61!9 = !{null, !10}
62!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
63!11 = !{!12, !13}
64!12 = !DILocalVariable(name: "p", arg: 1, scope: !7, file: !1, line: 4, type: !10)
65!13 = !DILocalVariable(name: "i", scope: !7, file: !1, line: 6, type: !10)
66!14 = !DILocation(line: 4, column: 15, scope: !7)
67!15 = !DILocation(line: 6, column: 13, scope: !7)
68!16 = !DILocation(line: 6, column: 7, scope: !7)
69!17 = !DILocation(line: 7, column: 13, scope: !7)
70!18 = !DILocation(line: 7, column: 9, scope: !7)
71!19 = !DILocation(line: 8, column: 7, scope: !20)
72!20 = distinct !DILexicalBlock(scope: !7, file: !1, line: 8, column: 7)
73!21 = !DILocation(line: 8, column: 7, scope: !7)
74!22 = !DILocation(line: 10, column: 3, scope: !7)
75!23 = !DILocation(line: 11, column: 1, scope: !7)
76