This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/11643] New: inline ASM; gcc choses same registers for differrent variables set to "r" (variable)
- From: "cb100 at gmx dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 23 Jul 2003 11:41:23 -0000
- Subject: [Bug c/11643] New: inline ASM; gcc choses same registers for differrent variables set to "r" (variable)
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11643
Summary: inline ASM; gcc choses same registers for differrent
variables set to "r" (variable)
Product: gcc
Version: 3.2.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: cb100 at gmx dot net
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu
It looks like the automatic register allocation in the extended ASM doesn't work
the way it should.
The function maxElement (see below) needs 3 registers, from which 2 are chosen
by gcc (array and maximum).
The bug (?) is the following: array and maximum receive the same register (edx
in my case).
A programm which illustrates the problem is given below: the maximal value
should be 3, but 0 is returned.
Removing "r" from array and maximum and replacing it with "S" and "D"
respectivly solves the problem.
------------------------- bug.c -------------------------
#include <stdlib.h>
#include <stdio.h>
#define UINT unsigned int
#define SIZE 4096
float maxElement(float *array, int n) {
float *maximum, max;
posix_memalign((void**) &maximum, 16, 16);
int i = n >> 4; // n/16
n &= 15; // n%16
__asm__ __volatile__ (
"movaps (%0), %%xmm0\n"
"loop_max_el:\n"
"movaps (%0), %%xmm1\n"
"movaps 16(%0), %%xmm2\n"
"movaps 32(%0), %%xmm3\n"
"movaps 48(%0), %%xmm4\n"
"maxps %%xmm4, %%xmm3\n"
"maxps %%xmm3, %%xmm2\n"
"maxps %%xmm2, %%xmm1\n"
"maxps %%xmm1, %%xmm0\n"
"addl $64, %0\n"
"subl $1, %%ecx\n"
"jnz loop_max_el\n"
"movaps %%xmm0, (%1)\n"
: "=r" (array)
: "0" (array), "r" (maximum), "c" (i)
: "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "memory"
);
max = maximum[0];
for (i=1;i<4;i++) // search for the maximal element
max = maximum[i]>max?maximum[i]:max;
return max;
}
int main(int argc, char** argv) {
float *array, retval;
posix_memalign((void**) &array, 16, sizeof(*array) * SIZE);
int i;
for (i=0;i<SIZE;i++) {
array[i] = (i%2)==0?2.0:3.0;
}
retval = maxElement(array, SIZE);
printf("value: %f\n", retval);
return EXIT_SUCCESS;
}
------------------------------------------------------------
This code is generated by compiling with "gcc -c -S bug.c" on my Pentium 4.
[...]
#APP
movaps (%edx), %xmm0 // array = edx
loop_max_el:
movaps (%edx), %xmm1
movaps 16(%edx), %xmm2
movaps 32(%edx), %xmm3
movaps 48(%edx), %xmm4
maxps %xmm4, %xmm3
maxps %xmm3, %xmm2
maxps %xmm2, %xmm1
maxps %xmm1, %xmm0
addl $64, %edx
subl $1, %ecx
jnz loop_max_el
movaps %xmm0, (%edx) // maximum = edx
#NO_APP
[...]
-------------------------------------------------
gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/specs
Configured with: /var/tmp/portage/gcc-3.2.3-r1/work/gcc-3.2.3/configure
--prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.2
--includedir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include
--datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2
--mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2/man
--infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.2/info --enable-shared
--host=i686-pc-linux-gnu --target=i686-pc-linux-gnu --with-system-zlib
--enable-languages=c,c++,ada,f77,objc,java --enable-threads=posix
--enable-long-long --disable-checking --enable-cstdio=stdio
--enable-clocale=generic --enable-__cxa_atexit
--enable-version-specific-runtime-libs
--with-gxx-include-dir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/g++-v3
--with-local-prefix=/usr/local --enable-shared --enable-nls
--without-included-gettext
Thread model: posix
gcc version 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r1, propolice)