From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5094C385042D; Thu, 30 Jul 2020 19:08:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5094C385042D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596136130; bh=rB1GJHkcy/xHIj+FCgtiEoTZKdKA2rfSVEZcS4uzhtg=; h=From:To:Subject:Date:From; b=X3QiRs+CfkzgsYr09tIRqZW1E/hJjtJQ3SMHKUjYxnQGJ2lEOIKurw4XxxcVxjw2m go3vuDL0UhQ+v27yXOGVbAxzabHP7D9gQhYl2UkEiBXkVFzrfpWm0f8LduorzBFQ4i dxEItzKV9icUHIwVZaaGqaNRFvhvGi8PJe113Q2o= From: "hugo_musso_gualandi at hotmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/96392] New: Optimize x+0.0 if x is an integer Date: Thu, 30 Jul 2020 19:08:50 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 10.1.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: hugo_musso_gualandi at hotmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Jul 2020 19:08:50 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96392 Bug ID: 96392 Summary: Optimize x+0.0 if x is an integer Product: gcc Version: 10.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: hugo_musso_gualandi at hotmail dot com Target Milestone: --- One way to convert an integer to a floating point number in C is to multipl= y it by 1.0. In this case, gcc is clever enough to optimize away the multiplicat= ion. Another way is to add 0.0. However, in this case, GCC does not optimize away the addition.=20 Example C code: double times1(int x) { return x * 1.0; } double plus0(int x) { return x + 0.0; } Output of objdump -d after compiling with gcc -O2 -c: 0000000000000000 : 0: 66 0f ef c0 pxor %xmm0,%xmm0 4: f2 0f 2a c7 cvtsi2sd %edi,%xmm0 8: c3 retq=20=20=20 9: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 0000000000000010 : 10: 66 0f ef c0 pxor %xmm0,%xmm0 14: f2 0f 2a c7 cvtsi2sd %edi,%xmm0 18: f2 0f 58 05 00 00 00 addsd 0x0(%rip),%xmm0 1f: 00=20 20: c3 retq=20=20=20 I believe that the reason that GCC does not optimize x+0.0 is that it is worried that x could be negative zero. However, promoting an integer to floating point can never yield negative zero so it should be possible to optimize in this particular case. (For the matter, Clang does optimize it.)=