1; RUN: opt -mtriple=x86_64-unknown-linux-gnu -loop-reduce -lsr-insns-cost=false -S < %s | FileCheck %s 2; Check LSR formula canonicalization will put loop invariant regs before 3; induction variable of current loop, so exprs involving loop invariant regs 4; can be promoted outside of current loop. 5 6target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 7 8define void @foo(i32 %size, i32 %nsteps, i8* nocapture %maxarray, i8* nocapture readnone %buffer, i32 %init) local_unnamed_addr #0 { 9entry: 10 %cmp25 = icmp sgt i32 %nsteps, 0 11 br i1 %cmp25, label %for.cond1.preheader.lr.ph, label %for.end12 12 13for.cond1.preheader.lr.ph: ; preds = %entry 14 %cmp223 = icmp sgt i32 %size, 1 15 %t0 = sext i32 %init to i64 16 %wide.trip.count = zext i32 %size to i64 17 %wide.trip.count31 = zext i32 %nsteps to i64 18 br label %for.cond1.preheader 19 20for.cond1.preheader: ; preds = %for.inc10, %for.cond1.preheader.lr.ph 21 %indvars.iv28 = phi i64 [ 0, %for.cond1.preheader.lr.ph ], [ %indvars.iv.next29, %for.inc10 ] 22 br i1 %cmp223, label %for.body3.lr.ph, label %for.inc10 23 24for.body3.lr.ph: ; preds = %for.cond1.preheader 25 %t1 = add nsw i64 %indvars.iv28, %t0 26 %t2 = trunc i64 %indvars.iv28 to i8 27 br label %for.body3 28 29; Make sure loop invariant items are grouped together so that load address can 30; be represented in one getelementptr. 31; CHECK-LABEL: for.body3: 32; CHECK-NEXT: [[LSR:%[^,]+]] = phi i64 [ 1, %for.body3.lr.ph ], [ {{.*}}, %for.body3 ] 33; CHECK-NOT: = phi i64 34; CHECK-NEXT: [[LOADADDR:%[^,]+]] = getelementptr i8, i8* {{.*}}, i64 [[LSR]] 35; CHECK-NEXT: = load i8, i8* [[LOADADDR]], align 1 36; CHECK: br i1 %exitcond, label %for.inc10.loopexit, label %for.body3 37 38for.body3: ; preds = %for.body3, %for.body3.lr.ph 39 %indvars.iv = phi i64 [ 1, %for.body3.lr.ph ], [ %indvars.iv.next, %for.body3 ] 40 %t5 = trunc i64 %indvars.iv to i8 41 %t3 = add nsw i64 %t1, %indvars.iv 42 %arrayidx = getelementptr inbounds i8, i8* %maxarray, i64 %t3 43 %t4 = load i8, i8* %arrayidx, align 1 44 %add5 = add i8 %t4, %t5 45 %add6 = add i8 %add5, %t2 46 %arrayidx9 = getelementptr inbounds i8, i8* %maxarray, i64 %indvars.iv 47 store i8 %add6, i8* %arrayidx9, align 1 48 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 49 %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count 50 br i1 %exitcond, label %for.inc10.loopexit, label %for.body3 51 52for.inc10.loopexit: ; preds = %for.body3 53 br label %for.inc10 54 55for.inc10: ; preds = %for.inc10.loopexit, %for.cond1.preheader 56 %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1 57 %exitcond32 = icmp eq i64 %indvars.iv.next29, %wide.trip.count31 58 br i1 %exitcond32, label %for.end12.loopexit, label %for.cond1.preheader 59 60for.end12.loopexit: ; preds = %for.inc10 61 br label %for.end12 62 63for.end12: ; preds = %for.end12.loopexit, %entry 64 ret void 65} 66