From patchwork Sat Jan 27 08:12:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 192988 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2395:b0:106:343:edcb with SMTP id gw21csp374591dyb; Sat, 27 Jan 2024 00:18:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IE+DM03s5JWHWZLcpj1wYcKWM8wLTKowDb/UxVYKHOLEFQd5A5IHB9ndYkY03xzGOOu7UzR X-Received: by 2002:a0c:e312:0:b0:681:95a2:472e with SMTP id s18-20020a0ce312000000b0068195a2472emr3340812qvl.18.1706343494620; Sat, 27 Jan 2024 00:18:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706343494; cv=pass; d=google.com; s=arc-20160816; b=Rq0b4AC176LBHKUg2JFQDnOmzBDefuKDu5Q5srUe1h4I5WKoPb1Oh4WMe1trSk/Sfa Z12+tQHWE5GsrA4b4+ftQl5RsXna17z5Db9y2w1SKuwsi+kGK1KEZIoreXZaMkuMO0UK cQlSSxqUFlazVU2vvqclHVQCeSMvVrztUFGST7aLHzHZLIORJdd9skgyFI7ZtAqqETxj ILIqEXDoEcdvIE17NeEzL5aXqK66WIguE/u9zoLTfi98oA0erDWwuHDR4lRoOGpIkk6t 7N1KErP2/MoaXXsT8jl2gTPiOle+5Xn4Sa2S6j6OGJvK4oWICOLdSCulDT7fLvIdQHPa KsMQ== 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:resent-to :resent-message-id:resent-date:resent-from:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=F/bX2J9CzM/CWC0bKpWlUUpVGgg3s7hFJSy9Z9OHhH4=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=ivDzcy9nyQEEWQIu6kwzg3KDOdjo/FGiAp8QAX0dyh88xlrSHwvO0dnn24kNEdJLg5 C7TG11iBpPGHxEXmfmG1NnB511RYgD55WKcTreCbiG5y0kOih+WoMF/cd5SrwHjjJ3mL cRM2aRHbSO4ohRH6eOdfxbnzQAwBWVrzyhM8gYxh02WwdptEd9VZ9ieLJBAg1Ihoqlu1 oZuHAr9O+uGTllTXDolXE/+rGs5LFTooAIZd+gG2GgKGiIb/6Z7sXR7v5nzyCVrUqiBv TCkk+yZk0EOoGv3cM7AKQAQ5OILKgC07HdDHW6eA6EVuUlVOHPKG8/47uPJ3GZRLIQbs 7Icg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FkjuD88a; 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 6-20020a0562140d0600b006819b16afb6si3023428qvh.580.2024.01.27.00.18.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jan 2024 00:18:14 -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=FkjuD88a; 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 5046D3858D28 for ; Sat, 27 Jan 2024 08:18:14 +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 B9E933858D28 for ; Sat, 27 Jan 2024 08:14:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B9E933858D28 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 B9E933858D28 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=1706343250; cv=none; b=H4oYnimAE+ypw0nMyZ5Zv0SmRydisKds5JoEP7M98d8ejNbueMhjSiZjEFoY0OkLnhEr5cXgoKp3XocTzE5QPUxkBs7BL6qD14hfSqnKhZaZ2Y3fN1shkToaNzWc9p/34SggTiK4yE0QBzllG2eUm/TtA6Tjk1E1XLVgU2DLnUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706343250; c=relaxed/simple; bh=wg5gHTlVyHYtHCEdUwiSWSKFUgr2s7Ho0DTphyV8IHE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=TCrGTLtBWOacAsvXsr57G7sS31Be2vgf3QZnuxGWgNwNTCnyYyk72wETQAKNpQMBebvctEaYukcPujX9dq0LxubPfS7hZHDUYFJZ08kbhtO59Pdf3/xo3MdD0O+IU8B+GXzpLpItjZJONSBzq2NO6HnacjQFhW/tjAKZ0WKr6V8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706343244; 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:resent-to:resent-from:resent-message-id; bh=F/bX2J9CzM/CWC0bKpWlUUpVGgg3s7hFJSy9Z9OHhH4=; b=FkjuD88aWczCZzAAGyKAwFmYYeRHwZlWWW43aWLMtmRae/xQMHEiL+wwVpavLc2oc/lfYg GVEP5ELCaCqdTabOO4etfwLY8lhOItZhl9gwbNC6CMByGVhuop0TshEApM+36p8ppuAa+Y fkE2q51tw4FQpwmL8CbAcqVw1OBp/lY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-664-dG53cOYxOBClEwfjOGIMtQ-1; Sat, 27 Jan 2024 03:14:00 -0500 X-MC-Unique: dG53cOYxOBClEwfjOGIMtQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 50C6A386915F; Sat, 27 Jan 2024 08:14:00 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 13DC8AD1; Sat, 27 Jan 2024 08:13:59 +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 40R8DvAb141169 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 27 Jan 2024 09:13:58 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 40R8Dvqf141168; Sat, 27 Jan 2024 09:13:57 +0100 Resent-From: Jakub Jelinek Resent-Date: Sat, 27 Jan 2024 09:13:57 +0100 Resent-Message-ID: Resent-To: Richard Biener , gcc-patches@gcc.gnu.org Date: Sat, 27 Jan 2024 09:12:38 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Avoid sign-extending cast to unsigned types feeding div/mod/float [PR113614] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.3 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: 1789230836022540351 X-GMAIL-MSGID: 1789230836022540351 Hi! The following testcase is miscompiled, because some narrower value is sign-extended to wider unsigned _BitInt used as division operand. handle_operand_addr for that case returns the narrower value and precision -prec_of_narrower_value. That works fine for multiplication (at least, normal multiplication, but we don't merge casts with .MUL_OVERFLOW or the ubsan multiplication right now), because the result is the same whether we treat the arguments as signed or unsigned. But is completely wrong for division/modulo or conversions to floating-point, if we pass negative prec for an input operand of a libgcc handler, those treat it like a negative number, not an unsigned one sign-extended from something smaller (and it doesn't know to what precision it has been extended). So, the following patch fixes it by making sure we don't merge such sign-extensions to unsigned _BitInt type with division, modulo or conversions to floating point. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-01-27 Jakub Jelinek PR tree-optimization/113614 * gimple-lower-bitint.cc (gimple_lower_bitint): Don't merge widening casts from signed to unsigned types with TRUNC_DIV_EXPR, TRUNC_MOD_EXPR or FLOAT_EXPR uses. * gcc.dg/torture/bitint-54.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2024-01-26 18:05:24.461891138 +0100 +++ gcc/gimple-lower-bitint.cc 2024-01-26 19:04:07.948780942 +0100 @@ -6102,17 +6102,27 @@ gimple_lower_bitint (void) && (TREE_CODE (rhs1) != SSA_NAME || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))) { - if (TREE_CODE (TREE_TYPE (rhs1)) != BITINT_TYPE - || (bitint_precision_kind (TREE_TYPE (rhs1)) - < bitint_prec_large)) - continue; if (is_gimple_assign (use_stmt)) switch (gimple_assign_rhs_code (use_stmt)) { - case MULT_EXPR: case TRUNC_DIV_EXPR: case TRUNC_MOD_EXPR: case FLOAT_EXPR: + /* For division, modulo and casts to floating + point, avoid representing unsigned operands + using negative prec if they were sign-extended + from narrower precision. */ + if (TYPE_UNSIGNED (TREE_TYPE (s)) + && !TYPE_UNSIGNED (TREE_TYPE (rhs1)) + && (TYPE_PRECISION (TREE_TYPE (s)) + > TYPE_PRECISION (TREE_TYPE (rhs1)))) + goto force_name; + /* FALLTHRU */ + case MULT_EXPR: + if (TREE_CODE (TREE_TYPE (rhs1)) != BITINT_TYPE + || (bitint_precision_kind (TREE_TYPE (rhs1)) + < bitint_prec_large)) + continue; /* Uses which use handle_operand_addr can't deal with nested casts. */ if (TREE_CODE (rhs1) == SSA_NAME @@ -6126,6 +6136,10 @@ gimple_lower_bitint (void) default: break; } + if (TREE_CODE (TREE_TYPE (rhs1)) != BITINT_TYPE + || (bitint_precision_kind (TREE_TYPE (rhs1)) + < bitint_prec_large)) + continue; if ((TYPE_PRECISION (TREE_TYPE (rhs1)) >= TYPE_PRECISION (TREE_TYPE (s))) && mergeable_op (use_stmt)) --- gcc/testsuite/gcc.dg/torture/bitint-54.c.jj 2024-01-26 19:09:01.436688318 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-54.c 2024-01-26 19:16:24.908504368 +0100 @@ -0,0 +1,29 @@ +/* PR tree-optimization/113614 */ +/* { 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" } { "" } } */ + +_BitInt(8) a; +_BitInt(8) b; +_BitInt(8) c; + +#if __BITINT_MAXWIDTH__ >= 256 +_BitInt(256) +foo (_BitInt(8) y, unsigned _BitInt(256) z) +{ + unsigned _BitInt(256) d = -y; + z /= d; + return z + a + b + c; +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 256 + if (foo (0xfwb, 0x24euwb)) + __builtin_abort (); +#endif + return 0; +}