From patchwork Thu Feb 22 08:46:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 86207 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1EE85385842B for ; Thu, 22 Feb 2024 08:46:55 +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 767D73858D37 for ; Thu, 22 Feb 2024 08:46:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 767D73858D37 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 767D73858D37 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=1708591583; cv=none; b=WR0F6VhCl7VX1pLWSUVcN3QO0T/MLo2YkIR/CrXa6ifPEawsIj0m4ia3NiY2VkQq30DmiAjd/BIi4uPkEo2tWVX+KdjLAQvDuXCQy4R6YSNvatwzeHb4ZWPMjaa/v0dHSvRDVjAg0Kf/jiYm1BGF9CKdqf20et8nxxN2iZfAPUk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708591583; c=relaxed/simple; bh=4io2syRoIUdwYl12nnDK+ydFX8g/jw13usJlcPlyEOE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=X65+tsF/nd6D8tfiMPYca/gQfVCtZMnKhZbID3B2YrRCmkKdYUv8GnsJDuHZyVnTGvxhwLIdcm9ZeIZgp1FH9j8pyH4FfcVhvg3kFuTzAP2uL4UA+HB+5Ptio1HZF67TIAMhLeSEbye/VJFKVT+tlPOSzmXhALJkb4ye1kB8oCk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708591582; 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=n8PAYpmC5liaCCDLldTTjJT772JLs5WkWPCtfMDxDbI=; b=eeTi697un7IVBTzySLntucFiCl2RbF0yPRjc1R5AGIIIfWuQ6HURt8N3TQEXqGw93plae9 +NUg6CUjkQALcy1eFm4nfemdUmXaFizsuzqA3WaNo/22DOEL1FtrENyIBtwkXz4bwN6RGl 9tOCiEONUCXNT9V64isTv99lxgL10qk= 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-679-n4jYepqvN2mWcWXWBwiBcw-1; Thu, 22 Feb 2024 03:46:19 -0500 X-MC-Unique: n4jYepqvN2mWcWXWBwiBcw-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 C35B7102021B; Thu, 22 Feb 2024 08:46:18 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 87A90492BD7; Thu, 22 Feb 2024 08:46: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 41M8kF5O2145745 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 22 Feb 2024 09:46:16 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41M8kEGH2145744; Thu, 22 Feb 2024 09:46:14 +0100 Date: Thu, 22 Feb 2024 09:46:14 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] bitintlower: Fix .MUL_OVERFLOW overflow checking [PR114038] 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.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_H2, 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+patchwork=sourceware.org@gcc.gnu.org Hi! Currently, bitint_large_huge::lower_mul_overflow uses cnt 1 only if startlimb == endlimb and in that case doesn't use a loop and handles everything in a special if: unsigned cnt; bool use_loop = false; if (startlimb == endlimb) cnt = 1; else if (startlimb + 1 == endlimb) cnt = 2; else if ((end % limb_prec) == 0) { cnt = 2; use_loop = true; } else { cnt = 3; use_loop = startlimb + 2 < endlimb; } if (cnt == 1) { ... } else The loop handling for the loop exit condition wants to compare if the incremented index is equal to endlimb, but that is correct only if end is not divisible by limb_prec and there will be a straight line check after the loop as well for the most significant limb. The code used endlimb + (cnt == 1) for that, but cnt == 1 is never true here, because cnt is either 2 or 3, so the right check is (cnt == 2). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-22 Jakub Jelinek PR tree-optimization/114038 * gimple-lower-bitint.cc (bitint_large_huge::lower_mul_overflow): Fix loop exit condition if end is divisible by limb_prec. * gcc.dg/torture/bitint-59.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2024-02-15 09:52:40.999145971 +0100 +++ gcc/gimple-lower-bitint.cc 2024-02-21 20:04:27.590388930 +0100 @@ -4497,7 +4497,7 @@ bitint_large_huge::lower_mul_overflow (t size_one_node); insert_before (g); g = gimple_build_cond (NE_EXPR, idx_next, - size_int (endlimb + (cnt == 1)), + size_int (endlimb + (cnt == 2)), NULL_TREE, NULL_TREE); insert_before (g); edge true_edge, false_edge; --- gcc/testsuite/gcc.dg/torture/bitint-59.c.jj 2024-02-21 20:07:11.028142323 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-59.c 2024-02-21 20:07:57.854498649 +0100 @@ -0,0 +1,22 @@ +/* PR tree-optimization/114038 */ +/* { 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__ >= 129 +int +foo (unsigned _BitInt(63) x, unsigned _BitInt(129) y) +{ + return __builtin_mul_overflow_p (y, x, 0); +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 129 + if (!foo (90, 0x80000000000000000000000000000000uwb)) + __builtin_abort (); +#endif +}