|
| 1 | +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py |
| 2 | +; RUN: opt -mtriple riscv64-linux-gnu -mattr=+v,+d -loop-vectorize < %s -S -o - | FileCheck %s -check-prefix=OUTLOOP |
| 3 | +; RUN: opt -mtriple riscv64-linux-gnu -mattr=+v,+d -loop-vectorize -prefer-inloop-reductions < %s -S -o - | FileCheck %s -check-prefix=INLOOP |
| 4 | + |
| 5 | + |
| 6 | +target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128" |
| 7 | +target triple = "riscv64" |
| 8 | + |
| 9 | +define i32 @add_i16_i32(i16* nocapture readonly %x, i32 %n) { |
| 10 | +; OUTLOOP-LABEL: @add_i16_i32( |
| 11 | +; OUTLOOP-NEXT: entry: |
| 12 | +; OUTLOOP-NEXT: [[CMP6:%.*]] = icmp sgt i32 [[N:%.*]], 0 |
| 13 | +; OUTLOOP-NEXT: br i1 [[CMP6]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_COND_CLEANUP:%.*]] |
| 14 | +; OUTLOOP: for.body.preheader: |
| 15 | +; OUTLOOP-NEXT: [[TMP0:%.*]] = call i32 @llvm.vscale.i32() |
| 16 | +; OUTLOOP-NEXT: [[TMP1:%.*]] = mul i32 [[TMP0]], 4 |
| 17 | +; OUTLOOP-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], [[TMP1]] |
| 18 | +; OUTLOOP-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]] |
| 19 | +; OUTLOOP: vector.ph: |
| 20 | +; OUTLOOP-NEXT: [[TMP2:%.*]] = call i32 @llvm.vscale.i32() |
| 21 | +; OUTLOOP-NEXT: [[TMP3:%.*]] = mul i32 [[TMP2]], 4 |
| 22 | +; OUTLOOP-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N]], [[TMP3]] |
| 23 | +; OUTLOOP-NEXT: [[N_VEC:%.*]] = sub i32 [[N]], [[N_MOD_VF]] |
| 24 | +; OUTLOOP-NEXT: br label [[VECTOR_BODY:%.*]] |
| 25 | +; OUTLOOP: vector.body: |
| 26 | +; OUTLOOP-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ] |
| 27 | +; OUTLOOP-NEXT: [[VEC_PHI:%.*]] = phi <vscale x 2 x i32> [ zeroinitializer, [[VECTOR_PH]] ], [ [[TMP20:%.*]], [[VECTOR_BODY]] ] |
| 28 | +; OUTLOOP-NEXT: [[VEC_PHI1:%.*]] = phi <vscale x 2 x i32> [ zeroinitializer, [[VECTOR_PH]] ], [ [[TMP21:%.*]], [[VECTOR_BODY]] ] |
| 29 | +; OUTLOOP-NEXT: [[TMP4:%.*]] = add i32 [[INDEX]], 0 |
| 30 | +; OUTLOOP-NEXT: [[TMP5:%.*]] = call i32 @llvm.vscale.i32() |
| 31 | +; OUTLOOP-NEXT: [[TMP6:%.*]] = mul i32 [[TMP5]], 2 |
| 32 | +; OUTLOOP-NEXT: [[TMP7:%.*]] = add i32 [[TMP6]], 0 |
| 33 | +; OUTLOOP-NEXT: [[TMP8:%.*]] = mul i32 [[TMP7]], 1 |
| 34 | +; OUTLOOP-NEXT: [[TMP9:%.*]] = add i32 [[INDEX]], [[TMP8]] |
| 35 | +; OUTLOOP-NEXT: [[TMP10:%.*]] = getelementptr inbounds i16, i16* [[X:%.*]], i32 [[TMP4]] |
| 36 | +; OUTLOOP-NEXT: [[TMP11:%.*]] = getelementptr inbounds i16, i16* [[X]], i32 [[TMP9]] |
| 37 | +; OUTLOOP-NEXT: [[TMP12:%.*]] = getelementptr inbounds i16, i16* [[TMP10]], i32 0 |
| 38 | +; OUTLOOP-NEXT: [[TMP13:%.*]] = bitcast i16* [[TMP12]] to <vscale x 2 x i16>* |
| 39 | +; OUTLOOP-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 2 x i16>, <vscale x 2 x i16>* [[TMP13]], align 2 |
| 40 | +; OUTLOOP-NEXT: [[TMP14:%.*]] = call i32 @llvm.vscale.i32() |
| 41 | +; OUTLOOP-NEXT: [[TMP15:%.*]] = mul i32 [[TMP14]], 2 |
| 42 | +; OUTLOOP-NEXT: [[TMP16:%.*]] = getelementptr inbounds i16, i16* [[TMP10]], i32 [[TMP15]] |
| 43 | +; OUTLOOP-NEXT: [[TMP17:%.*]] = bitcast i16* [[TMP16]] to <vscale x 2 x i16>* |
| 44 | +; OUTLOOP-NEXT: [[WIDE_LOAD2:%.*]] = load <vscale x 2 x i16>, <vscale x 2 x i16>* [[TMP17]], align 2 |
| 45 | +; OUTLOOP-NEXT: [[TMP18:%.*]] = sext <vscale x 2 x i16> [[WIDE_LOAD]] to <vscale x 2 x i32> |
| 46 | +; OUTLOOP-NEXT: [[TMP19:%.*]] = sext <vscale x 2 x i16> [[WIDE_LOAD2]] to <vscale x 2 x i32> |
| 47 | +; OUTLOOP-NEXT: [[TMP20]] = add <vscale x 2 x i32> [[VEC_PHI]], [[TMP18]] |
| 48 | +; OUTLOOP-NEXT: [[TMP21]] = add <vscale x 2 x i32> [[VEC_PHI1]], [[TMP19]] |
| 49 | +; OUTLOOP-NEXT: [[TMP22:%.*]] = call i32 @llvm.vscale.i32() |
| 50 | +; OUTLOOP-NEXT: [[TMP23:%.*]] = mul i32 [[TMP22]], 4 |
| 51 | +; OUTLOOP-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], [[TMP23]] |
| 52 | +; OUTLOOP-NEXT: [[TMP24:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]] |
| 53 | +; OUTLOOP-NEXT: br i1 [[TMP24]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]] |
| 54 | +; OUTLOOP: middle.block: |
| 55 | +; OUTLOOP-NEXT: [[BIN_RDX:%.*]] = add <vscale x 2 x i32> [[TMP21]], [[TMP20]] |
| 56 | +; OUTLOOP-NEXT: [[TMP25:%.*]] = call i32 @llvm.vector.reduce.add.nxv2i32(<vscale x 2 x i32> [[BIN_RDX]]) |
| 57 | +; OUTLOOP-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[N]], [[N_VEC]] |
| 58 | +; OUTLOOP-NEXT: br i1 [[CMP_N]], label [[FOR_COND_CLEANUP_LOOPEXIT:%.*]], label [[SCALAR_PH]] |
| 59 | +; OUTLOOP: scalar.ph: |
| 60 | +; OUTLOOP-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[FOR_BODY_PREHEADER]] ] |
| 61 | +; OUTLOOP-NEXT: [[BC_MERGE_RDX:%.*]] = phi i32 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[TMP25]], [[MIDDLE_BLOCK]] ] |
| 62 | +; OUTLOOP-NEXT: br label [[FOR_BODY:%.*]] |
| 63 | +; OUTLOOP: for.body: |
| 64 | +; OUTLOOP-NEXT: [[I_08:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ] |
| 65 | +; OUTLOOP-NEXT: [[R_07:%.*]] = phi i32 [ [[ADD:%.*]], [[FOR_BODY]] ], [ [[BC_MERGE_RDX]], [[SCALAR_PH]] ] |
| 66 | +; OUTLOOP-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i16, i16* [[X]], i32 [[I_08]] |
| 67 | +; OUTLOOP-NEXT: [[TMP26:%.*]] = load i16, i16* [[ARRAYIDX]], align 2 |
| 68 | +; OUTLOOP-NEXT: [[CONV:%.*]] = sext i16 [[TMP26]] to i32 |
| 69 | +; OUTLOOP-NEXT: [[ADD]] = add nsw i32 [[R_07]], [[CONV]] |
| 70 | +; OUTLOOP-NEXT: [[INC]] = add nuw nsw i32 [[I_08]], 1 |
| 71 | +; OUTLOOP-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], [[N]] |
| 72 | +; OUTLOOP-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP_LOOPEXIT]], label [[FOR_BODY]], !llvm.loop [[LOOP2:![0-9]+]] |
| 73 | +; OUTLOOP: for.cond.cleanup.loopexit: |
| 74 | +; OUTLOOP-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[ADD]], [[FOR_BODY]] ], [ [[TMP25]], [[MIDDLE_BLOCK]] ] |
| 75 | +; OUTLOOP-NEXT: br label [[FOR_COND_CLEANUP]] |
| 76 | +; OUTLOOP: for.cond.cleanup: |
| 77 | +; OUTLOOP-NEXT: [[R_0_LCSSA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[ADD_LCSSA]], [[FOR_COND_CLEANUP_LOOPEXIT]] ] |
| 78 | +; OUTLOOP-NEXT: ret i32 [[R_0_LCSSA]] |
| 79 | +; |
| 80 | +; INLOOP-LABEL: @add_i16_i32( |
| 81 | +; INLOOP-NEXT: entry: |
| 82 | +; INLOOP-NEXT: [[CMP6:%.*]] = icmp sgt i32 [[N:%.*]], 0 |
| 83 | +; INLOOP-NEXT: br i1 [[CMP6]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_COND_CLEANUP:%.*]] |
| 84 | +; INLOOP: for.body.preheader: |
| 85 | +; INLOOP-NEXT: [[TMP0:%.*]] = call i32 @llvm.vscale.i32() |
| 86 | +; INLOOP-NEXT: [[TMP1:%.*]] = mul i32 [[TMP0]], 8 |
| 87 | +; INLOOP-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], [[TMP1]] |
| 88 | +; INLOOP-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]] |
| 89 | +; INLOOP: vector.ph: |
| 90 | +; INLOOP-NEXT: [[TMP2:%.*]] = call i32 @llvm.vscale.i32() |
| 91 | +; INLOOP-NEXT: [[TMP3:%.*]] = mul i32 [[TMP2]], 8 |
| 92 | +; INLOOP-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N]], [[TMP3]] |
| 93 | +; INLOOP-NEXT: [[N_VEC:%.*]] = sub i32 [[N]], [[N_MOD_VF]] |
| 94 | +; INLOOP-NEXT: br label [[VECTOR_BODY:%.*]] |
| 95 | +; INLOOP: vector.body: |
| 96 | +; INLOOP-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ] |
| 97 | +; INLOOP-NEXT: [[VEC_PHI:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[TMP21:%.*]], [[VECTOR_BODY]] ] |
| 98 | +; INLOOP-NEXT: [[VEC_PHI1:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[TMP23:%.*]], [[VECTOR_BODY]] ] |
| 99 | +; INLOOP-NEXT: [[TMP4:%.*]] = add i32 [[INDEX]], 0 |
| 100 | +; INLOOP-NEXT: [[TMP5:%.*]] = call i32 @llvm.vscale.i32() |
| 101 | +; INLOOP-NEXT: [[TMP6:%.*]] = mul i32 [[TMP5]], 4 |
| 102 | +; INLOOP-NEXT: [[TMP7:%.*]] = add i32 [[TMP6]], 0 |
| 103 | +; INLOOP-NEXT: [[TMP8:%.*]] = mul i32 [[TMP7]], 1 |
| 104 | +; INLOOP-NEXT: [[TMP9:%.*]] = add i32 [[INDEX]], [[TMP8]] |
| 105 | +; INLOOP-NEXT: [[TMP10:%.*]] = getelementptr inbounds i16, i16* [[X:%.*]], i32 [[TMP4]] |
| 106 | +; INLOOP-NEXT: [[TMP11:%.*]] = getelementptr inbounds i16, i16* [[X]], i32 [[TMP9]] |
| 107 | +; INLOOP-NEXT: [[TMP12:%.*]] = getelementptr inbounds i16, i16* [[TMP10]], i32 0 |
| 108 | +; INLOOP-NEXT: [[TMP13:%.*]] = bitcast i16* [[TMP12]] to <vscale x 4 x i16>* |
| 109 | +; INLOOP-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i16>, <vscale x 4 x i16>* [[TMP13]], align 2 |
| 110 | +; INLOOP-NEXT: [[TMP14:%.*]] = call i32 @llvm.vscale.i32() |
| 111 | +; INLOOP-NEXT: [[TMP15:%.*]] = mul i32 [[TMP14]], 4 |
| 112 | +; INLOOP-NEXT: [[TMP16:%.*]] = getelementptr inbounds i16, i16* [[TMP10]], i32 [[TMP15]] |
| 113 | +; INLOOP-NEXT: [[TMP17:%.*]] = bitcast i16* [[TMP16]] to <vscale x 4 x i16>* |
| 114 | +; INLOOP-NEXT: [[WIDE_LOAD2:%.*]] = load <vscale x 4 x i16>, <vscale x 4 x i16>* [[TMP17]], align 2 |
| 115 | +; INLOOP-NEXT: [[TMP18:%.*]] = sext <vscale x 4 x i16> [[WIDE_LOAD]] to <vscale x 4 x i32> |
| 116 | +; INLOOP-NEXT: [[TMP19:%.*]] = sext <vscale x 4 x i16> [[WIDE_LOAD2]] to <vscale x 4 x i32> |
| 117 | +; INLOOP-NEXT: [[TMP20:%.*]] = call i32 @llvm.vector.reduce.add.nxv4i32(<vscale x 4 x i32> [[TMP18]]) |
| 118 | +; INLOOP-NEXT: [[TMP21]] = add i32 [[TMP20]], [[VEC_PHI]] |
| 119 | +; INLOOP-NEXT: [[TMP22:%.*]] = call i32 @llvm.vector.reduce.add.nxv4i32(<vscale x 4 x i32> [[TMP19]]) |
| 120 | +; INLOOP-NEXT: [[TMP23]] = add i32 [[TMP22]], [[VEC_PHI1]] |
| 121 | +; INLOOP-NEXT: [[TMP24:%.*]] = call i32 @llvm.vscale.i32() |
| 122 | +; INLOOP-NEXT: [[TMP25:%.*]] = mul i32 [[TMP24]], 8 |
| 123 | +; INLOOP-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], [[TMP25]] |
| 124 | +; INLOOP-NEXT: [[TMP26:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]] |
| 125 | +; INLOOP-NEXT: br i1 [[TMP26]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]] |
| 126 | +; INLOOP: middle.block: |
| 127 | +; INLOOP-NEXT: [[BIN_RDX:%.*]] = add i32 [[TMP23]], [[TMP21]] |
| 128 | +; INLOOP-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[N]], [[N_VEC]] |
| 129 | +; INLOOP-NEXT: br i1 [[CMP_N]], label [[FOR_COND_CLEANUP_LOOPEXIT:%.*]], label [[SCALAR_PH]] |
| 130 | +; INLOOP: scalar.ph: |
| 131 | +; INLOOP-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[FOR_BODY_PREHEADER]] ] |
| 132 | +; INLOOP-NEXT: [[BC_MERGE_RDX:%.*]] = phi i32 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[BIN_RDX]], [[MIDDLE_BLOCK]] ] |
| 133 | +; INLOOP-NEXT: br label [[FOR_BODY:%.*]] |
| 134 | +; INLOOP: for.body: |
| 135 | +; INLOOP-NEXT: [[I_08:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ] |
| 136 | +; INLOOP-NEXT: [[R_07:%.*]] = phi i32 [ [[ADD:%.*]], [[FOR_BODY]] ], [ [[BC_MERGE_RDX]], [[SCALAR_PH]] ] |
| 137 | +; INLOOP-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i16, i16* [[X]], i32 [[I_08]] |
| 138 | +; INLOOP-NEXT: [[TMP27:%.*]] = load i16, i16* [[ARRAYIDX]], align 2 |
| 139 | +; INLOOP-NEXT: [[CONV:%.*]] = sext i16 [[TMP27]] to i32 |
| 140 | +; INLOOP-NEXT: [[ADD]] = add nsw i32 [[R_07]], [[CONV]] |
| 141 | +; INLOOP-NEXT: [[INC]] = add nuw nsw i32 [[I_08]], 1 |
| 142 | +; INLOOP-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], [[N]] |
| 143 | +; INLOOP-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP_LOOPEXIT]], label [[FOR_BODY]], !llvm.loop [[LOOP2:![0-9]+]] |
| 144 | +; INLOOP: for.cond.cleanup.loopexit: |
| 145 | +; INLOOP-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[ADD]], [[FOR_BODY]] ], [ [[BIN_RDX]], [[MIDDLE_BLOCK]] ] |
| 146 | +; INLOOP-NEXT: br label [[FOR_COND_CLEANUP]] |
| 147 | +; INLOOP: for.cond.cleanup: |
| 148 | +; INLOOP-NEXT: [[R_0_LCSSA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[ADD_LCSSA]], [[FOR_COND_CLEANUP_LOOPEXIT]] ] |
| 149 | +; INLOOP-NEXT: ret i32 [[R_0_LCSSA]] |
| 150 | +; |
| 151 | +entry: |
| 152 | + %cmp6 = icmp sgt i32 %n, 0 |
| 153 | + br i1 %cmp6, label %for.body, label %for.cond.cleanup |
| 154 | + |
| 155 | +for.body: ; preds = %entry, %for.body |
| 156 | + %i.08 = phi i32 [ %inc, %for.body ], [ 0, %entry ] |
| 157 | + %r.07 = phi i32 [ %add, %for.body ], [ 0, %entry ] |
| 158 | + %arrayidx = getelementptr inbounds i16, i16* %x, i32 %i.08 |
| 159 | + %0 = load i16, i16* %arrayidx, align 2 |
| 160 | + %conv = sext i16 %0 to i32 |
| 161 | + %add = add nsw i32 %r.07, %conv |
| 162 | + %inc = add nuw nsw i32 %i.08, 1 |
| 163 | + %exitcond = icmp eq i32 %inc, %n |
| 164 | + br i1 %exitcond, label %for.cond.cleanup, label %for.body |
| 165 | + |
| 166 | +for.cond.cleanup: ; preds = %for.body, %entry |
| 167 | + %r.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ] |
| 168 | + ret i32 %r.0.lcssa |
| 169 | +} |
0 commit comments