From patchwork Tue Nov 28 08:29:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 170605 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3763745vqx; Tue, 28 Nov 2023 00:30:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IGVIw/pYwjHFKyNU1LFyodiejCeEKOX/Iaxt8NQNkMEFsTf35Vm923/bn3KxFU9TYhkTEi1 X-Received: by 2002:a05:6358:7212:b0:16e:4e29:44be with SMTP id h18-20020a056358721200b0016e4e2944bemr3460707rwa.30.1701160253214; Tue, 28 Nov 2023 00:30:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701160253; cv=pass; d=google.com; s=arc-20160816; b=B1m7oquEQXOKK99vLeMW/ijb8P6trdwvnwbGf8KGSXYm+kJju0cz+PUKqPJ1fg5AiP HTzrD/aIMj62CehH/41MpbqtVAB5Dsf4ZWk7k+wI13O0EPsz7KubF4k6E9jqhAJwYPBt LQXGXgmhc2pb1Md3LidmlN0/qeNeYKX9/XGRmfuTmdEdEoo/Ull6kejn1GlzoENkWLr/ zs7NbVM1bEP/YxHnWodpa30zzqmNnkjt/15Z/nwEKAjtmC5wuzSe4tYDB5rNgUPE+pCY b2SYDp8IqBtBH9zWD0xqLCk+U7ZtvWRBGsZYxRapV1ySMjiOUer6FcOkRbFkuA4XjuPJ uLIw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=Qc4enbIx2vGmHhMrXj8sCDnqIoxS84amR4BzYkMi/XQ=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=zbjOCDLCf6TvHoOpC/PLHmgGkUHwBFoibJDsRF1yTjdXZylx3f9OBmlg46FMg2h0Hc CK5YyyD4A4aR9BkRhE1pzDz/glDVmUlNOkNItGyZVpbYDjwQluox78AgjrqgRKyiC7ZO SzxLkH3mlXEAbVIVwy73CkWRvxOMQACLnTYoK61b2rfBmP9s0zCS9Jp3t4w9uGRmSuvT GdJcWC2ID2Kh4M0vAZbnep1swLtwFbShKzw4cqja3Gq0HzUV39CPOsG1310yVYJ6rZcP oWptkImiVKOtseMBsQi6cimgHz7EqlTu70oA60Q7BdttLl3xaxK6q/KN+Y3W43LrshgB nPLA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iw5oTGV+; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id jd14-20020a05622a718e00b00423c16ee9e1si4077951qtb.662.2023.11.28.00.30.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 00:30:53 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iw5oTGV+; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 003A63857B96 for ; Tue, 28 Nov 2023 08:30:53 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 9E77D3857437 for ; Tue, 28 Nov 2023 08:29:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9E77D3857437 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9E77D3857437 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701160188; cv=none; b=pLXbDxSKZsXte7YVKWQP8+1M+Pugd+/JIOm1FDXZmX//CcL/RhAyCUORknMwzwfEX7zrTdv6/Pry2kZKDlO5Oj8HkcJAdpny2AZMht+8nKM6RACWUy0+HT05JXlseNcckhSaE2wkWnPUIJyN84my0mSVtQ8wA2hHtN/PtDdFXS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701160188; c=relaxed/simple; bh=vo6aJyjrI6Esu0WhcZbJoWfGaokFMxYtTF6HE1DRoW8=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=hCKNcq7AGKd3MS287PLxXcEyp3jp+XTU9bqt4W2VF6hoPbRBWB9nXlNlrc/1/9A832nD/8a8WOT5icpRA13j980wrN+iyembnq/UEhV9IevaqiDOscb5S2QbhCwuBj3702XT7BTujIJsiqnSFMtzI4jBdP8ypMhWMA6IJ1n/55k= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701160186; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=Qc4enbIx2vGmHhMrXj8sCDnqIoxS84amR4BzYkMi/XQ=; b=iw5oTGV+gHUmfHqSVbdSxtY3Gs7TpiyU4yZ4CHhlqJKpOtywYyIpkrQaf8ViwWKbPrTQUp g2LXNRprB5xq80NJ0KjotQE5ZqXm9EWDlPlF1jBd8+bwOOdKCRMHo1Nt2nibzhgrSTVVnC ptkX43H2jEtimQ4Dph8XAFM5lMA9RzQ= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-391-QSwdpYkXMomLivhC7kCpCw-1; Tue, 28 Nov 2023 03:29:44 -0500 X-MC-Unique: QSwdpYkXMomLivhC7kCpCw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6E9C21C0512B; Tue, 28 Nov 2023 08:29:44 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.53]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 32D3140C6EBB; Tue, 28 Nov 2023 08:29:44 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3AS8Tf0I487768 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 28 Nov 2023 09:29:42 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3AS8TfaL487767; Tue, 28 Nov 2023 09:29:41 +0100 Date: Tue, 28 Nov 2023 09:29:40 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Fix popcount (X) + popcount (Y) simplification [PR112719] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783795813879749931 X-GMAIL-MSGID: 1783795813879749931 Hi! Since my PR112566 r14-5557 changes the following testcase ICEs, because .POPCOUNT (x) + .POPCOUNT (y) has a simplification attempted even when x and y have incompatible types (different precisions). Note, with _BitInt it can ICE already starting with r14-5435 and I think as a latent problem it exists for years, because IFN_POPCOUNT calls inherently can have different argument types and return type is always the same. The following patch fixes it by using widest_int during the analysis (which is where it was ICEing) and if it is optimizable, casting to the wider type so that bit_ior has matching argument types. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-11-28 Jakub Jelinek PR tree-optimization/112719 * match.pd (popcount (X) + popcount (Y) -> POPCOUNT (X | Y)): Deal with argument types with different precisions. Jakub --- gcc/match.pd.jj 2023-11-24 11:32:22.161777182 +0100 +++ gcc/match.pd 2023-11-27 10:43:54.857068074 +0100 @@ -8723,8 +8723,13 @@ (define_operator_list SYNC_FETCH_AND_AND (simplify (plus (POPCOUNT:s @0) (POPCOUNT:s @1)) (if (INTEGRAL_TYPE_P (type) - && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0) - (POPCOUNT (bit_ior @0 @1)))) + && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED), + widest_int::from (tree_nonzero_bits (@1), UNSIGNED)) + == 0)) + (with { tree utype = TREE_TYPE (@0); + if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1))) + utype = TREE_TYPE (@1); } + (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1)))))) /* popcount(X) == 0 is X == 0, and related (in)equalities. */ (for popcount (POPCOUNT) --- gcc/testsuite/gcc.dg/pr112719.c.jj 2023-11-27 10:35:44.428911015 +0100 +++ gcc/testsuite/gcc.dg/pr112719.c 2023-11-27 10:35:27.262153103 +0100 @@ -0,0 +1,18 @@ +/* PR tree-optimization/112719 */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ +/* { dg-additional-options "-msse4" { target i?86-*-* x86_64-*-* } } */ + +int +foo (unsigned int a, unsigned short b) +{ + return __builtin_popcountl (a) + __builtin_popcountl (b); +} + +int +bar (unsigned int a, unsigned short b) +{ + a &= 0xaaaaaaaaUL; + b &= 0x5555; + return __builtin_popcountll (a) + __builtin_popcountll (b); +}