Bug 21297 - [4.0 Regression] buf[i+i]=0 stores buf[i] when -O2
Summary: [4.0 Regression] buf[i+i]=0 stores buf[i] when -O2
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.1
Assignee: Jakub Jelinek
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: patch, wrong-code
Depends on:
Blocks:
 
Reported: 2005-04-30 15:54 UTC by akr
Modified: 2005-05-04 08:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 3.4.4 4.1.0
Known to fail: 4.0.0
Last reconfirmed: 2005-05-02 13:04:19


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description akr 2005-04-30 15:54:33 UTC
gcc 4.0.0 optimization seems to have a problem.
In the f function in following source, buf[i+i] = '\0' stores '\0' to buf[2].
But it should store to buf[4]. 

% cat t.c
extern int printf (__const char *__restrict __format, ...);

void
f(char *buf)
{
  int i;
  for (i = 0; i < 2; i++) ;
  printf("i=%d\n", i);
  buf[i+i] = '\0';
}

int main(int argc, char **argv)
{
  char buf[] = "0123456789";
  f(buf);
  printf("%s\n", buf);
  return 0;
}
% gcc -v -O2 t.c
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../gcc-4.0.0/configure --prefix=/home/src/gcc
Thread model: posix
gcc version 4.0.0
 /home/src/gcc/libexec/gcc/i686-pc-linux-gnu/4.0.0/cc1 -quiet -v t.c -quiet
-dumpbase t.c -mtune=pentiumpro -auxbase t -O2 -version -o /tmp/ccKF4bLU.s
ignoring nonexistent directory
"/home/src/gcc/lib/gcc/i686-pc-linux-gnu/4.0.0/../../../../i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /home/src/gcc/include
 /home/src/gcc/lib/gcc/i686-pc-linux-gnu/4.0.0/include
 /usr/include
End of search list.
GNU C version 4.0.0 (i686-pc-linux-gnu)
        compiled by GNU C version 4.0.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 as -V -Qy -o /tmp/ccm6JTQG.o /tmp/ccKF4bLU.s
GNU assembler version 2.15 (i386-linux) using BFD version 2.15
 /home/src/gcc/libexec/gcc/i686-pc-linux-gnu/4.0.0/collect2 --eh-frame-hdr -m
elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o
/home/src/gcc/lib/gcc/i686-pc-linux-gnu/4.0.0/crtbegin.o
-L/home/src/gcc/lib/gcc/i686-pc-linux-gnu/4.0.0
-L/home/src/gcc/lib/gcc/i686-pc-linux-gnu/4.0.0/../../.. /tmp/ccm6JTQG.o -lgcc
--as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed
/home/src/gcc/lib/gcc/i686-pc-linux-gnu/4.0.0/crtend.o /usr/lib/crtn.o
% ./a.out 
i=2
01

The binary produces "01".  But it should be "0123".
If gcc 3.3.5 is used, the binary produces "0123".
Comment 1 Andrew Pinski 2005-04-30 16:00:19 UTC
This is another expand issue:
;; *((char *) (unsigned int) (i + i) + buf) = 0
(insn 27 25 0 (set (mem:QI (plus:SI (mult:SI (reg/v:SI 60 [ i ])
                    (const_int 1 [0x1]))
                (reg/v/f:SI 61 [ buf ])) [0 S1 A8])
        (const_int 0 [0x0])) -1 (nil)
    (nil))

Why is it multiplying by one and not by two.

Confirmed.
Comment 2 Jakub Jelinek 2005-05-02 13:04:18 UTC
This is a target bug.
Comment 3 Jakub Jelinek 2005-05-02 13:42:24 UTC
Patch here: <http://gcc.gnu.org/ml/gcc-patches/2005-05/msg00073.html>
Comment 4 CVS Commits 2005-05-03 13:01:27 UTC
Subject: Bug 21297

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jakub@gcc.gnu.org	2005-05-03 12:58:13

Modified files:
	gcc            : ChangeLog 
	gcc/config/i386: i386.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.c-torture/execute: 20050502-2.c 

Log message:
	PR target/21297
	* config/i386/i386.c (legitimize_address): When canonicalizing
	ASHIFT into MULT, multiply by 1 << shift_count instead of
	1 << log2 (shift_count).
	
	* gcc.c-torture/execute/20050502-2.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.8572&r2=2.8573
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/i386/i386.c.diff?cvsroot=gcc&r1=1.816&r2=1.817
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5433&r2=1.5434
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20050502-2.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 5 CVS Commits 2005-05-04 07:24:54 UTC
Subject: Bug 21297

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	jakub@gcc.gnu.org	2005-05-04 07:24:34

Modified files:
	gcc            : ChangeLog 
	gcc/testsuite  : ChangeLog 
	gcc/config/i386: i386.c 
Added files:
	gcc/testsuite/gcc.c-torture/execute: 20050502-2.c 

Log message:
	PR target/21297
	* config/i386/i386.c (legitimize_address): When canonicalizing
	ASHIFT into MULT, multiply by 1 << shift_count instead of
	1 << log2 (shift_count).
	
	* gcc.c-torture/execute/20050502-2.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=2.7592.2.214&r2=2.7592.2.215
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.5084.2.157&r2=1.5084.2.158
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/i386/i386.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.795.6.6&r2=1.795.6.7
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20050502-2.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1

Comment 6 Giovanni Bajo 2005-05-04 08:16:29 UTC
Fixed!
Comment 7 CVS Commits 2005-05-26 17:50:02 UTC
Subject: Bug 21297

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-rhl-branch
Changes by:	jakub@gcc.gnu.org	2005-05-26 17:49:50

Modified files:
	gcc            : ChangeLog 
	gcc/config/i386: i386.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.c-torture/execute: 20050502-2.c 

Log message:
	PR target/21297
	* config/i386/i386.c (legitimize_address): When canonicalizing
	ASHIFT into MULT, multiply by 1 << shift_count instead of
	1 << log2 (shift_count).
	
	* gcc.c-torture/execute/20050502-2.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-rhl-branch&r1=2.2326.2.399.2.78&r2=2.2326.2.399.2.79
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/i386/i386.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-rhl-branch&r1=1.635.2.8.2.11&r2=1.635.2.8.2.12
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-rhl-branch&r1=1.3389.2.170.2.49&r2=1.3389.2.170.2.50
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20050502-2.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-rhl-branch&r1=NONE&r2=1.1.8.1

Comment 8 CVS Commits 2005-05-29 16:02:39 UTC
Subject: Bug 21297

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	pault@gcc.gnu.org	2005-05-29 16:02:11

Modified files:
	gcc/fortran    : trans-array.c trans-expr.c 

Log message:
	2005-05-29 Paul Thomas  <pault@gcc.gnu.org>
	
	PR fortran/16939
	PR fortran/17192
	PR fortran/17193
	PR fortran/17202
	PR fortran/18689
	PR fortran/18890
	PR fortran/21297
	* fortran/trans-array.c (gfc_conv_resolve_dependencies): Add string
	length to temp_ss for character pointer array assignments.
	* fortran/trans-expr.c (gfc_conv_variable): Correct errors in
	dereferencing of characters and character pointers.
	* fortran/trans-expr.c (gfc_conv_function_call): Provide string
	length as return argument for various kinds of handling of return.
	Return a char[]* temporary for character pointer functions and
	dereference the temporary upon return.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-array.c.diff?cvsroot=gcc&r1=1.45&r2=1.46
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-expr.c.diff?cvsroot=gcc&r1=1.44&r2=1.45