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: 80877 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9A37B3857720 for ; Tue, 28 Nov 2023 08:30:45 +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+patchwork=sourceware.org@gcc.gnu.org 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); +}