1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 2; RUN: opt < %s -loop-reduce -mtriple=x86_64 -S | FileCheck %s -check-prefix=INSN 3; RUN: opt < %s -loop-reduce -mtriple=x86_64 -lsr-insns-cost=false -S | FileCheck %s -check-prefix=REGS 4; RUN: llc < %s -O2 -mtriple=x86_64-unknown-unknown -lsr-insns-cost | FileCheck %s 5 6; OPT test checks that LSR optimize compare for static counter to compare with 0. 7 8; LLC test checks that LSR optimize compare for static counter. 9; That means that instead of creating the following: 10; movl %ecx, (%rdx,%rax,4) 11; incq %rax 12; cmpq $1024, %rax 13; LSR should optimize out cmp: 14; movl %ecx, 4096(%rdx,%rax) 15; addq $4, %rax 16; or 17; movl %ecx, 4096(%rdx,%rax,4) 18; incq %rax 19 20target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 21 22define void @foo(i32* nocapture readonly %x, i32* nocapture readonly %y, i32* nocapture %q) { 23; INSN-LABEL: @foo( 24; INSN-NEXT: entry: 25; INSN-NEXT: [[Q1:%.*]] = bitcast i32* [[Q:%.*]] to i8* 26; INSN-NEXT: [[Y3:%.*]] = bitcast i32* [[Y:%.*]] to i8* 27; INSN-NEXT: [[X7:%.*]] = bitcast i32* [[X:%.*]] to i8* 28; INSN-NEXT: br label [[FOR_BODY:%.*]] 29; INSN: for.cond.cleanup: 30; INSN-NEXT: ret void 31; INSN: for.body: 32; INSN-NEXT: [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[FOR_BODY]] ], [ -4096, [[ENTRY:%.*]] ] 33; INSN-NEXT: [[UGLYGEP8:%.*]] = getelementptr i8, i8* [[X7]], i64 [[LSR_IV]] 34; INSN-NEXT: [[UGLYGEP89:%.*]] = bitcast i8* [[UGLYGEP8]] to i32* 35; INSN-NEXT: [[SCEVGEP10:%.*]] = getelementptr i32, i32* [[UGLYGEP89]], i64 1024 36; INSN-NEXT: [[TMP:%.*]] = load i32, i32* [[SCEVGEP10]], align 4 37; INSN-NEXT: [[UGLYGEP4:%.*]] = getelementptr i8, i8* [[Y3]], i64 [[LSR_IV]] 38; INSN-NEXT: [[UGLYGEP45:%.*]] = bitcast i8* [[UGLYGEP4]] to i32* 39; INSN-NEXT: [[SCEVGEP6:%.*]] = getelementptr i32, i32* [[UGLYGEP45]], i64 1024 40; INSN-NEXT: [[TMP1:%.*]] = load i32, i32* [[SCEVGEP6]], align 4 41; INSN-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP]] 42; INSN-NEXT: [[UGLYGEP:%.*]] = getelementptr i8, i8* [[Q1]], i64 [[LSR_IV]] 43; INSN-NEXT: [[UGLYGEP2:%.*]] = bitcast i8* [[UGLYGEP]] to i32* 44; INSN-NEXT: [[SCEVGEP:%.*]] = getelementptr i32, i32* [[UGLYGEP2]], i64 1024 45; INSN-NEXT: store i32 [[ADD]], i32* [[SCEVGEP]], align 4 46; INSN-NEXT: [[LSR_IV_NEXT]] = add nsw i64 [[LSR_IV]], 4 47; INSN-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[LSR_IV_NEXT]], 0 48; INSN-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP:%.*]], label [[FOR_BODY]] 49; 50; REGS-LABEL: @foo( 51; REGS-NEXT: entry: 52; REGS-NEXT: br label [[FOR_BODY:%.*]] 53; REGS: for.cond.cleanup: 54; REGS-NEXT: ret void 55; REGS: for.body: 56; REGS-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ] 57; REGS-NEXT: [[SCEVGEP2:%.*]] = getelementptr i32, i32* [[X:%.*]], i64 [[INDVARS_IV]] 58; REGS-NEXT: [[TMP:%.*]] = load i32, i32* [[SCEVGEP2]], align 4 59; REGS-NEXT: [[SCEVGEP1:%.*]] = getelementptr i32, i32* [[Y:%.*]], i64 [[INDVARS_IV]] 60; REGS-NEXT: [[TMP1:%.*]] = load i32, i32* [[SCEVGEP1]], align 4 61; REGS-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP]] 62; REGS-NEXT: [[SCEVGEP:%.*]] = getelementptr i32, i32* [[Q:%.*]], i64 [[INDVARS_IV]] 63; REGS-NEXT: store i32 [[ADD]], i32* [[SCEVGEP]], align 4 64; REGS-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1 65; REGS-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[INDVARS_IV_NEXT]], 1024 66; REGS-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP:%.*]], label [[FOR_BODY]] 67; 68; CHECK-LABEL: foo: 69; CHECK: # %bb.0: # %entry 70; CHECK-NEXT: movq $-4096, %rax # imm = 0xF000 71; CHECK-NEXT: .p2align 4, 0x90 72; CHECK-NEXT: .LBB0_1: # %for.body 73; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 74; CHECK-NEXT: movl 4096(%rsi,%rax), %ecx 75; CHECK-NEXT: addl 4096(%rdi,%rax), %ecx 76; CHECK-NEXT: movl %ecx, 4096(%rdx,%rax) 77; CHECK-NEXT: addq $4, %rax 78; CHECK-NEXT: jne .LBB0_1 79; CHECK-NEXT: # %bb.2: # %for.cond.cleanup 80; CHECK-NEXT: retq 81entry: 82 br label %for.body 83 84for.cond.cleanup: ; preds = %for.body 85 ret void 86 87for.body: ; preds = %for.body, %entry 88 %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] 89 %arrayidx = getelementptr inbounds i32, i32* %x, i64 %indvars.iv 90 %tmp = load i32, i32* %arrayidx, align 4 91 %arrayidx2 = getelementptr inbounds i32, i32* %y, i64 %indvars.iv 92 %tmp1 = load i32, i32* %arrayidx2, align 4 93 %add = add nsw i32 %tmp1, %tmp 94 %arrayidx4 = getelementptr inbounds i32, i32* %q, i64 %indvars.iv 95 store i32 %add, i32* %arrayidx4, align 4 96 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 97 %exitcond = icmp eq i64 %indvars.iv.next, 1024 98 br i1 %exitcond, label %for.cond.cleanup, label %for.body 99} 100 101