From patchwork Tue Oct 3 17:48:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 148016 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp2251557vqb; Tue, 3 Oct 2023 10:49:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEYscadSAvOkhNPtpURq0ov8pQV5hnNOl52DJzpuXnppm1CvW8/yJRc9RhpSFpj9SYgyxJO X-Received: by 2002:a17:906:23ea:b0:9b2:ccd8:2d2c with SMTP id j10-20020a17090623ea00b009b2ccd82d2cmr11878452ejg.34.1696355356200; Tue, 03 Oct 2023 10:49:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696355356; cv=none; d=google.com; s=arc-20160816; b=B6OE0vmywH0hmujTTFYiUGaAbWfxw1XD+pH9Zn7OtvFjRLvIl7K04Ldg1+gyeJZsfp Exr15l/U7csrKAf/iWB8WZ1eMUpM8P1+XbhMwhiKikothHRyk+q2oVen6nnda9WgstoL EbVFPhWVrjgtF9CjiIsRUqp9ljJuiQPYVMk8K/rWMBDrsg1xRy7ddwGnSqH+LqGXwmbi UeC3eZN+xg+0iCJuu1wurjlasVHSfKRI2CfbYGqKA1bRDVzOhZBLCIqwg8mJeDjgyB54 QhI5JdZgXUCb8mvBeDTPezFoF57TnRhbJ6JyPlT1WMQyxkFCRHGYKnitmGk3DXMbmueS k4oQ== 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:in-reply-to :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:dmarc-filter:delivered-to; bh=dGTjKZMixOwB7/urcxwmE74NIQ9Wx/phH3HC2zwVNRo=; fh=yUb5n+SQyRJ2LPzcwKKr0ljoW/9jUIKYohZMxsDWu7w=; b=o3w87kZMEOZsRtmJAUh8Xh7nUO41EvqkSj/A4T0sNOcDRJK5FTUP1DWNCxjsHFOT5R qTkNH0GZiJM17EDYkidpfnrhN380fhkee3hJLXeuUMdy29m2z0b1O14Zi/jz2ec1veqA cRisj1nm5zcKXaiZYMueSwvbtYcQIG1BDRergjzPPNOOjtiV4R2c7s1aw6Qolz1ex0WU gQQkvqPSXuyP712d1u17LkzGA9D8dwNaBT4kLSkO75QQ3NKRuo1k7QBzhxF0JlTn6jAj 5GlaQHW2hkvCd/VkLiDWR82lKIJnh5DU/ukRZ0H5kkANovy0AdnUPiTWQKItAnJShK7D eFTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RNDyIvnt; 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 u21-20020a170906951500b0099bd5a1b111si803116ejx.411.2023.10.03.10.49.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 10:49:16 -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=RNDyIvnt; 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 AD6023858421 for ; Tue, 3 Oct 2023 17:48:58 +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 09E883858D28 for ; Tue, 3 Oct 2023 17:48:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 09E883858D28 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=1696355311; 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:in-reply-to:in-reply-to: references:references; bh=dGTjKZMixOwB7/urcxwmE74NIQ9Wx/phH3HC2zwVNRo=; b=RNDyIvnts4eRyzU0Nl5vHYTOipN95RfcBF8x29mgXwHSS3T99rKzbfe74KVvwNwWQrS8n1 e8DY3/VZNcZLNdlyZF/yX3Kk5Z2+r04YGn8omDW8j9kfv6C+uf69fJztHV8LTOmXks6D2u bzN/U44nS+tT1zHqD9UOR787T2XHI/E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-562-JAySnXUPNFOvRICbx-rLnw-1; Tue, 03 Oct 2023 13:48:18 -0400 X-MC-Unique: JAySnXUPNFOvRICbx-rLnw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 EF47185A5BA; Tue, 3 Oct 2023 17:48:17 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.202]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AF81F51E3; Tue, 3 Oct 2023 17:48:17 +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 393HmEV61788049 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 3 Oct 2023 19:48:15 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 393HmDgJ1788048; Tue, 3 Oct 2023 19:48:13 +0200 Date: Tue, 3 Oct 2023 19:48:13 +0200 From: Jakub Jelinek To: Richard Biener , Andrew Pinski Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Avoid other build_nonstandard_integer_type calls [PR111369] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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 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: 1778757514224883027 X-GMAIL-MSGID: 1778757514224883027 Hi! On Sat, Sep 30, 2023 at 11:57:38AM +0200, Jakub Jelinek wrote: > > This fixes PR111369, where one of the bitint*.c tests FAILs with > > GCC_TEST_RUN_EXPENSIVE=1. > > Though, I think there is an preexisting issue which the > build_nonstandard_integer_type didn't help with; if type is signed 1-bit > precision, then I think a ? ~t : t could be valid, but -(type)a would invoke > UB if a is 1 - the cast would make it -1 and negation of -1 in signed 1-bit > invokes UB. > So perhaps we should guard this optimization on type having element precision > 1 > or being unsigned. Plus the (convert:type @2) didn't make sense, @2 already > must have TREE_TYPE type. In the light of the PR111668 patch which shows that build_nonstandard_integer_type is needed (at least for some signed prec > 1 BOOLEAN_TYPEs if we use e.g. negation), I've reworked this patch and handled the last problematic build_nonstandard_integer_type call in there as well. In the x == cstN ? cst4 : cst3 optimization it uses build_nonstandard_integer_type solely for BOOLEAN_TYPEs (I really don't see why ENUMERAL_TYPEs would be a problem, we treat them in GIMPLE as uselessly convertible to same precision/sign INTEGER_TYPEs), for INTEGER_TYPEs it is really a no-op (might return a different type, but always INTEGER_TYPE with same TYPE_PRECISION same TYPE_UNSIGNED) and for BITINT_TYPE with larger precisions really harmful (we shouldn't create large precision INTEGER_TYPEs). The a?~t:t optimization just omits the negation of a in type for 1-bit precision types or any BOOLEAN_TYPEs. I think that is correct, because for both signed and unsigned 1-bit precision type, cast to type of a bool value yields already 0, -1 or 0, 1 values and for 1-bit precision negation of that is still 0, -1 or 0, 1 (except for invoking sometimes UB). And for signed larger precision BOOLEAN_TYPEs I think it is correct as well, cast of [0, 1] to type yields 0, -1 and those can be xored with 0 or -1 to yield the proper result, any other values would be UB. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-10-03 Jakub Jelinek PR middle-end/111369 * match.pd (x == cstN ? cst4 : cst3): Use build_nonstandard_integer_type only if type1 is BOOLEAN_TYPE. Fix comment typo. Formatting fix. (a?~t:t -> (-(a))^t): Always convert to type rather than using build_nonstandard_integer_type. Perform negation only if type has precision > 1 and is not signed BOOLEAN_TYPE. Jakub --- gcc/match.pd.jj 2023-10-03 10:33:30.817614648 +0200 +++ gcc/match.pd 2023-10-03 11:29:54.089566764 +0200 @@ -5178,7 +5178,7 @@ (define_operator_list SYNC_FETCH_AND_AND /* Optimize # x_5 in range [cst1, cst2] where cst2 = cst1 + 1 - x_5 ? cstN ? cst4 : cst3 + x_5 == cstN ? cst4 : cst3 # op is == or != and N is 1 or 2 to r_6 = x_5 + (min (cst3, cst4) - cst1) or r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which @@ -5214,7 +5214,8 @@ (define_operator_list SYNC_FETCH_AND_AND type1 = type; auto prec = TYPE_PRECISION (type1); auto unsign = TYPE_UNSIGNED (type1); - type1 = build_nonstandard_integer_type (prec, unsign); + if (TREE_CODE (type1) == BOOLEAN_TYPE) + type1 = build_nonstandard_integer_type (prec, unsign); min = wide_int::from (min, prec, TYPE_SIGN (TREE_TYPE (@0))); wide_int a = wide_int::from (wi::to_wide (arg0), prec, @@ -5253,14 +5254,7 @@ (define_operator_list SYNC_FETCH_AND_AND } (if (code == PLUS_EXPR) (convert (plus (convert:type1 @0) { arg; })) - (convert (minus { arg; } (convert:type1 @0))) - ) - ) - ) - ) - ) - ) -) + (convert (minus { arg; } (convert:type1 @0)))))))))) #endif (simplify @@ -6758,13 +6752,11 @@ (define_operator_list SYNC_FETCH_AND_AND (with { bool wascmp; } (if (INTEGRAL_TYPE_P (type) && bitwise_inverted_equal_p (@1, @2, wascmp) - && (!wascmp || element_precision (type) == 1)) - (with { - auto prec = TYPE_PRECISION (type); - auto unsign = TYPE_UNSIGNED (type); - tree inttype = build_nonstandard_integer_type (prec, unsign); - } - (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2))))))) + && (!wascmp || TYPE_PRECISION (type) == 1)) + (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE) + || TYPE_PRECISION (type) == 1) + (bit_xor (convert:type @0) @2) + (bit_xor (negate (convert:type @0)) @2))))) #endif /* Simplify pointer equality compares using PTA. */