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: 84818 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 10C6B3858C5F for ; Sat, 27 Jan 2024 08:17:51 +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+patchwork=sourceware.org@gcc.gnu.org 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; +}