1; Confirm that the line number for the do.body.preheader block
2; branch is the the start of the loop.
3
4; RUN: opt -simplifycfg -loop-simplify -keep-loops="false" -S <%s | FileCheck %s
5
6; CHECK: do.body.preheader:
7; CHECK-NEXT: phi
8; CHECK-NEXT: phi
9; CHECK-NEXT: br label %do.body, !dbg ![[DL:[0-9]+]]
10; CHECK: ![[DL]] = !DILocation(line: 4,
11
12; This IR can be generated by running:
13; clang src.cpp -O2 -g -S -emit-llvm -mllvm -opt-bisect-limit=62 -o -
14;
15; Where  src.cpp contains:
16; int foo(char *Bytes, int Count)
17; {
18;     int Total = 0;
19;     do
20;         Total += Bytes[--Count];
21;     while (Count);
22;     return Total;
23; }
24
25define dso_local i32 @"foo"(i8* nocapture readonly %Bytes, i32 %Count) local_unnamed_addr !dbg !8 {
26entry:
27  %0 = sext i32 %Count to i64, !dbg !10
28  %min.iters.check = icmp ult i32 %Count, 8, !dbg !10
29  br i1 %min.iters.check, label %do.body.preheader, label %vector.ph, !dbg !10
30
31vector.ph:                                        ; preds = %entry
32  %n.vec = and i64 %0, -8, !dbg !10
33  %ind.end = sub nsw i64 %0, %n.vec, !dbg !10
34  br label %vector.body, !dbg !10
35
36vector.body:                                      ; preds = %vector.body, %vector.ph
37  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
38  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %11, %vector.body ]
39  %vec.phi5 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %12, %vector.body ]
40  %1 = xor i64 %index, -1, !dbg !11
41  %2 = add i64 %1, %0, !dbg !11
42  %3 = getelementptr inbounds i8, i8* %Bytes, i64 %2, !dbg !11
43  %4 = getelementptr inbounds i8, i8* %3, i64 -3, !dbg !11
44  %5 = bitcast i8* %4 to <4 x i8>*, !dbg !11
45  %wide.load = load <4 x i8>, <4 x i8>* %5, align 1, !dbg !11, !tbaa !12
46  %reverse = shufflevector <4 x i8> %wide.load, <4 x i8> undef, <4 x i32> <i32 3, i32 2, i32 1, i32 0>, !dbg !11
47  %6 = getelementptr inbounds i8, i8* %3, i64 -4, !dbg !11
48  %7 = getelementptr inbounds i8, i8* %6, i64 -3, !dbg !11
49  %8 = bitcast i8* %7 to <4 x i8>*, !dbg !11
50  %wide.load6 = load <4 x i8>, <4 x i8>* %8, align 1, !dbg !11, !tbaa !12
51  %reverse7 = shufflevector <4 x i8> %wide.load6, <4 x i8> undef, <4 x i32> <i32 3, i32 2, i32 1, i32 0>, !dbg !11
52  %9 = sext <4 x i8> %reverse to <4 x i32>, !dbg !11
53  %10 = sext <4 x i8> %reverse7 to <4 x i32>, !dbg !11
54  %11 = add nsw <4 x i32> %vec.phi, %9, !dbg !11
55  %12 = add nsw <4 x i32> %vec.phi5, %10, !dbg !11
56  %index.next = add i64 %index, 8
57  %13 = icmp eq i64 %index.next, %n.vec
58  br i1 %13, label %middle.block, label %vector.body, !llvm.loop !15
59
60middle.block:                                     ; preds = %vector.body
61  %.lcssa12 = phi <4 x i32> [ %11, %vector.body ], !dbg !11
62  %.lcssa = phi <4 x i32> [ %12, %vector.body ], !dbg !11
63  %bin.rdx = add <4 x i32> %.lcssa, %.lcssa12, !dbg !11
64  %rdx.shuf = shufflevector <4 x i32> %bin.rdx, <4 x i32> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>, !dbg !11
65  %bin.rdx8 = add <4 x i32> %bin.rdx, %rdx.shuf, !dbg !11
66  %rdx.shuf9 = shufflevector <4 x i32> %bin.rdx8, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>, !dbg !11
67  %bin.rdx10 = add <4 x i32> %bin.rdx8, %rdx.shuf9, !dbg !11
68  %14 = extractelement <4 x i32> %bin.rdx10, i32 0, !dbg !11
69  %cmp.n = icmp eq i64 %n.vec, %0
70  br i1 %cmp.n, label %do.end, label %do.body.preheader, !dbg !10
71
72do.body.preheader:                                ; preds = %middle.block, %entry
73  %indvars.iv.ph = phi i64 [ %0, %entry ], [ %ind.end, %middle.block ]
74  %Total.0.ph = phi i32 [ 0, %entry ], [ %14, %middle.block ]
75  br label %do.body, !dbg !11
76
77do.body:                                          ; preds = %do.body.preheader, %do.body
78  %indvars.iv = phi i64 [ %indvars.iv.next, %do.body ], [ %indvars.iv.ph, %do.body.preheader ]
79  %Total.0 = phi i32 [ %add, %do.body ], [ %Total.0.ph, %do.body.preheader ], !dbg !18
80  %indvars.iv.next = add nsw i64 %indvars.iv, -1, !dbg !11
81  %arrayidx = getelementptr inbounds i8, i8* %Bytes, i64 %indvars.iv.next, !dbg !11
82  %15 = load i8, i8* %arrayidx, align 1, !dbg !11, !tbaa !12
83  %conv = sext i8 %15 to i32, !dbg !11
84  %add = add nsw i32 %Total.0, %conv, !dbg !11
85  %16 = icmp eq i64 %indvars.iv.next, 0
86  br i1 %16, label %do.end.loopexit, label %do.body, !dbg !11, !llvm.loop !19
87
88do.end.loopexit:                                  ; preds = %do.body
89  %add.lcssa11 = phi i32 [ %add, %do.body ], !dbg !11
90  br label %do.end, !dbg !21
91
92do.end:                                           ; preds = %do.end.loopexit, %middle.block
93  %add.lcssa = phi i32 [ %14, %middle.block ], [ %add.lcssa11, %do.end.loopexit ], !dbg !11
94  ret i32 %add.lcssa, !dbg !21
95}
96
97!llvm.dbg.cu = !{!0}
98!llvm.module.flags = !{!3, !4, !5, !6}
99!llvm.ident = !{!7}
100
101!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2, nameTableKind: None)
102!1 = !DIFile(filename: "src2.cpp", directory: "")
103!2 = !{}
104!3 = !{i32 2, !"CodeView", i32 1}
105!4 = !{i32 2, !"Debug Info Version", i32 3}
106!5 = !{i32 1, !"wchar_size", i32 2}
107!6 = !{i32 7, !"PIC Level", i32 2}
108!7 = !{!""}
109!8 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !9, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
110!9 = !DISubroutineType(types: !2)
111!10 = !DILocation(line: 4, scope: !8)
112!11 = !DILocation(line: 5, scope: !8)
113!12 = !{!13, !13, i64 0}
114!13 = !{!"omnipotent char", !14, i64 0}
115!14 = !{!"Simple C++ TBAA"}
116!15 = distinct !{!15, !10, !16, !17}
117!16 = !DILocation(line: 6, scope: !8)
118!17 = !{!"llvm.loop.isvectorized", i32 1}
119!18 = !DILocation(line: 0, scope: !8)
120!19 = distinct !{!19, !10, !16, !20, !17}
121!20 = !{!"llvm.loop.unroll.runtime.disable"}
122!21 = !DILocation(line: 7, scope: !8)
123