From patchwork Mon Jan 15 08:33:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 84074 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 3D02F3858409 for ; Mon, 15 Jan 2024 08:38:52 +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 4814F3858D20 for ; Mon, 15 Jan 2024 08:34:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4814F3858D20 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 4814F3858D20 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=1705307669; cv=none; b=JY7aXbN7rNkM+tQ+ETpqpGhDzxCXXrjVdeuVah+771v/+ctQOOCrarQ9OG9/Hzs1fcX5zNLLYBjpqDzqqdd1209C3GkfOdQ6vivRQkVrTzmofrwXrY+ulPsae0MN8ILF8EtjA1YugQ8ZhYdIEz83JpYBu2lfYMnHQsdRukT8Lh8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705307669; c=relaxed/simple; bh=AYOSZVyrs3SG3qbsoE/r7zdv+Wv19OM1v1Lrs8Voch4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=II+itn3aTlCM8ZHsaAW5IeoKOhpmx1Ddom4VVs+TvBJdo8moWSsKVZm1Xz3e4mE65EIr4YEI0OLE8wHBncEncHnXpAm0Cz7A/w19RdfMGa8pEHK1PDe/hdCUlMJg/suQCdPH7IdUdM72dke9xy+4C9MKLlEtW/eo2cmXA+F/luM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705307661; 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=eyJLBRCfwdAo2GgKb07YBOstvmm506Y15KxWe3fXVN8=; b=F5p9a9OX/mlExdQw625VToPh/xZx1QHFtHKDl76V0RIUSQ0mt1VdYaqGs9CD36MqLHh0hr 67WykzXd31mF02uEkCouHHhC4UX8ldjn5TabBfCiZHLbMfDFXy2nJEumx+oHYMK01wJWws I/YvX9o9dMnurYEmbOtB7VmA3aVApzA= 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-494-rE918hwdM8iScS1OdGWAYw-1; Mon, 15 Jan 2024 03:33:39 -0500 X-MC-Unique: rE918hwdM8iScS1OdGWAYw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 699F3811E86; Mon, 15 Jan 2024 08:33:39 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2D9401C060B3; Mon, 15 Jan 2024 08:33:39 +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 40F8XaWT1542536 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 15 Jan 2024 09:33:36 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 40F8XZW21542535; Mon, 15 Jan 2024 09:33:35 +0100 Date: Mon, 15 Jan 2024 09:33:35 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Fix up handling of INTEGER_CSTs in handle_operand in right shifts or comparisons [PR113370] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.2 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, 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! The INTEGER_CST code uses the remainder bits in computations whether to use whole constant or just part of it and extend it at runtime, and furthermore uses it to avoid using all bits even when using the (almost) whole constant. The problem is that the prec % (2 * limb_prec) computation it uses is appropriate only for the normal lowering of mergeable operations (where we process 2 limbs at a time in a loop starting with least significant limbs and process the remaining 0-2 limbs after the loop (there with constant indexes). For that case it is ok not to emit the upper prec % (2 * limb_prec) bits into the constant, because those bits will be extracted using INTEGER_CST idx and so will be used directly in the statements as INTEGER_CSTs. For other cases, where we either process just a single limb in a loop, process it downwards (e.g. non-equality comparisons) or with some runtime addends (some shifts), there is either just at most one limb lowered with INTEGER_CST idx after the loop (e.g. for right shift) or before the loop (e.g. non-equality comparisons), or all limbs are processed with non-INTEGER_CST indexes (e.g. for left shift, when m_var_msb is set). Now, the m_var_msb case is already handled through if (m_var_msb) type = TREE_TYPE (op); else /* If we have a guarantee the most significant partial limb (if any) will be only accessed through handle_operand with INTEGER_CST idx, we don't need to include the partial limb in .rodata. */ type = build_bitint_type (prec - rem, 1); but for the right shifts or comparisons the prec - rem when rem was prec % (2 * limb_prec) was incorrect, so the following patch fixes it to use remainder for 2 limbs only if m_upwards_2limb and remainder for 1 limb otherwise. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-01-15 Jakub Jelinek PR tree-optimization/113370 * gimple-lower-bitint.cc (bitint_large_huge::handle_operand): Only set rem to prec % (2 * limb_prec) if m_upwards_2limb, otherwise set it to just prec % limb_prec. * gcc.dg/torture/bitint-48.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2024-01-13 11:29:08.005574338 +0100 +++ gcc/gimple-lower-bitint.cc 2024-01-13 11:33:55.284646856 +0100 @@ -869,7 +869,7 @@ bitint_large_huge::handle_operand (tree && m_data[m_data_cnt + 1] == NULL_TREE)) { unsigned int prec = TYPE_PRECISION (TREE_TYPE (op)); - unsigned int rem = prec % (2 * limb_prec); + unsigned int rem = prec % ((m_upwards_2limb ? 2 : 1) * limb_prec); int ext; unsigned min_prec = bitint_min_cst_precision (op, ext); if (m_first) @@ -996,7 +996,7 @@ bitint_large_huge::handle_operand (tree if (m_data[m_data_cnt + 1] == integer_type_node) { unsigned int prec = TYPE_PRECISION (TREE_TYPE (op)); - unsigned rem = prec % (2 * limb_prec); + unsigned rem = prec % ((m_upwards_2limb ? 2 : 1) * limb_prec); int ext = wi::neg_p (wi::to_wide (op)) ? -1 : 0; tree c = m_data[m_data_cnt]; unsigned min_prec = TYPE_PRECISION (TREE_TYPE (c)); --- gcc/testsuite/gcc.dg/torture/bitint-48.c.jj 2024-01-13 11:46:30.707309245 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-48.c 2024-01-13 11:46:25.493380637 +0100 @@ -0,0 +1,23 @@ +/* PR tree-optimization/113370 */ +/* { 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__ >= 255 +_BitInt(255) +foo (int s) +{ + return -(_BitInt(255)) 3 >> s; +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 255 + if (foo (51) != -1) + __builtin_abort (); +#endif + return 0; +}