1; RUN: opt < %s -S -simplifycfg | FileCheck %s
2
3; Note: This patch fixes the regression introduced by pr38762.
4;
5; When SimplifyCFG changes the PHI node into a select instruction, the debug
6; information becomes ambiguous. It causes the debugger to display unreached
7; lines and invalid variable values.
8;
9; When the function 'hoistAllInstructionsInto' hoist a basic block:
10; - Remove their dbg.values.
11; - Set their debug locations to the values from the insertion point.
12;
13; But, if one of the instructions being hoisted is a debug intrinsic from an
14; inlined function, assigning it the debug location from the insertion point
15; will create a mismatch between the intrinsic's subprogram and the location's
16; subprogram, causing the assertion "Expected inlined-at fields to agree" in
17; SelectionDAG".
18
19; IR generated with:
20; clang -S -g -gno-column-info -O2 -emit-llvm pr39243.cpp -o pr39243.ll -mllvm -opt-bisect-limit=103
21
22; // pr39243.cpp
23; union onion {
24;   double dd;
25;   int ii[2];
26; };
27;
28; int alpha;
29; int bravo();
30;
31; int charlie() {
32;   int delta = 0;
33;   return bravo();
34; }
35;
36; int echo(onion foxtrot) {
37;   alpha = foxtrot.ii[0];
38;   if (alpha) {
39;     int golf = bravo();
40;     return -golf;
41;   }
42;   alpha = foxtrot.ii[1];
43;   return -charlie();
44; }
45
46; Change the debug locations associated with the PHI nodes being promoted, to
47; the debug locations from the insertion point in the dominant block.
48
49; CHECK-LABEL: entry
50; CHECK:  %foxtrot.sroa.0.0.extract.trunc = trunc i64 %foxtrot.coerce to i32
51; CHECK:  %tobool = icmp eq i32 %foxtrot.sroa.0.0.extract.trunc, 0
52; CHECK:  %foxtrot.sroa.2.0.extract.shift = lshr i64 %foxtrot.coerce, 32
53; CHECK-NOT:  call void @llvm.dbg.value(metadata i32 %foxtrot.sroa.2.0.extract.trunc, metadata !30, metadata !DIExpression(DW_OP_LLVM_fragment, 32, 32)), !dbg !34
54; CHECK:  %foxtrot.sroa.2.0.extract.trunc = trunc i64 %foxtrot.sroa.2.0.extract.shift to i32
55; CHECK:  store i32 %foxtrot.sroa.2.0.extract.trunc, i32* @alpha, align 4, !dbg !25
56; CHECK-NOT:  call void @llvm.dbg.value(metadata i32 0, metadata !15, metadata !DIExpression()), !dbg !43
57
58; ModuleID = 'pr39243.cpp'
59source_filename = "pr39243.cpp"
60target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
61target triple = "x86_64-pc-linux-gnu"
62
63@alpha = dso_local local_unnamed_addr global i32 0, align 4, !dbg !0
64
65define dso_local i32 @_Z7charliev() local_unnamed_addr #0 {
66entry:
67  %call = tail call i32 @_Z5bravov()
68  ret i32 %call
69}
70
71declare dso_local i32 @_Z5bravov() local_unnamed_addr #1
72
73define dso_local i32 @_Z4echo5onion(i64 %foxtrot.coerce) local_unnamed_addr #0 !dbg !18 {
74entry:
75  %foxtrot.sroa.0.0.extract.trunc = trunc i64 %foxtrot.coerce to i32
76  store i32 %foxtrot.sroa.0.0.extract.trunc, i32* @alpha, align 4
77  %tobool = icmp eq i32 %foxtrot.sroa.0.0.extract.trunc, 0
78  br i1 %tobool, label %if.end, label %return
79
80if.end:                                           ; preds = %entry
81  %foxtrot.sroa.2.0.extract.shift = lshr i64 %foxtrot.coerce, 32
82  %foxtrot.sroa.2.0.extract.trunc = trunc i64 %foxtrot.sroa.2.0.extract.shift to i32
83  call void @llvm.dbg.value(metadata i32 %foxtrot.sroa.2.0.extract.trunc, metadata !30, metadata !DIExpression(DW_OP_LLVM_fragment, 32, 32)), !dbg !34
84  store i32 %foxtrot.sroa.2.0.extract.trunc, i32* @alpha, align 4, !dbg !42
85  call void @llvm.dbg.value(metadata i32 0, metadata !15, metadata !DIExpression()), !dbg !43
86  br label %return
87
88return:                                           ; preds = %entry, %if.end
89  %call.i = tail call i32 @_Z5bravov()
90  %retval.0 = sub nsw i32 0, %call.i
91  ret i32 %retval.0
92}
93
94declare void @llvm.dbg.value(metadata, metadata, metadata) #2
95
96!llvm.dbg.cu = !{!2}
97!llvm.module.flags = !{!7, !8, !9}
98!llvm.ident = !{!10}
99
100!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
101!1 = distinct !DIGlobalVariable(name: "alpha", scope: !2, file: !3, line: 6, type: !6, isLocal: false, isDefinition: true)
102!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "clang version 8.0.0 (trunk 344502)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, nameTableKind: None)
103!3 = !DIFile(filename: "pr39243.cpp", directory: ".")
104!4 = !{}
105!5 = !{!0}
106!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
107!7 = !{i32 2, !"Dwarf Version", i32 4}
108!8 = !{i32 2, !"Debug Info Version", i32 3}
109!9 = !{i32 1, !"wchar_size", i32 4}
110!10 = !{!"clang version 8.0.0 (trunk 344502)"}
111!11 = distinct !DISubprogram(name: "charlie", linkageName: "_Z7charliev", scope: !3, file: !3, line: 9, type: !12, isLocal: false, isDefinition: true, scopeLine: 9, flags: DIFlagPrototyped, isOptimized: true, unit: !2, retainedNodes: !14)
112!12 = !DISubroutineType(types: !13)
113!13 = !{!6}
114!14 = !{!15}
115!15 = !DILocalVariable(name: "delta", scope: !11, file: !3, line: 10, type: !6)
116!18 = distinct !DISubprogram(name: "echo", linkageName: "_Z4echo5onion", scope: !3, file: !3, line: 14, type: !19, isLocal: false, isDefinition: true, scopeLine: 14, flags: DIFlagPrototyped, isOptimized: true, unit: !2, retainedNodes: !29)
117!19 = !DISubroutineType(types: !20)
118!20 = !{!6, !21}
119!21 = distinct !DICompositeType(tag: DW_TAG_union_type, name: "onion", file: !3, line: 1, size: 64, flags: DIFlagTypePassByValue, elements: !22, identifier: "_ZTS5onion")
120!22 = !{!23, !25}
121!23 = !DIDerivedType(tag: DW_TAG_member, name: "dd", scope: !21, file: !3, line: 2, baseType: !24, size: 64)
122!24 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
123!25 = !DIDerivedType(tag: DW_TAG_member, name: "ii", scope: !21, file: !3, line: 3, baseType: !26, size: 64)
124!26 = !DICompositeType(tag: DW_TAG_array_type, baseType: !6, size: 64, elements: !27)
125!27 = !{!28}
126!28 = !DISubrange(count: 2)
127!29 = !{!30}
128!30 = !DILocalVariable(name: "foxtrot", arg: 1, scope: !18, file: !3, line: 14, type: !21)
129!34 = !DILocation(line: 14, scope: !18)
130!42 = !DILocation(line: 20, scope: !18)
131!43 = !DILocation(line: 10, scope: !11, inlinedAt: !44)
132!44 = distinct !DILocation(line: 21, scope: !18)
133