From patchwork Sat Mar 11 01:27:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 67951 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp64833wrd; Fri, 10 Mar 2023 17:28:50 -0800 (PST) X-Google-Smtp-Source: AK7set+srzOt9mGhtWxbC2kLXe5RYYNMk7FVwluVeP41cF9xRsbDbcltnM0rF/tE1giNOBG7U42j X-Received: by 2002:a17:907:c284:b0:913:ff28:59bd with SMTP id tk4-20020a170907c28400b00913ff2859bdmr16971105ejc.52.1678498130104; Fri, 10 Mar 2023 17:28:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678498130; cv=none; d=google.com; s=arc-20160816; b=OacHXOusnkQa2zJ4oM3V2Pp1bI6sm300m0BPc+7K9YkY0C/3qhqu0QjO3rXcvLYxlR 8mU3Wc2Q9o1EEeffhBwH8NfjxtlLtBkFCtOSTzNhSL2qoeL9Gq1hcWZZNfgAIgs+Wc+A U3LkFwAWR9qV3bGWprS2xjo7IMFcKFpng/vGdyue4Onoyfz8tlyPPaqHG56Od7wSYiSo Mo0UN6S0x42O8cW7zMrGyJZOaDXtPF4bhFMvJ/M9pzhrWQbxvOMIAlRIYLEEpIo6+F2e b/rGO6FwERGOZcQfsjN3fYVcnUEgAd1+B0Dq3WD6TzJbX5pCcFFDbqXrYQr2YnK/55Jy gv1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=7gRLQMDtPYcgW5Xe+SQsy0I9ROuSOBqBeNoOcYSyJDg=; b=ds/ce7h86z+5ItZS88Nrv6TDjo6GlY6zHhxxzsOu7+UWj8bDbBGjuwiNsaP+V3mcm1 UNse6raKz6R27uhVL5ulRy/d7V1RCP5Fu9uQgF5WHY8+HrodDFr0G2pMpgjxlnHbqZFj 8y3QtTDhRoaGQcNDrJHwR0RIH6aGOKCtso22KUDSVo3vaqzAKq0lnntJCrt9bbvHh8eR f6WSOQK2/RxuLixaewu56NytHLkeW0RvyB8plbPfiRu66qml+pOpKawuGhIU1+patUrA LzDJ83BeYa34pRXsrX3f2h/jfHuK6O74DvH6XjmJajDAGIEXnmlFhB23x/MIk/15uY3f QkJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=a4Vkw45K; 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 d25-20020a170906345900b0091d9b16e932si1003032ejb.972.2023.03.10.17.28.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 17:28:50 -0800 (PST) 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=a4Vkw45K; 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 DFA9E3858004 for ; Sat, 11 Mar 2023 01:28:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DFA9E3858004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678498128; bh=7gRLQMDtPYcgW5Xe+SQsy0I9ROuSOBqBeNoOcYSyJDg=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=a4Vkw45KoeiXqlHcx9VshQxsWliX0V7SMVjdEmNJsFr+8OPIHb9C6GK/7uSSjstCH oAgHMkMsbxE5Ebu+MN1rUsJYWxmX6sMq35aaUyZc9EhEQeZDNcCek78uPW28nsTK69 g8QJ4dc/3pUgQAkfMc/Qn/DgnrwMC0Ze7KPY7H+w= 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 B2DD03858D1E for ; Sat, 11 Mar 2023 01:28:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B2DD03858D1E 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 32ANgJFu007315 for ; Fri, 10 Mar 2023 17:28:01 -0800 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3p80k7cqf8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 10 Mar 2023 17:28:01 -0800 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.42; Fri, 10 Mar 2023 17:27:59 -0800 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.42 via Frontend Transport; Fri, 10 Mar 2023 17:27:58 -0800 Received: from vpnclient.wrightpinski.org.com (unknown [10.76.242.80]) by maili.marvell.com (Postfix) with ESMTP id 708953F704D; Fri, 10 Mar 2023 17:27:58 -0800 (PST) To: CC: Andrew Pinski Subject: [COMMITTED/12] Fix PR 105532: match.pd patterns calling tree_nonzero_bits with vector types Date: Fri, 10 Mar 2023 17:27:49 -0800 Message-ID: <20230311012749.2949900-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: 8CPhev1JKZxfyJGgndj1NgHKkqsg8Wzq X-Proofpoint-ORIG-GUID: 8CPhev1JKZxfyJGgndj1NgHKkqsg8Wzq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-10_10,2023-03-10_01,2023-02-09_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: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: 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?1760032855166642708?= X-GMAIL-MSGID: =?utf-8?q?1760032855166642708?= 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. Committed after a 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. (cherry picked from commit 193fccaa5c3525e979a989835c47c76d2c49d10c) --- 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 ef352af1572..fc2833bbdca 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1268,7 +1268,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)))))) #endif @@ -7169,7 +7170,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. */ @@ -7201,15 +7203,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)); +}