1; RUN: opt -S -loop-reduce %s -o - | FileCheck %s
2; REQUIRES: x86-registered-target
3
4;; Ensure that we retain debuginfo for the induction variable and dependant
5;; variables when loop strength reduction is applied to the loop.
6;; This IR produced from:
7;;
8;; clang -S -emit-llvm -Xclang -disable-llvm-passes -g lsr-basic.cpp -o
9;; Then executing opt -O2 up to the the loopFullUnroll pass.
10;; void mul_to_addition(unsigned k, unsigned size, unsigned *data) {
11;;     int i = 0;
12;; #pragma clang loop vectorize(disable)
13;;     while (i < size) {
14;;         int comp = (4 * i) + k;
15;;         data[i] = comp;
16;;        i += 1;
17;;     }
18;; }
19; CHECK: call void @llvm.dbg.value(metadata i64 %lsr.iv, metadata ![[i:[0-9]+]], metadata !DIExpression())
20; 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_mul, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value))
21; CHECK: call void @llvm.dbg.value(metadata i64 %lsr.iv, metadata ![[i]],  metadata !DIExpression(DW_OP_consts, 1, DW_OP_plus, DW_OP_stack_value))
22; CHECK: ![[i]] = !DILocalVariable(name: "i"
23; CHECK: ![[comp]] = !DILocalVariable(name: "comp"
24
25target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
26target triple = "x86_64-unknown-linux-gnu"
27
28define dso_local void @_Z15mul_to_additionjjPj(i32 %k, i32 %size, i32* nocapture %data) local_unnamed_addr !dbg !7 {
29entry:
30  call void @llvm.dbg.value(metadata i32 %k, metadata !13, metadata !DIExpression()), !dbg !14
31  call void @llvm.dbg.value(metadata i32 %size, metadata !15, metadata !DIExpression()), !dbg !14
32  call void @llvm.dbg.value(metadata i32* %data, metadata !16, metadata !DIExpression()), !dbg !14
33  call void @llvm.dbg.value(metadata i32 0, metadata !17, metadata !DIExpression()), !dbg !14
34  br label %while.cond, !dbg !14
35
36while.cond:                                       ; preds = %while.body, %entry
37  %i.0 = phi i32 [ 0, %entry ], [ %add1, %while.body ], !dbg !14
38  call void @llvm.dbg.value(metadata i32 %i.0, metadata !17, metadata !DIExpression()), !dbg !14
39  %cmp = icmp ult i32 %i.0, %size, !dbg !14
40  br i1 %cmp, label %while.body, label %while.end, !dbg !14
41
42while.body:                                       ; preds = %while.cond
43  %mul = mul nsw i32 %i.0, 4, !dbg !19
44  %add = add i32 %mul, %k, !dbg !19
45  call void @llvm.dbg.value(metadata i32 %add, metadata !21, metadata !DIExpression()), !dbg !19
46  %idxprom = zext i32 %i.0 to i64, !dbg !19
47  %arrayidx = getelementptr inbounds i32, i32* %data, i64 %idxprom, !dbg !19
48  store i32 %add, i32* %arrayidx, align 4, !dbg !19
49  %add1 = add nuw nsw i32 %i.0, 1, !dbg !19
50  call void @llvm.dbg.value(metadata i32 %add1, metadata !17, metadata !DIExpression()), !dbg !14
51  br label %while.cond, !dbg !14, !llvm.loop !22
52
53while.end:                                        ; preds = %while.cond
54  ret void, !dbg !14
55}
56
57; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
58declare void @llvm.dbg.value(metadata, metadata, metadata)
59
60!llvm.dbg.cu = !{!0}
61!llvm.module.flags = !{!3, !4, !5}
62!llvm.ident = !{!6}
63
64!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 12.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
65!1 = !DIFile(filename: "basic.cpp", directory: "/test")
66!2 = !{}
67!3 = !{i32 7, !"Dwarf Version", i32 4}
68!4 = !{i32 2, !"Debug Info Version", i32 3}
69!5 = !{i32 1, !"wchar_size", i32 4}
70!6 = !{!"clang version 12.0.0"}
71!7 = distinct !DISubprogram(name: "mul_to_addition", linkageName: "_Z15mul_to_additionjjPj", scope: !8, file: !8, line: 64, type: !9, scopeLine: 64, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
72!8 = !DIFile(filename: "./basic.cpp", directory: "/test")
73!9 = !DISubroutineType(types: !10)
74!10 = !{null, !11, !11, !12}
75!11 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
76!12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
77!13 = !DILocalVariable(name: "k", arg: 1, scope: !7, file: !8, line: 64, type: !11)
78!14 = !DILocation(line: 0, scope: !7)
79!15 = !DILocalVariable(name: "size", arg: 2, scope: !7, file: !8, line: 64, type: !11)
80!16 = !DILocalVariable(name: "data", arg: 3, scope: !7, file: !8, line: 64, type: !12)
81!17 = !DILocalVariable(name: "i", scope: !7, file: !8, line: 65, type: !18)
82!18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
83!19 = !DILocation(line: 68, column: 23, scope: !20)
84!20 = distinct !DILexicalBlock(scope: !7, file: !8, line: 67, column: 22)
85!21 = !DILocalVariable(name: "comp", scope: !20, file: !8, line: 68, type: !18)
86!22 = distinct !{!22, !23, !24, !25, !26}
87!23 = !DILocation(line: 67, column: 5, scope: !7)
88!24 = !DILocation(line: 71, column: 5, scope: !7)
89!25 = !{!"llvm.loop.mustprogress"}
90!26 = !{!"llvm.loop.vectorize.width", i32 1}
91