[net.bugs.4bsd] [4bsd-f77 #32] F77 sometimes won't optimize subscripts

4bsd-f77@utah-cs.UUCP (08/25/84)

From: Donn Seeley <donn@utah-cs.arpa>

Subject: F77 sometimes won't optimize subscripts
Index:	usr.bin/f77/src/f77pass1 4.2BSD

Description:
	Expressions in subscripts are not subject to expansion prior to
	optimization in f77, and as a result some optimizations are
	missed.

Repeat-By:
	Clip out the following program and compile it down to assembly
	language with the optimizer on:

	----------------------------------------------------------------
		program memo

		integer i, j, m(8)

		i = 2
		j = i ** 3
		m(i ** 3) = j + 1

		print *, m(8)

		stop
		end
	----------------------------------------------------------------

	You should see something like the following (this has been
	prettied up for readability):

	----------------------------------------------------------------
		movl	$2,{i}
		mull3	{i},{i},r0
		mull3	{i},r0,{j}
		movl	{i},-4(fp)
		movl	-4(fp),-8(fp)
		addl3	$1,{j},r0
		mull3	-4(fp),-4(fp),r1
		mull2	-8(fp),r1
		movl	r0,&{m}-4[r1]
	----------------------------------------------------------------

	Results will vary depending on whether you installed the fix
	for exponentiations and CSE which I posted earlier.

Fix:
	The problem is that the routine expand() in optim.c is used to
	convert complicated operations like min/max or exponentiations
	into simpler forms that can be optimized, but it fails to
	convert subscripts.  The following change fixes this problem:

	----------------------------------------------------------------
	*** /tmp/,RCSt1002935	Wed Aug  1 16:07:11 1984
	--- optim.c	Wed Aug  1 16:06:45 1984
	***************
	*** 535,540
		case TTEMP:
			if (p->tempblock.istemp)
				frtemp(p);
			break;
	  
		default:

	--- 538,547 -----
		case TTEMP:
			if (p->tempblock.istemp)
				frtemp(p);
	+ 		break;
	+ 
	+ 	case TADDR:
	+ 		p->addrblock.memoffset = expand( p->addrblock.memoffset );
			break;
	  
		default:
	----------------------------------------------------------------

	This change by itself is not enough.  Since expansions can
	create 'comma' operators, and the common subexpression
	elimination code requires that expressions not contain comma
	operators, rmcommaop() in optcse.c must be changed so that
	comma operators in subscripts get removed too:

	----------------------------------------------------------------
	*** /tmp/,RCSt1002953	Wed Aug  1 16:11:29 1984
	--- optcse.c	Wed Aug  1 16:05:30 1984
	***************
	*** 859,864
		case TLIST:
			for (cp = p->listblock.listp; cp; cp = cp->nextp)
				cp->datap = (tagptr) rmcommaop (cp->datap,sl);
			return (p);
	  
		default:

	--- 862,871 -----
		case TLIST:
			for (cp = p->listblock.listp; cp; cp = cp->nextp)
				cp->datap = (tagptr) rmcommaop (cp->datap,sl);
	+ 		return (p);
	+ 
	+ 	case TADDR:
	+ 		p->addrblock.memoffset = rmcommaop (p->addrblock.memoffset,sl);
			return (p);
	  
		default:
	----------------------------------------------------------------

Donn Seeley    University of Utah CS Dept    donn@utah-cs.arpa
40 46' 6"N 111 50' 34"W    (801) 581-5668    decvax!utah-cs!donn