From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id DFDC53858D37 for ; Wed, 28 Jun 2023 12:33:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DFDC53858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 1B7472189F for ; Wed, 28 Jun 2023 12:33:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1687955639; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=+/VXvN1qxAzXt3otExelkzZ0yM7sRriIkf51BkKJCAg=; b=mySXUeH2+Ad8Rfb5kuS4aJH9GqIr2+wdeXFY1lmuwMMOPv0KSEyL2rfXwb2xrzUepvUKHN SEjh94zWHcgVmZkd/mtZ56wzR+48478LktuGIP2WjYUSq9cLZtMyrrEqyfJ9mg8yz0wVeT /llX+Dfn27bv0J61mKM+/49ccSbtSDc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1687955639; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=+/VXvN1qxAzXt3otExelkzZ0yM7sRriIkf51BkKJCAg=; b=LY7SvJvZl+3mBdAopdDAPnhTPEgxAxqe9H4i8XjoTRNXdTQc5fYT4cwuiciCB6lneMquEa skuq4FkTELWt33BA== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 112EB2C308 for ; Wed, 28 Jun 2023 12:33:59 +0000 (UTC) Date: Wed, 28 Jun 2023 12:33:59 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/110451 - hoist invariant compare after interchange User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,MISSING_MID,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Message-ID: <20230628123359.Hj_vgrFT0nYIWZGunae8UvUExidDIyY4hEBjjmOKCvU@z> The following adjusts the cost model of invariant motion to consider [VEC_]COND_EXPRs and comparisons producing a data value as expensive. For 503.bwaves_r this avoids an unnecessarily high vectorization factor because of an integer comparison besides data operations on double. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/110451 * tree-ssa-loop-im.cc (stmt_cost): [VEC_]COND_EXPR and tcc_comparison are expensive. * gfortran.dg/vect/pr110451.f: New testcase. --- gcc/testsuite/gfortran.dg/vect/pr110451.f | 51 +++++++++++++++++++++++ gcc/tree-ssa-loop-im.cc | 11 ++++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/vect/pr110451.f diff --git a/gcc/testsuite/gfortran.dg/vect/pr110451.f b/gcc/testsuite/gfortran.dg/vect/pr110451.f new file mode 100644 index 00000000000..ba77b0dd174 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/vect/pr110451.f @@ -0,0 +1,51 @@ +! { dg-do compile } +! { dg-require-effective-target vect_condition } +! { dg-require-effective-target vect_double } +! { dg-additional-options "-ffast-math -floop-interchange -fdump-tree-linterchange-details -fdump-tree-vect-details" } +! { dg-additional-options "-mprefer-vector-width=128" { target x86_64-*-* i?86-*-* } } + + subroutine mat_times_vec(y,x,a,axp,ayp,azp,axm,aym,azm, + $ nb,nx,ny,nz) + implicit none + integer nb,nx,ny,nz,i,j,k,m,l,kit,im1,ip1,jm1,jp1,km1,kp1 + + real*8 y(nb,nx,ny,nz),x(nb,nx,ny,nz) + + real*8 a(nb,nb,nx,ny,nz), + 1 axp(nb,nb,nx,ny,nz),ayp(nb,nb,nx,ny,nz),azp(nb,nb,nx,ny,nz), + 2 axm(nb,nb,nx,ny,nz),aym(nb,nb,nx,ny,nz),azm(nb,nb,nx,ny,nz) + + + do k=1,nz + km1=mod(k+nz-2,nz)+1 + kp1=mod(k,nz)+1 + do j=1,ny + jm1=mod(j+ny-2,ny)+1 + jp1=mod(j,ny)+1 + do i=1,nx + im1=mod(i+nx-2,nx)+1 + ip1=mod(i,nx)+1 + do l=1,nb + y(l,i,j,k)=0.0d0 + do m=1,nb + y(l,i,j,k)=y(l,i,j,k)+ + 1 a(l,m,i,j,k)*x(m,i,j,k)+ + 2 axp(l,m,i,j,k)*x(m,ip1,j,k)+ + 3 ayp(l,m,i,j,k)*x(m,i,jp1,k)+ + 4 azp(l,m,i,j,k)*x(m,i,j,kp1)+ + 5 axm(l,m,i,j,k)*x(m,im1,j,k)+ + 6 aym(l,m,i,j,k)*x(m,i,jm1,k)+ + 7 azm(l,m,i,j,k)*x(m,i,j,km1) + enddo + enddo + enddo + enddo + enddo + return + end + +! loop interchange adds a conditional on m != 1 in the innermost loop +! verify that is hoisted and thus not affecting the vectorization factor + +! { dg-final { scan-tree-dump-times "is interchanged" 1 "linterchange" } } +! { dg-final { scan-tree-dump "vectorization factor = 2" "vect" { target x86_64-*-* i?86-*-* } } } diff --git a/gcc/tree-ssa-loop-im.cc b/gcc/tree-ssa-loop-im.cc index 86ce6acb023..f5b01e986ae 100644 --- a/gcc/tree-ssa-loop-im.cc +++ b/gcc/tree-ssa-loop-im.cc @@ -617,7 +617,8 @@ stmt_cost (gimple *stmt) if (gimple_code (stmt) != GIMPLE_ASSIGN) return 1; - switch (gimple_assign_rhs_code (stmt)) + enum tree_code code = gimple_assign_rhs_code (stmt); + switch (code) { case MULT_EXPR: case WIDEN_MULT_EXPR: @@ -645,6 +646,11 @@ stmt_cost (gimple *stmt) /* Shifts and rotates are usually expensive. */ return LIM_EXPENSIVE; + case COND_EXPR: + case VEC_COND_EXPR: + /* Conditionals are expensive. */ + return LIM_EXPENSIVE; + case CONSTRUCTOR: /* Make vector construction cost proportional to the number of elements. */ @@ -658,6 +664,9 @@ stmt_cost (gimple *stmt) return 0; default: + /* Comparisons are usually expensive. */ + if (TREE_CODE_CLASS (code) == tcc_comparison) + return LIM_EXPENSIVE; return 1; } } -- 2.35.3