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