1; RUN: opt -S -loop-reduce %s | FileCheck %s
2; REQUIRES: x86-registered-target
3
4;; Ensure that SCEV-based salvaging in Loop Strength Reduction can salvage
5;; variadic dbg.value intrinsics. Generated from the following C++:
6
7;; clang -S -emit-llvm -Xclang -disable-llvm-passes -g lsr-variadic.cpp -o
8;; Then running 'opt -O2' up until LSR.
9;; void mul_to_addition(unsigned k, unsigned l, unsigned m, unsigned size, unsigned *data) {
10;;     unsigned i = 0;
11;; #pragma clang loop vectorize(disable)
12;;     while (i < size) {
13;;         unsigned comp = (4 * i) + k;
14;;         unsigned comp2 = comp * l;
15;;         unsigned comp3 = comp2 << m;
16;;         data[i] = comp;
17;;         i++;
18;;     }
19;; }
20;; This produces variadic dbg.value intrinsics with location op DIArglists
21;; of length two and three.
22;; A fourth dbg.value was added artificially by copying a generated dbg.value
23;; and the modifying the position of the optimised-out value in the location
24;; list.
25
26; CHECK: call void @llvm.dbg.value(metadata !DIArgList(i64 %lsr.iv, i32 %k), metadata ![[comp:[0-9]+]], metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_consts, 4, DW_OP_div, DW_OP_consts, 4, DW_OP_mul, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value))
27; CHECK: call void @llvm.dbg.value(metadata !DIArgList(i64 %lsr.iv, i32 %l, i32 %k), metadata ![[comp2:[0-9]+]], metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_consts, 4, DW_OP_div, DW_OP_consts, 4, DW_OP_mul, DW_OP_LLVM_arg, 2, DW_OP_plus, DW_OP_LLVM_arg, 1, DW_OP_mul, DW_OP_stack_value))
28; CHECK: call void @llvm.dbg.value(metadata !DIArgList(i64 %lsr.iv, i32 %m, i32 %l, i32 %k), metadata ![[comp3:[0-9]+]], metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_consts, 4, DW_OP_div, DW_OP_consts, 4, DW_OP_mul, DW_OP_LLVM_arg, 3, DW_OP_plus, DW_OP_LLVM_arg, 2, DW_OP_mul, DW_OP_LLVM_arg, 1, DW_OP_shl, DW_OP_stack_value))
29; CHECK: call void @llvm.dbg.value(metadata !DIArgList(i64 %lsr.iv, i32 %m, i32 %l, i32 %k), metadata ![[comp3:[0-9]+]], metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_consts, 4, DW_OP_div, DW_OP_consts, 4, DW_OP_mul, DW_OP_LLVM_arg, 3, DW_OP_plus, DW_OP_LLVM_arg, 2, DW_OP_mul, DW_OP_LLVM_arg, 1, DW_OP_shl, DW_OP_stack_value))
30; CHECK: ![[comp]] = !DILocalVariable(name: "comp"
31; CHECK: ![[comp2]] = !DILocalVariable(name: "comp2"
32; CHECK: ![[comp3]] = !DILocalVariable(name: "comp3"
33
34
35target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
36target triple = "x86_64-unknown-linux-gnu"
37
38define dso_local void @_Z15mul_to_additionjjjjPj(i32 %k, i32 %l, i32 %m, i32 %size, i32* nocapture %data) local_unnamed_addr #0 !dbg !7 {
39entry:
40  call void @llvm.dbg.value(metadata i32 %k, metadata !14, metadata !DIExpression()), !dbg !24
41  call void @llvm.dbg.value(metadata i32 %l, metadata !15, metadata !DIExpression()), !dbg !24
42  call void @llvm.dbg.value(metadata i32 %m, metadata !16, metadata !DIExpression()), !dbg !24
43  call void @llvm.dbg.value(metadata i32 %size, metadata !17, metadata !DIExpression()), !dbg !24
44  call void @llvm.dbg.value(metadata i32* %data, metadata !18, metadata !DIExpression()), !dbg !24
45  call void @llvm.dbg.value(metadata i32 0, metadata !19, metadata !DIExpression()), !dbg !24
46  %cmp9.not = icmp eq i32 %size, 0, !dbg !25
47  br i1 %cmp9.not, label %while.end, label %while.body.preheader, !dbg !26
48
49while.body.preheader:                             ; preds = %entry
50  %wide.trip.count = zext i32 %size to i64, !dbg !25
51  br label %while.body, !dbg !26
52
53while.body:                                       ; preds = %while.body, %while.body.preheader
54  %indvars.iv = phi i64 [ 0, %while.body.preheader ], [ %indvars.iv.next, %while.body ]
55  call void @llvm.dbg.value(metadata i64 %indvars.iv, metadata !19, metadata !DIExpression()), !dbg !24
56  %0 = trunc i64 %indvars.iv to i32, !dbg !27
57  %mul = shl i32 %0, 2, !dbg !27
58  %add = add i32 %mul, %k, !dbg !28
59  call void @llvm.dbg.value(metadata i32 %add, metadata !20, metadata !DIExpression()), !dbg !29
60  call void @llvm.dbg.value(metadata !DIArgList(i32 %add, i32 %l), metadata !22, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_mul, DW_OP_stack_value)), !dbg !29
61  call void @llvm.dbg.value(metadata !DIArgList(i32 %add, i32 %m, i32 %l), metadata !23, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 2, DW_OP_mul, DW_OP_LLVM_arg, 1, DW_OP_shl, DW_OP_stack_value)), !dbg !29
62  call void @llvm.dbg.value(metadata !DIArgList(i32 %m, i32 %add, i32 %l), metadata !23, metadata !DIExpression(DW_OP_LLVM_arg, 1, DW_OP_LLVM_arg, 2, DW_OP_mul, DW_OP_LLVM_arg, 0, DW_OP_shl, DW_OP_stack_value)), !dbg !29
63  %arrayidx = getelementptr inbounds i32, i32* %data, i64 %indvars.iv, !dbg !30
64  store i32 %add, i32* %arrayidx, align 4, !dbg !31
65  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1, !dbg !32
66  call void @llvm.dbg.value(metadata i64 %indvars.iv.next, metadata !19, metadata !DIExpression()), !dbg !24
67  %exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count, !dbg !25
68  br i1 %exitcond, label %while.body, label %while.end.loopexit, !dbg !26, !llvm.loop !33
69
70while.end.loopexit:                               ; preds = %while.body
71  br label %while.end, !dbg !37
72
73while.end:                                        ; preds = %while.end.loopexit, %entry
74  ret void, !dbg !37
75}
76
77; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
78declare void @llvm.dbg.value(metadata, metadata, metadata)
79
80attributes #0 = { "target-cpu"="x86-64" }
81
82
83!llvm.dbg.cu = !{!0}
84!llvm.module.flags = !{!2, !3, !4, !5}
85!llvm.ident = !{!6}
86
87!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 14.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
88!1 = !DIFile(filename: "variadic2.cpp", directory: "/test")
89!2 = !{i32 7, !"Dwarf Version", i32 4}
90!3 = !{i32 2, !"Debug Info Version", i32 3}
91!4 = !{i32 1, !"wchar_size", i32 4}
92!5 = !{i32 7, !"uwtable", i32 1}
93!6 = !{!"clang version 14.0.0"}
94!7 = distinct !DISubprogram(name: "mul_to_addition", linkageName: "_Z15mul_to_additionjjjjPj", scope: !8, file: !8, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !13)
95!8 = !DIFile(filename: "./variadic2.cpp", directory: "/test")
96!9 = !DISubroutineType(types: !10)
97!10 = !{null, !11, !11, !11, !11, !12}
98!11 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
99!12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
100!13 = !{!14, !15, !16, !17, !18, !19, !20, !22, !23}
101!14 = !DILocalVariable(name: "k", arg: 1, scope: !7, file: !8, line: 1, type: !11)
102!15 = !DILocalVariable(name: "l", arg: 2, scope: !7, file: !8, line: 1, type: !11)
103!16 = !DILocalVariable(name: "m", arg: 3, scope: !7, file: !8, line: 1, type: !11)
104!17 = !DILocalVariable(name: "size", arg: 4, scope: !7, file: !8, line: 1, type: !11)
105!18 = !DILocalVariable(name: "data", arg: 5, scope: !7, file: !8, line: 1, type: !12)
106!19 = !DILocalVariable(name: "i", scope: !7, file: !8, line: 2, type: !11)
107!20 = !DILocalVariable(name: "comp", scope: !21, file: !8, line: 5, type: !11)
108!21 = distinct !DILexicalBlock(scope: !7, file: !8, line: 4, column: 23)
109!22 = !DILocalVariable(name: "comp2", scope: !21, file: !8, line: 6, type: !11)
110!23 = !DILocalVariable(name: "comp3", scope: !21, file: !8, line: 7, type: !11)
111!24 = !DILocation(line: 0, scope: !7)
112!25 = !DILocation(line: 4, column: 15, scope: !7)
113!26 = !DILocation(line: 4, column: 6, scope: !7)
114!27 = !DILocation(line: 5, column: 29, scope: !21)
115!28 = !DILocation(line: 5, column: 34, scope: !21)
116!29 = !DILocation(line: 0, scope: !21)
117!30 = !DILocation(line: 8, column: 10, scope: !21)
118!31 = !DILocation(line: 8, column: 18, scope: !21)
119!32 = !DILocation(line: 9, column: 11, scope: !21)
120!33 = distinct !{!33, !26, !34, !35, !36}
121!34 = !DILocation(line: 10, column: 6, scope: !7)
122!35 = !{!"llvm.loop.mustprogress"}
123!36 = !{!"llvm.loop.vectorize.width", i32 1}
124!37 = !DILocation(line: 11, column: 2, scope: !7)
125