From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id 6BE783858D28 for ; Sat, 27 Jan 2024 12:02:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6BE783858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6BE783858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706356933; cv=none; b=xAhz0njg9qkHqB3UiMBn9UZuizgpl4QcJryK4LixvWYVvpnW4d4b88RUg6IQ9hpGki0cHb3fMSALNleiU4c1DHsdssu0lg1Cu8JzHxSNq/XVTYW+4wpKTb9/08oGQ+9rY963MjdFrjBWA9IeFRz+urB2G8tbdtvNqw73ybYiwYk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706356933; c=relaxed/simple; bh=lhnB0gQyc5ewnKdZ+B+jE7IBC0vJ1t1zUBAyaHAfzNE=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: Mime-Version:Subject:Date:Message-Id:To; b=hjSVnyzFGAqg+OHhq0G1nFOCdc8V7OcgzDzNQRdsRLhjz1ywZrRJvetZudjxLmoBQPNJnu4Ny8sUikOprDF4d14vhqyUhOauDQAQKNEFLIrZOxiWswmT0WVHMaemPG+bBCg6uTDPSyAw3Puyyg0ubK9qB8f0WDyzx9USn1QGqQk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 3603021C14; Sat, 27 Jan 2024 12:02:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1706356928; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m05k8UdoecAHvB0axx8Iqc7U011VIZNZy2wq3Izpics=; b=D75k8eZ7p9EatWkSDkCZ7abgHs4WAJGCeDo24k/lB2jqN0y1+pYwWuq/sDRRT9k0zi9Rjg VVbKt+dfYypg7vG301Xc9dH7I1rl/SR6LM+5hT4d5RiQ0QhlCHNZOCPBPRB4SkfA7pB9WH C6hiuaHVoA/Z7KVUM90+DXqapDPrBBo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1706356928; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m05k8UdoecAHvB0axx8Iqc7U011VIZNZy2wq3Izpics=; b=Z3iqMyFff5Gzz/OqagwU6627ndjWr2/DQO8kNLqW8k0lc4mrxrT9veWEQZ/l+Qw2UmIiiW lKH0eq48QJvjybCg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1706356927; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m05k8UdoecAHvB0axx8Iqc7U011VIZNZy2wq3Izpics=; b=JjTHsctLwXszVAO0xHyvlkz+aduOA7l0iHVFVMG4O17b0/ZEkdETbsJU9CeGPhAdoThfKH xGaQnEvV2jpOo48FgG7CFWZgG8PnDWzW6sxICE1P3heOEsbX3YwPrXgMS0Co8OnsQiZGds Sd2oiFc0Y4uIZIMl2a+cuNf1ztGIljY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1706356927; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m05k8UdoecAHvB0axx8Iqc7U011VIZNZy2wq3Izpics=; b=62I33k7eM+LnrWxOphJUnbyTmmjh41Dt8lEeMnyicMl3J5q8SrxuY91GIeH78CMoWzUJEK 6Jc4oj6kZJJPi5Bw== Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 22CE8139B7; Sat, 27 Jan 2024 12:02:07 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id G9A2CL/wtGU7eAAAn2gu4w (envelope-from ); Sat, 27 Jan 2024 12:02:07 +0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Richard Biener Mime-Version: 1.0 (1.0) Subject: Re: [PATCH] lower-bitint: Avoid sign-extending cast to unsigned types feeding div/mod/float [PR113614] Date: Sat, 27 Jan 2024 13:01:56 +0100 Message-Id: References: Cc: gcc-patches@gcc.gnu.org In-Reply-To: To: Jakub Jelinek X-Mailer: iPhone Mail (21D50) Authentication-Results: smtp-out1.suse.de; none X-Spam-Level: X-Spam-Score: -0.86 X-Spamd-Result: default: False [-0.86 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; BAYES_HAM(-0.06)[61.46%]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; MV_CASE(0.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-1.00)[-1.000]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; RCPT_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[gimple-lower-bitint.cc:url]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; MID_RHS_MATCH_FROM(0.00)[] X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,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 List-Id: > Am 27.01.2024 um 09:18 schrieb Jakub Jelinek : >=20 > =EF=BB=BFHi! >=20 > 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 precisio= n > it has been extended). >=20 > 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. >=20 > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Ok Richard=20 > 2024-01-27 Jakub Jelinek >=20 > 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. >=20 > * gcc.dg/torture/bitint-54.c: New test. >=20 > --- 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) !=3D SSA_NAME > || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))) > { > - if (TREE_CODE (TREE_TYPE (rhs1)) !=3D 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)) !=3D 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) =3D=3D SSA_NAME > @@ -6126,6 +6136,10 @@ gimple_lower_bitint (void) > default: > break; > } > + if (TREE_CODE (TREE_TYPE (rhs1)) !=3D BITINT_TYPE > + || (bitint_precision_kind (TREE_TYPE (rhs1)) > + < bitint_prec_large)) > + continue; > if ((TYPE_PRECISION (TREE_TYPE (rhs1)) > >=3D TYPE_PRECISION (TREE_TYPE (s))) > && mergeable_op (use_stmt)) > --- gcc/testsuite/gcc.dg/torture/bitint-54.c.jj 2024-01-26 19:09:01.436= 688318 +0100 > +++ gcc/testsuite/gcc.dg/torture/bitint-54.c 2024-01-26 19:16:24.908504= 368 +0100 > @@ -0,0 +1,29 @@ > +/* PR tree-optimization/113614 */ > +/* { dg-do run { target bitint } } */ > +/* { dg-options "-std=3Dc23 -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__ >=3D 256 > +_BitInt(256) > +foo (_BitInt(8) y, unsigned _BitInt(256) z) > +{ > + unsigned _BitInt(256) d =3D -y; > + z /=3D d; > + return z + a + b + c; > +} > +#endif > + > +int > +main () > +{ > +#if __BITINT_MAXWIDTH__ >=3D 256 > + if (foo (0xfwb, 0x24euwb)) > + __builtin_abort (); > +#endif > + return 0; > +} >=20 > Jakub >=20