From patchwork Tue Oct 3 17:37:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 148015 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp2246253vqb; Tue, 3 Oct 2023 10:38:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG1kZZqHxsQG657C7C57QB1nkwoF5ow8DhcoQ7eHoyGm0DyNZVrZRwrlc+WdBTE2fUTwFIs X-Received: by 2002:a17:906:8a73:b0:9b2:ba65:db21 with SMTP id hy19-20020a1709068a7300b009b2ba65db21mr14142678ejc.45.1696354718164; Tue, 03 Oct 2023 10:38:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696354718; cv=none; d=google.com; s=arc-20160816; b=UhOFdRGWYmakc8Yd0vaT0Puekm0e9zbLwyY7U4bCX7u34K+KD4He/2V4cv2E6igdt7 CPWXcn4jT9o1DojGeRX+hIUbOtST0zzDjS5a/ZZ73JE05hHUKPRCvaR2D+zRc/utTCW/ X1YPew/B20O34pWgCPrhPrUTLCRsC7A/FToernw0jC99+lD3WC7+O+46Y9khcGK/Vqw+ 45tBkcHUX6L0tQNam/7fA1jr3uBPHaohcgLAHGaCJgdQi6ay8eeeI/6fgRtLaf/Ueeu1 cLkD/Hp9cE9oR4r5rKH7qLx93vKRB/aMBbCJZ4JS0L5mBxyY191NQremkpuRndT5DHgR 0h+Q== ARC-Message-Signature: i=1; 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 :dmarc-filter:delivered-to; bh=wZvkhHaIofWsRG802nBxD7A0RLew/09zG8C/pxlhI0E=; fh=yUb5n+SQyRJ2LPzcwKKr0ljoW/9jUIKYohZMxsDWu7w=; b=ZwGWzTeGjqk+wLbIGoPXhprDcITs7fX7E+W3H36gd0dGRmsf82X8vsdgHtrvE6buFr CcU/D/SiQZr493CAYeQStheaYXt28m2y2xPhqcE0XpHHiLW+0Uhq8bNcT3Gu6BItweCM M/UnDUgH6izeD354xXUosIS0/cphOs2RHiAt2R9i7233j/fsYggEKFSwrdnBahhOUW9N fRHlcEXiJetQEDNGIS4R4CyXx+ETJ0EhW7HvBrq2kf2QKaGeSjZJC3yeliYKEaAA5rXK Fsf1qDKupmj5GBsC24QqFPvcNQKGoEp7JHgHyqRXqBmtVIQ1UMCMENGCo11px1HwnNFV aRpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OPWXUXzL; 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=redhat.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h11-20020a170906854b00b00994b9b22346si844322ejy.887.2023.10.03.10.38.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 10:38: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=@redhat.com header.s=mimecast20190719 header.b=OPWXUXzL; 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 689C0385DC1D for ; Tue, 3 Oct 2023 17:38:31 +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 289E13858D28 for ; Tue, 3 Oct 2023 17:38:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 289E13858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696354685; 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=wZvkhHaIofWsRG802nBxD7A0RLew/09zG8C/pxlhI0E=; b=OPWXUXzLcyPS9m6xvWyvpizmQetu/mPjV9SgAUp1CKkzOncucbx81k9OjPkDmLWBGsOIlh 4HHYzrqxgXKNAJ703MXaLzTctzW9wgQ0YaVPJRpJSyc3YphjL2q+8Euc99e48tzzovU+OO QQMHcDZYGZzoNRHFSLzkujEeZK2AKio= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-171-ZJPR7TgsOIq8kOkF2Bgu_g-1; Tue, 03 Oct 2023 13:38:02 -0400 X-MC-Unique: ZJPR7TgsOIq8kOkF2Bgu_g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 14CB929ABA3B; Tue, 3 Oct 2023 17:38:02 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.202]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C84442026D4B; Tue, 3 Oct 2023 17:38:01 +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 393Hbwvo1788031 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 3 Oct 2023 19:37:59 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 393Hbveg1788030; Tue, 3 Oct 2023 19:37:57 +0200 Date: Tue, 3 Oct 2023 19:37:57 +0200 From: Jakub Jelinek To: Richard Biener , Andrew Pinski Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Fix up a ? cst1 : cst2 regression on signed bool [PR111668] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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 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: 1778756844974397380 X-GMAIL-MSGID: 1778756844974397380 Hi! My relatively recent changes to these simplifiers to avoid doing build_nonstandard_integer_type (primarily for BITINT_TYPE) broke PR111668, a recurrence of the PR110487 bug. I thought the build_nonstandard_integer_type isn't ever needed there, but there is one special case where it is. For the a ? -1 : 0 and a ? 0 : -1 simplifications there are actually 3 different cases. One is for signed 1-bit precision types (signed kind of implied from integer_all_onesp, because otherwise it would match integer_onep earlier), where the simplifier wierdly was matching them using the a ? powerof2cst : 0 -> a << (log2(powerof2cst)) simplification and then another simplifier optimizing away the left shift when log2(powerof2cst) was 0. Another one is signed BOOLEAN_TYPE with precision > 1, where indeed we shouldn't be doing the negation in type, because it isn't well defined in that type, the type only has 2 valid values, 0 and -1. As an alternative, we could also e.g. cast to signed 1-bit precision BOOLEAN_TYPE and then extend to type. And the last case is what we were doing for types which have both 1 and -1 (all all ones) as valid values (i.e. all signed/unsigned ENUMERAL_TYPEs, INTEGRAL_TYPEs and BITINT_TYPEs with precision > 1). The following patch avoids the hops through << 0 for 1-bit precision and uses build_nonstandard_integer_type solely for the BOOLEAN_TYPE types (where we have a guarantee the precision is reasonably small, nothing ought to be created 129+ bit precision BOOLEAN_TYPEs). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-10-03 Jakub Jelinek PR tree-optimization/111668 * match.pd (a ? CST1 : CST2): Handle the a ? -1 : 0 and a ? 0 : -1 cases before the powerof2cst cases and differentiate between 1-bit precision types, larger precision boolean types and other integral types. Fix comment pastos and formatting. Jakub --- gcc/match.pd.jj 2023-10-02 09:42:01.657836005 +0200 +++ gcc/match.pd 2023-10-03 10:33:30.817614648 +0200 @@ -5100,36 +5100,53 @@ (define_operator_list SYNC_FETCH_AND_AND (switch (if (integer_zerop (@2)) (switch - /* a ? 1 : 0 -> a if 0 and 1 are integral types. */ + /* a ? 1 : 0 -> a if 0 and 1 are integral types. */ (if (integer_onep (@1)) (convert (convert:boolean_type_node @0))) + /* a ? -1 : 0 -> -a. */ + (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1)) + (if (TYPE_PRECISION (type) == 1) + /* For signed 1-bit precision just cast bool to the type. */ + (convert (convert:boolean_type_node @0)) + (if (TREE_CODE (type) == BOOLEAN_TYPE) + (with { + tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type), + TYPE_UNSIGNED (type)); + } + (convert (negate (convert:intt (convert:boolean_type_node @0))))) + (negate (convert:type (convert:boolean_type_node @0)))))) /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */ (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1)) (with { tree shift = build_int_cst (integer_type_node, tree_log2 (@1)); } - (lshift (convert (convert:boolean_type_node @0)) { shift; }))) - /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1 - here as the powerof2cst case above will handle that case correctly. */ - (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1)) - (negate (convert:type (convert:boolean_type_node @0)))))) + (lshift (convert (convert:boolean_type_node @0)) { shift; }))))) (if (integer_zerop (@1)) (switch - /* a ? 0 : 1 -> !a. */ + /* a ? 0 : 1 -> !a. */ (if (integer_onep (@2)) (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))) - /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */ + /* a ? 0 : -1 -> -(!a). */ + (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2)) + (if (TYPE_PRECISION (type) == 1) + /* For signed 1-bit precision just cast bool to the type. */ + (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })) + (if (TREE_CODE (type) == BOOLEAN_TYPE) + (with { + tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type), + TYPE_UNSIGNED (type)); + } + (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0) + { boolean_true_node; }))))) + (negate (convert:type (bit_xor (convert:boolean_type_node @0) + { boolean_true_node; })))))) + /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */ (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2)) (with { tree shift = build_int_cst (integer_type_node, tree_log2 (@2)); } (lshift (convert (bit_xor (convert:boolean_type_node @0) - { boolean_true_node; })) { shift; }))) - /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1 - here as the powerof2cst case above will handle that case correctly. */ - (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2)) - (negate (convert:type (bit_xor (convert:boolean_type_node @0) - { boolean_true_node; })))))))) + { boolean_true_node; })) { shift; }))))))) /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1) for unsigned types. */