From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id B62E03858282 for ; Wed, 10 Apr 2024 10:03:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B62E03858282 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B62E03858282 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=89.208.246.23 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712743438; cv=none; b=ijbr5OU3HYvG5EeEbUvtE6u3bBbz1/dwekdeR1SmGqrzi2TtXp36xbcovz6g0mrLrTLbkkZE1QyFvnxK/j4cKdoDGnaMpnxO94QSu39WqnZVCkKt7MeRoof1zww1yCwCi0KNPnHkH3ZzBGPG2YhgU/dUPaP8ocf8HmyTpUSKIh0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712743438; c=relaxed/simple; bh=LqmnZwc1MhB6M310NJoFdJX4X6H22i/NeNdTybzYFNk=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=BgodllW10xBbAdVPqvkUpH/Jc5cneUG1MV9nZih5XbpODA1kw4hME3TOrDfV6sChcRaBwAPt+jZjxMXs1m0q3Y+gQnk2MRNwNBoTpyeOiyabFf+goyn7VvEOKoXCbAUeTqfiFEMWfTIPs4RcsFDPT8E8tzMRpGOEMYpzFm6BfSg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1712743425; bh=LqmnZwc1MhB6M310NJoFdJX4X6H22i/NeNdTybzYFNk=; h=Subject:From:To:Date:In-Reply-To:References:From; b=gAdPQzOlNsrs65dXBjOGmrVFfTEKMqzhIKgelMlVYrYDLJVzjmJ6A0qN+eLpAiwXb lnMo9z4tLSnmOuRvDxb3khdyq4EfkNGYtWkuFZ+3uXpNGB4L8TyZ4E+EKCn+6xJj79 wdTPwMnRhGJQJEXJgdflOlG+V+HJB6VUrMkCAqaE= Received: from [127.0.0.1] (unknown [IPv6:2001:470:683e::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384)) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 0F8C71A3FDF; Wed, 10 Apr 2024 06:03:44 -0400 (EDT) Message-ID: Subject: Re: AW: optimizer discards sign information From: Xi Ruoyao To: LIU Hao , stefan@franke.ms, gcc-help@gcc.gnu.org Date: Wed, 10 Apr 2024 18:03:43 +0800 In-Reply-To: <20096916-dc47-4cc9-93c1-bc910ef07327@126.com> References: <016501da8b24$735d5170$5a17f450$@franke.ms> <018301da8b28$e7a601e0$b6f205a0$@franke.ms> <018d01da8b2c$59e89e90$0db9dbb0$@franke.ms> <20096916-dc47-4cc9-93c1-bc910ef07327@126.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.0 MIME-Version: 1.0 X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,LIKELY_SPAM_FROM,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 2024-04-10 at 17:57 +0800, LIU Hao via Gcc-help wrote: > =E5=9C=A8 2024-04-10 17:49, stefan@franke.ms=C2=A0=E5=86=99=E9=81=93: > > But I keep considering this as a bug. And clang behaves correctly! >=20 > Yes there have been many reports [1]. It's a missed optimization. Note that for this specific case: typedef unsigned long long int u64; typedef unsigned int u32; typedef unsigned short u16; =20 u64 foo(u16 *a, u16 *b) { u32 x =3D *a * *b; u64 r =3D x; return r >> 31; } =20 gcc yields =20 foo: xor eax, eax ret =20 clang yields =20 foo: # @foo movzx ecx, word ptr [rdi] movzx eax, word ptr [rsi] imul eax, ecx shr eax, 31 ret =20 It's actually a missed-optimization of **clang**. Optimizing this function to always return 0 **is** correct. But for the general case: u64 foo(u16 a, u16 b) { u32 x =3D a * b; u64 r =3D x; return r; } there is a missed-optimization of GCC (redundant sign extension). > You may work around it by using 32-bit parameters, or casting either > operand to u32; casting the result will not help. Indeed. --=20 Xi Ruoyao School of Aerospace Science and Technology, Xidian University