github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/cmd/6c/reg.c (about)

     1  // Inferno utils/6c/reg.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6c/reg.c
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  #include "gc.h"
    32  
    33  static	void	fixjmp(Reg*);
    34  
    35  Reg*
    36  rega(void)
    37  {
    38  	Reg *r;
    39  
    40  	r = freer;
    41  	if(r == R) {
    42  		r = alloc(sizeof(*r));
    43  	} else
    44  		freer = r->link;
    45  
    46  	*r = zreg;
    47  	return r;
    48  }
    49  
    50  int
    51  rcmp(const void *a1, const void *a2)
    52  {
    53  	Rgn *p1, *p2;
    54  	int c1, c2;
    55  
    56  	p1 = (Rgn*)a1;
    57  	p2 = (Rgn*)a2;
    58  	c1 = p2->cost;
    59  	c2 = p1->cost;
    60  	if(c1 -= c2)
    61  		return c1;
    62  	return p2->varno - p1->varno;
    63  }
    64  
    65  void
    66  regopt(Prog *p)
    67  {
    68  	Reg *r, *r1, *r2;
    69  	Prog *p1;
    70  	int i, z;
    71  	int32 initpc, val, npc;
    72  	uint32 vreg;
    73  	Bits bit;
    74  	struct
    75  	{
    76  		int32	m;
    77  		int32	c;
    78  		Reg*	p;
    79  	} log5[6], *lp;
    80  
    81  	firstr = R;
    82  	lastr = R;
    83  	nvar = 0;
    84  	regbits = RtoB(D_SP) | RtoB(D_AX) | RtoB(D_X0);
    85  	for(z=0; z<BITS; z++) {
    86  		externs.b[z] = 0;
    87  		params.b[z] = 0;
    88  		consts.b[z] = 0;
    89  		addrs.b[z] = 0;
    90  	}
    91  
    92  	/*
    93  	 * pass 1
    94  	 * build aux data structure
    95  	 * allocate pcs
    96  	 * find use and set of variables
    97  	 */
    98  	val = 5L * 5L * 5L * 5L * 5L;
    99  	lp = log5;
   100  	for(i=0; i<5; i++) {
   101  		lp->m = val;
   102  		lp->c = 0;
   103  		lp->p = R;
   104  		val /= 5L;
   105  		lp++;
   106  	}
   107  	val = 0;
   108  	for(; p != P; p = p->link) {
   109  		switch(p->as) {
   110  		case ADATA:
   111  		case AGLOBL:
   112  		case ANAME:
   113  		case ASIGNAME:
   114  		case AFUNCDATA:
   115  			continue;
   116  		}
   117  		r = rega();
   118  		if(firstr == R) {
   119  			firstr = r;
   120  			lastr = r;
   121  		} else {
   122  			lastr->link = r;
   123  			r->p1 = lastr;
   124  			lastr->s1 = r;
   125  			lastr = r;
   126  		}
   127  		r->prog = p;
   128  		r->pc = val;
   129  		val++;
   130  
   131  		lp = log5;
   132  		for(i=0; i<5; i++) {
   133  			lp->c--;
   134  			if(lp->c <= 0) {
   135  				lp->c = lp->m;
   136  				if(lp->p != R)
   137  					lp->p->log5 = r;
   138  				lp->p = r;
   139  				(lp+1)->c = 0;
   140  				break;
   141  			}
   142  			lp++;
   143  		}
   144  
   145  		r1 = r->p1;
   146  		if(r1 != R)
   147  		switch(r1->prog->as) {
   148  		case ARET:
   149  		case AJMP:
   150  		case AIRETL:
   151  		case AIRETQ:
   152  			r->p1 = R;
   153  			r1->s1 = R;
   154  		}
   155  
   156  		bit = mkvar(r, &p->from);
   157  		if(bany(&bit))
   158  		switch(p->as) {
   159  		/*
   160  		 * funny
   161  		 */
   162  		case ALEAL:
   163  		case ALEAQ:
   164  			for(z=0; z<BITS; z++)
   165  				addrs.b[z] |= bit.b[z];
   166  			break;
   167  
   168  		/*
   169  		 * left side read
   170  		 */
   171  		default:
   172  			for(z=0; z<BITS; z++)
   173  				r->use1.b[z] |= bit.b[z];
   174  			break;
   175  		}
   176  
   177  		bit = mkvar(r, &p->to);
   178  		if(bany(&bit))
   179  		switch(p->as) {
   180  		default:
   181  			diag(Z, "reg: unknown op: %A", p->as);
   182  			break;
   183  
   184  		/*
   185  		 * right side read
   186  		 */
   187  		case ACMPB:
   188  		case ACMPL:
   189  		case ACMPQ:
   190  		case ACMPW:
   191  		case APREFETCHT0:
   192  		case APREFETCHT1:
   193  		case APREFETCHT2:
   194  		case APREFETCHNTA:
   195  		case ACOMISS:
   196  		case ACOMISD:
   197  		case AUCOMISS:
   198  		case AUCOMISD:
   199  			for(z=0; z<BITS; z++)
   200  				r->use2.b[z] |= bit.b[z];
   201  			break;
   202  
   203  		/*
   204  		 * right side write
   205  		 */
   206  		case ANOP:
   207  		case AMOVL:
   208  		case AMOVQ:
   209  		case AMOVB:
   210  		case AMOVW:
   211  		case AMOVBLSX:
   212  		case AMOVBLZX:
   213  		case AMOVBQSX:
   214  		case AMOVBQZX:
   215  		case AMOVLQSX:
   216  		case AMOVLQZX:
   217  		case AMOVWLSX:
   218  		case AMOVWLZX:
   219  		case AMOVWQSX:
   220  		case AMOVWQZX:
   221  		case AMOVQL:
   222  
   223  		case AMOVSS:
   224  		case AMOVSD:
   225  		case ACVTSD2SL:
   226  		case ACVTSD2SQ:
   227  		case ACVTSD2SS:
   228  		case ACVTSL2SD:
   229  		case ACVTSL2SS:
   230  		case ACVTSQ2SD:
   231  		case ACVTSQ2SS:
   232  		case ACVTSS2SD:
   233  		case ACVTSS2SL:
   234  		case ACVTSS2SQ:
   235  		case ACVTTSD2SL:
   236  		case ACVTTSD2SQ:
   237  		case ACVTTSS2SL:
   238  		case ACVTTSS2SQ:
   239  			for(z=0; z<BITS; z++)
   240  				r->set.b[z] |= bit.b[z];
   241  			break;
   242  
   243  		/*
   244  		 * right side read+write
   245  		 */
   246  		case AADDB:
   247  		case AADDL:
   248  		case AADDQ:
   249  		case AADDW:
   250  		case AANDB:
   251  		case AANDL:
   252  		case AANDQ:
   253  		case AANDW:
   254  		case ASUBB:
   255  		case ASUBL:
   256  		case ASUBQ:
   257  		case ASUBW:
   258  		case AORB:
   259  		case AORL:
   260  		case AORQ:
   261  		case AORW:
   262  		case AXORB:
   263  		case AXORL:
   264  		case AXORQ:
   265  		case AXORW:
   266  		case ASALB:
   267  		case ASALL:
   268  		case ASALQ:
   269  		case ASALW:
   270  		case ASARB:
   271  		case ASARL:
   272  		case ASARQ:
   273  		case ASARW:
   274  		case AROLB:
   275  		case AROLL:
   276  		case AROLQ:
   277  		case AROLW:
   278  		case ARORB:
   279  		case ARORL:
   280  		case ARORQ:
   281  		case ARORW:
   282  		case ASHLB:
   283  		case ASHLL:
   284  		case ASHLQ:
   285  		case ASHLW:
   286  		case ASHRB:
   287  		case ASHRL:
   288  		case ASHRQ:
   289  		case ASHRW:
   290  		case AIMULL:
   291  		case AIMULQ:
   292  		case AIMULW:
   293  		case ANEGL:
   294  		case ANEGQ:
   295  		case ANOTL:
   296  		case ANOTQ:
   297  		case AADCL:
   298  		case AADCQ:
   299  		case ASBBL:
   300  		case ASBBQ:
   301  
   302  		case AADDSD:
   303  		case AADDSS:
   304  		case ACMPSD:
   305  		case ACMPSS:
   306  		case ADIVSD:
   307  		case ADIVSS:
   308  		case AMAXSD:
   309  		case AMAXSS:
   310  		case AMINSD:
   311  		case AMINSS:
   312  		case AMULSD:
   313  		case AMULSS:
   314  		case ARCPSS:
   315  		case ARSQRTSS:
   316  		case ASQRTSD:
   317  		case ASQRTSS:
   318  		case ASUBSD:
   319  		case ASUBSS:
   320  		case AXORPD:
   321  			for(z=0; z<BITS; z++) {
   322  				r->set.b[z] |= bit.b[z];
   323  				r->use2.b[z] |= bit.b[z];
   324  			}
   325  			break;
   326  
   327  		/*
   328  		 * funny
   329  		 */
   330  		case ACALL:
   331  			for(z=0; z<BITS; z++)
   332  				addrs.b[z] |= bit.b[z];
   333  			break;
   334  		}
   335  
   336  		switch(p->as) {
   337  		case AIMULL:
   338  		case AIMULQ:
   339  		case AIMULW:
   340  			if(p->to.type != D_NONE)
   341  				break;
   342  
   343  		case AIDIVB:
   344  		case AIDIVL:
   345  		case AIDIVQ:
   346  		case AIDIVW:
   347  		case AIMULB:
   348  		case ADIVB:
   349  		case ADIVL:
   350  		case ADIVQ:
   351  		case ADIVW:
   352  		case AMULB:
   353  		case AMULL:
   354  		case AMULQ:
   355  		case AMULW:
   356  
   357  		case ACWD:
   358  		case ACDQ:
   359  		case ACQO:
   360  			r->regu |= RtoB(D_AX) | RtoB(D_DX);
   361  			break;
   362  
   363  		case AREP:
   364  		case AREPN:
   365  		case ALOOP:
   366  		case ALOOPEQ:
   367  		case ALOOPNE:
   368  			r->regu |= RtoB(D_CX);
   369  			break;
   370  
   371  		case AMOVSB:
   372  		case AMOVSL:
   373  		case AMOVSQ:
   374  		case AMOVSW:
   375  		case ACMPSB:
   376  		case ACMPSL:
   377  		case ACMPSQ:
   378  		case ACMPSW:
   379  			r->regu |= RtoB(D_SI) | RtoB(D_DI);
   380  			break;
   381  
   382  		case ASTOSB:
   383  		case ASTOSL:
   384  		case ASTOSQ:
   385  		case ASTOSW:
   386  		case ASCASB:
   387  		case ASCASL:
   388  		case ASCASQ:
   389  		case ASCASW:
   390  			r->regu |= RtoB(D_AX) | RtoB(D_DI);
   391  			break;
   392  
   393  		case AINSB:
   394  		case AINSL:
   395  		case AINSW:
   396  		case AOUTSB:
   397  		case AOUTSL:
   398  		case AOUTSW:
   399  			r->regu |= RtoB(D_DI) | RtoB(D_DX);
   400  			break;
   401  		}
   402  	}
   403  	if(firstr == R)
   404  		return;
   405  	initpc = pc - val;
   406  	npc = val;
   407  
   408  	/*
   409  	 * pass 2
   410  	 * turn branch references to pointers
   411  	 * build back pointers
   412  	 */
   413  	for(r = firstr; r != R; r = r->link) {
   414  		p = r->prog;
   415  		if(p->to.type == D_BRANCH) {
   416  			val = p->to.offset - initpc;
   417  			r1 = firstr;
   418  			while(r1 != R) {
   419  				r2 = r1->log5;
   420  				if(r2 != R && val >= r2->pc) {
   421  					r1 = r2;
   422  					continue;
   423  				}
   424  				if(r1->pc == val)
   425  					break;
   426  				r1 = r1->link;
   427  			}
   428  			if(r1 == R) {
   429  				nearln = p->lineno;
   430  				diag(Z, "ref not found\n%P", p);
   431  				continue;
   432  			}
   433  			if(r1 == r) {
   434  				nearln = p->lineno;
   435  				diag(Z, "ref to self\n%P", p);
   436  				continue;
   437  			}
   438  			r->s2 = r1;
   439  			r->p2link = r1->p2;
   440  			r1->p2 = r;
   441  		}
   442  	}
   443  	if(debug['R']) {
   444  		p = firstr->prog;
   445  		print("\n%L %D\n", p->lineno, &p->from);
   446  	}
   447  
   448  	/*
   449  	 * pass 2.1
   450  	 * fix jumps
   451  	 */
   452  	fixjmp(firstr);
   453  
   454  	/*
   455  	 * pass 2.5
   456  	 * find looping structure
   457  	 */
   458  	for(r = firstr; r != R; r = r->link)
   459  		r->active = 0;
   460  	change = 0;
   461  	loopit(firstr, npc);
   462  	if(debug['R'] && debug['v']) {
   463  		print("\nlooping structure:\n");
   464  		for(r = firstr; r != R; r = r->link) {
   465  			print("%d:%P", r->loop, r->prog);
   466  			for(z=0; z<BITS; z++)
   467  				bit.b[z] = r->use1.b[z] |
   468  					   r->use2.b[z] |
   469  					   r->set.b[z];
   470  			if(bany(&bit)) {
   471  				print("\t");
   472  				if(bany(&r->use1))
   473  					print(" u1=%B", r->use1);
   474  				if(bany(&r->use2))
   475  					print(" u2=%B", r->use2);
   476  				if(bany(&r->set))
   477  					print(" st=%B", r->set);
   478  			}
   479  			print("\n");
   480  		}
   481  	}
   482  
   483  	/*
   484  	 * pass 3
   485  	 * iterate propagating usage
   486  	 * 	back until flow graph is complete
   487  	 */
   488  loop1:
   489  	change = 0;
   490  	for(r = firstr; r != R; r = r->link)
   491  		r->active = 0;
   492  	for(r = firstr; r != R; r = r->link)
   493  		if(r->prog->as == ARET)
   494  			prop(r, zbits, zbits);
   495  loop11:
   496  	/* pick up unreachable code */
   497  	i = 0;
   498  	for(r = firstr; r != R; r = r1) {
   499  		r1 = r->link;
   500  		if(r1 && r1->active && !r->active) {
   501  			prop(r, zbits, zbits);
   502  			i = 1;
   503  		}
   504  	}
   505  	if(i)
   506  		goto loop11;
   507  	if(change)
   508  		goto loop1;
   509  
   510  
   511  	/*
   512  	 * pass 4
   513  	 * iterate propagating register/variable synchrony
   514  	 * 	forward until graph is complete
   515  	 */
   516  loop2:
   517  	change = 0;
   518  	for(r = firstr; r != R; r = r->link)
   519  		r->active = 0;
   520  	synch(firstr, zbits);
   521  	if(change)
   522  		goto loop2;
   523  
   524  
   525  	/*
   526  	 * pass 5
   527  	 * isolate regions
   528  	 * calculate costs (paint1)
   529  	 */
   530  	r = firstr;
   531  	if(r) {
   532  		for(z=0; z<BITS; z++)
   533  			bit.b[z] = (r->refahead.b[z] | r->calahead.b[z]) &
   534  			  ~(externs.b[z] | params.b[z] | addrs.b[z] | consts.b[z]);
   535  		if(bany(&bit)) {
   536  			nearln = r->prog->lineno;
   537  			warn(Z, "used and not set: %B", bit);
   538  			if(debug['R'] && !debug['w'])
   539  				print("used and not set: %B\n", bit);
   540  		}
   541  	}
   542  	if(debug['R'] && debug['v'])
   543  		print("\nprop structure:\n");
   544  	for(r = firstr; r != R; r = r->link)
   545  		r->act = zbits;
   546  	rgp = region;
   547  	nregion = 0;
   548  	for(r = firstr; r != R; r = r->link) {
   549  		if(debug['R'] && debug['v']) {
   550  			print("%P\t", r->prog);
   551  			if(bany(&r->set))
   552  				print("s:%B ", r->set);
   553  			if(bany(&r->refahead))
   554  				print("ra:%B ", r->refahead);
   555  			if(bany(&r->calahead))
   556  				print("ca:%B ", r->calahead);
   557  			print("\n");
   558  		}
   559  		for(z=0; z<BITS; z++)
   560  			bit.b[z] = r->set.b[z] &
   561  			  ~(r->refahead.b[z] | r->calahead.b[z] | addrs.b[z]);
   562  		if(bany(&bit)) {
   563  			nearln = r->prog->lineno;
   564  			warn(Z, "set and not used: %B", bit);
   565  			if(debug['R'])
   566  				print("set and not used: %B\n", bit);
   567  			excise(r);
   568  		}
   569  		for(z=0; z<BITS; z++)
   570  			bit.b[z] = LOAD(r) & ~(r->act.b[z] | addrs.b[z]);
   571  		while(bany(&bit)) {
   572  			i = bnum(bit);
   573  			rgp->enter = r;
   574  			rgp->varno = i;
   575  			change = 0;
   576  			if(debug['R'] && debug['v'])
   577  				print("\n");
   578  			paint1(r, i);
   579  			bit.b[i/32] &= ~(1L<<(i%32));
   580  			if(change <= 0) {
   581  				if(debug['R'])
   582  					print("%L$%d: %B\n",
   583  						r->prog->lineno, change, blsh(i));
   584  				continue;
   585  			}
   586  			rgp->cost = change;
   587  			nregion++;
   588  			if(nregion >= NRGN)
   589  				fatal(Z, "too many regions");
   590  			rgp++;
   591  		}
   592  	}
   593  	qsort(region, nregion, sizeof(region[0]), rcmp);
   594  
   595  	/*
   596  	 * pass 6
   597  	 * determine used registers (paint2)
   598  	 * replace code (paint3)
   599  	 */
   600  	rgp = region;
   601  	for(i=0; i<nregion; i++) {
   602  		bit = blsh(rgp->varno);
   603  		vreg = paint2(rgp->enter, rgp->varno);
   604  		vreg = allreg(vreg, rgp);
   605  		if(debug['R']) {
   606  			print("%L$%d %R: %B\n",
   607  				rgp->enter->prog->lineno,
   608  				rgp->cost,
   609  				rgp->regno,
   610  				bit);
   611  		}
   612  		if(rgp->regno != 0)
   613  			paint3(rgp->enter, rgp->varno, vreg, rgp->regno);
   614  		rgp++;
   615  	}
   616  	/*
   617  	 * pass 7
   618  	 * peep-hole on basic block
   619  	 */
   620  	if(!debug['R'] || debug['P'])
   621  		peep();
   622  
   623  	/*
   624  	 * pass 8
   625  	 * recalculate pc
   626  	 */
   627  	val = initpc;
   628  	for(r = firstr; r != R; r = r1) {
   629  		r->pc = val;
   630  		p = r->prog;
   631  		p1 = P;
   632  		r1 = r->link;
   633  		if(r1 != R)
   634  			p1 = r1->prog;
   635  		for(; p != p1; p = p->link) {
   636  			switch(p->as) {
   637  			default:
   638  				val++;
   639  				break;
   640  
   641  			case ANOP:
   642  			case ADATA:
   643  			case AGLOBL:
   644  			case ANAME:
   645  			case ASIGNAME:
   646  			case AFUNCDATA:
   647  				break;
   648  			}
   649  		}
   650  	}
   651  	pc = val;
   652  
   653  	/*
   654  	 * fix up branches
   655  	 */
   656  	if(debug['R'])
   657  		if(bany(&addrs))
   658  			print("addrs: %B\n", addrs);
   659  
   660  	r1 = 0; /* set */
   661  	for(r = firstr; r != R; r = r->link) {
   662  		p = r->prog;
   663  		if(p->to.type == D_BRANCH) {
   664  			p->to.offset = r->s2->pc;
   665  			p->to.u.branch = r->s2->prog;
   666  		}
   667  		r1 = r;
   668  	}
   669  
   670  	/*
   671  	 * last pass
   672  	 * eliminate nops
   673  	 * free aux structures
   674  	 */
   675  	for(p = firstr->prog; p != P; p = p->link){
   676  		while(p->link && p->link->as == ANOP)
   677  			p->link = p->link->link;
   678  	}
   679  	if(r1 != R) {
   680  		r1->link = freer;
   681  		freer = firstr;
   682  	}
   683  }
   684  
   685  /*
   686   * add mov b,rn
   687   * just after r
   688   */
   689  void
   690  addmove(Reg *r, int bn, int rn, int f)
   691  {
   692  	Prog *p, *p1;
   693  	Addr *a;
   694  	Var *v;
   695  
   696  	p1 = alloc(sizeof(*p1));
   697  	*p1 = zprog;
   698  	p = r->prog;
   699  
   700  	p1->link = p->link;
   701  	p->link = p1;
   702  	p1->lineno = p->lineno;
   703  
   704  	v = var + bn;
   705  
   706  	a = &p1->to;
   707  	a->sym = v->sym;
   708  	a->offset = v->offset;
   709  	a->etype = v->etype;
   710  	a->type = v->name;
   711  
   712  	p1->as = AMOVL;
   713  	if(v->etype == TCHAR || v->etype == TUCHAR)
   714  		p1->as = AMOVB;
   715  	if(v->etype == TSHORT || v->etype == TUSHORT)
   716  		p1->as = AMOVW;
   717  	if(v->etype == TVLONG || v->etype == TUVLONG || (v->etype == TIND && ewidth[TIND] == 8))
   718  		p1->as = AMOVQ;
   719  	if(v->etype == TFLOAT)
   720  		p1->as = AMOVSS;
   721  	if(v->etype == TDOUBLE)
   722  		p1->as = AMOVSD;
   723  
   724  	p1->from.type = rn;
   725  	if(!f) {
   726  		p1->from = *a;
   727  		*a = zprog.from;
   728  		a->type = rn;
   729  		if(v->etype == TUCHAR)
   730  			p1->as = AMOVB;
   731  		if(v->etype == TUSHORT)
   732  			p1->as = AMOVW;
   733  	}
   734  	if(debug['R'])
   735  		print("%P\t.a%P\n", p, p1);
   736  }
   737  
   738  uint32
   739  doregbits(int r)
   740  {
   741  	uint32 b;
   742  
   743  	b = 0;
   744  	if(r >= D_INDIR)
   745  		r -= D_INDIR;
   746  	if(r >= D_AX && r <= D_R15)
   747  		b |= RtoB(r);
   748  	else
   749  	if(r >= D_AL && r <= D_R15B)
   750  		b |= RtoB(r-D_AL+D_AX);
   751  	else
   752  	if(r >= D_AH && r <= D_BH)
   753  		b |= RtoB(r-D_AH+D_AX);
   754  	else
   755  	if(r >= D_X0 && r <= D_X0+15)
   756  		b |= FtoB(r);
   757  	return b;
   758  }
   759  
   760  Bits
   761  mkvar(Reg *r, Addr *a)
   762  {
   763  	Var *v;
   764  	int i, t, n, et, z;
   765  	int32 o;
   766  	Bits bit;
   767  	LSym *s;
   768  
   769  	/*
   770  	 * mark registers used
   771  	 */
   772  	t = a->type;
   773  	r->regu |= doregbits(t);
   774  	r->regu |= doregbits(a->index);
   775  
   776  	switch(t) {
   777  	default:
   778  		goto none;
   779  	case D_ADDR:
   780  		a->type = a->index;
   781  		bit = mkvar(r, a);
   782  		for(z=0; z<BITS; z++)
   783  			addrs.b[z] |= bit.b[z];
   784  		a->type = t;
   785  		goto none;
   786  	case D_EXTERN:
   787  	case D_STATIC:
   788  	case D_PARAM:
   789  	case D_AUTO:
   790  		n = t;
   791  		break;
   792  	}
   793  	s = a->sym;
   794  	if(s == nil)
   795  		goto none;
   796  	if(s->name[0] == '.')
   797  		goto none;
   798  	et = a->etype;
   799  	o = a->offset;
   800  	v = var;
   801  	for(i=0; i<nvar; i++) {
   802  		if(s == v->sym)
   803  		if(n == v->name)
   804  		if(o == v->offset)
   805  			goto out;
   806  		v++;
   807  	}
   808  	if(nvar >= NVAR)
   809  		fatal(Z, "variable not optimized: %s", s->name);
   810  	i = nvar;
   811  	nvar++;
   812  	v = &var[i];
   813  	v->sym = s;
   814  	v->offset = o;
   815  	v->name = n;
   816  	v->etype = et;
   817  	if(debug['R'])
   818  		print("bit=%2d et=%2d %D\n", i, et, a);
   819  
   820  out:
   821  	bit = blsh(i);
   822  	if(n == D_EXTERN || n == D_STATIC)
   823  		for(z=0; z<BITS; z++)
   824  			externs.b[z] |= bit.b[z];
   825  	if(n == D_PARAM)
   826  		for(z=0; z<BITS; z++)
   827  			params.b[z] |= bit.b[z];
   828  	if(v->etype != et || !(typechlpfd[et] || typev[et]))	/* funny punning */
   829  		for(z=0; z<BITS; z++)
   830  			addrs.b[z] |= bit.b[z];
   831  	return bit;
   832  
   833  none:
   834  	return zbits;
   835  }
   836  
   837  void
   838  prop(Reg *r, Bits ref, Bits cal)
   839  {
   840  	Reg *r1, *r2;
   841  	int z;
   842  
   843  	for(r1 = r; r1 != R; r1 = r1->p1) {
   844  		for(z=0; z<BITS; z++) {
   845  			ref.b[z] |= r1->refahead.b[z];
   846  			if(ref.b[z] != r1->refahead.b[z]) {
   847  				r1->refahead.b[z] = ref.b[z];
   848  				change++;
   849  			}
   850  			cal.b[z] |= r1->calahead.b[z];
   851  			if(cal.b[z] != r1->calahead.b[z]) {
   852  				r1->calahead.b[z] = cal.b[z];
   853  				change++;
   854  			}
   855  		}
   856  		switch(r1->prog->as) {
   857  		case ACALL:
   858  			for(z=0; z<BITS; z++) {
   859  				cal.b[z] |= ref.b[z] | externs.b[z];
   860  				ref.b[z] = 0;
   861  			}
   862  			break;
   863  
   864  		case ATEXT:
   865  			for(z=0; z<BITS; z++) {
   866  				cal.b[z] = 0;
   867  				ref.b[z] = 0;
   868  			}
   869  			break;
   870  
   871  		case ARET:
   872  			for(z=0; z<BITS; z++) {
   873  				cal.b[z] = externs.b[z];
   874  				ref.b[z] = 0;
   875  			}
   876  		}
   877  		for(z=0; z<BITS; z++) {
   878  			ref.b[z] = (ref.b[z] & ~r1->set.b[z]) |
   879  				r1->use1.b[z] | r1->use2.b[z];
   880  			cal.b[z] &= ~(r1->set.b[z] | r1->use1.b[z] | r1->use2.b[z]);
   881  			r1->refbehind.b[z] = ref.b[z];
   882  			r1->calbehind.b[z] = cal.b[z];
   883  		}
   884  		if(r1->active)
   885  			break;
   886  		r1->active = 1;
   887  	}
   888  	for(; r != r1; r = r->p1)
   889  		for(r2 = r->p2; r2 != R; r2 = r2->p2link)
   890  			prop(r2, r->refbehind, r->calbehind);
   891  }
   892  
   893  /*
   894   * find looping structure
   895   *
   896   * 1) find reverse postordering
   897   * 2) find approximate dominators,
   898   *	the actual dominators if the flow graph is reducible
   899   *	otherwise, dominators plus some other non-dominators.
   900   *	See Matthew S. Hecht and Jeffrey D. Ullman,
   901   *	"Analysis of a Simple Algorithm for Global Data Flow Problems",
   902   *	Conf.  Record of ACM Symp. on Principles of Prog. Langs, Boston, Massachusetts,
   903   *	Oct. 1-3, 1973, pp.  207-217.
   904   * 3) find all nodes with a predecessor dominated by the current node.
   905   *	such a node is a loop head.
   906   *	recursively, all preds with a greater rpo number are in the loop
   907   */
   908  int32
   909  postorder(Reg *r, Reg **rpo2r, int32 n)
   910  {
   911  	Reg *r1;
   912  
   913  	r->rpo = 1;
   914  	r1 = r->s1;
   915  	if(r1 && !r1->rpo)
   916  		n = postorder(r1, rpo2r, n);
   917  	r1 = r->s2;
   918  	if(r1 && !r1->rpo)
   919  		n = postorder(r1, rpo2r, n);
   920  	rpo2r[n] = r;
   921  	n++;
   922  	return n;
   923  }
   924  
   925  int32
   926  rpolca(int32 *idom, int32 rpo1, int32 rpo2)
   927  {
   928  	int32 t;
   929  
   930  	if(rpo1 == -1)
   931  		return rpo2;
   932  	while(rpo1 != rpo2){
   933  		if(rpo1 > rpo2){
   934  			t = rpo2;
   935  			rpo2 = rpo1;
   936  			rpo1 = t;
   937  		}
   938  		while(rpo1 < rpo2){
   939  			t = idom[rpo2];
   940  			if(t >= rpo2)
   941  				fatal(Z, "bad idom");
   942  			rpo2 = t;
   943  		}
   944  	}
   945  	return rpo1;
   946  }
   947  
   948  int
   949  doms(int32 *idom, int32 r, int32 s)
   950  {
   951  	while(s > r)
   952  		s = idom[s];
   953  	return s == r;
   954  }
   955  
   956  int
   957  loophead(int32 *idom, Reg *r)
   958  {
   959  	int32 src;
   960  
   961  	src = r->rpo;
   962  	if(r->p1 != R && doms(idom, src, r->p1->rpo))
   963  		return 1;
   964  	for(r = r->p2; r != R; r = r->p2link)
   965  		if(doms(idom, src, r->rpo))
   966  			return 1;
   967  	return 0;
   968  }
   969  
   970  void
   971  loopmark(Reg **rpo2r, int32 head, Reg *r)
   972  {
   973  	if(r->rpo < head || r->active == head)
   974  		return;
   975  	r->active = head;
   976  	r->loop += LOOP;
   977  	if(r->p1 != R)
   978  		loopmark(rpo2r, head, r->p1);
   979  	for(r = r->p2; r != R; r = r->p2link)
   980  		loopmark(rpo2r, head, r);
   981  }
   982  
   983  void
   984  loopit(Reg *r, int32 nr)
   985  {
   986  	Reg *r1;
   987  	int32 i, d, me;
   988  
   989  	if(nr > maxnr) {
   990  		rpo2r = alloc(nr * sizeof(Reg*));
   991  		idom = alloc(nr * sizeof(int32));
   992  		maxnr = nr;
   993  	}
   994  
   995  	d = postorder(r, rpo2r, 0);
   996  	if(d > nr)
   997  		fatal(Z, "too many reg nodes");
   998  	nr = d;
   999  	for(i = 0; i < nr / 2; i++){
  1000  		r1 = rpo2r[i];
  1001  		rpo2r[i] = rpo2r[nr - 1 - i];
  1002  		rpo2r[nr - 1 - i] = r1;
  1003  	}
  1004  	for(i = 0; i < nr; i++)
  1005  		rpo2r[i]->rpo = i;
  1006  
  1007  	idom[0] = 0;
  1008  	for(i = 0; i < nr; i++){
  1009  		r1 = rpo2r[i];
  1010  		me = r1->rpo;
  1011  		d = -1;
  1012  		if(r1->p1 != R && r1->p1->rpo < me)
  1013  			d = r1->p1->rpo;
  1014  		for(r1 = r1->p2; r1 != nil; r1 = r1->p2link)
  1015  			if(r1->rpo < me)
  1016  				d = rpolca(idom, d, r1->rpo);
  1017  		idom[i] = d;
  1018  	}
  1019  
  1020  	for(i = 0; i < nr; i++){
  1021  		r1 = rpo2r[i];
  1022  		r1->loop++;
  1023  		if(r1->p2 != R && loophead(idom, r1))
  1024  			loopmark(rpo2r, i, r1);
  1025  	}
  1026  }
  1027  
  1028  void
  1029  synch(Reg *r, Bits dif)
  1030  {
  1031  	Reg *r1;
  1032  	int z;
  1033  
  1034  	for(r1 = r; r1 != R; r1 = r1->s1) {
  1035  		for(z=0; z<BITS; z++) {
  1036  			dif.b[z] = (dif.b[z] &
  1037  				~(~r1->refbehind.b[z] & r1->refahead.b[z])) |
  1038  					r1->set.b[z] | r1->regdiff.b[z];
  1039  			if(dif.b[z] != r1->regdiff.b[z]) {
  1040  				r1->regdiff.b[z] = dif.b[z];
  1041  				change++;
  1042  			}
  1043  		}
  1044  		if(r1->active)
  1045  			break;
  1046  		r1->active = 1;
  1047  		for(z=0; z<BITS; z++)
  1048  			dif.b[z] &= ~(~r1->calbehind.b[z] & r1->calahead.b[z]);
  1049  		if(r1->s2 != R)
  1050  			synch(r1->s2, dif);
  1051  	}
  1052  }
  1053  
  1054  uint32
  1055  allreg(uint32 b, Rgn *r)
  1056  {
  1057  	Var *v;
  1058  	int i;
  1059  
  1060  	v = var + r->varno;
  1061  	r->regno = 0;
  1062  	switch(v->etype) {
  1063  
  1064  	default:
  1065  		diag(Z, "unknown etype %d/%d", bitno(b), v->etype);
  1066  		break;
  1067  
  1068  	case TCHAR:
  1069  	case TUCHAR:
  1070  	case TSHORT:
  1071  	case TUSHORT:
  1072  	case TINT:
  1073  	case TUINT:
  1074  	case TLONG:
  1075  	case TULONG:
  1076  	case TVLONG:
  1077  	case TUVLONG:
  1078  	case TIND:
  1079  	case TARRAY:
  1080  		i = BtoR(~b);
  1081  		if(i && r->cost > 0) {
  1082  			r->regno = i;
  1083  			return RtoB(i);
  1084  		}
  1085  		break;
  1086  
  1087  	case TDOUBLE:
  1088  	case TFLOAT:
  1089  		i = BtoF(~b);
  1090  		if(i && r->cost > 0) {
  1091  			r->regno = i;
  1092  			return FtoB(i);
  1093  		}
  1094  		break;
  1095  	}
  1096  	return 0;
  1097  }
  1098  
  1099  void
  1100  paint1(Reg *r, int bn)
  1101  {
  1102  	Reg *r1;
  1103  	Prog *p;
  1104  	int z;
  1105  	uint32 bb;
  1106  
  1107  	z = bn/32;
  1108  	bb = 1L<<(bn%32);
  1109  	if(r->act.b[z] & bb)
  1110  		return;
  1111  	for(;;) {
  1112  		if(!(r->refbehind.b[z] & bb))
  1113  			break;
  1114  		r1 = r->p1;
  1115  		if(r1 == R)
  1116  			break;
  1117  		if(!(r1->refahead.b[z] & bb))
  1118  			break;
  1119  		if(r1->act.b[z] & bb)
  1120  			break;
  1121  		r = r1;
  1122  	}
  1123  
  1124  	if(LOAD(r) & ~(r->set.b[z]&~(r->use1.b[z]|r->use2.b[z])) & bb) {
  1125  		change -= CLOAD * r->loop;
  1126  		if(debug['R'] && debug['v'])
  1127  			print("%d%P\td %B $%d\n", r->loop,
  1128  				r->prog, blsh(bn), change);
  1129  	}
  1130  	for(;;) {
  1131  		r->act.b[z] |= bb;
  1132  		p = r->prog;
  1133  
  1134  		if(r->use1.b[z] & bb) {
  1135  			change += CREF * r->loop;
  1136  			if(debug['R'] && debug['v'])
  1137  				print("%d%P\tu1 %B $%d\n", r->loop,
  1138  					p, blsh(bn), change);
  1139  		}
  1140  
  1141  		if((r->use2.b[z]|r->set.b[z]) & bb) {
  1142  			change += CREF * r->loop;
  1143  			if(debug['R'] && debug['v'])
  1144  				print("%d%P\tu2 %B $%d\n", r->loop,
  1145  					p, blsh(bn), change);
  1146  		}
  1147  
  1148  		if(STORE(r) & r->regdiff.b[z] & bb) {
  1149  			change -= CLOAD * r->loop;
  1150  			if(debug['R'] && debug['v'])
  1151  				print("%d%P\tst %B $%d\n", r->loop,
  1152  					p, blsh(bn), change);
  1153  		}
  1154  
  1155  		if(r->refbehind.b[z] & bb)
  1156  			for(r1 = r->p2; r1 != R; r1 = r1->p2link)
  1157  				if(r1->refahead.b[z] & bb)
  1158  					paint1(r1, bn);
  1159  
  1160  		if(!(r->refahead.b[z] & bb))
  1161  			break;
  1162  		r1 = r->s2;
  1163  		if(r1 != R)
  1164  			if(r1->refbehind.b[z] & bb)
  1165  				paint1(r1, bn);
  1166  		r = r->s1;
  1167  		if(r == R)
  1168  			break;
  1169  		if(r->act.b[z] & bb)
  1170  			break;
  1171  		if(!(r->refbehind.b[z] & bb))
  1172  			break;
  1173  	}
  1174  }
  1175  
  1176  uint32
  1177  regset(Reg *r, uint32 bb)
  1178  {
  1179  	uint32 b, set;
  1180  	Addr v;
  1181  	int c;
  1182  
  1183  	set = 0;
  1184  	v = zprog.from;
  1185  	while(b = bb & ~(bb-1)) {
  1186  		v.type = b & 0xFFFF? BtoR(b): BtoF(b);
  1187  		if(v.type == 0)
  1188  			diag(Z, "zero v.type for %#ux", b);
  1189  		c = copyu(r->prog, &v, A);
  1190  		if(c == 3)
  1191  			set |= b;
  1192  		bb &= ~b;
  1193  	}
  1194  	return set;
  1195  }
  1196  
  1197  uint32
  1198  reguse(Reg *r, uint32 bb)
  1199  {
  1200  	uint32 b, set;
  1201  	Addr v;
  1202  	int c;
  1203  
  1204  	set = 0;
  1205  	v = zprog.from;
  1206  	while(b = bb & ~(bb-1)) {
  1207  		v.type = b & 0xFFFF? BtoR(b): BtoF(b);
  1208  		c = copyu(r->prog, &v, A);
  1209  		if(c == 1 || c == 2 || c == 4)
  1210  			set |= b;
  1211  		bb &= ~b;
  1212  	}
  1213  	return set;
  1214  }
  1215  
  1216  uint32
  1217  paint2(Reg *r, int bn)
  1218  {
  1219  	Reg *r1;
  1220  	int z;
  1221  	uint32 bb, vreg, x;
  1222  
  1223  	z = bn/32;
  1224  	bb = 1L << (bn%32);
  1225  	vreg = regbits;
  1226  	if(!(r->act.b[z] & bb))
  1227  		return vreg;
  1228  	for(;;) {
  1229  		if(!(r->refbehind.b[z] & bb))
  1230  			break;
  1231  		r1 = r->p1;
  1232  		if(r1 == R)
  1233  			break;
  1234  		if(!(r1->refahead.b[z] & bb))
  1235  			break;
  1236  		if(!(r1->act.b[z] & bb))
  1237  			break;
  1238  		r = r1;
  1239  	}
  1240  	for(;;) {
  1241  		r->act.b[z] &= ~bb;
  1242  
  1243  		vreg |= r->regu;
  1244  
  1245  		if(r->refbehind.b[z] & bb)
  1246  			for(r1 = r->p2; r1 != R; r1 = r1->p2link)
  1247  				if(r1->refahead.b[z] & bb)
  1248  					vreg |= paint2(r1, bn);
  1249  
  1250  		if(!(r->refahead.b[z] & bb))
  1251  			break;
  1252  		r1 = r->s2;
  1253  		if(r1 != R)
  1254  			if(r1->refbehind.b[z] & bb)
  1255  				vreg |= paint2(r1, bn);
  1256  		r = r->s1;
  1257  		if(r == R)
  1258  			break;
  1259  		if(!(r->act.b[z] & bb))
  1260  			break;
  1261  		if(!(r->refbehind.b[z] & bb))
  1262  			break;
  1263  	}
  1264  
  1265  	bb = vreg;
  1266  	for(; r; r=r->s1) {
  1267  		x = r->regu & ~bb;
  1268  		if(x) {
  1269  			vreg |= reguse(r, x);
  1270  			bb |= regset(r, x);
  1271  		}
  1272  	}
  1273  	return vreg;
  1274  }
  1275  
  1276  void
  1277  paint3(Reg *r, int bn, int32 rb, int rn)
  1278  {
  1279  	Reg *r1;
  1280  	Prog *p;
  1281  	int z;
  1282  	uint32 bb;
  1283  
  1284  	z = bn/32;
  1285  	bb = 1L << (bn%32);
  1286  	if(r->act.b[z] & bb)
  1287  		return;
  1288  	for(;;) {
  1289  		if(!(r->refbehind.b[z] & bb))
  1290  			break;
  1291  		r1 = r->p1;
  1292  		if(r1 == R)
  1293  			break;
  1294  		if(!(r1->refahead.b[z] & bb))
  1295  			break;
  1296  		if(r1->act.b[z] & bb)
  1297  			break;
  1298  		r = r1;
  1299  	}
  1300  
  1301  	if(LOAD(r) & ~(r->set.b[z] & ~(r->use1.b[z]|r->use2.b[z])) & bb)
  1302  		addmove(r, bn, rn, 0);
  1303  	for(;;) {
  1304  		r->act.b[z] |= bb;
  1305  		p = r->prog;
  1306  
  1307  		if(r->use1.b[z] & bb) {
  1308  			if(debug['R'])
  1309  				print("%P", p);
  1310  			addreg(&p->from, rn);
  1311  			if(debug['R'])
  1312  				print("\t.c%P\n", p);
  1313  		}
  1314  		if((r->use2.b[z]|r->set.b[z]) & bb) {
  1315  			if(debug['R'])
  1316  				print("%P", p);
  1317  			addreg(&p->to, rn);
  1318  			if(debug['R'])
  1319  				print("\t.c%P\n", p);
  1320  		}
  1321  
  1322  		if(STORE(r) & r->regdiff.b[z] & bb)
  1323  			addmove(r, bn, rn, 1);
  1324  		r->regu |= rb;
  1325  
  1326  		if(r->refbehind.b[z] & bb)
  1327  			for(r1 = r->p2; r1 != R; r1 = r1->p2link)
  1328  				if(r1->refahead.b[z] & bb)
  1329  					paint3(r1, bn, rb, rn);
  1330  
  1331  		if(!(r->refahead.b[z] & bb))
  1332  			break;
  1333  		r1 = r->s2;
  1334  		if(r1 != R)
  1335  			if(r1->refbehind.b[z] & bb)
  1336  				paint3(r1, bn, rb, rn);
  1337  		r = r->s1;
  1338  		if(r == R)
  1339  			break;
  1340  		if(r->act.b[z] & bb)
  1341  			break;
  1342  		if(!(r->refbehind.b[z] & bb))
  1343  			break;
  1344  	}
  1345  }
  1346  
  1347  void
  1348  addreg(Addr *a, int rn)
  1349  {
  1350  
  1351  	a->sym = 0;
  1352  	a->offset = 0;
  1353  	a->type = rn;
  1354  }
  1355  
  1356  int32
  1357  RtoB(int r)
  1358  {
  1359  
  1360  	if(r < D_AX || r > D_R15)
  1361  		return 0;
  1362  	return 1L << (r-D_AX);
  1363  }
  1364  
  1365  int
  1366  BtoR(int32 b)
  1367  {
  1368  
  1369  	b &= 0xffffL;
  1370  	if(nacl)
  1371  		b &= ~((1<<(D_BP-D_AX)) | (1<<(D_R15-D_AX)));
  1372  	if(b == 0)
  1373  		return 0;
  1374  	return bitno(b) + D_AX;
  1375  }
  1376  
  1377  /*
  1378   *	bit	reg
  1379   *	16	X5
  1380   *	17	X6
  1381   *	18	X7
  1382   */
  1383  int32
  1384  FtoB(int f)
  1385  {
  1386  	if(f < FREGMIN || f > FREGEXT)
  1387  		return 0;
  1388  	return 1L << (f - FREGMIN + 16);
  1389  }
  1390  
  1391  int
  1392  BtoF(int32 b)
  1393  {
  1394  
  1395  	b &= 0x70000L;
  1396  	if(b == 0)
  1397  		return 0;
  1398  	return bitno(b) - 16 + FREGMIN;
  1399  }
  1400  
  1401  /* what instruction does a JMP to p eventually land on? */
  1402  static Reg*
  1403  chasejmp(Reg *r, int *jmploop)
  1404  {
  1405  	int n;
  1406  
  1407  	n = 0;
  1408  	for(; r; r=r->s2) {
  1409  		if(r->prog->as != AJMP || r->prog->to.type != D_BRANCH)
  1410  			break;
  1411  		if(++n > 10) {
  1412  			*jmploop = 1;
  1413  			break;
  1414  		}
  1415  	}
  1416  	return r;
  1417  }
  1418  
  1419  /* mark all code reachable from firstp as alive */
  1420  static void
  1421  mark(Reg *firstr)
  1422  {
  1423  	Reg *r;
  1424  	Prog *p;
  1425  
  1426  	for(r=firstr; r; r=r->link) {
  1427  		if(r->active)
  1428  			break;
  1429  		r->active = 1;
  1430  		p = r->prog;
  1431  		if(p->as != ACALL && p->to.type == D_BRANCH)
  1432  			mark(r->s2);
  1433  		if(p->as == AJMP || p->as == ARET || p->as == AUNDEF)
  1434  			break;
  1435  	}
  1436  }
  1437  
  1438  /*
  1439   * the code generator depends on being able to write out JMP
  1440   * instructions that it can jump to now but fill in later.
  1441   * the linker will resolve them nicely, but they make the code
  1442   * longer and more difficult to follow during debugging.
  1443   * remove them.
  1444   */
  1445  static void
  1446  fixjmp(Reg *firstr)
  1447  {
  1448  	int jmploop;
  1449  	Reg *r;
  1450  	Prog *p;
  1451  
  1452  	if(debug['R'] && debug['v'])
  1453  		print("\nfixjmp\n");
  1454  
  1455  	// pass 1: resolve jump to AJMP, mark all code as dead.
  1456  	jmploop = 0;
  1457  	for(r=firstr; r; r=r->link) {
  1458  		p = r->prog;
  1459  		if(debug['R'] && debug['v'])
  1460  			print("%04d %P\n", (int)r->pc, p);
  1461  		if(p->as != ACALL && p->to.type == D_BRANCH && r->s2 && r->s2->prog->as == AJMP) {
  1462  			r->s2 = chasejmp(r->s2, &jmploop);
  1463  			p->to.offset = r->s2->pc;
  1464  			p->to.u.branch = r->s2->prog;
  1465  			if(debug['R'] && debug['v'])
  1466  				print("->%P\n", p);
  1467  		}
  1468  		r->active = 0;
  1469  	}
  1470  	if(debug['R'] && debug['v'])
  1471  		print("\n");
  1472  
  1473  	// pass 2: mark all reachable code alive
  1474  	mark(firstr);
  1475  
  1476  	// pass 3: delete dead code (mostly JMPs).
  1477  	for(r=firstr; r; r=r->link) {
  1478  		if(!r->active) {
  1479  			p = r->prog;
  1480  			if(p->link == P && p->as == ARET && r->p1 && r->p1->prog->as != ARET) {
  1481  				// This is the final ARET, and the code so far doesn't have one.
  1482  				// Let it stay.
  1483  			} else {
  1484  				if(debug['R'] && debug['v'])
  1485  					print("del %04d %P\n", (int)r->pc, p);
  1486  				p->as = ANOP;
  1487  			}
  1488  		}
  1489  	}
  1490  
  1491  	// pass 4: elide JMP to next instruction.
  1492  	// only safe if there are no jumps to JMPs anymore.
  1493  	if(!jmploop) {
  1494  		for(r=firstr; r; r=r->link) {
  1495  			p = r->prog;
  1496  			if(p->as == AJMP && p->to.type == D_BRANCH && r->s2 == r->link) {
  1497  				if(debug['R'] && debug['v'])
  1498  					print("del %04d %P\n", (int)r->pc, p);
  1499  				p->as = ANOP;
  1500  			}
  1501  		}
  1502  	}
  1503  
  1504  	// fix back pointers.
  1505  	for(r=firstr; r; r=r->link) {
  1506  		r->p2 = R;
  1507  		r->p2link = R;
  1508  	}
  1509  	for(r=firstr; r; r=r->link) {
  1510  		if(r->s2) {
  1511  			r->p2link = r->s2->p2;
  1512  			r->s2->p2 = r;
  1513  		}
  1514  	}
  1515  
  1516  	if(debug['R'] && debug['v']) {
  1517  		print("\n");
  1518  		for(r=firstr; r; r=r->link)
  1519  			print("%04d %P\n", (int)r->pc, r->prog);
  1520  		print("\n");
  1521  	}
  1522  }
  1523