1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 2; RUN: llc < %s -mtriple=riscv64 | FileCheck %s 3 4 5; Make sure we don't emit a pair of shift for the zext in the preheader. We 6; can tell that bit 31 is 0 in the preheader and rely on %n already being 7; sign extended without adding zeros explicitly. 8define void @test1(ptr nocapture noundef %a, i32 noundef signext %n) { 9; CHECK-LABEL: test1: 10; CHECK: # %bb.0: # %entry 11; CHECK-NEXT: blez a1, .LBB0_2 12; CHECK-NEXT: .LBB0_1: # %for.body 13; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 14; CHECK-NEXT: lw a2, 0(a0) 15; CHECK-NEXT: addiw a2, a2, 4 16; CHECK-NEXT: sw a2, 0(a0) 17; CHECK-NEXT: addi a1, a1, -1 18; CHECK-NEXT: addi a0, a0, 4 19; CHECK-NEXT: bnez a1, .LBB0_1 20; CHECK-NEXT: .LBB0_2: # %for.cond.cleanup 21; CHECK-NEXT: ret 22entry: 23 %cmp3 = icmp sgt i32 %n, 0 24 br i1 %cmp3, label %for.body.preheader, label %for.cond.cleanup 25 26for.body.preheader: ; preds = %entry 27 %wide.trip.count = zext i32 %n to i64 28 br label %for.body 29 30for.cond.cleanup: ; preds = %for.body, %entry 31 ret void 32 33for.body: ; preds = %for.body.preheader, %for.body 34 %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ] 35 %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv 36 %0 = load i32, ptr %arrayidx, align 4 37 %add = add nsw i32 %0, 4 38 store i32 %add, ptr %arrayidx, align 4 39 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 40 %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count 41 br i1 %exitcond.not, label %for.cond.cleanup, label %for.body 42} 43 44; Make sure we convert the 4294967294 in for.body.preheader.new to -2 based on 45; the upper 33 bits being zero by the dominating condition %cmp3. 46define void @test2(ptr nocapture noundef %a, i32 noundef signext %n) { 47; CHECK-LABEL: test2: 48; CHECK: # %bb.0: # %entry 49; CHECK-NEXT: blez a1, .LBB1_7 50; CHECK-NEXT: # %bb.1: # %for.body.preheader 51; CHECK-NEXT: li a3, 1 52; CHECK-NEXT: andi a2, a1, 1 53; CHECK-NEXT: bne a1, a3, .LBB1_3 54; CHECK-NEXT: # %bb.2: 55; CHECK-NEXT: li a3, 0 56; CHECK-NEXT: j .LBB1_5 57; CHECK-NEXT: .LBB1_3: # %for.body.preheader.new 58; CHECK-NEXT: li a3, 0 59; CHECK-NEXT: andi a1, a1, -2 60; CHECK-NEXT: addi a4, a0, 4 61; CHECK-NEXT: .LBB1_4: # %for.body 62; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 63; CHECK-NEXT: lw a5, -4(a4) 64; CHECK-NEXT: lw a6, 0(a4) 65; CHECK-NEXT: addiw a5, a5, 4 66; CHECK-NEXT: sw a5, -4(a4) 67; CHECK-NEXT: addiw a5, a6, 4 68; CHECK-NEXT: sw a5, 0(a4) 69; CHECK-NEXT: addi a3, a3, 2 70; CHECK-NEXT: addi a4, a4, 8 71; CHECK-NEXT: bne a1, a3, .LBB1_4 72; CHECK-NEXT: .LBB1_5: # %for.cond.cleanup.loopexit.unr-lcssa 73; CHECK-NEXT: beqz a2, .LBB1_7 74; CHECK-NEXT: # %bb.6: # %for.body.epil 75; CHECK-NEXT: slli a1, a3, 2 76; CHECK-NEXT: add a0, a0, a1 77; CHECK-NEXT: lw a1, 0(a0) 78; CHECK-NEXT: addiw a1, a1, 4 79; CHECK-NEXT: sw a1, 0(a0) 80; CHECK-NEXT: .LBB1_7: # %for.cond.cleanup 81; CHECK-NEXT: ret 82entry: 83 %cmp3 = icmp sgt i32 %n, 0 84 br i1 %cmp3, label %for.body.preheader, label %for.cond.cleanup 85 86for.body.preheader: ; preds = %entry 87 %wide.trip.count = zext i32 %n to i64 88 %xtraiter = and i64 %wide.trip.count, 1 89 %0 = icmp eq i32 %n, 1 90 br i1 %0, label %for.cond.cleanup.loopexit.unr-lcssa, label %for.body.preheader.new 91 92for.body.preheader.new: ; preds = %for.body.preheader 93 %unroll_iter = and i64 %wide.trip.count, 4294967294 94 br label %for.body 95 96for.cond.cleanup.loopexit.unr-lcssa: ; preds = %for.body, %for.body.preheader 97 %indvars.iv.unr = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next.1, %for.body ] 98 %lcmp.mod.not = icmp eq i64 %xtraiter, 0 99 br i1 %lcmp.mod.not, label %for.cond.cleanup, label %for.body.epil 100 101for.body.epil: ; preds = %for.cond.cleanup.loopexit.unr-lcssa 102 %arrayidx.epil = getelementptr inbounds i32, ptr %a, i64 %indvars.iv.unr 103 %1 = load i32, ptr %arrayidx.epil, align 4 104 %add.epil = add nsw i32 %1, 4 105 store i32 %add.epil, ptr %arrayidx.epil, align 4 106 br label %for.cond.cleanup 107 108for.cond.cleanup: ; preds = %for.body.epil, %for.cond.cleanup.loopexit.unr-lcssa, %entry 109 ret void 110 111for.body: ; preds = %for.body, %for.body.preheader.new 112 %indvars.iv = phi i64 [ 0, %for.body.preheader.new ], [ %indvars.iv.next.1, %for.body ] 113 %niter = phi i64 [ 0, %for.body.preheader.new ], [ %niter.next.1, %for.body ] 114 %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv 115 %2 = load i32, ptr %arrayidx, align 4 116 %add = add nsw i32 %2, 4 117 store i32 %add, ptr %arrayidx, align 4 118 %indvars.iv.next = or i64 %indvars.iv, 1 119 %arrayidx.1 = getelementptr inbounds i32, ptr %a, i64 %indvars.iv.next 120 %3 = load i32, ptr %arrayidx.1, align 4 121 %add.1 = add nsw i32 %3, 4 122 store i32 %add.1, ptr %arrayidx.1, align 4 123 %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2 124 %niter.next.1 = add i64 %niter, 2 125 %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter 126 br i1 %niter.ncmp.1, label %for.cond.cleanup.loopexit.unr-lcssa, label %for.body 127} 128