From patchwork Tue Nov 28 08:36:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 170618 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3766114vqx; Tue, 28 Nov 2023 00:36:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXRCFRumMX9Xucj/QE+yxG/tcg2fRC2/lrZ0ZbKBr3ONWlFM4CHLI5Yzl1WJZROcr7vFfU X-Received: by 2002:ac8:5710:0:b0:418:1fa1:4be3 with SMTP id 16-20020ac85710000000b004181fa14be3mr16832798qtw.47.1701160598830; Tue, 28 Nov 2023 00:36:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701160598; cv=pass; d=google.com; s=arc-20160816; b=sAigC8H+x+U6TcKmavJAB8simCILLa+BjZOLBfhQ3D7e1C6zsw8UZ56rBs5jM9+yJq jEgbqKzZ74gU5b4xBLts11yVkIFl12ss/vzrB2XRdmwzGyLYvulzwb/NIlMQGJ7dOGor eQ95ZABWoyMAGS8Gf02tujn7uSWGQunX8Y7KMmRzwfrUJ9BZqRDwT2wC27w66xiHdaai mzDt8XbWJ3AxDveo1fjG8+NPXUU767PNylLPYMOlyMEvjyq9+uM1TRny3515D/i4OryH 0tpsjo9LEzfB+rVi3Poj07Fbv0Rfhto7LL7K3Vh57I1BF44P6IgMNuc8dHn0m6B28AZr kUCA== 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=GwZNS2GLZgUqz/E0FuKspq5C+EhLYi1IbnXm5LK2mcI=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=Ks+CvQySwvBl/18UVBZaxMVsO0o2SXnTpxTxJPenctfcRlQ/l84ctu8evNUNp1G34p SvJ2syMIDDl7B6lOhUYl+bosaXPKKAR6qhAQ5R06nND6lr0WNFeKNBPy8i2kT0bfDzKm PEKi9hoe//6+iag99kZist+syVMHrFCUdMnRH+zXaoh0ReVQOwsTuNhwU4IPL4rFzBey pfEUc4XwYXktU4DQdj4wUaFT7aeyam+72xn27vTOVGQZj4v2FWRQMYHNVO9Wxc/XZdK7 NmqvFddKt8GUgZeBi/7uMNudxpo/WvW/t+1KfX6r+/X+X+on0sT1wokzctteA7EyJP0K n8vw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SrUoaSvq; 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 c17-20020a05622a059100b00423a5a04647si6859418qtb.467.2023.11.28.00.36.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 00:36:38 -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=SrUoaSvq; 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 8C4E5385AC2C for ; Tue, 28 Nov 2023 08:36:38 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 57BB13858D37 for ; Tue, 28 Nov 2023 08:36:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 57BB13858D37 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 57BB13858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701160573; cv=none; b=IBY718+r3VDVvTCzEXc2Lamc6kKpiaVjhDrcNK62EJXxNPaOOCl5EIQxJO7D1KSyQx0v7QUgIMcFfNlmt3oLBru7/7ouYwqQmWXb25skF1vfDFqOSOOK3IMPH8LkAAM9orz8n/yTBGvZX3WIUM6fLghsae+azUdXs+UhW13NWJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701160573; c=relaxed/simple; bh=eflBKs0eri81hQ08Jd9wYgHidnuQsxLVFnXxQr5y7bo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=IDptwfxtT2ZgGEWdXOjufp6sI6LwLsUh8BWeSy7a59uToU2axLwETpInIMU3FWJorENxooHl/gc3G+CWFoeppmaMim2u3g+JxVPBIITpE6zzOErXs3dUQm7ERFclzsxwqtd6YktZGdt+yAJ3V6PaNjNWdvaBZFSnTvUlYzPMqFg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701160571; 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=GwZNS2GLZgUqz/E0FuKspq5C+EhLYi1IbnXm5LK2mcI=; b=SrUoaSvqsByRrFQhKnSdd1MTfq60yDMywZJgIsCyn9T3ayqLHTdSN0TSg5OEtkRCOZdp10 Yf8abQ2lHuZ1xm/EQQH/227CTrZnz4eCbHPiT5HcwsBFv9XQDHQIMX/1qXLerJwncMG08N K97RU5WCFC8wAbDRgqCIxpd+YK6wFYc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-204-jIUJpmq3Ns-FiaQQucfzNw-1; Tue, 28 Nov 2023 03:36:09 -0500 X-MC-Unique: jIUJpmq3Ns-FiaQQucfzNw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 C8D34101A529; Tue, 28 Nov 2023 08:36:08 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.53]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8CB5B1C060B0; Tue, 28 Nov 2023 08:36:08 +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 3AS8a6MM487784 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 28 Nov 2023 09:36:06 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3AS8a5xp487783; Tue, 28 Nov 2023 09:36:05 +0100 Date: Tue, 28 Nov 2023 09:36:05 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Fix parity (X) ^ parity (Y) simplification [PR112719] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 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_H3, 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: 1783796176023442374 X-GMAIL-MSGID: 1783796176023442374 Hi! When looking around, I've noticed we have a similar simplification for parity (with ^ rather than +). Note, unlike the popcount one, this one doesn't check for INTEGRAL_TYPE_P (type) (which rules out vector simplification), so I've used the old handling for types_match and otherwise do it only for scalar argument types and handle different precision in there. The testcase ICEs without the previous patch on the first function, but strangely not on the second which tests parity. The reason is that in this case there is no wi::bit_and test like for popcount and for BITINT_TYPEs build_call_internal actually refuses to create it and thus the whole simplification fails. While .{CLZ,CTZ,CLRSB,FFS,POPCOUNT,PARITY} ifns are direct optab ifns for normal integer and vector types (and thus it is desirable to punt if there is no supported optab for them), they have this large/huge _BitInt extension before bitint lowering, so the patch also adjusts build_call_internal to allow that case. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-11-28 Jakub Jelinek PR tree-optimization/112719 * match.pd (parity(X)^parity(Y) -> parity(X^Y)): Handle case of mismatched types. * gimple-match-exports.cc (build_call_internal): Add special-case for bit query ifns on large/huge BITINT_TYPE before bitint lowering. * gcc.dg/bitint-43.c: New test. Jakub --- gcc/match.pd.jj 2023-11-27 10:43:54.857068074 +0100 +++ gcc/match.pd 2023-11-27 12:02:29.372447640 +0100 @@ -8873,7 +8873,14 @@ (define_operator_list SYNC_FETCH_AND_AND /* parity(X)^parity(Y) is parity(X^Y). */ (simplify (bit_xor (PARITY:s @0) (PARITY:s @1)) - (PARITY (bit_xor @0 @1))) + (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))) + (PARITY (bit_xor @0 @1)) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && INTEGRAL_TYPE_P (TREE_TYPE (@1))) + (with { tree utype = TREE_TYPE (@0); + if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1))) + utype = TREE_TYPE (@1); } + (PARITY (bit_xor (convert:utype @0) (convert:utype @1))))))) #if GIMPLE /* parity(zext(X)) == parity(X). */ --- gcc/gimple-match-exports.cc.jj 2023-11-07 08:32:01.726254366 +0100 +++ gcc/gimple-match-exports.cc 2023-11-27 12:31:06.273397492 +0100 @@ -236,7 +236,30 @@ build_call_internal (internal_fn fn, gim tree_pair types = direct_internal_fn_types (fn, res_op->type, res_op->ops); if (!direct_internal_fn_supported_p (fn, types, OPTIMIZE_FOR_BOTH)) - return NULL; + { + switch (fn) + { + case IFN_CLZ: + case IFN_CTZ: + case IFN_CLRSB: + case IFN_FFS: + case IFN_POPCOUNT: + case IFN_PARITY: + /* For these 6 builtins large/huge _BitInt operand is ok + before bitint lowering pass. */ + if (res_op->num_ops >= 1 + && TREE_CODE (TREE_TYPE (res_op->ops[0])) == BITINT_TYPE + && (TYPE_PRECISION (TREE_TYPE (res_op->ops[0])) + > MAX_FIXED_MODE_SIZE) + && cfun + && (cfun->curr_properties & PROP_gimple_lbitint) == 0) + break; + return NULL; + + default: + return NULL; + } + } } return gimple_build_call_internal (fn, res_op->num_ops, res_op->op_or_null (0), --- gcc/testsuite/gcc.dg/bitint-43.c.jj 2023-11-27 12:32:18.043392501 +0100 +++ gcc/testsuite/gcc.dg/bitint-43.c 2023-11-27 12:32:58.677823505 +0100 @@ -0,0 +1,19 @@ +/* PR tree-optimization/112719 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-O2" } */ + +#if __BITINT_MAXWIDTH__ >= 252 +int +foo (unsigned _BitInt(239) x, unsigned _BitInt(252) y) +{ + x &= 0x2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaauwb; + y &= 0x555555555555555555555555555555555555555555555555555555555555555uwb; + return __builtin_popcountg (x) + __builtin_popcountg (y); +} + +int +bar (unsigned _BitInt(239) x, unsigned _BitInt(252) y) +{ + return __builtin_parityg (x) ^ __builtin_parityg (y); +} +#endif