223 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; RUN: opt < %s -passes=loop-vectorize,dce -force-vector-interleave=1 -force-vector-width=4 -S | FileCheck %s3 4target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"5target triple = "x86_64-unknown-linux-gnu"6 7; PR157948; incorrect addition of llvm.mem.parallel_loop_access metadata is undefined9; behaviour. Vectorizer ignores the memory dependency checks and goes ahead and10; vectorizes this loop with uniform stores which has an output dependency.11 12; void foo(int *a, int *b, int k, int m) {13; for (int i = 0; i < m; i++) {14; for (int j = 0; j < m; j++) {15; a[i] = a[i + j + k] + 1; <<<16; }17; b[i] = b[i] + 3;18; }19; }20 21; Function Attrs: nounwind uwtable22define void @foo(ptr nocapture %a, ptr nocapture %b, i32 %k, i32 %m) #0 {23; CHECK-LABEL: @foo(24; CHECK-NEXT: entry:25; CHECK-NEXT: [[CMP27:%.*]] = icmp sgt i32 [[M:%.*]], 026; CHECK-NEXT: br i1 [[CMP27]], label [[FOR_BODY3_LR_PH_US_PREHEADER:%.*]], label [[FOR_END15:%.*]]27; CHECK: for.body3.lr.ph.us.preheader:28; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[M]], -129; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[K:%.*]] to i6430; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[M]] to i6431; CHECK-NEXT: br label [[FOR_BODY3_LR_PH_US:%.*]]32; CHECK: for.end.us:33; CHECK-NEXT: [[ARRAYIDX9_US:%.*]] = getelementptr inbounds i32, ptr [[B:%.*]], i64 [[INDVARS_IV33:%.*]]34; CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[ARRAYIDX9_US]], align 4, !llvm.mem.parallel_loop_access [[META0:![0-9]+]]35; CHECK-NEXT: [[ADD10_US:%.*]] = add nsw i32 [[TMP3]], 336; CHECK-NEXT: store i32 [[ADD10_US]], ptr [[ARRAYIDX9_US]], align 4, !llvm.mem.parallel_loop_access [[META0]]37; CHECK-NEXT: [[INDVARS_IV_NEXT34:%.*]] = add i64 [[INDVARS_IV33]], 138; CHECK-NEXT: [[LFTR_WIDEIV35:%.*]] = trunc i64 [[INDVARS_IV_NEXT34]] to i3239; CHECK-NEXT: [[EXITCOND36:%.*]] = icmp eq i32 [[LFTR_WIDEIV35]], [[M]]40; CHECK-NEXT: br i1 [[EXITCOND36]], label [[FOR_END15_LOOPEXIT:%.*]], label [[FOR_BODY3_LR_PH_US]], !llvm.loop [[LOOP2:![0-9]+]]41; CHECK: for.body3.us:42; CHECK-NEXT: [[INDVARS_IV29:%.*]] = phi i64 [ [[BC_RESUME_VAL:%.*]], [[SCALAR_PH:%.*]] ], [ [[INDVARS_IV_NEXT30:%.*]], [[FOR_BODY3_US:%.*]] ]43; CHECK-NEXT: [[TMP4:%.*]] = trunc i64 [[INDVARS_IV29]] to i3244; CHECK-NEXT: [[ADD4_US:%.*]] = add i32 [[ADD_US:%.*]], [[TMP4]]45; CHECK-NEXT: [[IDXPROM_US:%.*]] = sext i32 [[ADD4_US]] to i6446; CHECK-NEXT: [[ARRAYIDX_US:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[IDXPROM_US]]47; CHECK-NEXT: [[TMP5:%.*]] = load i32, ptr [[ARRAYIDX_US]], align 4, !llvm.mem.parallel_loop_access [[META0]]48; CHECK-NEXT: [[ADD5_US:%.*]] = add nsw i32 [[TMP5]], 149; CHECK-NEXT: store i32 [[ADD5_US]], ptr [[ARRAYIDX7_US:%.*]], align 4, !llvm.mem.parallel_loop_access [[META0]]50; CHECK-NEXT: [[INDVARS_IV_NEXT30]] = add i64 [[INDVARS_IV29]], 151; CHECK-NEXT: [[LFTR_WIDEIV31:%.*]] = trunc i64 [[INDVARS_IV_NEXT30]] to i3252; CHECK-NEXT: [[EXITCOND32:%.*]] = icmp eq i32 [[LFTR_WIDEIV31]], [[M]]53; CHECK-NEXT: br i1 [[EXITCOND32]], label [[FOR_END_US:%.*]], label [[FOR_BODY3_US]], !llvm.loop [[LOOP3:![0-9]+]]54; CHECK: for.body3.lr.ph.us:55; CHECK-NEXT: [[INDVARS_IV33]] = phi i64 [ [[INDVARS_IV_NEXT34]], [[FOR_END_US]] ], [ 0, [[FOR_BODY3_LR_PH_US_PREHEADER]] ]56; CHECK-NEXT: [[TMP6:%.*]] = add i64 [[TMP1]], [[INDVARS_IV33]]57; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[TMP6]] to i3258; CHECK-NEXT: [[TMP8:%.*]] = trunc i64 [[INDVARS_IV33]] to i3259; CHECK-NEXT: [[ADD_US]] = add i32 [[TMP8]], [[K]]60; CHECK-NEXT: [[ARRAYIDX7_US]] = getelementptr inbounds i32, ptr [[A]], i64 [[INDVARS_IV33]]61; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP2]], 462; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH]], label [[VECTOR_SCEVCHECK:%.*]]63; CHECK: vector.scevcheck:64; CHECK-NEXT: [[TMP9:%.*]] = add i32 [[TMP7]], [[TMP0]]65; CHECK-NEXT: [[TMP10:%.*]] = icmp slt i32 [[TMP9]], [[TMP7]]66; CHECK-NEXT: br i1 [[TMP10]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]67; CHECK: vector.ph:68; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[TMP2]], 469; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[TMP2]], [[N_MOD_VF]]70; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]71; CHECK: vector.body:72; CHECK-NEXT: [[OFFSET_IDX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]73; CHECK-NEXT: [[TMP11:%.*]] = trunc i64 [[OFFSET_IDX]] to i3274; CHECK-NEXT: [[TMP13:%.*]] = add i32 [[ADD_US]], [[TMP11]]75; CHECK-NEXT: [[TMP14:%.*]] = sext i32 [[TMP13]] to i6476; CHECK-NEXT: [[TMP15:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[TMP14]]77; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i32>, ptr [[TMP15]], align 478; CHECK-NEXT: [[TMP17:%.*]] = add nsw <4 x i32> [[WIDE_LOAD]], splat (i32 1)79; CHECK-NEXT: [[TMP18:%.*]] = extractelement <4 x i32> [[TMP17]], i32 380; CHECK-NEXT: store i32 [[TMP18]], ptr [[ARRAYIDX7_US]], align 4, !llvm.mem.parallel_loop_access [[META0]]81; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[OFFSET_IDX]], 482; CHECK-NEXT: [[TMP19:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]83; CHECK-NEXT: br i1 [[TMP19]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]84; CHECK: middle.block:85; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[TMP2]], [[N_VEC]]86; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_END_US]], label [[SCALAR_PH]]87; CHECK: scalar.ph:88; CHECK-NEXT: [[BC_RESUME_VAL]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[FOR_BODY3_LR_PH_US]] ], [ 0, [[VECTOR_SCEVCHECK]] ]89; CHECK-NEXT: br label [[FOR_BODY3_US]]90; CHECK: for.end15.loopexit:91; CHECK-NEXT: br label [[FOR_END15]]92; CHECK: for.end15:93; CHECK-NEXT: ret void94;95entry:96 %cmp27 = icmp sgt i32 %m, 097 br i1 %cmp27, label %for.body3.lr.ph.us, label %for.end1598 99for.end.us: ; preds = %for.body3.us100 %arrayidx9.us = getelementptr inbounds i32, ptr %b, i64 %indvars.iv33101 %0 = load i32, ptr %arrayidx9.us, align 4, !llvm.mem.parallel_loop_access !3102 %add10.us = add nsw i32 %0, 3103 store i32 %add10.us, ptr %arrayidx9.us, align 4, !llvm.mem.parallel_loop_access !3104 %indvars.iv.next34 = add i64 %indvars.iv33, 1105 %lftr.wideiv35 = trunc i64 %indvars.iv.next34 to i32106 %exitcond36 = icmp eq i32 %lftr.wideiv35, %m107 br i1 %exitcond36, label %for.end15, label %for.body3.lr.ph.us, !llvm.loop !5108 109for.body3.us: ; preds = %for.body3.us, %for.body3.lr.ph.us110 %indvars.iv29 = phi i64 [ 0, %for.body3.lr.ph.us ], [ %indvars.iv.next30, %for.body3.us ]111 %1 = trunc i64 %indvars.iv29 to i32112 %add4.us = add i32 %add.us, %1113 %idxprom.us = sext i32 %add4.us to i64114 %arrayidx.us = getelementptr inbounds i32, ptr %a, i64 %idxprom.us115 %2 = load i32, ptr %arrayidx.us, align 4, !llvm.mem.parallel_loop_access !3116 %add5.us = add nsw i32 %2, 1117 store i32 %add5.us, ptr %arrayidx7.us, align 4, !llvm.mem.parallel_loop_access !3118 %indvars.iv.next30 = add i64 %indvars.iv29, 1119 %lftr.wideiv31 = trunc i64 %indvars.iv.next30 to i32120 %exitcond32 = icmp eq i32 %lftr.wideiv31, %m121 br i1 %exitcond32, label %for.end.us, label %for.body3.us, !llvm.loop !4122 123for.body3.lr.ph.us: ; preds = %for.end.us, %entry124 %indvars.iv33 = phi i64 [ %indvars.iv.next34, %for.end.us ], [ 0, %entry ]125 %3 = trunc i64 %indvars.iv33 to i32126 %add.us = add i32 %3, %k127 %arrayidx7.us = getelementptr inbounds i32, ptr %a, i64 %indvars.iv33128 br label %for.body3.us129 130for.end15: ; preds = %for.end.us, %entry131 ret void132}133 134; Same test as above, but without the invalid parallel_loop_access metadata.135 136; Here we can see the vectorizer does the mem dep checks and decides it is137; unsafe to vectorize.138define void @no-par-mem-metadata(ptr nocapture %a, ptr nocapture %b, i32 %k, i32 %m) #0 {139; CHECK-LABEL: @no-par-mem-metadata(140; CHECK-NEXT: entry:141; CHECK-NEXT: [[CMP27:%.*]] = icmp sgt i32 [[M:%.*]], 0142; CHECK-NEXT: br i1 [[CMP27]], label [[FOR_BODY3_LR_PH_US_PREHEADER:%.*]], label [[FOR_END15:%.*]]143; CHECK: for.body3.lr.ph.us.preheader:144; CHECK-NEXT: br label [[FOR_BODY3_LR_PH_US:%.*]]145; CHECK: for.end.us:146; CHECK-NEXT: [[ARRAYIDX9_US:%.*]] = getelementptr inbounds i32, ptr [[B:%.*]], i64 [[INDVARS_IV33:%.*]]147; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX9_US]], align 4148; CHECK-NEXT: [[ADD10_US:%.*]] = add nsw i32 [[TMP0]], 3149; CHECK-NEXT: store i32 [[ADD10_US]], ptr [[ARRAYIDX9_US]], align 4150; CHECK-NEXT: [[INDVARS_IV_NEXT34:%.*]] = add i64 [[INDVARS_IV33]], 1151; CHECK-NEXT: [[LFTR_WIDEIV35:%.*]] = trunc i64 [[INDVARS_IV_NEXT34]] to i32152; CHECK-NEXT: [[EXITCOND36:%.*]] = icmp eq i32 [[LFTR_WIDEIV35]], [[M]]153; CHECK-NEXT: br i1 [[EXITCOND36]], label [[FOR_END15_LOOPEXIT:%.*]], label [[FOR_BODY3_LR_PH_US]], !llvm.loop [[LOOP2]]154; CHECK: for.body3.us:155; CHECK-NEXT: [[INDVARS_IV29:%.*]] = phi i64 [ 0, [[FOR_BODY3_LR_PH_US]] ], [ [[INDVARS_IV_NEXT30:%.*]], [[FOR_BODY3_US:%.*]] ]156; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[INDVARS_IV29]] to i32157; CHECK-NEXT: [[ADD4_US:%.*]] = add i32 [[ADD_US:%.*]], [[TMP1]]158; CHECK-NEXT: [[IDXPROM_US:%.*]] = sext i32 [[ADD4_US]] to i64159; CHECK-NEXT: [[ARRAYIDX_US:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[IDXPROM_US]]160; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX_US]], align 4161; CHECK-NEXT: [[ADD5_US:%.*]] = add nsw i32 [[TMP2]], 1162; CHECK-NEXT: store i32 [[ADD5_US]], ptr [[ARRAYIDX7_US:%.*]], align 4163; CHECK-NEXT: [[INDVARS_IV_NEXT30]] = add i64 [[INDVARS_IV29]], 1164; CHECK-NEXT: [[LFTR_WIDEIV31:%.*]] = trunc i64 [[INDVARS_IV_NEXT30]] to i32165; CHECK-NEXT: [[EXITCOND32:%.*]] = icmp eq i32 [[LFTR_WIDEIV31]], [[M]]166; CHECK-NEXT: br i1 [[EXITCOND32]], label [[FOR_END_US:%.*]], label [[FOR_BODY3_US]], !llvm.loop [[LOOP1:![0-9]+]]167; CHECK: for.body3.lr.ph.us:168; CHECK-NEXT: [[INDVARS_IV33]] = phi i64 [ [[INDVARS_IV_NEXT34]], [[FOR_END_US]] ], [ 0, [[FOR_BODY3_LR_PH_US_PREHEADER]] ]169; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[INDVARS_IV33]] to i32170; CHECK-NEXT: [[ADD_US]] = add i32 [[TMP3]], [[K:%.*]]171; CHECK-NEXT: [[ARRAYIDX7_US]] = getelementptr inbounds i32, ptr [[A]], i64 [[INDVARS_IV33]]172; CHECK-NEXT: br label [[FOR_BODY3_US]]173; CHECK: for.end15.loopexit:174; CHECK-NEXT: br label [[FOR_END15]]175; CHECK: for.end15:176; CHECK-NEXT: ret void177;178entry:179 %cmp27 = icmp sgt i32 %m, 0180 br i1 %cmp27, label %for.body3.lr.ph.us, label %for.end15181 182for.end.us: ; preds = %for.body3.us183 %arrayidx9.us = getelementptr inbounds i32, ptr %b, i64 %indvars.iv33184 %0 = load i32, ptr %arrayidx9.us, align 4185 %add10.us = add nsw i32 %0, 3186 store i32 %add10.us, ptr %arrayidx9.us, align 4187 %indvars.iv.next34 = add i64 %indvars.iv33, 1188 %lftr.wideiv35 = trunc i64 %indvars.iv.next34 to i32189 %exitcond36 = icmp eq i32 %lftr.wideiv35, %m190 br i1 %exitcond36, label %for.end15, label %for.body3.lr.ph.us, !llvm.loop !5191 192for.body3.us: ; preds = %for.body3.us, %for.body3.lr.ph.us193 %indvars.iv29 = phi i64 [ 0, %for.body3.lr.ph.us ], [ %indvars.iv.next30, %for.body3.us ]194 %1 = trunc i64 %indvars.iv29 to i32195 %add4.us = add i32 %add.us, %1196 %idxprom.us = sext i32 %add4.us to i64197 %arrayidx.us = getelementptr inbounds i32, ptr %a, i64 %idxprom.us198 %2 = load i32, ptr %arrayidx.us, align 4199 %add5.us = add nsw i32 %2, 1200 store i32 %add5.us, ptr %arrayidx7.us, align 4201 %indvars.iv.next30 = add i64 %indvars.iv29, 1202 %lftr.wideiv31 = trunc i64 %indvars.iv.next30 to i32203 %exitcond32 = icmp eq i32 %lftr.wideiv31, %m204 br i1 %exitcond32, label %for.end.us, label %for.body3.us, !llvm.loop !4205 206for.body3.lr.ph.us: ; preds = %for.end.us, %entry207 %indvars.iv33 = phi i64 [ %indvars.iv.next34, %for.end.us ], [ 0, %entry ]208 %3 = trunc i64 %indvars.iv33 to i32209 %add.us = add i32 %3, %k210 %arrayidx7.us = getelementptr inbounds i32, ptr %a, i64 %indvars.iv33211 br label %for.body3.us212 213for.end15: ; preds = %for.end.us, %entry214 ret void215}216 217attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "use-soft-float"="false" }218 219!3 = !{!4, !5}220!4 = !{!4}221!5 = !{!5}222 223