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 nsw i64 [[T0]], -1
18; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
19; CHECK:       for.body:
20; CHECK-NEXT:    [[LSR_IV1:%.*]] = phi i64 [ [[LSR_IV_NEXT2:%.*]], [[FOR_INC:%.*]] ], [ 1, [[ENTRY:%.*]] ]
21; CHECK-NEXT:    [[INDVARS_IV2:%.*]] = phi i64 [ [[INDVARS_IV_NEXT3:%.*]], [[FOR_INC]] ], [ 0, [[ENTRY]] ]
22; CHECK-NEXT:    br i1 [[CMP215]], label [[FOR_BODY2_PREHEADER:%.*]], label [[FOR_INC]]
23; CHECK:       for.body2.preheader:
24; CHECK-NEXT:    br label [[FOR_BODY2:%.*]]
25; CHECK:       for.body2:
26; CHECK-NEXT:    [[LSR_IV3:%.*]] = phi i8* [ [[SCEVGEP:%.*]], [[FOR_BODY2]] ], [ [[MAXARRAY:%.*]], [[FOR_BODY2_PREHEADER]] ]
27; CHECK-NEXT:    [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[FOR_BODY2]] ], [ [[TMP0]], [[FOR_BODY2_PREHEADER]] ]
28; CHECK-NEXT:    [[SCEVGEP6:%.*]] = getelementptr i8, i8* [[LSR_IV3]], i64 1
29; CHECK-NEXT:    [[V1:%.*]] = load i8, i8* [[SCEVGEP6]], align 1
30; CHECK-NEXT:    [[SCEVGEP5:%.*]] = getelementptr i8, i8* [[LSR_IV3]], i64 [[TMP0]]
31; CHECK-NEXT:    [[V2:%.*]] = load i8, i8* [[SCEVGEP5]], align 1
32; CHECK-NEXT:    [[TMPV:%.*]] = xor i8 [[V1]], [[V2]]
33; CHECK-NEXT:    [[SCEVGEP4:%.*]] = getelementptr i8, i8* [[LSR_IV3]], i64 [[LSR_IV1]]
34; CHECK-NEXT:    store i8 [[TMPV]], i8* [[SCEVGEP4]], align 1
35; CHECK-NEXT:    [[LSR_IV_NEXT]] = add i64 [[LSR_IV]], -1
36; CHECK-NEXT:    [[SCEVGEP]] = getelementptr i8, i8* [[LSR_IV3]], i64 1
37; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i64 [[LSR_IV_NEXT]], 0
38; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_BODY2]], label [[FOR_INC_LOOPEXIT:%.*]]
39; CHECK:       for.inc.loopexit:
40; CHECK-NEXT:    br label [[FOR_INC]]
41; CHECK:       for.inc:
42; CHECK-NEXT:    [[INDVARS_IV_NEXT3]] = add nuw nsw i64 [[INDVARS_IV2]], 1
43; CHECK-NEXT:    [[LSR_IV_NEXT2]] = add nuw nsw i64 [[LSR_IV1]], [[T0]]
44; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i64 [[INDVARS_IV_NEXT3]], [[T1]]
45; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]]
46; CHECK:       for.end.loopexit:
47; CHECK-NEXT:    ret void
48;
49entry:
50  %cmp215 = icmp sgt i32 %size, 1
51  %t0 = zext i32 %size to i64
52  %t1 = sext i32 %nsteps to i64
53  %sub2 = sub i64 %t0, 2
54  br label %for.body
55
56for.body:                                         ; preds = %for.inc, %entry
57  %indvars.iv2 = phi i64 [ %indvars.iv.next3, %for.inc ], [ 0, %entry ]
58  %t2 = mul nsw i64 %indvars.iv2, %t0
59  br i1 %cmp215, label %for.body2.preheader, label %for.inc
60
61for.body2.preheader:                              ; preds = %for.body
62  br label %for.body2
63
64; Check LSR only generates two induction variables for for.body2 one for compare and
65; one to shared by multiple array accesses.
66
67for.body2:                                        ; preds = %for.body2.preheader, %for.body2
68  %indvars.iv = phi i64 [ 1, %for.body2.preheader ], [ %indvars.iv.next, %for.body2 ]
69  %arrayidx1 = getelementptr inbounds i8, i8* %maxarray, i64 %indvars.iv
70  %v1 = load i8, i8* %arrayidx1, align 1
71  %idx2 = add nsw i64 %indvars.iv, %sub2
72  %arrayidx2 = getelementptr inbounds i8, i8* %maxarray, i64 %idx2
73  %v2 = load i8, i8* %arrayidx2, align 1
74  %tmpv = xor i8 %v1, %v2
75  %t4 = add nsw i64 %t2, %indvars.iv
76  %add.ptr = getelementptr inbounds i8, i8* %maxarray, i64 %t4
77  store i8 %tmpv, i8* %add.ptr, align 1
78  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
79  %wide.trip.count = zext i32 %size to i64
80  %exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count
81  br i1 %exitcond, label %for.body2, label %for.inc.loopexit
82
83for.inc.loopexit:                                 ; preds = %for.body2
84  br label %for.inc
85
86for.inc:                                          ; preds = %for.inc.loopexit, %for.body
87  %indvars.iv.next3 = add nuw nsw i64 %indvars.iv2, 1
88  %cmp = icmp slt i64 %indvars.iv.next3, %t1
89  br i1 %cmp, label %for.body, label %for.end.loopexit
90
91for.end.loopexit:                                 ; preds = %for.inc
92  ret void
93}
94