1; RUN: opt -S -loop-fusion -loop-fusion-peel-max-count=3 < %s | FileCheck %s
2
3; Tests whether we can fuse two loops together if they have constant but a
4; different tripcount.
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.
7
8; C Code
9;
10; int B[1024];
11;
12; void function(int *arg) {
13;   for (int i = 0; i != 100; ++i)
14;     arg[i] = ((i - 3)*(i+3)) % i;
15;
16;   for (int i = 3; i != 100; ++i)
17;     B[i] = ((i-6)*(i+3)) % i;
18; }
19
20; CHECK-LABEL: void @function(i32* 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
47; CHECK:       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
53; CHECK:       for.end:
54; CHECK-NEXT:    ret void
55
56@B = common global [1024 x i32] zeroinitializer, align 16
57
58define void @function(i32* noalias %arg) {
59for.first.preheader:
60  br label %for.first
61
62for.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, i32* %arg, i64 %indvars.iv23
72  store i32 %tmp12, i32* %tmp13, align 4
73  br label %for.first.latch
74
75for.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
80
81for.second.preheader:                            ; preds = %for.first.latch
82  br label %for.second
83
84for.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], [1024 x i32]* @B, i64 0, i64 %indvars.iv1
94  store i32 %tmp25, i32* %tmp26, align 4
95  br label %for.second.latch
96
97for.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
102
103for.end:                                        ; preds = %for.second.latch
104  ret void
105}
106