From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 97585 invoked by alias); 27 Sep 2015 21:49:04 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 97542 invoked by uid 48); 27 Sep 2015 21:48:59 -0000 From: "aurelien at aurel32 dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/67736] New: Wrong optimization with -fexpensive-optimizations on mips64el Date: Sun, 27 Sep 2015 21:49:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 5.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: aurelien at aurel32 dot net 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 cf_gcchost cf_gcctarget cf_gccbuild Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-09/txt/msg02128.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67736 Bug ID: 67736 Summary: Wrong optimization with -fexpensive-optimizations on mips64el Product: gcc Version: 5.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: aurelien at aurel32 dot net Target Milestone: --- Host: mips64el-unknown-linux-gnu Target: mips64el-unknown-linux-gnu Build: mips64el-unknown-linux-gnu The following code is wrongly compiled on a mips64el target when using -fexpensive-optimizations: #include #include int compare(uint64_t state, uint32_t *last, uint8_t buf) { if (*last == ((state | buf) & 0xFFFFFFFF)) { printf("B %"PRIx64" %"PRIx32"\n", state, *last); return 0; } return 1; } It produces the following code: 00000000000009a0 : 9a0: 30c200ff andi v0,a2,0xff 9a4: 8ca60000 lw a2,0(a1) 9a8: 00441025 or v0,v0,a0 9ac: 10460004 beq v0,a2,9c0 9b0: 24020001 li v0,1 9b4: 03e00008 jr ra 9b8: 00000000 nop 9bc: 00000000 nop ... Note how the comparison is done incorrectly, dropping the & 0xFFFFFFFF and sign-extending the value when loading *last. Using -fno-expensive-optimizations produces the following valid code: 00000000000009a0 : 9a0: 30c200ff andi v0,a2,0xff 9a4: 8ca60000 lw a2,0(a1) 9a8: 00441025 or v0,v0,a0 9ac: 7cc3f803 dext v1,a2,0x0,0x20 9b0: 7c42f803 dext v0,v0,0x0,0x20 9b4: 10620004 beq v1,v0,9c8 9b8: 24020001 li v0,1 9bc: 03e00008 jr ra 9c0: 00000000 nop 9c4: 00000000 nop 9c8: 67bdfff0 daddiu sp Here both values are zero extended to 32-bit before the comparison. Alternatively, when removing the line with the printf, GCC also produces correct code, which is more optimized: 0000000000000960 : 960: 30c600ff andi a2,a2,0xff 964: 00c42025 or a0,a2,a0 968: 9ca20000 lwu v0,0(a1) 96c: 7c86f803 dext a2,a0,0x0,0x20 970: 00c21026 xor v0,a2,v0 974: 03e00008 jr ra 978: 0002102b sltu v0,zero,v0 97c: 00000000 nop Note that at least gcc 4.8.5, 4.9.3 and 5.2.1 are affected.