From patchwork Wed Nov 2 21:46:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 14493 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp158931wru; Wed, 2 Nov 2022 14:47:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6USxiNeklRbRM7RLc/0I1lGdZ2bIHUTmPnglieG9SRLeCwfGmEZMQisXBQXBmT+Z2jvNAF X-Received: by 2002:a50:ec99:0:b0:462:2c1c:8764 with SMTP id e25-20020a50ec99000000b004622c1c8764mr26630002edr.325.1667425658965; Wed, 02 Nov 2022 14:47:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667425658; cv=none; d=google.com; s=arc-20160816; b=BV+EEzsiLrXTBHsP8bNBvgXw1b2SCciQCd4mkGJ0XFToHL9aSbC6VIvdr6Gl9HfFvI w9/8+p8jkheVEAnBNfHqnC9/n4nkt+fOlzf+3inJUwVfztlq/90TjGOB49P1VQnPC2xX 5+oNLZ6M+/JkTvqcVqGLk0iVsbLbnkir8/O+++6zRxuwQ7CCHbxWWEwrLwRRPSBY5dN8 yBYWWlm4aiWdxDwiT8Lf6QHGhQly4S/ATLzemQq2gU2Df1jz4ODRI9X4jBq775iPUN1M wz1g9nEj0u6jgs1gYhg7DhOGoHwHebZea50KYl6XhUb8xoHYkfzUE4QypqCGrEo/aE7G FSDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:references:in-reply-to:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=VSzl6r9SECoJPe75bxMmQieETJNTtUJscdIrpN3DFYQ=; b=huz8+OOiYQYdgeu4pMQ4hOy8u9M0V9/U5KLW57pXAsTRhq7jRd8lPRpB43or5DAoRU MISJRLcJ/ONS0AsusIoqAjT9rnEYWQnbkIfVh/2ytDloqpammIRyu7Ek/okirOzoV74n vJT46JSeYxCJAKWt57SxHnYvbEXwgPYnAuTSjno42PTruwa0x7X/YccN95bWO1IzKzly mT3cyIUfwXcRq0ALtngH2DdjebeSsvEDbzFdeGcZ0JyXZLzM7fwC8FQTML6evINjAOzv CkT8G9zs5kDx8g4FfVWx6pEBajP7k/nP1T6adAi0Mw2JIB3ouL5QHPR+B8KxEoSCwj/g DzLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=mHF3ab7r; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id wt5-20020a170906ee8500b007aa5e6c4b5asi17931714ejb.128.2022.11.02.14.47.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Nov 2022 14:47:38 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=mHF3ab7r; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F22573856DD2 for ; Wed, 2 Nov 2022 21:47:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F22573856DD2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667425647; bh=VSzl6r9SECoJPe75bxMmQieETJNTtUJscdIrpN3DFYQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=mHF3ab7rCQ6vQMPHlQvtAFbKrRtFsnVEPocnVdx0VCU1kLGywMh/sB8IsskvbiTw2 TTHcTEEtpc+JQJx2iMt6QmHnJIsE3QJmhatcBOEz12hU0QWdXU+WX4x9/A9IyIBtpP Bl8+Z3So/nI8nHBSqYIbKNN1/MQzwR0AJzmpdqGk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id EF09638582BF for ; Wed, 2 Nov 2022 21:46:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EF09638582BF Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2A2Hbe6r027052 for ; Wed, 2 Nov 2022 14:46:42 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3kkw3y0w5s-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 02 Nov 2022 14:46:42 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Wed, 2 Nov 2022 14:46:40 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.18 via Frontend Transport; Wed, 2 Nov 2022 14:46:40 -0700 Received: from linux.marvell.com (unknown [10.69.242.198]) by maili.marvell.com (Postfix) with ESMTP id 0FAEC3F7040; Wed, 2 Nov 2022 14:46:40 -0700 (PDT) To: Subject: [PATCH 1/2] Fix PR 105532: match.pd patterns calling tree_nonzero_bits with vector types Date: Wed, 2 Nov 2022 14:46:34 -0700 Message-ID: <1667425595-2654-2-git-send-email-apinski@marvell.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1667425595-2654-1-git-send-email-apinski@marvell.com> References: <1667425595-2654-1-git-send-email-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-GUID: Zi6IdgUoLVQAgaqnSrdgiUnpO9Ml8uK4 X-Proofpoint-ORIG-GUID: Zi6IdgUoLVQAgaqnSrdgiUnpO9Ml8uK4 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-02_15,2022-11-02_01,2022-06-22_01 X-Spam-Status: No, score=-14.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: apinski--- via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: apinski@marvell.com Cc: Andrew Pinski Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748422528070747703?= X-GMAIL-MSGID: =?utf-8?q?1748422528070747703?= 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. 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 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)); +} From patchwork Wed Nov 2 21:46:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 14494 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp159314wru; Wed, 2 Nov 2022 14:48:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Vldk1mfX7wezTkZ4EW6NDhm/TyuIvkm2rfhdOZBj8xJ3bPFeU5ia9kTZGmUeePhymUv0x X-Received: by 2002:a05:6402:3896:b0:45c:93c3:3569 with SMTP id fd22-20020a056402389600b0045c93c33569mr27314627edb.37.1667425731696; Wed, 02 Nov 2022 14:48:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667425731; cv=none; d=google.com; s=arc-20160816; b=vEle+6taZIF9plpAFq4CKwSLZVUGxKAxSsCx/pbSDY3Ypq0iuRFYGoW//8BVeIV6aL aPZjXiU+wHVKfMUJm8PcH2Q+Kr26oAA3kxyjTyzg9Hl/AhHuyiAMtkCTsx1ZcyrxBCvt UGF0n0ng4GnBSpTXMnCugH8MUzNf5zOIwf6qyVM5ZP9nd19PVXnbxnaM3xzUJUkdELTI FQNAqp2cGmkwmnPKWKSP/JnQxvyLkx6Ro4285gSIj48RbmgP600u29d4lDLb38HzKxji ft5NBarX2kb1PWAGvOiXDlw2V6Loce3u/wazWxGp5eewpbI4WBVVAcdZ27Be6ABZ1guK sasQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:references:in-reply-to:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=StuS/AKSCLpkjdSpzzBHMdOQajNW7R9lxDmm2ogfSJE=; b=bXOjXX9zjOL0Fl7iENrxl5W3pJ1PkXRO1sxzdwdRxsJTmcjSTPY7fHYwfB5Y7ftsOa WbwVMazF6Ksss5RNCWUUuPceuODDtyhHkmZs3k3bFCNEFuOldsbgmAto5E4WFelIKS5N XRMiIBUJgBl1ILCzSgAWd1s/lm49uNhcPlejdlJC9Fda5dyJdi14M0xIUs112XWajwgM PyZ14G0pH/hrac5v8inJHMpIV5G+DJnChjXRf9jYimw7ucCltYbqo/tWZCBbJgvYgdFl SQMjLI10GLKH0kqvFZtJI/aSuK+JK+RaVt0QAZkdK3K6BkDUG+wQWYF5C5Q6DU9DGkxq MAAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Apqfr+vX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t16-20020a056402241000b004534c7d4ebfsi17221620eda.434.2022.11.02.14.48.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Nov 2022 14:48:51 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Apqfr+vX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C3FB53856969 for ; Wed, 2 Nov 2022 21:48:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C3FB53856969 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667425706; bh=StuS/AKSCLpkjdSpzzBHMdOQajNW7R9lxDmm2ogfSJE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Apqfr+vXlvn9uKfhRD5Tu7XHhLxwIdtD5ysfXzGUppzjuEBVWzBsqJYFJd1cD64AK 8BBRvnOmRNHruoZq5WMHv+Zd39IAivKxdz0F/rhW7n6Wic5X4p7QOU5Hd7TbDT1MsI egt7heUDyM599XTuU9FyK3bhyrKs6t+zeGV4RWhM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 5B9DB3857356 for ; Wed, 2 Nov 2022 21:46:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5B9DB3857356 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2A2Hbe6s027052 for ; Wed, 2 Nov 2022 14:46:43 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3kkw3y0w5s-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 02 Nov 2022 14:46:42 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Wed, 2 Nov 2022 14:46:40 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Wed, 2 Nov 2022 14:46:40 -0700 Received: from linux.marvell.com (unknown [10.69.242.198]) by maili.marvell.com (Postfix) with ESMTP id 771D53F7066; Wed, 2 Nov 2022 14:46:40 -0700 (PDT) To: Subject: [PATCH 2/2] Add assert for type on tree_nonzero_bits Date: Wed, 2 Nov 2022 14:46:35 -0700 Message-ID: <1667425595-2654-3-git-send-email-apinski@marvell.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1667425595-2654-1-git-send-email-apinski@marvell.com> References: <1667425595-2654-1-git-send-email-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-GUID: FuUSulIoqxxfBzSbBCaoK7KvdYSLSK6F X-Proofpoint-ORIG-GUID: FuUSulIoqxxfBzSbBCaoK7KvdYSLSK6F X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-02_15,2022-11-02_01,2022-06-22_01 X-Spam-Status: No, score=-14.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: apinski--- via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: apinski@marvell.com Cc: Andrew Pinski Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748422604163591837?= X-GMAIL-MSGID: =?utf-8?q?1748422604163591837?= From: Andrew Pinski Right now anyone could call tree_nonzero_bits with either complex or vector types and this will return the wrong thing. So just assert that nobody calls it with this. OK? Bootstrapped and tested with no regressions on x86_64-linux-gnu. gcc/ChangeLog: * fold-const.cc (tree_nonzero_bits): Add assert. --- gcc/fold-const.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 7e1ea58518b..3ccac9b28df 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -16567,6 +16567,9 @@ c_getstr (tree str) wide_int tree_nonzero_bits (const_tree t) { + gcc_assert (TREE_CODE (TREE_TYPE (t)) != VECTOR_TYPE + && TREE_CODE (TREE_TYPE (t)) != COMPLEX_TYPE); + switch (TREE_CODE (t)) { case INTEGER_CST: