Path: utzoo!telly!ddsw1!mcdchg!rutgers!tut.cis.ohio-state.edu!ODDJOB.UCHICAGO.EDU!lmayk!lgm
From: lmayk!lgm@ODDJOB.UCHICAGO.EDU
Newsgroups: gnu.gcc.bug
Subject: GCC Version 1.31, '-fstrength-reduce' generates bad assembly
Message-ID: <8811260051.aa00576@gamma.eecs.nwu.edu>
Date: 26 Nov 88 00:58:11 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 102

SYNOPSIS:
The '-fstrength-reduce' flag can produce incorrect code when
it attempts to reduce the strength of a nontrivial (i.e., function-
requiring) multiplication more than once within the same loop.
The example below shows incorrect assembly language generated by
'-O -fstrength-reduce'; '-O' alone generates correct code.

The example is simplified from "cccp.c".

MACHINE AND OS:
AT&T UNIX PC 3B1 (based on Motorola 68010 microprocessor) running
OS Version 3.5 (compatible with UNIX System V Release 2).

CONFIGURATION FILES:
(usual for the UNIX PC)
"tm-3b1.h"
"xm-3b1.h"	<== By the way, the reference to "config-m68k.h"
			in this file must be changed to "xm-m68k.h".
"m68k.md"
"output-m68k.c"

TRANSCRIPT (INPUT FILE, COMMAND LINE, OUTPUT):
_______________________________________
$ cat badstr.c
struct	S {				/* structure is 14 bytes long */
	int	dummy1;
	short	dummy2;
	char	*field1;
	char	*field2;
};

extern	void	g();

void
f( nels, arr )
int	nels;
struct	S	*arr;
{
	int	i;

	for ( i = 0; i < nels; ++i ) {
		if ( arr[ i ].field1 )
			g( arr[ i ].field1 );
		if ( arr[ i ].field2 )
			g( arr[ i ].field2 );
	}
}
$ gcc -v -S -O -fstrength-reduce badstr.c
gcc version 1.31
 /usr/local/lib/gcc-cpp -v -undef -D__GNU__ -D__GNUC__ -Dmc68k -Dunix -Dunixpc -D__OPTIMIZE__ badstr.c /tmp/cca03041.cpp
GNU CPP version 1.31
 /usr/local/lib/gcc-cc1 /tmp/cca03041.cpp -quiet -dumpbase badstr.c -fstrength-reduce -O -version -o badstr.s
GNU C version 1.31 (68k, SGS/AT&T unixpc syntax) compiled by GNU C version 1.31.
$ cat badstr.s
	file	"badstr.c"
text
	even
	global f
f:
	link.w %a6,&0
	movm.l &0x3f20,-(%sp)
	mov.l 8(%fp),%d4
	mov.l 12(%fp),%a2
	clr.l %d2
	cmp.l %d4,%d2
	ble.w L%8
	clr.l %d3
L%7:
	tst.l 6(%a2,%d3.l)		#  %d3  is correct here
	beq.w L%5
	mov.l 6(%a2,%d5.l),-(%sp)	#  %d5  is garbage here
	jsr g
	addq.w &4,%sp
L%5:
	tst.l 10(%a2,%d6.l)		#  %d6  is garbage here
	beq.w L%4
	mov.l 10(%a2,%d7.l),-(%sp)	#  %d7  is garbage here
	jsr g
	addq.w &4,%sp
L%4:
	mov.l &14,%d0
	add.l %d0,%d3		#  %d3  is correctly incremented by 14
	addq.l &1,%d2
	cmp.l %d4,%d2
	bgt.w L%7
L%8:
	movm.l -28(%a6),&0x4fc
	unlk %a6
	rts
$
_______________________________________

EXPLANATION OF TRANSCRIPT:
The generated assembly language above *uses*  %d5 ,  %d6 ,  and
%d7  without ever assigning them meaningful values.  The correct
register to use in all three places would have been  %d3 .


	Lawrence G. Mayka
	Aurora, Illinois

	chinet!lmayk!lgm