🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Robert E. Brown Fri, 24 Jul 1992 19:50
optimization question
#197
Author: Robert E. Brown
Date: Fri, 24 Jul 1992 19:50
117 lines
3170 bytes
While not exactly a bug report, perhaps the lack of consistency in GCC's
output will count as a bug ...


I am machine generating a lot of C code that contains redundant stores to
global variables and GCC (versions 2.2 and 2.2.2) is not optimizing away
some of the stores that I expect it to delete.  It's likely, however, that
I don't fully understand when it's possible to remove stores to global
variables that are not declared as "volatile".  For instance, is it legal to
optimize the following code:

	long R, A, B, C, D;

	void
	foo()
	{
	R = 0;
		A = (R < 0);
		B = (R == 0);
		C = 0;
		D = 0;

	R = 1;
		A = (R < 0);
		B = (R == 0);
		C = 0;
		D = 0;
	}


into assembly code that implements just

	{
	R = 1;
		A = 0;
		B = 0;
		C = 0;
		D = 0;
	}

or does ANSI say that the compiler has to be smart about making things look
right to a signal handler that may get invoked while foo() is running?
Here's what "gcc -O -S foo.c" (gcc version 2.2.2) produces when configured for
"next" -- a 68K-based computer.  This output seems to be consistent with the
desire to make the global variables consistent with respect to a signal
handler.  Signals can be delivered between each pair of sequential
instructions, and the handler would not see a variable state that's
inconsistent with naive code for foo().  The compiler has deleted some stores,
however.

	#NO_APP
	.text
		.align 1
	.globl _foo
	_foo:
		link a6,#0
		clrl _R
		clrl _A
		moveq #1,d0
		movel d0,_B
		clrl _C
		clrl _D
		movel d0,_R
		clrl _B
		unlk a6
		rts
	.comm _R,4
	.comm _A,4
	.comm _B,4
	.comm _C,4
	.comm _D,4


On the SPARC, "gcc -O -S foo.c" (GCC version 2.2, configured for "sparc")
produces the following code, which retains all the stores to the global
variables.  Even C and D are written more than once -- and their values
definitely don't after the first store!  What is the best code that GCC could
produce?  Must it look like the 68K code, or can it be optimized to the
assembly code equivalent to {R=1;A=B=C=D=0}?

	gcc2_compiled.:
	.text
		.align 4
		.global _foo
		.proc	020
	_foo:
		!#PROLOGUE# 0
		save %sp,-112,%sp
		!#PROLOGUE# 1
		sethi %hi(_R),%i3
		st %g0,[%i3+%lo(_R)]
		sethi %hi(_A),%i2
		st %g0,[%i2+%lo(_A)]
		sethi %hi(_B),%i1
		mov 1,%g2
		st %g2,[%i1+%lo(_B)]
		sethi %hi(_C),%i0
		st %g0,[%i0+%lo(_C)]
		sethi %hi(_D),%g3
		st %g0,[%g3+%lo(_D)]
		st %g2,[%i3+%lo(_R)]
		st %g0,[%i2+%lo(_A)]
		st %g0,[%i1+%lo(_B)]
		st %g0,[%i0+%lo(_C)]
		st %g0,[%g3+%lo(_D)]
		ret
		restore
		.global _R
		.common _R,8,"bss"
		.global _A
		.common _A,8,"bss"
		.global _B
		.common _B,8,"bss"
		.global _C
		.common _C,8,"bss"
		.global _D
		.common _D,8,"bss"
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads