Bug 40747 - [4.4/4.5 Regression] wrong code for int-is-in-range test at -O1 and above
Summary: [4.4/4.5 Regression] wrong code for int-is-in-range test at -O1 and above
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.0
: P1 normal
Target Milestone: 4.4.1
Assignee: Jakub Jelinek
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords:
: 40547 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-07-14 16:32 UTC by Zdenek Sojka
Modified: 2009-10-21 10:51 UTC (History)
4 users (show)

See Also:
Host: x86_64-pc-linux-gnu
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-07-14 17:25:58


Attachments
preprocessed source (3.27 KB, text/plain)
2009-07-14 16:35 UTC, Zdenek Sojka
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Zdenek Sojka 2009-07-14 16:32:05 UTC
Tested with:

trunk r149624
# ./gcc -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-languages=c,c++ --prefix=/mnt/svn/gcc-trunk/build/
Thread model: posix
gcc version 4.5.0 20090714 (experimental) (GCC)

and gentoo's 4.4.0, 4.5_alpha20090709

The following code is miscompiled
------------------------
// command line: gcc tst.c -c -o tst.o -O1
// or: with -m32
int f3(int i) {
	return (i < 4 && i >= 0) ? i : 4;
}
------------------------

Resulting (relevant) asm is:
------------------------
f3:
.LFB0:
	.cfi_startproc
	cmpl	$4, %edi
	movl	$4, %eax
	cmovle	%edi, %eax
	ret
	.cfi_endproc
------------------------

For the following code:
------------------------
// command line: gcc tst.c -c -o tst.o -O1
// or: with -m32
#include <stdio.h>

int f3(int i) {
	return (i < 4 && i >= 0) ? i : 4;
}

int main() {
	printf("%d %d %d\n", f3(-1), f3(2), f3(5));
}
------------------------
Result is "-1 2 4", but it should be "4 2 4" (as it is with -O0)
Comment 1 Zdenek Sojka 2009-07-14 16:35:54 UTC
Created attachment 18194 [details]
preprocessed source

most of that file is content of included <stdio.h>
Comment 2 Ozkan Sezer 2009-07-14 17:16:33 UTC
Also happens with i686-pc-linux-gnu with gcc-4.4.1 (gcc-4_4-branch, r149543).
Comment 3 Jakub Jelinek 2009-07-14 17:25:54 UTC
Confirmed, introduced between r134374 + r134467, looking into it.
Comment 4 Jakub Jelinek 2009-07-14 18:00:10 UTC
Indeed, caused by http://gcc.gnu.org/ml/gcc-patches/2008-04/msg01303.html
(r134384).
Comment 5 Ozkan Sezer 2009-07-15 08:19:08 UTC
This bug may result in unreliable binary outputs, why is it targeted for fixing in 4.4.2 and not in 4.4.1?
Comment 6 Jakub Jelinek 2009-07-15 08:28:40 UTC
I'm already bootstrapping/regtesting a fix, will post afterwards.
If it gets approved quickly, I'll include it in 4.4.1-rc1 I plan to roll today.
Comment 7 Jakub Jelinek 2009-07-15 09:48:22 UTC
Patch posted: http://gcc.gnu.org/ml/gcc-patches/2009-07/msg00842.html
Comment 8 Jakub Jelinek 2009-07-15 10:18:10 UTC
Subject: Bug 40747

Author: jakub
Date: Wed Jul 15 10:17:54 2009
New Revision: 149675

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=149675
Log:
	PR middle-end/40747
	* fold-const.c (fold_cond_expr_with_comparison): When folding
	< and <= to MIN, make sure the MIN uses the same type as the
	comparison's operands.

	* gcc.c-torture/execute/pr40747.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr40747.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 9 Jakub Jelinek 2009-07-15 11:23:36 UTC
Subject: Bug 40747

Author: jakub
Date: Wed Jul 15 11:23:22 2009
New Revision: 149681

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=149681
Log:
	PR middle-end/40747
	* fold-const.c (fold_cond_expr_with_comparison): When folding
	< and <= to MIN, make sure the MIN uses the same type as the
	comparison's operands.

	* gcc.c-torture/execute/pr40747.c: New test.

Added:
    branches/gcc-4_4-branch/gcc/testsuite/gcc.c-torture/execute/pr40747.c
Modified:
    branches/gcc-4_4-branch/gcc/ChangeLog
    branches/gcc-4_4-branch/gcc/fold-const.c
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog

Comment 10 Jakub Jelinek 2009-07-15 11:26:30 UTC
Fixed.
Comment 11 Mikael Pettersson 2009-10-21 10:51:00 UTC
*** Bug 40547 has been marked as a duplicate of this bug. ***