1; RUN: opt < %s  -O1  -S -loop-versioning-licm -licm -debug-only=loop-versioning-licm -disable-loop-unrolling 2>&1 | FileCheck %s
2; REQUIRES: asserts
3;
4; Test to confirm loop is a good candidate for LoopVersioningLICM
5; It also confirms invariant moved out of loop.
6;
7; CHECK: Loop: Loop at depth 2 containing: %for.body3.us<header><latch><exiting>
8; CHECK-NEXT:     Loop Versioning found to be beneficial
9;
10; CHECK: for.cond1.for.inc17_crit_edge.us.loopexit5:       ; preds = %for.body3.us
11; CHECK-NEXT: %add14.us.lcssa = phi float [ %add14.us, %for.body3.us ]
12; CHECK-NEXT: store float %add14.us.lcssa, float* %arrayidx.us, align 4, !alias.scope !7, !noalias !8
13; CHECK-NEXT: br label %for.cond1.for.inc17_crit_edge.us
14;
15define i32 @foo(float* nocapture %var2, float** nocapture readonly %var3, i32 %itr) #0 {
16entry:
17  %cmp38 = icmp sgt i32 %itr, 1
18  br i1 %cmp38, label %for.body3.lr.ph.us, label %for.end19
19
20for.body3.us:                                     ; preds = %for.body3.us, %for.body3.lr.ph.us
21  %0 = phi float [ %.pre, %for.body3.lr.ph.us ], [ %add14.us, %for.body3.us ]
22  %indvars.iv = phi i64 [ 1, %for.body3.lr.ph.us ], [ %indvars.iv.next, %for.body3.us ]
23  %1 = trunc i64 %indvars.iv to i32
24  %conv.us = sitofp i32 %1 to float
25  %add.us = fadd float %conv.us, %0
26  %arrayidx7.us = getelementptr inbounds float, float* %3, i64 %indvars.iv
27  store float %add.us, float* %arrayidx7.us, align 4
28  %2 = load float, float* %arrayidx.us, align 4
29  %add14.us = fadd float %2, %add.us
30  store float %add14.us, float* %arrayidx.us, align 4
31  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
32  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
33  %exitcond = icmp eq i32 %lftr.wideiv, %itr
34  br i1 %exitcond, label %for.cond1.for.inc17_crit_edge.us, label %for.body3.us
35
36for.body3.lr.ph.us:                               ; preds = %entry, %for.cond1.for.inc17_crit_edge.us
37  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %for.cond1.for.inc17_crit_edge.us ], [ 1, %entry ]
38  %arrayidx.us = getelementptr inbounds float, float* %var2, i64 %indvars.iv40
39  %arrayidx6.us = getelementptr inbounds float*, float** %var3, i64 %indvars.iv40
40  %3 = load float*, float** %arrayidx6.us, align 8
41  %.pre = load float, float* %arrayidx.us, align 4
42  br label %for.body3.us
43
44for.cond1.for.inc17_crit_edge.us:                 ; preds = %for.body3.us
45  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
46  %lftr.wideiv42 = trunc i64 %indvars.iv.next41 to i32
47  %exitcond43 = icmp eq i32 %lftr.wideiv42, %itr
48  br i1 %exitcond43, label %for.end19, label %for.body3.lr.ph.us
49
50for.end19:                                        ; preds = %for.cond1.for.inc17_crit_edge.us, %entry
51  ret i32 0
52}
53