When vectorizing the body of an if-converted loop with BB vectorization
we can end up vectorizing masked stores.  But the code tracking whether
a loop used masked stores failed to check we're doing loop
vectorization.

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

        PR tree-optimization/123741
        * tree-vect-stmts.cc (vectorizable_store): Add missing check
        on loop vectorization.

        * gfortran.dg/vect/vect-pr123741.f90: New testcase.
---
 .../gfortran.dg/vect/vect-pr123741.f90        | 50 +++++++++++++++++++
 gcc/tree-vect-stmts.cc                        |  2 +-
 2 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/vect/vect-pr123741.f90

diff --git a/gcc/testsuite/gfortran.dg/vect/vect-pr123741.f90 
b/gcc/testsuite/gfortran.dg/vect/vect-pr123741.f90
new file mode 100644
index 00000000000..5288ea6df63
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/vect/vect-pr123741.f90
@@ -0,0 +1,50 @@
+! { dg-do compile }
+! { dg-additional-options "-Ofast" }
+! { dg-additional-options "-mavx2" { target avx2 } }
+
+module m1
+parameter( n1=5,n2=5,n3=5,n4=5 )
+integer:: p(n1,n2,n3,n4)
+
+contains
+subroutine s1(i,nn2,nn4)
+integer:: i(n1,n2,n3,n4)
+i= p
+forall (k2=1:nn2,k4=1:nn4,mod(k2,3)/=0 .and. mod(k4,3)/=0) 
+where( mod(i(:,k2,:,k4),4) /=0 ) &
+ i(:,k2,:,k4) = i(:,k2,:,k4) + 1.0 / real(k2+k4 -1)
+end forall
+end subroutine
+
+subroutine s2(i,nn2,nn4)
+integer:: i(n1,n2,n3,n4)
+integer,allocatable:: temp(:,:,:,:)
+i= p
+allocate(temp(n1,n2,n3,n4))
+do k4=1,nn4
+do k2=1,nn2
+if (mod(k2,3)/=0 .and. mod(k4,3)/=0)then
+where( mod(i(:,k2,:,k4),4) /=0 ) &
+ temp(:,k2,:,k4) = i(:,k2,:,k4) +1.0 / real(k2+k4 -1)
+end if
+end do
+end do
+do k4=1,nn4
+do k2=1,nn2
+if (mod(k2,3)/=0 .and. mod(k4,3)/=0)then
+where( mod(i(:,k2,:,k4),4) /=0 ) &
+ i(:,k2,:,k4) = temp(:,k2,:,k4)
+end if
+end do
+end do
+end subroutine
+end
+
+use m1
+integer:: i1(n1,n2,n3,n4),i2(n1,n2,n3,n4)
+p= reshape([(k,k=1,(n1)*(n2)*(n3)*(n4))],[n1,n2,n3,n4])
+call s1(i1,n2,n4)
+call s2(i2,n2,n4)
+if (any(i1/=i2)) print *,101
+print *,'OK'
+end
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index 5fc115e1a17..234a3aa4f4b 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -8694,7 +8694,7 @@ vectorizable_store (vec_info *vinfo,
                                            memory_access_type, loop_lens);
     }
 
-  if (mask_node && !costing_p)
+  if (loop_vinfo && mask_node && !costing_p)
     LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
 
   /* In case the vectorization factor (VF) is bigger than the number
-- 
2.51.0

Reply via email to