1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -loop-reduce -S < %s | FileCheck %s
3
4; Check when we use an outerloop induction variable inside of an innerloop
5; induction value expr, LSR can still choose to use single induction variable
6; for the innerloop and share it in multiple induction value exprs.
7
8target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
9target triple = "x86_64-unknown-linux-gnu"
10
11define void @foo(i32 %size, i32 %nsteps, i32 %hsize, i32* %lined, i8* %maxarray) {
12; CHECK-LABEL: @foo(
13; CHECK-NEXT:  entry:
14; CHECK-NEXT:    [[CMP215:%.*]] = icmp sgt i32 [[SIZE:%.*]], 1
15; CHECK-NEXT:    [[T0:%.*]] = zext i32 [[SIZE]] to i64
16; CHECK-NEXT:    [[T1:%.*]] = sext i32 [[NSTEPS:%.*]] to i64
17; CHECK-NEXT:    [[TMP0:%.*]] = add i64 [[T0]], -1
18; CHECK-NEXT:    [[TMP1:%.*]] = inttoptr i64 [[TMP0]] to i8*
19; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
20; CHECK:       for.body:
21; CHECK-NEXT:    [[LSR_IV1:%.*]] = phi i64 [ [[LSR_IV_NEXT2:%.*]], [[FOR_INC:%.*]] ], [ 1, [[ENTRY:%.*]] ]
22; CHECK-NEXT:    [[INDVARS_IV2:%.*]] = phi i64 [ [[INDVARS_IV_NEXT3:%.*]], [[FOR_INC]] ], [ 0, [[ENTRY]] ]
23; CHECK-NEXT:    [[LSR_IV13:%.*]] = inttoptr i64 [[LSR_IV1]] to i8*
24; CHECK-NEXT:    br i1 [[CMP215]], label [[FOR_BODY2_PREHEADER:%.*]], label [[FOR_INC]]
25; CHECK:       for.body2.preheader:
26; CHECK-NEXT:    br label [[FOR_BODY2:%.*]]
27; CHECK:       for.body2:
28; CHECK-NEXT:    [[LSR_IV4:%.*]] = phi i8* [ [[SCEVGEP:%.*]], [[FOR_BODY2]] ], [ [[MAXARRAY:%.*]], [[FOR_BODY2_PREHEADER]] ]
29; CHECK-NEXT:    [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[FOR_BODY2]] ], [ [[TMP0]], [[FOR_BODY2_PREHEADER]] ]
30; CHECK-NEXT:    [[LSR_IV45:%.*]] = ptrtoint i8* [[LSR_IV4]] to i64
31; CHECK-NEXT:    [[SCEVGEP8:%.*]] = getelementptr i8, i8* [[LSR_IV4]], i64 1
32; CHECK-NEXT:    [[V1:%.*]] = load i8, i8* [[SCEVGEP8]], align 1
33; CHECK-NEXT:    [[SCEVGEP7:%.*]] = getelementptr i8, i8* [[TMP1]], i64 [[LSR_IV45]]
34; CHECK-NEXT:    [[V2:%.*]] = load i8, i8* [[SCEVGEP7]], align 1
35; CHECK-NEXT:    [[TMPV:%.*]] = xor i8 [[V1]], [[V2]]
36; CHECK-NEXT:    [[SCEVGEP6:%.*]] = getelementptr i8, i8* [[LSR_IV13]], i64 [[LSR_IV45]]
37; CHECK-NEXT:    store i8 [[TMPV]], i8* [[SCEVGEP6]], align 1
38; CHECK-NEXT:    [[LSR_IV_NEXT]] = add i64 [[LSR_IV]], -1
39; CHECK-NEXT:    [[SCEVGEP]] = getelementptr i8, i8* [[LSR_IV4]], i64 1
40; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i64 [[LSR_IV_NEXT]], 0
41; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_BODY2]], label [[FOR_INC_LOOPEXIT:%.*]]
42; CHECK:       for.inc.loopexit:
43; CHECK-NEXT:    br label [[FOR_INC]]
44; CHECK:       for.inc:
45; CHECK-NEXT:    [[INDVARS_IV_NEXT3]] = add nuw nsw i64 [[INDVARS_IV2]], 1
46; CHECK-NEXT:    [[LSR_IV_NEXT2]] = add nuw nsw i64 [[LSR_IV1]], [[T0]]
47; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i64 [[INDVARS_IV_NEXT3]], [[T1]]
48; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]]
49; CHECK:       for.end.loopexit:
50; CHECK-NEXT:    ret void
51;
52entry:
53  %cmp215 = icmp sgt i32 %size, 1
54  %t0 = zext i32 %size to i64
55  %t1 = sext i32 %nsteps to i64
56  %sub2 = sub i64 %t0, 2
57  br label %for.body
58
59for.body:                                         ; preds = %for.inc, %entry
60  %indvars.iv2 = phi i64 [ %indvars.iv.next3, %for.inc ], [ 0, %entry ]
61  %t2 = mul nsw i64 %indvars.iv2, %t0
62  br i1 %cmp215, label %for.body2.preheader, label %for.inc
63
64for.body2.preheader:                              ; preds = %for.body
65  br label %for.body2
66
67; Check LSR only generates two induction variables for for.body2 one for compare and
68; one to shared by multiple array accesses.
69
70for.body2:                                        ; preds = %for.body2.preheader, %for.body2
71  %indvars.iv = phi i64 [ 1, %for.body2.preheader ], [ %indvars.iv.next, %for.body2 ]
72  %arrayidx1 = getelementptr inbounds i8, i8* %maxarray, i64 %indvars.iv
73  %v1 = load i8, i8* %arrayidx1, align 1
74  %idx2 = add nsw i64 %indvars.iv, %sub2
75  %arrayidx2 = getelementptr inbounds i8, i8* %maxarray, i64 %idx2
76  %v2 = load i8, i8* %arrayidx2, align 1
77  %tmpv = xor i8 %v1, %v2
78  %t4 = add nsw i64 %t2, %indvars.iv
79  %add.ptr = getelementptr inbounds i8, i8* %maxarray, i64 %t4
80  store i8 %tmpv, i8* %add.ptr, align 1
81  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
82  %wide.trip.count = zext i32 %size to i64
83  %exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count
84  br i1 %exitcond, label %for.body2, label %for.inc.loopexit
85
86for.inc.loopexit:                                 ; preds = %for.body2
87  br label %for.inc
88
89for.inc:                                          ; preds = %for.inc.loopexit, %for.body
90  %indvars.iv.next3 = add nuw nsw i64 %indvars.iv2, 1
91  %cmp = icmp slt i64 %indvars.iv.next3, %t1
92  br i1 %cmp, label %for.body, label %for.end.loopexit
93
94for.end.loopexit:                                 ; preds = %for.inc
95  ret void
96}
97
98