From patchwork Fri Dec 1 07:29:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 172301 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp941085vqy; Thu, 30 Nov 2023 23:29:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IGf6sIHfm1Th9ZDa+NfMDt9eYCXZBYcfgkVvxbtJfQwZfoE15Sm1Sg4Stb0lX2jxykg4OfU X-Received: by 2002:a05:620a:8188:b0:77d:cdd9:fbb4 with SMTP id ot8-20020a05620a818800b0077dcdd9fbb4mr6118272qkn.62.1701415787909; Thu, 30 Nov 2023 23:29:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701415787; cv=pass; d=google.com; s=arc-20160816; b=APPr+VW+TiaD1+94xTo8WzyH0Q6kiLhQyMauWjxaJ2LXH8GMoA10E1YS60k6d7s8+u L3AU0muEo9vQraeGDZwMFyUXRpowpbND/j5SY2cU6Iz6MV0H+fv2J9wk2auz9UHPG7cl ghOvdy7ZJVd61grdm1iG0i/DyxQq2brxzMMS5hQVFlQm2KHOtpot5m77wP1u9/uq4l7H sSu3SDdHCIbA7n9WHWWnLgG6vskB78hZ0SIKXLtqWuzA3M4Wk1KXHDnP6Xt7G7P3rNOo C7dA1HYGHbXP5zQqvV8i7SfyxzUGgWB2bKKVuE1v8A5nfWsR4ljgyFMmq4/jbaNhzHME sCgQ== 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=AePgKzXLkRZE5/D1hQJs/AKrYpwyl/E3KMlA4UgmDsg=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=mhYl9wuKrNN5KaQnHdc9Wm1k5Rqy1BC8+mtQuu95ME3cNAXHN+Bhnv0Ik4+Z0aJMvu 0cEh43zz7b4qNK17CbpZaa50rDr6FbCfYg/3YjqmYR+MrhvASKKS79pUxmQJEMijlaUu 0A8ioeqr90g7VMymbkxgU6Dz8iBG0B2NO3juiuoLzdu0zfDl5NsvzeLBxrRME/Zrnmy7 EIH1QSgxRT6IwrAQr2G1A9P2m/anUOR6ZrrqLOkJuMJvIV1roT1lf8HdEhbgsdStysH4 cUN9ASSYOqc/ja15JWqxhqjnLMyhDy19G0hj7JDZ5WiGaFk100h4iOJCgyLzhV8ohL4n /Qgg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SS7Fienw; arc=pass (i=1); 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dy8-20020a05620a60c800b0077771ab761bsi2576446qkb.35.2023.11.30.23.29.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 23:29:47 -0800 (PST) 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=SS7Fienw; arc=pass (i=1); 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 A8C3C3856DD4 for ; Fri, 1 Dec 2023 07:29:47 +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 A37B13858CDB for ; Fri, 1 Dec 2023 07:29:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A37B13858CDB 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 A37B13858CDB 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=1701415764; cv=none; b=nP+lsgwm0yN/vL/KB6BqCHWlMnJ/Fxf1Fhh6/V2pB/q3DLoPUxZiZEeNALmSm7/7Rx/7zdQ94chcuDjSpduR5ppkAoo7k+RjwYKd2p/4CahZyqU21uKFhCgQF5Wug3j0p6u0BGlVl9/kXQbi8fs4MXtebd/7WxjXFUF2BfuuuLI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701415764; c=relaxed/simple; bh=DqwGHne06L4jLsuWMTfil7YwchnkhWI1XyDDH416Q4A=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=RDUkVJTi2kLqVhwGu0rMBTjx9CsrJrosPreZ532Srb9GrcO81bm8a2h0dtHorL+PWVOrkSyl1n+iXrQVbfXq3kjWHE5aTwgX0nPddHKbpZCMzO7L5oV3AMfVcsL7wQK7SFOQvlRao4kv+THGe8fEVVbkD+r8zplJGooETSZB8lY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701415762; 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=AePgKzXLkRZE5/D1hQJs/AKrYpwyl/E3KMlA4UgmDsg=; b=SS7FienwcCGHBuAIoshsU0HS6QqazHGFsh7FdFAIAuxiPkLu12rSLCv7svvj10M6VM70dg xKo7iMZ18UqaTdTOzi3HmajNzZ63UqpqyD/rHnJoNOzUUwoKGPnj3S45rhP7Y/n0w7QZ5f Omeyi2mXaXhwise2+3B35vP/DC1hw+s= 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-455-dr5O5oq_NtO79DII70FriA-1; Fri, 01 Dec 2023 02:29:19 -0500 X-MC-Unique: dr5O5oq_NtO79DII70FriA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 C9581280C2A1; Fri, 1 Dec 2023 07:29:18 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.157]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8DC18492BFC; Fri, 1 Dec 2023 07:29:18 +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 3B17TFrJ540141 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 1 Dec 2023 08:29:16 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3B17TFeY540140; Fri, 1 Dec 2023 08:29:15 +0100 Date: Fri, 1 Dec 2023 08:29:14 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Fix _BitInt .{ADD,SUB}_OVERFLOW lowering [PR112750] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 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: 1784063760946411459 X-GMAIL-MSGID: 1784063760946411459 Hi! The .{ADD,SUB}_OVERFLOW lowering is implemented by performing normal addition/subtraction (perhaps extended to even more bits than normally by continuing with extended values of operands after running of normal bits) and in addition to that trying to compute if certain sets of bits are either all zero or all sign extensions of the least significant bit. That code is in a lot of cases guarded just by a single condition (which can be idx > startlimb, idx >= startlimb or idx == startlimb) or by 2 conditions - if (idx >= startlimb) { if (idx == startlimb) ... else ... } Now, if_then_if_then_else when the second argument is NULL works just as if_then and sets m_gsi to be within the initially empty then block and that is where we emit code for constant tidx startlimb + (cmp_code == GT_EXPR). But in the 2 conditions case, m_gsi is set to the initially empty else block, so using EQ_EXPR for the condition was incorrect (and strangely nothing in the testsuite caught that), because the code for extracting the lowest set of bits (i.e. when tidx is startlimb) is then done when idx is not startlimb rather than when it is. The following patch fixes that. Note, when developing the lowering, I was using gcov to make sure all code is covered by the testsuite with minimum exceptions, so no idea how this slipped out. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-01 Jakub Jelinek PR middle-end/112750 * gimple-lower-bitint.cc (bitint_large_huge::lower_addsub_overflow): Use NE_EXPR rather than EQ_EXPR for g2 if !single_comparison and adjust probabilities. * gcc.dg/bitint-41.c: Use -std=c23 rather than -std=c2x. * gcc.dg/torture/bitint-43.c: Likewise. * gcc.dg/torture/bitint-44.c: Likewise. * gcc.dg/torture/bitint-45.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2023-11-24 11:30:24.416427246 +0100 +++ gcc/gimple-lower-bitint.cc 2023-11-30 10:57:40.095555940 +0100 @@ -4028,11 +4028,11 @@ bitint_large_huge::lower_addsub_overflow edge edge_true_true, edge_true_false, edge_false; gimple *g2 = NULL; if (!single_comparison) - g2 = gimple_build_cond (EQ_EXPR, idx, + g2 = gimple_build_cond (NE_EXPR, idx, size_int (startlimb), NULL_TREE, NULL_TREE); if_then_if_then_else (g, g2, profile_probability::likely (), - profile_probability::unlikely (), + profile_probability::likely (), edge_true_true, edge_true_false, edge_false); unsigned tidx = startlimb + (cmp_code == GT_EXPR); --- gcc/testsuite/gcc.dg/bitint-41.c.jj 2023-11-23 12:59:48.027443972 +0100 +++ gcc/testsuite/gcc.dg/bitint-41.c 2023-11-30 11:05:54.956550967 +0100 @@ -1,6 +1,6 @@ /* PR middle-end/112336 */ /* { dg-do compile { target bitint } } */ -/* { dg-options "-std=c2x" } */ +/* { dg-options "-std=c23" } */ unsigned _BitInt(1) v1; unsigned _BitInt(1) *p1 = &v1; --- gcc/testsuite/gcc.dg/torture/bitint-43.c.jj 2023-11-20 09:49:35.236668167 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-43.c 2023-11-30 11:06:31.840028866 +0100 @@ -1,6 +1,6 @@ /* PR c/111309 */ /* { dg-do run { target bitint } } */ -/* { dg-options "-std=c2x -pedantic-errors" } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ /* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ /* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ --- gcc/testsuite/gcc.dg/torture/bitint-44.c.jj 2023-11-14 10:52:16.192276014 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-44.c 2023-11-30 11:06:37.494948817 +0100 @@ -1,6 +1,6 @@ /* PR c/111309 */ /* { dg-do run { target bitint } } */ -/* { dg-options "-std=c2x -pedantic-errors" } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ /* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ /* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ --- gcc/testsuite/gcc.dg/torture/bitint-45.c.jj 2023-11-30 11:07:40.778053018 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-45.c 2023-11-30 11:07:19.294357123 +0100 @@ -0,0 +1,32 @@ +/* PR middle-end/112750 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +#if __BITINT_MAXWIDTH__ >= 256 +_BitInt(256) a = __INT_MAX__ + (_BitInt(256)) 1; +_BitInt(256) b = __INT_MAX__; +#endif +#if __BITINT_MAXWIDTH__ >= 512 +_BitInt(512) c = 0x7fffffffffffffffffffffffffffffffffffffffwb + (_BitInt(512)) 1; +_BitInt(512) d = 0x7fffffffffffffffffffffffffffffffffffffffwb; +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 256 + if (!__builtin_sub_overflow_p (a, 0, 0)) + __builtin_abort (); + if (!__builtin_sub_overflow_p (b, -1, 0)) + __builtin_abort (); +#endif +#if __BITINT_MAXWIDTH__ >= 512 + if (!__builtin_sub_overflow_p (c, 0, (_BitInt(160)) 0)) + __builtin_abort (); + if (!__builtin_sub_overflow_p (d, -1, 0)) + __builtin_abort (); +#endif + return 0; +}