From patchwork Fri Feb 23 08:07:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 205218 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp445580dyb; Fri, 23 Feb 2024 00:09:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU2TBngIrclP4y5qyTp66tGMi08WJ5ssgAGnJjW+7Vy3biJFWUh0TSAhgijbH2EafztRayWg4O/PjRra0vKjSMOxVe7vg== X-Google-Smtp-Source: AGHT+IGwmVUXPpPMf8/CnzEbt7krmTgZOwavA7SfHE2CHK12XIsDiQmVftXNkVNpWNVRLmjZuTct X-Received: by 2002:a05:620a:51d0:b0:787:a315:fb2e with SMTP id cx16-20020a05620a51d000b00787a315fb2emr1467176qkb.33.1708675759941; Fri, 23 Feb 2024 00:09:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708675759; cv=pass; d=google.com; s=arc-20160816; b=JWJR3Uq/NikUQ30BFpBrzn5ur+aShW5qpqWIViA6xB4ruNQNqDNJEVvZNydGyBl+9E jbSZkxM4yDFt9TWWBlVubZ22pvqVe3gt5YeN+caqb7N7vxzP7UC9XXnBAu74q5aTsyAD 1E42q0TUC5ALqcFcwRjsvi90fSk17ryKGXU9p0NKWuV3rMejGVps/890vGB0vt3OuzRw VU66v4sEFBCE2CQQfLuFDcjJL3sKDRXHkhTRPQ5MwQK9SumCxgJR+D36psRuRJs8EERY bSud3HTvqifYLadzXwVhLImNsJ1gZZzxPdq/svb9GyJI0/6VE8DJ6HzmhhvS89rGCCrd ubog== 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=tPp+tCSFQ88EkOwn0eLzezh3vieRnoIZktkuBe1DuUI=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=QVR/5vYu4qJU3oNQRU2vJh1TGgJfqiN0lq4yHHq4Ss2OKaf9mYFyJuV/vA3OUi6kOn Rr/iNzINjVIx2phpZgRp/OgbY06SfHD0pMQSb4ouH6zyigjvt0Y6ZLC3k7uCEUDERVcf dcms5BeoST63IuOZz7oBwW3wMzEvq3Oax2sPQify9t1apj/d54QrOV+tJOZAhQ81LPSo hWIQau0kFOFPxc8p7CbCnnpxxsR5qw7xV8ClFz8eqkLwNy0LkHA5Nv1n57cYXteSAhSP 2stnbfusiEWHZBCmA8DZdew8b4lsrFkVFviNJcShrZiouuc1owei+lLvVU1buJBnX1Ea eqOA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WbRxMsMz; 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 wk22-20020a05620a579600b00787b57ab193si359781qkn.94.2024.02.23.00.09.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 00:09:19 -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=WbRxMsMz; 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 8FB4E3858282 for ; Fri, 23 Feb 2024 08:09:19 +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 A242B3858293 for ; Fri, 23 Feb 2024 08:07:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A242B3858293 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 A242B3858293 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=1708675657; cv=none; b=OwS+yDqfb83TQCMQFP/ZXjNXAh19cI6bH1tex2Y7OCn/cZ7R4BvNWtzo3AgFdOFqzA0/c0gf9BOG7/AZMvDD/zY5+vOwYfKzvtb9qqcmH33qIc2HNLIhcV1Qrj7vewrS4N7BAAx/Uvg9Gfszx0FGJy+6VJlTpk7Biw00DIsqxss= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708675657; c=relaxed/simple; bh=+vtdLZM3MYvuTnjIxrV5ZY/Bv/NKNr+jCk7PGZmlmZo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=riosEFKUKOFlGAsAngZxEySBycv9B8/NKhWQbn5WSv5p/qMiPXIhHPmRNH+PqQQsJtaV6+0HU4P1X6MEKxlSN4mXZ9YKmNChy2BFqYTQQG5nURNEaM0Ce4kV0Pn0WouF8FoUF6T3gjMX/UWQoLs0+kCMa4+pq9z+mIAxovI7b/8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708675646; 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=tPp+tCSFQ88EkOwn0eLzezh3vieRnoIZktkuBe1DuUI=; b=WbRxMsMzJiaKb9C1jBFFKAIGNqAUFmSExv9uLb01Jq9o1HuV8kXPU/9MxqtXZmlg7ums/T ThxyWo+JPITCb5xaJ4AuTWMc4ie3CLn6GMf3/81lHKFeGHIYRUYxYAnEF1+9e3G9uZW5q0 LE/emd6gP3CoJl/KtvLY8qoBTJnaCg0= 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-60-04KVdm64NF2q-kEb7RS32w-1; Fri, 23 Feb 2024 03:07:22 -0500 X-MC-Unique: 04KVdm64NF2q-kEb7RS32w-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 94A1885A58A; Fri, 23 Feb 2024 08:07:22 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 585CA2166AEE; Fri, 23 Feb 2024 08:07:22 +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 41N87Juf955887 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 23 Feb 2024 09:07:20 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41N87Icv955886; Fri, 23 Feb 2024 09:07:18 +0100 Date: Fri, 23 Feb 2024 09:07:18 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] bitintlower: Fix .{ADD,SUB}_OVERFLOW lowering [PR114040] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 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_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: 1791676393511525109 X-GMAIL-MSGID: 1791676393511525109 Hi! The following testcases show 2 bugs in the .{ADD,SUB}_OVERFLOW lowering, both related to storing of the REALPART_EXPR part of the result. On the first testcase prec is 255, prec_limbs is 4 and for the second limb in the loop the store of the REALPART_EXPR of .USUBC (_30) is stored through: if (_27 <= 3) goto ; [80.00%] else goto ; [20.00%] [local count: 1073741824]: if (_27 < 3) goto ; [80.00%] else goto ; [20.00%] [local count: 1073741824]: bitint.3[_27] = _30; goto ; [100.00%] [local count: 858993464]: MEM[(unsigned long *)&bitint.3 + 24B] = _30; [local count: 1073741824]: The first check is right, as prec_limbs is 4, we don't want to store bitint.3[4] or above at all, those limbs are just computed for the overflow checking and nothing else, so _27 > 4 leads to no store. But the other condition is exact opposite of what should be done, if the current index of the second limb (_27) is < 3, then it should bitint.3[_27] = _30; and if it is == 3, it should MEM[(unsigned long *)&bitint.3 + 24B] = _30; and (especially important for the targets which would bitinfo.extended = 1) should actually in this case zero extend it from the 63 bits to 64, that is the handling of the partial limb. The if_then_if_then_else helper if there are 2 conditions sets m_gsi to be at the start of the edge_true_false->dest bb, i.e. when the first condition is true and second false, and that is where we store the SSA_NAME indexed limb store, so the condition needs to be reversed. The following patch does that and adds the cast as well, the usual assumption that already handle_operand has the partial limb type doesn't have to be true here, because the source operand could have much larger precision than the REALPART_EXPR of the lhs. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-23 Jakub Jelinek PR tree-optimization/114040 * gimple-lower-bitint.cc (bitint_large_huge::lower_addsub_overflow): Use EQ_EXPR rather than LT_EXPR for g2 condition and change its probability from likely to unlikely. When handling the true true store, first cast to limb_access_type and then to l's type. * gcc.dg/torture/bitint-60.c: New test. * gcc.dg/torture/bitint-61.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2024-02-22 10:13:54.123058152 +0100 +++ gcc/gimple-lower-bitint.cc 2024-02-22 14:51:01.655335350 +0100 @@ -4255,12 +4255,12 @@ bitint_large_huge::lower_addsub_overflow NULL_TREE, NULL_TREE); gimple *g2 = NULL; if (!single_comparison) - g2 = gimple_build_cond (LT_EXPR, idx, + g2 = gimple_build_cond (EQ_EXPR, idx, size_int (prec_limbs - 1), NULL_TREE, NULL_TREE); edge edge_true_true, edge_true_false, edge_false; if_then_if_then_else (g, g2, profile_probability::likely (), - profile_probability::likely (), + profile_probability::unlikely (), edge_true_true, edge_true_false, edge_false); tree l = limb_access (type, var ? var : obj, idx, true); @@ -4269,8 +4269,11 @@ bitint_large_huge::lower_addsub_overflow if (!single_comparison) { m_gsi = gsi_after_labels (edge_true_true->src); - l = limb_access (type, var ? var : obj, - size_int (prec_limbs - 1), true); + tree plm1idx = size_int (prec_limbs - 1); + tree plm1type = limb_access_type (type, plm1idx); + l = limb_access (type, var ? var : obj, plm1idx, true); + if (!useless_type_conversion_p (plm1type, TREE_TYPE (rhs))) + rhs = add_cast (plm1type, rhs); if (!useless_type_conversion_p (TREE_TYPE (l), TREE_TYPE (rhs))) rhs = add_cast (TREE_TYPE (l), rhs); --- gcc/testsuite/gcc.dg/torture/bitint-60.c.jj 2024-02-22 14:54:52.996121115 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-60.c 2024-02-22 14:54:42.656264784 +0100 @@ -0,0 +1,24 @@ +/* PR tree-optimization/114040 */ +/* { 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__ >= 8671 +__attribute__((noipa)) unsigned +foo (unsigned _BitInt(8671) x, unsigned y, unsigned _BitInt(512) z) +{ + unsigned _BitInt (8671) r + = x * __builtin_sub_overflow_p (y * z, 0, (unsigned _BitInt(255)) 0); + return r; +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 8671 + if (foo (1, 1, 0xfffa46471e7c2dd60000000000000000wb)) + __builtin_abort (); +#endif +} --- gcc/testsuite/gcc.dg/torture/bitint-61.c.jj 2024-02-22 15:19:24.562704298 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-61.c 2024-02-22 15:19:57.964241439 +0100 @@ -0,0 +1,36 @@ +/* PR tree-optimization/114040 */ +/* { dg-do run { target { bitint && int128 } } } */ +/* { dg-options "-std=c23" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +unsigned a; +signed char b; +short c; +long d; +__int128 e; +int f; + +#if __BITINT_MAXWIDTH__ >= 511 +__attribute__((noinline)) void +foo (_BitInt(3) x, unsigned _BitInt(511) y, unsigned *z) +{ + int g = __builtin_sub_overflow_p (y ^ x, 0, (unsigned _BitInt(255)) 0); + unsigned h = y + e, i = h + d; + unsigned _BitInt(2) j = i + g; + unsigned k = j + c; + unsigned l = k + a + f + b; + *z = l; +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 511 + unsigned x; + foo (0, 0x81e4a5fa7c408f370000000000000000uwb, &x); + if (x) + __builtin_abort (); +#endif +}