1; RUN: opt < %s  -O1  -S -loop-versioning-licm -licm -debug-only=loop-versioning-licm -enable-new-pm=0 2>&1 | FileCheck %s
2; RUN: opt < %s -S -passes='default<O1>,loop-versioning-licm,licm' --aa-pipeline=default -debug-only=loop-versioning-licm 2>&1 | FileCheck %s
3; REQUIRES: asserts
4;
5; Test to confirm loop is a candidate for LoopVersioningLICM.
6; It also confirms invariant moved out of loop.
7;
8; CHECK: Loop: Loop at depth 2 containing: %for.body3<header><latch><exiting>
9; CHECK-NEXT:   Loop Versioning found to be beneficial
10;
11; CHECK: for.body3:
12; CHECK-NEXT: %[[induction:.*]] = phi i32 [ %arrayidx7.promoted, %for.body3.ph ], [ %add8, %for.body3 ]
13; CHECK-NEXT: %j.113 = phi i32 [ %j.016, %for.body3.ph ], [ %inc, %for.body3 ]
14; CHECK-NEXT: %idxprom = zext i32 %j.113 to i64
15; CHECK-NEXT: %arrayidx = getelementptr inbounds i32, i32* %var1, i64 %idxprom
16; CHECK-NEXT: store i32 %add, i32* %arrayidx, align 4, !alias.scope !2, !noalias !2
17; CHECK-NEXT: %add8 = add nsw i32 %[[induction]], %add
18; CHECK-NEXT: %inc = add nuw i32 %j.113, 1
19; CHECK-NEXT: %cmp2 = icmp ult i32 %inc, %itr
20; CHECK-NEXT: br i1 %cmp2, label %for.body3, label %for.inc11.loopexit.loopexit7, !llvm.loop !5
21define i32 @foo(i32* nocapture %var1, i32* nocapture readnone %var2, i32* nocapture %var3, i32 %itr) #0 {
22entry:
23  %cmp14 = icmp eq i32 %itr, 0
24  br i1 %cmp14, label %for.end13, label %for.cond1.preheader.preheader
25
26for.cond1.preheader.preheader:                    ; preds = %entry
27  br label %for.cond1.preheader
28
29for.cond1.preheader:                              ; preds = %for.cond1.preheader.preheader, %for.inc11
30  %j.016 = phi i32 [ %j.1.lcssa, %for.inc11 ], [ 0, %for.cond1.preheader.preheader ]
31  %i.015 = phi i32 [ %inc12, %for.inc11 ], [ 0, %for.cond1.preheader.preheader ]
32  %cmp212 = icmp ult i32 %j.016, %itr
33  br i1 %cmp212, label %for.body3.lr.ph, label %for.inc11
34
35for.body3.lr.ph:                                  ; preds = %for.cond1.preheader
36  %add = add i32 %i.015, %itr
37  %idxprom6 = zext i32 %i.015 to i64
38  %arrayidx7 = getelementptr inbounds i32, i32* %var3, i64 %idxprom6
39  br label %for.body3
40
41for.body3:                                        ; preds = %for.body3.lr.ph, %for.body3
42  %j.113 = phi i32 [ %j.016, %for.body3.lr.ph ], [ %inc, %for.body3 ]
43  %idxprom = zext i32 %j.113 to i64
44  %arrayidx = getelementptr inbounds i32, i32* %var1, i64 %idxprom
45  store i32 %add, i32* %arrayidx, align 4
46  %0 = load i32, i32* %arrayidx7, align 4
47  %add8 = add nsw i32 %0, %add
48  store i32 %add8, i32* %arrayidx7, align 4
49  %inc = add nuw i32 %j.113, 1
50  %cmp2 = icmp ult i32 %inc, %itr
51  br i1 %cmp2, label %for.body3, label %for.inc11.loopexit
52
53for.inc11.loopexit:                               ; preds = %for.body3
54  br label %for.inc11
55
56for.inc11:                                        ; preds = %for.inc11.loopexit, %for.cond1.preheader
57  %j.1.lcssa = phi i32 [ %j.016, %for.cond1.preheader ], [ %itr, %for.inc11.loopexit ]
58  %inc12 = add nuw i32 %i.015, 1
59  %cmp = icmp ult i32 %inc12, %itr
60  br i1 %cmp, label %for.cond1.preheader, label %for.end13.loopexit
61
62for.end13.loopexit:                               ; preds = %for.inc11
63  br label %for.end13
64
65for.end13:                                        ; preds = %for.end13.loopexit, %entry
66  ret i32 0
67}
68
69