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: 204623 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:aa16:b0:108:e6aa:91d0 with SMTP id by22csp115674dyb; Thu, 22 Feb 2024 00:47:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVQXDkzzzgaialkx5MXfaQ6M3gvazoTyV/05p5KipS5Ru+MkUjQwbotXhoBSWp9TjJ69HAIP1jIxvVcg5GX4xtm+VzDTw== X-Google-Smtp-Source: AGHT+IFj4OodU4nrMxtNIg7dDGn+J+IdKTLtVcEZarUvUdBfjFjWHIvBi9UM3DptiE6duA1cFYIk X-Received: by 2002:a05:620a:461e:b0:787:a0d2:460e with SMTP id br30-20020a05620a461e00b00787a0d2460emr1039483qkb.35.1708591638779; Thu, 22 Feb 2024 00:47:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708591638; cv=pass; d=google.com; s=arc-20160816; b=bp4I4inJp+PRndoGQFtk4RR5wfti4W+ONuWyGRiKPmcfcKrak/yiyF4Q9m96pfPXIp AmbnjaBc4t0DueJa16QiPoDHErW4/9bCQ63HT7tX8SchP0X4fT6ZYoNaieAfWgUnoKh1 F8GACPVVYUIbyk+EModQnm0j2tRsDihzZ+lDTwas5GP6rgo8EGnXQtCx52XbNEOZtBRc BajiOOTTNYYDwcUTVjWBav/HoUMJWKkHAQTg0T8fezbIkke5igEC99ZjEXT77MKduzRW pKlx2lPKw3gTJQIAfrnMxbPtKlCZgRvE8ptSyH1kUq1R536SRejc7+sOEv/+sQN8hv2c C8Og== 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=n8PAYpmC5liaCCDLldTTjJT772JLs5WkWPCtfMDxDbI=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=yKgkRoKaOgoyTRE1dC0G/uDp5aWElEyLNofE7lUxMN0suf4mIozNM3cuUB0VD4+8GT Vf3JI6Tc7E8uMhMu0PD7Qse21odSrGT8RjQPnuBZFHmq9gqeM48kPI+hsOjKTPmBoWeF VGP9qi8PYTPhCndVr6SjGsddhwi1mvs2spxA8tm7Hpqwc9Aja1z+5QfWOJqgMNUjw5Zv XVVJI6WEu2aFO2etSJmydnrAPfu4SAgjuzI0cqkTTelbezzlVf8pNIo6shd9io+nA3p0 gAVcBkUUj7MyGXDxaNmEO1U5bPtIjFaVzipOFW2B8NTSRQiQYyi4ax8+hKnQTH+FSMtL 1ZZg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eeTi697u; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dw26-20020a05620a601a00b00787548c638fsi12249142qkb.541.2024.02.22.00.47.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 00:47:18 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eeTi697u; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 799353858414 for ; Thu, 22 Feb 2024 08:47:18 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791588186284258089 X-GMAIL-MSGID: 1791588186284258089 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 +}