From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id B003E3858421 for ; Tue, 20 Dec 2022 13:45:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B003E3858421 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12d.google.com with SMTP id f34so3453581lfv.10 for ; Tue, 20 Dec 2022 05:45:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=dLMqXCqhH0cMBObQ43oFeaidRUkGsx8NVjNDr8H8fj0=; b=TqkldXsXGPvSFDbOvK8hyab/aSbS+NMc+SY3OAa/SqnPTAyhDH59GERpG+rKPN5d18 tx6fe8X8e7F6AEaBgOgG87uQb7KNbs+B7+qHkcANazWjC+cfAdHCxl016Ub9F9kRkWM4 uw7a4MYirBcgvrvic9ZtUI3/c1hVV9/5YvvHrkmbOvijfls96gomdEoNEqTMs/fKuRLb bU/ULBvhK5VFYhl0Ex6clskr2WTbJePxWoNO/jMolR/xbNECXIU0VVohR81Ayw86Bnvn V3hFgAV61hHZccEnKs7P21B0j1BAAgPx60uHa1FbAXOxdS7Krrnw69YkoAV3jO22BoTi qG1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dLMqXCqhH0cMBObQ43oFeaidRUkGsx8NVjNDr8H8fj0=; b=qpQzPGs4qDDwl+fXibzmt7K0hCDw+vSfIFEvBg6JxbQpA28fqvmTC8r4gaW23OQNER Wm79WuX+YI/xYClOIvbEuN3ZpRiXlgHM8Mnh69vvilCLrTqrzKfG16TmD5NFwL5M5xxK DNQZU9pXqhvkJPesSNpcUOoJfYW9RT/1wk9MnLdnGC/QY8tZekmFiDq9ybFRVcl80+sn Eql+fIi7bGPAb6Xo29Z1d3km+bShETH/JQHAVkRVetbQCfFNUjOLXCGTZJhIJxkfdO3m 9slYelcGXGpzZCmJ5jf4FrOfQbJQA7TTayf39wmwLFkWXnMmkYJS2QO6I9rJ+JMEeQSW lDiA== X-Gm-Message-State: ANoB5pkTKZFKJFXe6PwgQrSL9+6dAmdApZsODIurPH7iJ9w4TrYFRmhu zTLbFa8IWbvizC24tpTrPvDEiPyUf46QVdE82+U= X-Google-Smtp-Source: AA0mqf7v+5LNPweMv4RpuhibDB2ODATDL5Qb2idvqB/iqaOiOgS93WkwwCi+UPkhm1+NI1WsODwjtFpUF1BIQLtTTLk= X-Received: by 2002:a05:6512:38b2:b0:4b3:a022:2176 with SMTP id o18-20020a05651238b200b004b3a0222176mr36162651lft.395.1671543937902; Tue, 20 Dec 2022 05:45:37 -0800 (PST) MIME-Version: 1.0 References: <1667425595-2654-1-git-send-email-apinski@marvell.com> <1667425595-2654-2-git-send-email-apinski@marvell.com> In-Reply-To: From: Richard Biener Date: Tue, 20 Dec 2022 14:45:25 +0100 Message-ID: Subject: Re: [PATCH 1/2] Fix PR 105532: match.pd patterns calling tree_nonzero_bits with vector types To: apinski@marvell.com Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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: On Sat, Nov 5, 2022 at 12:44 PM Richard Biener wrote: > > On Wed, Nov 2, 2022 at 10:47 PM apinski--- via Gcc-patches > wrote: > > > > From: Andrew Pinski > > > > Even though this PR was reported with an ubsan issue, the problem is > > tree_nonzero_bits is being called with an expression which is a vector type. > > It seems to me the semantics > for vectors should be clear but the users didn't expect that result? > > > This fixes three patterns I noticed which does that. > > And adds a testcase for one of the patterns. > > > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions > > OK. You didn't push this yet? > > gcc/ChangeLog: > > > > PR tree-optimization/105532 > > * match.pd (~(X >> Y) -> ~X >> Y): Check if it is an integral > > type before calling tree_nonzero_bits. > > (popcount(X) + popcount(Y)): Likewise. > > (popcount(X&C1)): Likewise. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.c-torture/compile/vector-shift-1.c: New test. > > --- > > gcc/match.pd | 25 +++++++++++-------- > > .../gcc.c-torture/compile/vector-shift-1.c | 8 ++++++ > > 2 files changed, 22 insertions(+), 11 deletions(-) > > create mode 100644 gcc/testsuite/gcc.c-torture/compile/vector-shift-1.c > > > > diff --git a/gcc/match.pd b/gcc/match.pd > > index 194ba8f5188..5833e05a926 100644 > > --- a/gcc/match.pd > > +++ b/gcc/match.pd > > @@ -1371,7 +1371,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > /* For logical right shifts, this is possible only if @0 doesn't > > have MSB set and the logical right shift is changed into > > arithmetic shift. */ > > - (if (!wi::neg_p (tree_nonzero_bits (@0))) > > + (if (INTEGRAL_TYPE_P (type) > > + && !wi::neg_p (tree_nonzero_bits (@0))) > > (with { tree stype = signed_type_for (TREE_TYPE (@0)); } > > (convert (rshift (bit_not! (convert:stype @0)) @1)))))) > > > > @@ -7518,7 +7519,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */ > > (simplify > > (plus (POPCOUNT:s @0) (POPCOUNT:s @1)) > > - (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0) > > + (if (INTEGRAL_TYPE_P (type) > > + && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0) > > (POPCOUNT (bit_ior @0 @1)))) > > > > /* popcount(X) == 0 is X == 0, and related (in)equalities. */ > > @@ -7550,15 +7552,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > (for pfun (POPCOUNT PARITY) > > (simplify > > (pfun @0) > > - (with { wide_int nz = tree_nonzero_bits (@0); } > > - (switch > > - (if (nz == 1) > > - (convert @0)) > > - (if (wi::popcount (nz) == 1) > > - (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > > - (convert (rshift:utype (convert:utype @0) > > - { build_int_cst (integer_type_node, > > - wi::ctz (nz)); })))))))) > > + (if (INTEGRAL_TYPE_P (type)) > > + (with { wide_int nz = tree_nonzero_bits (@0); } > > + (switch > > + (if (nz == 1) > > + (convert @0)) > > + (if (wi::popcount (nz) == 1) > > + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > > + (convert (rshift:utype (convert:utype @0) > > + { build_int_cst (integer_type_node, > > + wi::ctz (nz)); }))))))))) > > > > #if GIMPLE > > /* 64- and 32-bits branchless implementations of popcount are detected: > > diff --git a/gcc/testsuite/gcc.c-torture/compile/vector-shift-1.c b/gcc/testsuite/gcc.c-torture/compile/vector-shift-1.c > > new file mode 100644 > > index 00000000000..142ea56d5bb > > --- /dev/null > > +++ b/gcc/testsuite/gcc.c-torture/compile/vector-shift-1.c > > @@ -0,0 +1,8 @@ > > +typedef unsigned char __attribute__((__vector_size__ (1))) U; > > + > > +U > > +foo (U u) > > +{ > > + u = u == u; > > + return (~(u >> 255)); > > +} > > -- > > 2.17.1 > >