1; RUN: opt < %s -S -loop-unroll -unroll-runtime=true -unroll-count=4 -unroll-remainder -instcombine | FileCheck %s
2
3; CHECK-LABEL: unroll
4define i32 @unroll(i32* nocapture readonly %a, i32* nocapture readonly %b, i32 %N) local_unnamed_addr #0 {
5entry:
6  %cmp9 = icmp eq i32 %N, 0
7  br i1 %cmp9, label %for.cond.cleanup, label %for.body.lr.ph
8
9for.body.lr.ph:
10  %wide.trip.count = zext i32 %N to i64
11  br label %for.body
12
13for.cond.cleanup:
14  %c.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
15  ret i32 %c.0.lcssa
16
17; CHECK-LABEL: for.body.lr.ph
18; CHECK: [[COUNT:%[a-z.0-9]+]] = add nsw i64 %wide.trip.count, -1
19; CHECK: %xtraiter = and i64 %wide.trip.count, 3
20; CHECK: [[CMP:%[a-z.0-9]+]] = icmp ult i64 [[COUNT]], 3
21; CHECK: br i1 [[CMP]], label %[[CLEANUP:.*]], label %for.body.lr.ph.new
22
23; CHECK-LABEL: for.body.lr.ph.new:
24; CHECK: %unroll_iter = and i64 %wide.trip.count, 4294967292
25; CHECK: br label %for.body
26
27; CHECK: [[CLEANUP]]:
28; CHECK: [[MOD:%[a-z.0-9]+]] = icmp eq i64 %xtraiter, 0
29; CHECK: br i1 [[MOD]], label %[[EXIT:.*]], label %[[EPIL_PEEL0_PRE:.*]]
30
31; CHECK: [[EPIL_PEEL0_PRE]]:
32; CHECK: br label %[[EPIL_PEEL0:.*]]
33
34; CHECK: [[EPIL_PEEL0]]:
35; CHECK: [[PEEL_CMP0:%[a-z.0-9]+]] = icmp eq i64 %xtraiter, 1
36; CHECK: br i1 [[PEEL_CMP0]], label %[[EPIL_EXIT:.*]], label %[[EPIL_PEEL1:.*]]
37
38; CHECK: [[EPIL_EXIT]]:
39; CHECK: br label %[[EXIT]]
40
41; CHECK: [[EXIT]]:
42; CHECK: ret i32
43
44; CHECK-LABEL: for.body:
45; CHECK: [[INDVAR0:%[a-z.0-9]+]] = phi i64 [ 0, %for.body.lr.ph
46; CHECK: [[ITER:%[a-z.0-9]+]] = phi i64 [ %unroll_iter
47; CHECK: or i64 [[INDVAR0]], 1
48; CHECK: or i64 [[INDVAR0]], 2
49; CHECK: or i64 [[INDVAR0]], 3
50; CHECK: add nuw nsw i64 [[INDVAR0]], 4
51; CHECK: [[SUB:%[a-z.0-9]+]] = add i64 [[ITER]], -4
52; CHECK: [[ITER_CMP:%[a-z.0-9]+]] = icmp eq i64 [[SUB]], 0
53; CHECK: br i1 [[ITER_CMP]], label %[[LOOP_EXIT:.*]], label %for.body
54
55; CHECK: [[EPIL_PEEL1]]:
56; CHECK: [[PEEL_CMP1:%[a-z.0-9]+]] = icmp eq i64 %xtraiter, 2
57; CHECK: br i1 [[PEEL_CMP1]], label %[[EPIL_EXIT]], label %[[EPIL_PEEL2:.*]]
58
59; CHECK: [[EPIL_PEEL2]]:
60; CHECK: br label %[[EXIT]]
61
62for.body:
63  %indvars.iv = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next, %for.body ]
64  %c.010 = phi i32 [ 0, %for.body.lr.ph ], [ %add, %for.body ]
65  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
66  %0 = load i32, i32* %arrayidx, align 4
67  %arrayidx2 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
68  %1 = load i32, i32* %arrayidx2, align 4
69  %mul = mul nsw i32 %1, %0
70  %add = add nsw i32 %mul, %c.010
71  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
72  %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
73  br i1 %exitcond, label %for.cond.cleanup, label %for.body
74}
75