804 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --filter-out-after "^scalar.ph:" --version 32; RUN: opt < %s -passes=loop-vectorize,instcombine -enable-histogram-loop-vectorization -sve-gather-overhead=2 -sve-scatter-overhead=2 -debug-only=loop-vectorize -S 2>&1 | FileCheck %s3; REQUIRES: asserts4 5target triple = "aarch64-unknown-linux-gnu"6 7;; Based on the following C code:8;;9;; void simple_histogram(int *buckets, unsigned *indices, int N) {10;; for (int i = 0; i < N; ++i)11;; buckets[indices[i]]++;12;; }13 14;; Confirm finding a histogram operation15; CHECK-LABEL: Checking a loop in 'simple_histogram'16; CHECK: LV: Checking for a histogram on: store i32 %inc, ptr %gep.bucket, align 417; CHECK: LV: Found histogram for: store i32 %inc, ptr %gep.bucket, align 418 19;; Confirm cost calculation for runtime checks20; CHECK-LABEL: LV: Checking a loop in 'simple_histogram_rtdepcheck'21; CHECK: Calculating cost of runtime checks:22; CHECK: Total cost of runtime checks:23; CHECK: LV: Minimum required TC for runtime checks to be profitable:24 25;; Confirm inability to vectorize with potential alias to buckets26; CHECK-LABEL: LV: Checking a loop in 'simple_histogram_unsafe_alias'27; CHECK: LV: Can't vectorize due to memory conflicts28; CHECK-NEXT: LV: Not vectorizing: Cannot prove legality.29 30define void @simple_histogram(ptr noalias %buckets, ptr readonly %indices, i64 %N) #0 {31; CHECK-LABEL: define void @simple_histogram(32; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0:[0-9]+]] {33; CHECK-NEXT: entry:34; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()35; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 236; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP1]]37; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]38; CHECK: vector.ph:39; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64()40; CHECK-NEXT: [[TMP5:%.*]] = shl nuw nsw i64 [[TMP4]], 241; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP5]]42; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]43; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]44; CHECK: vector.body:45; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]46; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]47; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP8]], align 448; CHECK-NEXT: [[TMP9:%.*]] = zext <vscale x 4 x i32> [[WIDE_LOAD]] to <vscale x 4 x i64>49; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], <vscale x 4 x i64> [[TMP9]]50; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP10]], i32 1, <vscale x 4 x i1> splat (i1 true))51; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP5]]52; CHECK-NEXT: [[TMP11:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]53; CHECK-NEXT: br i1 [[TMP11]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]54; CHECK: middle.block:55; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]56; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]57; CHECK: scalar.ph:58;59entry:60 br label %for.body61 62for.body:63 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]64 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv65 %l.idx = load i32, ptr %gep.indices, align 466 %idxprom1 = zext i32 %l.idx to i6467 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom168 %l.bucket = load i32, ptr %gep.bucket, align 469 %inc = add nsw i32 %l.bucket, 170 store i32 %inc, ptr %gep.bucket, align 471 %iv.next = add nuw nsw i64 %iv, 172 %exitcond = icmp eq i64 %iv.next, %N73 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !474 75for.exit:76 ret void77}78 79define void @simple_histogram_inc_param(ptr noalias %buckets, ptr readonly %indices, i64 %N, i32 %incval) #0 {80; CHECK-LABEL: define void @simple_histogram_inc_param(81; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], i64 [[N:%.*]], i32 [[INCVAL:%.*]]) #[[ATTR0]] {82; CHECK-NEXT: entry:83; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()84; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 285; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP1]]86; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]87; CHECK: vector.ph:88; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64()89; CHECK-NEXT: [[TMP5:%.*]] = shl nuw nsw i64 [[TMP4]], 290; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP5]]91; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]92; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]93; CHECK: vector.body:94; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]95; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]96; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP8]], align 497; CHECK-NEXT: [[TMP9:%.*]] = zext <vscale x 4 x i32> [[WIDE_LOAD]] to <vscale x 4 x i64>98; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], <vscale x 4 x i64> [[TMP9]]99; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP10]], i32 [[INCVAL]], <vscale x 4 x i1> splat (i1 true))100; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP5]]101; CHECK-NEXT: [[TMP11:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]102; CHECK-NEXT: br i1 [[TMP11]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]103; CHECK: middle.block:104; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]105; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]106; CHECK: scalar.ph:107;108entry:109 br label %for.body110 111for.body:112 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]113 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv114 %l.idx = load i32, ptr %gep.indices, align 4115 %idxprom1 = zext i32 %l.idx to i64116 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1117 %l.bucket = load i32, ptr %gep.bucket, align 4118 %inc = add nsw i32 %l.bucket, %incval119 store i32 %inc, ptr %gep.bucket, align 4120 %iv.next = add nuw nsw i64 %iv, 1121 %exitcond = icmp eq i64 %iv.next, %N122 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4123 124for.exit:125 ret void126}127 128define void @simple_histogram_sub(ptr noalias %buckets, ptr readonly %indices, i64 %N) #0 {129; CHECK-LABEL: define void @simple_histogram_sub(130; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {131; CHECK-NEXT: entry:132; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()133; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 2134; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP1]]135; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]136; CHECK: vector.ph:137; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64()138; CHECK-NEXT: [[TMP5:%.*]] = shl nuw nsw i64 [[TMP4]], 2139; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP5]]140; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]141; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]142; CHECK: vector.body:143; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]144; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]145; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP8]], align 4146; CHECK-NEXT: [[TMP9:%.*]] = sext <vscale x 4 x i32> [[WIDE_LOAD]] to <vscale x 4 x i64>147; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], <vscale x 4 x i64> [[TMP9]]148; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP10]], i32 -1, <vscale x 4 x i1> splat (i1 true))149; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP5]]150; CHECK-NEXT: [[TMP11:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]151; CHECK-NEXT: br i1 [[TMP11]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]152; CHECK: middle.block:153; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]154; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]155; CHECK: scalar.ph:156;157entry:158 br label %for.body159 160for.body:161 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]162 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv163 %l.idx = load i32, ptr %gep.indices, align 4164 %idxprom1 = sext i32 %l.idx to i64165 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1166 %l.bucket = load i32, ptr %gep.bucket, align 4167 %inc = sub nsw i32 %l.bucket, 1168 store i32 %inc, ptr %gep.bucket, align 4169 %iv.next = add nuw nsw i64 %iv, 1170 %exitcond = icmp eq i64 %iv.next, %N171 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4172 173for.exit:174 ret void175}176 177define void @conditional_histogram(ptr noalias %buckets, ptr readonly %indices, ptr readonly %conds, i64 %N) #0 {178; CHECK-LABEL: define void @conditional_histogram(179; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], ptr readonly [[CONDS:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {180; CHECK-NEXT: entry:181; CHECK-NEXT: [[TMP6:%.*]] = call i64 @llvm.vscale.i64()182; CHECK-NEXT: [[TMP3:%.*]] = shl nuw nsw i64 [[TMP6]], 2183; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP3]]184; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]185; CHECK: vector.ph:186; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64()187; CHECK-NEXT: [[TMP5:%.*]] = shl nuw nsw i64 [[TMP4]], 2188; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP5]]189; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]190; CHECK-NEXT: br label [[FOR_BODY:%.*]]191; CHECK: vector.body:192; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[FOR_BODY]] ]193; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]194; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP8]], align 4195; CHECK-NEXT: [[TMP9:%.*]] = zext <vscale x 4 x i32> [[WIDE_LOAD]] to <vscale x 4 x i64>196; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], <vscale x 4 x i64> [[TMP9]]197; CHECK-NEXT: [[TMP12:%.*]] = getelementptr inbounds i32, ptr [[CONDS]], i64 [[INDEX]]198; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP12]], align 4199; CHECK-NEXT: [[TMP13:%.*]] = icmp sgt <vscale x 4 x i32> [[WIDE_LOAD1]], splat (i32 5100)200; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP10]], i32 1, <vscale x 4 x i1> [[TMP13]])201; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP5]]202; CHECK-NEXT: [[TMP14:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]203; CHECK-NEXT: br i1 [[TMP14]], label [[MIDDLE_BLOCK:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP8:![0-9]+]]204; CHECK: middle.block:205; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]206; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]207; CHECK: scalar.ph:208;209entry:210 br label %for.body211 212for.body:213 %iv = phi i64 [ 0, %entry ], [ %iv.next, %next ]214 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv215 %l.idx = load i32, ptr %gep.indices, align 4216 %idxprom1 = zext i32 %l.idx to i64217 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1218 %condidx = getelementptr inbounds i32, ptr %conds, i64 %iv219 %conddata = load i32, ptr %condidx, align 4220 %ifcond = icmp sgt i32 %conddata, 5100221 br i1 %ifcond, label %iftrue, label %next222 223iftrue:224 %l.bucket = load i32, ptr %gep.bucket, align 4225 %inc = add nsw i32 %l.bucket, 1226 store i32 %inc, ptr %gep.bucket, align 4227 br label %next228 229next:230 %iv.next = add nuw nsw i64 %iv, 1231 %exitcond = icmp eq i64 %iv.next, %N232 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4233 234for.exit:235 ret void236}237 238define void @histogram_8bit(ptr noalias %buckets, ptr readonly %indices, i64 %N) #0 {239; CHECK-LABEL: define void @histogram_8bit(240; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {241; CHECK-NEXT: iter.check:242; CHECK-NEXT: [[TMP5:%.*]] = call i64 @llvm.vscale.i64()243; CHECK-NEXT: [[TMP9:%.*]] = shl nuw nsw i64 [[TMP5]], 3244; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP9]]245; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[ENTRY:%.*]]246; CHECK: vector.main.loop.iter.check:247; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()248; CHECK-NEXT: [[TMP6:%.*]] = shl nuw nsw i64 [[TMP2]], 4249; CHECK-NEXT: [[MIN_ITERS_CHECK1:%.*]] = icmp ult i64 [[N]], [[TMP6]]250; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK1]], label [[VEC_EPILOG_PH:%.*]], label [[VECTOR_PH:%.*]]251; CHECK: vector.ph:252; CHECK-NEXT: [[TMP3:%.*]] = call i64 @llvm.vscale.i64()253; CHECK-NEXT: [[TMP4:%.*]] = shl nuw nsw i64 [[TMP3]], 4254; CHECK-NEXT: [[DOTNOT:%.*]] = add nsw i64 [[TMP4]], -1255; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]256; CHECK-NEXT: [[N_VEC1:%.*]] = sub i64 [[N]], [[N_VEC]]257; CHECK-NEXT: br label [[FOR_BODY:%.*]]258; CHECK: vector.body:259; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]260; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[IV]]261; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 16 x i32>, ptr [[ARRAYIDX]], align 4262; CHECK-NEXT: [[TMP8:%.*]] = zext <vscale x 16 x i32> [[WIDE_LOAD]] to <vscale x 16 x i64>263; CHECK-NEXT: [[TMP17:%.*]] = getelementptr inbounds i8, ptr [[BUCKETS]], <vscale x 16 x i64> [[TMP8]]264; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv16p0.i8(<vscale x 16 x ptr> [[TMP17]], i8 1, <vscale x 16 x i1> splat (i1 true))265; CHECK-NEXT: [[IV_NEXT]] = add nuw i64 [[IV]], [[TMP4]]266; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i64 [[IV_NEXT]], [[N_VEC1]]267; CHECK-NEXT: br i1 [[TMP10]], label [[MIDDLE_BLOCK:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]268; CHECK: middle.block:269; CHECK-NEXT: [[CMP_N1:%.*]] = icmp eq i64 [[N_VEC]], 0270; CHECK-NEXT: br i1 [[CMP_N1]], label [[FOR_EXIT:%.*]], label [[VEC_EPILOG_ITER_CHECK:%.*]]271; CHECK: vec.epilog.iter.check:272;273entry:274 br label %for.body275 276for.body:277 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]278 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv279 %l.idx = load i32, ptr %gep.indices, align 4280 %idxprom1 = zext i32 %l.idx to i64281 %gep.bucket = getelementptr inbounds i8, ptr %buckets, i64 %idxprom1282 %l.bucket = load i8, ptr %gep.bucket, align 4283 %inc = add nsw i8 %l.bucket, 1284 store i8 %inc, ptr %gep.bucket, align 4285 %iv.next = add nuw nsw i64 %iv, 1286 %exitcond = icmp eq i64 %iv.next, %N287 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4288 289for.exit:290 ret void291}292 293;; We don't currently support floating point histograms.294define void @histogram_float(ptr noalias %buckets, ptr readonly %indices, i64 %N) #0 {295; CHECK-LABEL: define void @histogram_float(296; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {297; CHECK-NEXT: entry:298; CHECK-NEXT: br label [[FOR_BODY:%.*]]299; CHECK: for.body:300; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]301; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds nuw i32, ptr [[INDICES]], i64 [[IV]]302; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4303; CHECK-NEXT: [[IDXPROM1:%.*]] = zext i32 [[TMP0]] to i64304; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds nuw float, ptr [[BUCKETS]], i64 [[IDXPROM1]]305; CHECK-NEXT: [[TMP1:%.*]] = load float, ptr [[ARRAYIDX2]], align 4306; CHECK-NEXT: [[INC:%.*]] = fadd fast float [[TMP1]], 1.000000e+00307; CHECK-NEXT: store float [[INC]], ptr [[ARRAYIDX2]], align 4308; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1309; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]310; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_EXIT:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP12:![0-9]+]]311; CHECK: for.exit:312; CHECK-NEXT: ret void313;314entry:315 br label %for.body316 317for.body:318 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]319 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv320 %l.idx = load i32, ptr %gep.indices, align 4321 %idxprom1 = zext i32 %l.idx to i64322 %gep.bucket = getelementptr inbounds float, ptr %buckets, i64 %idxprom1323 %l.bucket = load float, ptr %gep.bucket, align 4324 %inc = fadd fast float %l.bucket, 1.0325 store float %inc, ptr %gep.bucket, align 4326 %iv.next = add nuw nsw i64 %iv, 1327 %exitcond = icmp eq i64 %iv.next, %N328 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4329 330for.exit:331 ret void332}333 334;; We don't support histograms with a update value that's not loop-invariant.335define void @histogram_varying_increment(ptr noalias %buckets, ptr readonly %indices, ptr readonly %incvals, i64 %N) #0 {336; CHECK-LABEL: define void @histogram_varying_increment(337; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], ptr readonly [[INCVALS:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {338; CHECK-NEXT: entry:339; CHECK-NEXT: br label [[FOR_BODY:%.*]]340; CHECK: for.body:341; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]342; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds nuw i32, ptr [[INDICES]], i64 [[IV]]343; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4344; CHECK-NEXT: [[IDXPROM1:%.*]] = zext i32 [[TMP0]] to i64345; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds nuw i32, ptr [[BUCKETS]], i64 [[IDXPROM1]]346; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4347; CHECK-NEXT: [[INCIDX:%.*]] = getelementptr inbounds nuw i32, ptr [[INCVALS]], i64 [[IV]]348; CHECK-NEXT: [[INCVAL:%.*]] = load i32, ptr [[INCIDX]], align 4349; CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP1]], [[INCVAL]]350; CHECK-NEXT: store i32 [[INC]], ptr [[ARRAYIDX2]], align 4351; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1352; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]353; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_EXIT:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP12]]354; CHECK: for.exit:355; CHECK-NEXT: ret void356;357entry:358 br label %for.body359 360for.body:361 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]362 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv363 %l.idx = load i32, ptr %gep.indices, align 4364 %idxprom1 = zext i32 %l.idx to i64365 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1366 %l.bucket = load i32, ptr %gep.bucket, align 4367 %gep.incvals = getelementptr inbounds i32, ptr %incvals, i64 %iv368 %l.incval = load i32, ptr %gep.incvals, align 4369 %inc = add nsw i32 %l.bucket, %l.incval370 store i32 %inc, ptr %gep.bucket, align 4371 %iv.next = add nuw nsw i64 %iv, 1372 %exitcond = icmp eq i64 %iv.next, %N373 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4374 375for.exit:376 ret void377}378 379;; Test that interleaving works when vectorizing.380define void @simple_histogram_user_interleave(ptr noalias %buckets, ptr readonly %indices, i64 %N) #0 {381; CHECK-LABEL: define void @simple_histogram_user_interleave(382; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {383; CHECK-NEXT: entry:384; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()385; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 3386; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP1]]387; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]388; CHECK: vector.ph:389; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64()390; CHECK-NEXT: [[TMP5:%.*]] = shl nuw nsw i64 [[TMP4]], 3391; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP5]]392; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]393; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]394; CHECK: vector.body:395; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]396; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]397; CHECK-NEXT: [[TMP15:%.*]] = call i64 @llvm.vscale.i64()398; CHECK-NEXT: [[DOTIDX:%.*]] = shl nuw nsw i64 [[TMP15]], 4399; CHECK-NEXT: [[TMP17:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP8]], i64 [[DOTIDX]]400; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP8]], align 4401; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP17]], align 4402; CHECK-NEXT: [[TMP9:%.*]] = zext <vscale x 4 x i32> [[WIDE_LOAD]] to <vscale x 4 x i64>403; CHECK-NEXT: [[TMP19:%.*]] = zext <vscale x 4 x i32> [[WIDE_LOAD1]] to <vscale x 4 x i64>404; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], <vscale x 4 x i64> [[TMP9]]405; CHECK-NEXT: [[TMP21:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], <vscale x 4 x i64> [[TMP19]]406; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP10]], i32 1, <vscale x 4 x i1> splat (i1 true))407; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP21]], i32 1, <vscale x 4 x i1> splat (i1 true))408; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP5]]409; CHECK-NEXT: [[TMP11:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]410; CHECK-NEXT: br i1 [[TMP11]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP14:![0-9]+]]411; CHECK: middle.block:412; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]413; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]414; CHECK: scalar.ph:415;416entry:417 br label %for.body418 419for.body:420 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]421 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv422 %l.idx = load i32, ptr %gep.indices, align 4423 %idxprom1 = zext i32 %l.idx to i64424 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1425 %l.bucket = load i32, ptr %gep.bucket, align 4426 %inc = add nsw i32 %l.bucket, 1427 store i32 %inc, ptr %gep.bucket, align 4428 %iv.next = add nuw nsw i64 %iv, 1429 %exitcond = icmp eq i64 %iv.next, %N430 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !0431 432for.exit:433 ret void434}435 436;; Test that we can handle more than one GEP index.437@idx_array = dso_local local_unnamed_addr global [1048576 x i32] zeroinitializer, align 4438@data_array = dso_local local_unnamed_addr global [1048576 x i32] zeroinitializer, align 4439 440define void @histogram_array_3op_gep(i64 noundef %N) #0 {441; CHECK-LABEL: define void @histogram_array_3op_gep(442; CHECK-SAME: i64 noundef [[N:%.*]]) #[[ATTR0]] {443; CHECK-NEXT: entry:444; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()445; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 2446; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP1]]447; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]448; CHECK: vector.ph:449; CHECK-NEXT: [[TMP3:%.*]] = call i64 @llvm.vscale.i64()450; CHECK-NEXT: [[TMP4:%.*]] = shl nuw nsw i64 [[TMP3]], 2451; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP4]]452; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]453; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]454; CHECK: vector.body:455; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]456; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i32, ptr @idx_array, i64 [[INDEX]]457; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP5]], align 4458; CHECK-NEXT: [[TMP14:%.*]] = sext <vscale x 4 x i32> [[WIDE_LOAD1]] to <vscale x 4 x i64>459; CHECK-NEXT: [[TMP11:%.*]] = getelementptr inbounds i32, ptr @data_array, <vscale x 4 x i64> [[TMP14]]460; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP11]], i32 1, <vscale x 4 x i1> splat (i1 true))461; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]462; CHECK-NEXT: [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]463; CHECK-NEXT: br i1 [[TMP8]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP16:![0-9]+]]464; CHECK: middle.block:465; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]466; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]467; CHECK: scalar.ph:468;469entry:470 br label %for.body471 472for.body:473 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]474 %gep.indices = getelementptr inbounds [1048576 x i32], ptr @idx_array, i64 0, i64 %iv475 %l.idx = load i32, ptr %gep.indices, align 4476 %idxprom5 = sext i32 %l.idx to i64477 %gep.bucket = getelementptr inbounds [1048576 x i32], ptr @data_array, i64 0, i64 %idxprom5478 %l.bucket = load i32, ptr %gep.bucket, align 4479 %inc = add nsw i32 %l.bucket, 1480 store i32 %inc, ptr %gep.bucket, align 4481 %iv.next = add nuw nsw i64 %iv, 1482 %exitcond = icmp eq i64 %iv.next, %N483 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4484 485for.exit:486 ret void487}488 489;; Add a struct into the mix, use a different constant index.490;; { unused, buckets }491%somestruct = type { [1048576 x i32], [1048576 x i32] }492 493define void @histogram_array_4op_gep_nonzero_const_idx(i64 noundef %N, ptr readonly %indices, ptr noalias %data.struct) #0 {494; CHECK-LABEL: define void @histogram_array_4op_gep_nonzero_const_idx(495; CHECK-SAME: i64 noundef [[N:%.*]], ptr readonly [[INDICES:%.*]], ptr noalias [[DATA_STRUCT:%.*]]) #[[ATTR0]] {496; CHECK-NEXT: entry:497; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()498; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 2499; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP1]]500; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[ENTRY:%.*]]501; CHECK: vector.ph:502; CHECK-NEXT: [[TMP3:%.*]] = call i64 @llvm.vscale.i64()503; CHECK-NEXT: [[TMP4:%.*]] = shl nuw nsw i64 [[TMP3]], 2504; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP4]]505; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]506; CHECK-NEXT: br label [[FOR_BODY:%.*]]507; CHECK: vector.body:508; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]509; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[IV]]510; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP5]], align 4511; CHECK-NEXT: [[TMP6:%.*]] = sext <vscale x 4 x i32> [[WIDE_LOAD]] to <vscale x 4 x i64>512; CHECK-NEXT: [[DOTSPLIT:%.*]] = getelementptr inbounds nuw i8, ptr [[DATA_STRUCT]], i64 8388608513; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, ptr [[DOTSPLIT]], <vscale x 4 x i64> [[TMP6]]514; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP7]], i32 1, <vscale x 4 x i1> splat (i1 true))515; CHECK-NEXT: [[IV_NEXT]] = add nuw i64 [[IV]], [[TMP4]]516; CHECK-NEXT: [[TMP8:%.*]] = icmp eq i64 [[IV_NEXT]], [[N_VEC]]517; CHECK-NEXT: br i1 [[TMP8]], label [[MIDDLE_BLOCK:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP18:![0-9]+]]518; CHECK: middle.block:519; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]520; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]521; CHECK: scalar.ph:522;523entry:524 br label %for.body525 526for.body:527 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]528 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv529 %l.idx = load i32, ptr %gep.indices, align 4530 %idxprom5 = sext i32 %l.idx to i64531 %gep.bucket = getelementptr inbounds %somestruct, ptr %data.struct, i32 1, i32 0, i64 %idxprom5532 %l.bucket = load i32, ptr %gep.bucket, align 4533 %inc = add nsw i32 %l.bucket, 1534 store i32 %inc, ptr %gep.bucket, align 4535 %iv.next = add nuw nsw i64 %iv, 1536 %exitcond = icmp eq i64 %iv.next, %N537 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4538 539for.exit:540 ret void541}542 543;; Make sure the histogram intrinsic uses the active lane mask when tail folding.544define void @simple_histogram_tailfold(ptr noalias %buckets, ptr readonly %indices, i64 %N) #0 {545; CHECK-LABEL: define void @simple_histogram_tailfold(546; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {547; CHECK-NEXT: entry:548; CHECK-NEXT: br label [[VECTOR_PH:%.*]]549; CHECK: vector.ph:550; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()551; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP2]], 2552; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64()553; CHECK-NEXT: [[TMP5:%.*]] = shl nuw nsw i64 [[TMP4]], 2554; CHECK-NEXT: [[TMP6:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[N]], i64 [[TMP5]])555; CHECK-NEXT: [[ACTIVE_LANE_MASK_ENTRY:%.*]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 0, i64 [[N]])556; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]557; CHECK: vector.body:558; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]559; CHECK-NEXT: [[ACTIVE_LANE_MASK:%.*]] = phi <vscale x 4 x i1> [ [[ACTIVE_LANE_MASK_ENTRY]], [[VECTOR_PH]] ], [ [[ACTIVE_LANE_MASK_NEXT:%.*]], [[VECTOR_BODY]] ]560; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]561; CHECK-NEXT: [[WIDE_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.masked.load.nxv4i32.p0(ptr align 4 [[TMP8]], <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> poison)562; CHECK-NEXT: [[TMP9:%.*]] = zext <vscale x 4 x i32> [[WIDE_LOAD]] to <vscale x 4 x i64>563; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], <vscale x 4 x i64> [[TMP9]]564; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP10]], i32 1, <vscale x 4 x i1> [[ACTIVE_LANE_MASK]])565; CHECK-NEXT: [[INDEX_NEXT]] = add i64 [[INDEX]], [[TMP1]]566; CHECK-NEXT: [[ACTIVE_LANE_MASK_NEXT]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 [[INDEX]], i64 [[TMP6]])567; CHECK-NEXT: [[TMP11:%.*]] = extractelement <vscale x 4 x i1> [[ACTIVE_LANE_MASK_NEXT]], i64 0568; CHECK-NEXT: br i1 [[TMP11]], label [[VECTOR_BODY]], label [[MIDDLE_BLOCK:%.*]], !llvm.loop [[LOOP20:![0-9]+]]569; CHECK: middle.block:570; CHECK-NEXT: br label [[FOR_EXIT:%.*]]571; CHECK: for.exit:572; CHECK-NEXT: ret void573;574entry:575 br label %for.body576 577for.body:578 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]579 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv580 %l.idx = load i32, ptr %gep.indices, align 4581 %idxprom1 = zext i32 %l.idx to i64582 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1583 %l.bucket = load i32, ptr %gep.bucket, align 4584 %inc = add nsw i32 %l.bucket, 1585 store i32 %inc, ptr %gep.bucket, align 4586 %iv.next = add nuw nsw i64 %iv, 1587 %exitcond = icmp eq i64 %iv.next, %N588 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !2589 590for.exit:591 ret void592}593 594;; Check that we can still vectorize a histogram when LAA found another dependency595;; that doesn't conflict with the buckets.596define void @simple_histogram_rtdepcheck(ptr noalias %buckets, ptr %array, ptr %indices, i64 %N) #0 {597; CHECK-LABEL: define void @simple_histogram_rtdepcheck(598; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr [[ARRAY:%.*]], ptr [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {599; CHECK-NEXT: entry:600; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()601; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 2602; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP1]], i64 8)603; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP2]]604; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]605; CHECK: vector.memcheck:606; CHECK-NEXT: [[ARRAY1:%.*]] = ptrtoint ptr [[ARRAY]] to i64607; CHECK-NEXT: [[INDICES2:%.*]] = ptrtoint ptr [[INDICES]] to i64608; CHECK-NEXT: [[TMP3:%.*]] = call i64 @llvm.vscale.i64()609; CHECK-NEXT: [[TMP4:%.*]] = shl nuw nsw i64 [[TMP3]], 4610; CHECK-NEXT: [[TMP5:%.*]] = sub i64 [[ARRAY1]], [[INDICES2]]611; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP5]], [[TMP4]]612; CHECK-NEXT: br i1 [[DIFF_CHECK]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]613; CHECK: vector.ph:614; CHECK-NEXT: [[TMP7:%.*]] = call i64 @llvm.vscale.i64()615; CHECK-NEXT: [[TMP8:%.*]] = shl nuw nsw i64 [[TMP7]], 2616; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP8]]617; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]618; CHECK-NEXT: [[TMP9:%.*]] = call <vscale x 4 x i32> @llvm.stepvector.nxv4i32()619; CHECK-NEXT: [[TMP11:%.*]] = trunc nuw nsw i64 [[TMP8]] to i32620; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP11]], i64 0621; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <vscale x 4 x i32> [[DOTSPLATINSERT]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer622; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]623; CHECK: vector.body:624; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]625; CHECK-NEXT: [[VEC_IND:%.*]] = phi <vscale x 4 x i32> [ [[TMP9]], [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]626; CHECK-NEXT: [[TMP12:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]627; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP12]], align 4628; CHECK-NEXT: [[TMP13:%.*]] = zext <vscale x 4 x i32> [[WIDE_LOAD]] to <vscale x 4 x i64>629; CHECK-NEXT: [[TMP14:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], <vscale x 4 x i64> [[TMP13]]630; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv4p0.i32(<vscale x 4 x ptr> [[TMP14]], i32 1, <vscale x 4 x i1> splat (i1 true))631; CHECK-NEXT: [[TMP15:%.*]] = getelementptr inbounds i32, ptr [[ARRAY]], i64 [[INDEX]]632; CHECK-NEXT: store <vscale x 4 x i32> [[VEC_IND]], ptr [[TMP15]], align 4633; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP8]]634; CHECK-NEXT: [[VEC_IND_NEXT]] = add <vscale x 4 x i32> [[VEC_IND]], [[DOTSPLAT]]635; CHECK-NEXT: [[TMP16:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]636; CHECK-NEXT: br i1 [[TMP16]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP21:![0-9]+]]637; CHECK: middle.block:638; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]639; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]640; CHECK: scalar.ph:641;642entry:643 br label %for.body644 645for.body:646 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]647 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv648 %l.idx = load i32, ptr %gep.indices, align 4649 %idxprom1 = zext i32 %l.idx to i64650 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1651 %l.bucket = load i32, ptr %gep.bucket, align 4652 %inc = add nsw i32 %l.bucket, 1653 store i32 %inc, ptr %gep.bucket, align 4654 %idx.addr = getelementptr inbounds i32, ptr %array, i64 %iv655 %iv.trunc = trunc i64 %iv to i32656 store i32 %iv.trunc, ptr %idx.addr, align 4657 %iv.next = add nuw nsw i64 %iv, 1658 %exitcond = icmp eq i64 %iv.next, %N659 br i1 %exitcond, label %for.exit, label %for.body660 661for.exit:662 ret void663}664 665;; Make sure we don't vectorize if there's a potential alias between buckets666;; and indices.667define void @simple_histogram_unsafe_alias(ptr %buckets, ptr %indices, i64 %N) #0 {668; CHECK-LABEL: define void @simple_histogram_unsafe_alias(669; CHECK-SAME: ptr [[BUCKETS:%.*]], ptr [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {670; CHECK-NEXT: entry:671; CHECK-NEXT: br label [[FOR_BODY:%.*]]672; CHECK: for.body:673; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]674; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds nuw i32, ptr [[INDICES]], i64 [[IV]]675; CHECK-NEXT: [[TMP12:%.*]] = load i32, ptr [[ARRAYIDX]], align 4676; CHECK-NEXT: [[IDXPROM1:%.*]] = zext i32 [[TMP12]] to i64677; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds nuw i32, ptr [[BUCKETS]], i64 [[IDXPROM1]]678; CHECK-NEXT: [[TMP13:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4679; CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP13]], 1680; CHECK-NEXT: store i32 [[INC]], ptr [[ARRAYIDX2]], align 4681; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1682; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]683; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_EXIT:%.*]], label [[FOR_BODY]]684; CHECK: for.exit:685; CHECK-NEXT: ret void686;687entry:688 br label %for.body689 690for.body:691 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]692 %gep.indices = getelementptr inbounds i32, ptr %indices, i64 %iv693 %l.idx = load i32, ptr %gep.indices, align 4694 %idxprom1 = zext i32 %l.idx to i64695 %gep.bucket = getelementptr inbounds i32, ptr %buckets, i64 %idxprom1696 %l.bucket = load i32, ptr %gep.bucket, align 4697 %inc = add nsw i32 %l.bucket, 1698 store i32 %inc, ptr %gep.bucket, align 4699 %iv.next = add nuw nsw i64 %iv, 1700 %exitcond = icmp eq i64 %iv.next, %N701 br i1 %exitcond, label %for.exit, label %for.body702 703for.exit:704 ret void705}706 707define void @simple_histogram_64b(ptr noalias %buckets, ptr readonly %indices, i64 %N) #0 {708; CHECK-LABEL: define void @simple_histogram_64b(709; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {710; CHECK-NEXT: entry:711; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()712; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 1713; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP1]]714; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]715; CHECK: vector.ph:716; CHECK-NEXT: [[TMP3:%.*]] = call i64 @llvm.vscale.i64()717; CHECK-NEXT: [[TMP4:%.*]] = shl nuw nsw i64 [[TMP3]], 1718; CHECK-NEXT: [[DOTNOT:%.*]] = sub nsw i64 0, [[TMP4]]719; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N]], [[DOTNOT]]720; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]721; CHECK: vector.body:722; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]723; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i64, ptr [[INDICES]], i64 [[INDEX]]724; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 2 x i64>, ptr [[TMP5]], align 4725; CHECK-NEXT: [[TMP6:%.*]] = getelementptr inbounds i64, ptr [[BUCKETS]], <vscale x 2 x i64> [[WIDE_LOAD]]726; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.nxv2p0.i64(<vscale x 2 x ptr> [[TMP6]], i64 1, <vscale x 2 x i1> splat (i1 true))727; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]728; CHECK-NEXT: [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]729; CHECK-NEXT: br i1 [[TMP7]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP23:![0-9]+]]730; CHECK: middle.block:731; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]732; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_EXIT:%.*]], label [[SCALAR_PH]]733; CHECK: scalar.ph:734;735entry:736 br label %for.body737 738for.body:739 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]740 %gep.indices = getelementptr inbounds i64, ptr %indices, i64 %iv741 %l.idx = load i64, ptr %gep.indices, align 4742 %gep.bucket = getelementptr inbounds i64, ptr %buckets, i64 %l.idx743 %l.bucket = load i64, ptr %gep.bucket, align 4744 %inc = add nsw i64 %l.bucket, 1745 store i64 %inc, ptr %gep.bucket, align 4746 %iv.next = add nuw nsw i64 %iv, 1747 %exitcond = icmp eq i64 %iv.next, %N748 br i1 %exitcond, label %for.exit, label %for.body, !llvm.loop !4749 750for.exit:751 ret void752}753 754; The histogram operation generates vectors. This example used to crash755; due to a missing entry in a switch statement.756define void @histogram_generates_vectors_crash(ptr %data_array, ptr noalias %indices) {757; CHECK-LABEL: define void @histogram_generates_vectors_crash(758; CHECK-SAME: ptr [[DATA_ARRAY:%.*]], ptr noalias [[INDICES:%.*]]) {759; CHECK-NEXT: entry:760; CHECK-NEXT: br label [[FOR_BODY:%.*]]761; CHECK: for.body:762; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]763; CHECK-NEXT: [[GEP_INDICES:%.*]] = getelementptr [1048576 x i32], ptr [[INDICES]], i64 [[IV]]764; CHECK-NEXT: [[L_IDX:%.*]] = load i32, ptr [[GEP_INDICES]], align 4765; CHECK-NEXT: [[IDXPROM5:%.*]] = sext i32 [[L_IDX]] to i64766; CHECK-NEXT: [[GEP_BUCKET:%.*]] = getelementptr [1048576 x i32], ptr [[DATA_ARRAY]], i64 [[IDXPROM5]]767; CHECK-NEXT: [[L_BUCKET:%.*]] = load i32, ptr [[GEP_BUCKET]], align 4768; CHECK-NEXT: [[INC:%.*]] = add i32 [[L_BUCKET]], 1769; CHECK-NEXT: store i32 [[INC]], ptr [[GEP_BUCKET]], align 4770; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1771; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[IV]], 1772; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_EXIT:%.*]], label [[FOR_BODY]]773; CHECK: for.exit:774; CHECK-NEXT: ret void775;776entry:777 br label %for.body778 779for.body:780 %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]781 %gep.indices = getelementptr [1048576 x i32], ptr %indices, i64 %iv782 %l.idx = load i32, ptr %gep.indices, align 4783 %idxprom5 = sext i32 %l.idx to i64784 %gep.bucket = getelementptr [1048576 x i32], ptr %data_array, i64 %idxprom5785 %l.bucket = load i32, ptr %gep.bucket, align 4786 %inc = add i32 %l.bucket, 1787 store i32 %inc, ptr %gep.bucket, align 4788 %iv.next = add i64 %iv, 1789 %exitcond = icmp eq i64 %iv, 1790 br i1 %exitcond, label %for.exit, label %for.body791 792for.exit:793 ret void794}795 796attributes #0 = { "target-features"="+sve2" vscale_range(1,16) }797 798!0 = distinct !{!0, !1}799!1 = !{!"llvm.loop.interleave.count", i32 2}800!2 = distinct !{!2, !3}801!3 = !{!"llvm.loop.vectorize.predicate.enable", i1 true}802!4 = distinct !{!4, !5}803!5 = !{!"llvm.loop.interleave.count", i32 1}804