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: 188068 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2614:b0:101:6a76:bbe3 with SMTP id mm20csp1575333dyc; Mon, 15 Jan 2024 00:40:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHIQebnzoif4/xM9OBNZqi0UrqX6w6cgemOiHncedAsKGRDMQDkd18KC0qyyf8Ca30+Zx44 X-Received: by 2002:a05:622a:c3:b0:429:d922:2d89 with SMTP id p3-20020a05622a00c300b00429d9222d89mr5676822qtw.132.1705308022428; Mon, 15 Jan 2024 00:40:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705308022; cv=pass; d=google.com; s=arc-20160816; b=gH/M5D6gfllmnWojOEiT9R7PtXJHxuXsmWPlrU+XL4E6Rkn4cqoOcxJJmSHXwFxjIN FqYymQ1oWGaX4v+LSsvQ6tnlEJWcWJR/aZyh22tcswRdld/nGDxEnfVmCNNA61W8k8WN gaHY6yPRVk4AWu5Joav5BPGCrX2dLXCfnhC8bDmrJAmOu3oOkWAaYbhdS7Xlyk99slY0 rxB8utI34/KwTEKeESn/YLyaFEzZLGv4CTtJeTfBJa/ix7tv2Mqxoed851SJnYlbZJ5m KT/CeD+0MgCjteeJt0nndUBWPhaCEQP5JBkoEQ4x1MVnqDriGRtpQGDdhdly2mD02aXK B0+g== 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=eyJLBRCfwdAo2GgKb07YBOstvmm506Y15KxWe3fXVN8=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=RmJ/71r1x+fg1l3FOCi8c4pFJn9Wo4j85b9577mc7j3uRsrCldaF6YnLyUfkLBnxKC FkeCfuDAeYSNUHuRz57iYb1Jg3cGzjtbGPnykuab4Jzq9aROElaDZpKdC2w3pnsTkWVC haQSX8hYZAUMkB4UKJOnQZJpB1DDdIKxHliTcFzg2YCGWBBBQzS/UxJz8lI2hRoh2vQq D55Ilr7ZrK94gEsaRYNKfv5WPZuysTfZxpeDsWIwcdA2IZxF+/MCji7WBEF6os322xsG i0yK0IefsoGbdWKXPui7B8eNIU1gwTafQ9M/Y2iM5rvzMVNr4OGow6HUBaqVTpJPUMNp JKgA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=F5p9a9OX; 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 d10-20020a05622a100a00b00429fc1c2142si109436qte.707.2024.01.15.00.40.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 00:40:22 -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=F5p9a9OX; 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 E3BE33858415 for ; Mon, 15 Jan 2024 08:40:21 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788145064926103848 X-GMAIL-MSGID: 1788145064926103848 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; +}