1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -loop-reduce -S < %s | FileCheck %s
3; Check when we use an outerloop induction variable inside of an innerloop
4; induction value expr, LSR can still choose to use single induction variable
5; for the innerloop and share it in multiple induction value exprs.
6
7target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8target triple = "x86_64-unknown-linux-gnu"
9
10define void @foo(i32 %size, i32 %nsteps, i32 %hsize, i32* %lined, i8* %maxarray) {
11entry:
12  %cmp215 = icmp sgt i32 %size, 1
13  %t0 = zext i32 %size to i64
14  %t1 = sext i32 %nsteps to i64
15  %sub2 = sub i64 %t0, 2
16  br label %for.body
17
18for.body:                                         ; preds = %for.inc, %entry
19  %indvars.iv2 = phi i64 [ %indvars.iv.next3, %for.inc ], [ 0, %entry ]
20  %t2 = mul nsw i64 %indvars.iv2, %t0
21  br i1 %cmp215, label %for.body2.preheader, label %for.inc
22
23for.body2.preheader:                              ; preds = %for.body
24  br label %for.body2
25
26; Check LSR only generates two induction variables for for.body2 one for compare and
27; one to shared by multiple array accesses.
28; CHECK: for.body2:
29; CHECK-NEXT: [[LSRAR:%[^,]+]] = phi i8* [ %scevgep, %for.body2 ], [ %maxarray, %for.body2.preheader ]
30; CHECK-NEXT: [[LSR:%[^,]+]] = phi i64 [ %lsr.iv.next, %for.body2 ], [ %0, %for.body2.preheader ]
31; CHECK-NOT:  = phi i64 [ {{.*}}, %for.body2 ], [ {{.*}}, %for.body2.preheader ]
32; CHECK:      [[LSRINT:%[^,]+]] = ptrtoint i8* [[LSRAR]] to i64
33; CHECK:      [[SCEVGEP1:%[^,]+]] = getelementptr i8, i8* [[LSRAR]], i64 1
34; CHECK:      {{.*}} = load i8, i8* [[SCEVGEP1]], align 1
35; CHECK:      [[SCEVGEP2:%[^,]+]] = getelementptr i8, i8* %1, i64 [[LSRINT]]
36; CHECK:      {{.*}} = load i8, i8* [[SCEVGEP2]], align 1
37; CHECK:      [[SCEVGEP3:%[^,]+]] = getelementptr i8, i8* {{.*}}, i64 [[LSRINT]]
38; CHECK:      store i8 {{.*}}, i8* [[SCEVGEP3]], align 1
39; CHECK:      [[LSRNEXT:%[^,]+]] = add i64 [[LSR]], -1
40; CHECK:      %exitcond = icmp ne i64 [[LSRNEXT]], 0
41; CHECK:      br i1 %exitcond, label %for.body2, label %for.inc.loopexit
42
43for.body2:                                        ; preds = %for.body2.preheader, %for.body2
44  %indvars.iv = phi i64 [ 1, %for.body2.preheader ], [ %indvars.iv.next, %for.body2 ]
45  %arrayidx1 = getelementptr inbounds i8, i8* %maxarray, i64 %indvars.iv
46  %v1 = load i8, i8* %arrayidx1, align 1
47  %idx2 = add nsw i64 %indvars.iv, %sub2
48  %arrayidx2 = getelementptr inbounds i8, i8* %maxarray, i64 %idx2
49  %v2 = load i8, i8* %arrayidx2, align 1
50  %tmpv = xor i8 %v1, %v2
51  %t4 = add nsw i64 %t2, %indvars.iv
52  %add.ptr = getelementptr inbounds i8, i8* %maxarray, i64 %t4
53  store i8 %tmpv, i8* %add.ptr, align 1
54  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
55  %wide.trip.count = zext i32 %size to i64
56  %exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count
57  br i1 %exitcond, label %for.body2, label %for.inc.loopexit
58
59for.inc.loopexit:                                 ; preds = %for.body2
60  br label %for.inc
61
62for.inc:                                          ; preds = %for.inc.loopexit, %for.body
63  %indvars.iv.next3 = add nuw nsw i64 %indvars.iv2, 1
64  %cmp = icmp slt i64 %indvars.iv.next3, %t1
65  br i1 %cmp, label %for.body, label %for.end.loopexit
66
67for.end.loopexit:                                 ; preds = %for.inc
68  ret void
69}
70