1 ; RUN: opt -S -passes=loop-fusion -loop-fusion-peel-max-count=3 < %s | FileCheck %s
3 ; Tests whether we can fuse two loops together if they have constant but a
5 ; The first three iterations of the first loop should be peeled, and then the
6 ; two loops should be fused together in this example.
12 ; void function(int *arg) {
13 ; for (int i = 0; i != 100; ++i)
14 ; arg[i] = ((i - 3)*(i+3)) % i;
16 ; for (int i = 3; i != 100; ++i)
17 ; B[i] = ((i-6)*(i+3)) % i;
20 ; CHECK-LABEL: void @function(ptr noalias %arg)
21 ; CHECK-NEXT: for.first.preheader:
22 ; CHECK-NEXT: br label %for.first.peel.begin
23 ; CHECK: for.first.peel.begin:
24 ; CHECK-NEXT: br label %for.first.peel
25 ; CHECK: for.first.peel:
26 ; CHECK: br label %for.first.latch.peel
27 ; CHECK: for.first.latch.peel:
28 ; CHECK: br label %for.first.peel.next
29 ; CHECK: for.first.peel.next:
30 ; CHECK-NEXT: br label %for.first.peel2
31 ; CHECK: for.first.peel2:
32 ; CHECK: br label %for.first.latch.peel10
33 ; CHECK: for.first.latch.peel10:
34 ; CHECK: br label %for.first.peel.next1
35 ; CHECK: for.first.peel.next1:
36 ; CHECK-NEXT: br label %for.first.peel15
37 ; CHECK: for.first.peel15:
38 ; CHECK: br label %for.first.latch.peel23
39 ; CHECK: for.first.latch.peel23:
40 ; CHECK: br label %for.first.peel.next14
41 ; CHECK: for.first.peel.next14:
42 ; CHECK-NEXT: br label %for.first.peel.next27
43 ; CHECK: for.first.peel.next27:
44 ; CHECK-NEXT: br label %for.first.preheader.peel.newph
45 ; CHECK: for.first.preheader.peel.newph:
46 ; CHECK-NEXT: br label %for.first
48 ; CHECK: br label %for.first.latch
49 ; CHECK: for.first.latch:
50 ; CHECK: br label %for.second.latch
51 ; CHECK: for.second.latch:
52 ; CHECK: br i1 %exitcond, label %for.first, label %for.end
54 ; CHECK-NEXT: ret void
56 @B = common global [1024 x i32] zeroinitializer, align 16
58 define void @function(ptr noalias %arg) {
62 for.first: ; preds = %for.first.preheader, %for.first.latch
63 %.014 = phi i32 [ 0, %for.first.preheader ], [ %tmp15, %for.first.latch ]
64 %indvars.iv23 = phi i64 [ 0, %for.first.preheader ], [ %indvars.iv.next3, %for.first.latch ]
65 %tmp = add nsw i32 %.014, -3
66 %tmp8 = add nuw nsw i64 %indvars.iv23, 3
67 %tmp9 = trunc i64 %tmp8 to i32
68 %tmp10 = mul nsw i32 %tmp, %tmp9
69 %tmp11 = trunc i64 %indvars.iv23 to i32
70 %tmp12 = srem i32 %tmp10, %tmp11
71 %tmp13 = getelementptr inbounds i32, ptr %arg, i64 %indvars.iv23
72 store i32 %tmp12, ptr %tmp13, align 4
73 br label %for.first.latch
75 for.first.latch: ; preds = %for.first
76 %indvars.iv.next3 = add nuw nsw i64 %indvars.iv23, 1
77 %tmp15 = add nuw nsw i32 %.014, 1
78 %exitcond4 = icmp ne i64 %indvars.iv.next3, 100
79 br i1 %exitcond4, label %for.first, label %for.second.preheader
81 for.second.preheader: ; preds = %for.first.latch
84 for.second: ; preds = %for.second.preheader, %for.second.latch
85 %.02 = phi i32 [ 0, %for.second.preheader ], [ %tmp28, %for.second.latch ]
86 %indvars.iv1 = phi i64 [ 3, %for.second.preheader ], [ %indvars.iv.next, %for.second.latch ]
87 %tmp20 = add nsw i32 %.02, -3
88 %tmp21 = add nuw nsw i64 %indvars.iv1, 3
89 %tmp22 = trunc i64 %tmp21 to i32
90 %tmp23 = mul nsw i32 %tmp20, %tmp22
91 %tmp24 = trunc i64 %indvars.iv1 to i32
92 %tmp25 = srem i32 %tmp23, %tmp24
93 %tmp26 = getelementptr inbounds [1024 x i32], ptr @B, i64 0, i64 %indvars.iv1
94 store i32 %tmp25, ptr %tmp26, align 4
95 br label %for.second.latch
97 for.second.latch: ; preds = %for.second
98 %indvars.iv.next = add nuw nsw i64 %indvars.iv1, 1
99 %tmp28 = add nuw nsw i32 %.02, 1
100 %exitcond = icmp ne i64 %indvars.iv.next, 100
101 br i1 %exitcond, label %for.second, label %for.end
103 for.end: ; preds = %for.second.latch