|
|
1.1 root 1: /*
2: * C object code improver-- second part
3: */
4:
5: #include "c2.h"
6: #include <stdio.h>
7: #include <ctype.h>
8:
9: #define NUSE 6
10: int ioflag;
11: int bitsize[4] = {0,8,16,32}; /* index by type codes */
12: struct node *uses[NUSE]; /* for backwards flow analysis */
13: char *lastrand; /* last operand of instruction */
14: struct node *bflow();
15:
16: redun3(p) register struct node *p; {
17: /* check for 3 addr instr which should be 2 addr */
18: if (OP3==((p->subop>>4)&0xF)) {
19: if (equstr(regs[RT1],regs[RT3])
20: && (p->op==ADD || p->op==MUL || p->op==AND || p->op==OR || p->op==XOR)) {
21: register char *t=regs[RT1]; regs[RT1]=regs[RT2]; regs[RT2]=t;
22: }
23: if (equstr(regs[RT2],regs[RT3])) {
24: p->subop=(p->subop&0xF)|(OP2<<4); p->pop=0;
25: lastrand=regs[RT2]; *regs[RT3]=0; return(1);
26: }
27: } return(0);
28: }
29:
30: bmove() {
31: register struct node *p, *lastp; register char *cp1,*cp2; register int r;
32: refcount();
33: for (p=lastp= &first; 0!=(p=p->forw); lastp=p);
34: clearreg(); clearuse();
35: for (p=lastp; p!= &first; p=p->back) {
36: if (debug) {
37: printf("Uses:\n");
38: for (r=NUSE;--r>=0;) if (uses[r])
39: printf("%d: %s\n",r,uses[r]->code? uses[r]->code:"");
40: printf("-\n");
41: }
42: r=(p->subop>>4)&0xF;
43: splitrand(p);
44: if (OP3==r && 0!=redun3(p)) {newcode(p); redunm++;}
45: /* ops that do nothing */
46: if(p->op==MOV && equstr(regs[RT1], regs[RT2]))
47: if(p->forw->op!=CBR) {
48: delnode(p); redunm++; continue;
49: } else {
50: p->op=TST; p->pop=0;
51: while(*p->code++ != ',');
52: redunm++;
53: }
54: else if((cp1=p->code, *cp1++)=='$' &&
55: (*cp1=='0' || *cp1=='1' || *cp1++=='-' && *cp1=='1') && cp1[1]==',') {
56: switch((p->code[1]<<8)|p->op) {
57: case (('0'<<8)|ADD):
58: case (('0'<<8)|SUB):
59: case (('-'<<8)|AND):
60: case (('0'<<8)|OR):
61: case (('0'<<8)|XOR):
62: case (('1'<<8)|MUL):
63: case (('1'<<8)|DIV):
64: case (('0'<<8)|SHAL):
65: case (('0'<<8)|SHAR):
66: case (('0'<<8)|SHL):
67: case (('0'<<8)|SHR):
68: if(r == OP2) {
69: if(p->forw->op!=CBR) {
70: delnode(p); redunm++; continue;
71: } else {
72: p->op=TST; p->subop&=0xF; p->pop=0;
73: while(*p->code++ != ',');
74: redunm++;
75: }
76: } else { /* OP3 or shift */
77: p->op=MOV; p->subop&=0xF; p->pop=0;
78: while(*p->code++ != ',');
79: p = p->forw; redunm++; continue;
80: }
81: break;
82: case (('0'<<8)|MUL):
83: case (('0'<<8)|AND):
84: p->op=CLR; p->subop&=0xF; p->pop=0;
85: while(*p->code++ != ',');
86: if(r == OP3)
87: while(*p->code++ != ',');
88: redunm++;
89: }
90: }
91: switch (p->op) {
92: case LABEL: case DLABEL:
93: for (r=NUSE; --r>=0;)
94: if (uses[r]) p->ref=(struct node *) (((int)p->ref)|(1<<r));
95: break;
96: case CALLS:
97: case CALLF:
98: clearuse(); goto std;
99: case 0:
100: clearuse(); break;
101: case CVT:
102: { long n;
103: if ((p->subop&0xF)!=LONG) goto std; cp1=p->code;
104: if (*cp1++!='$') goto std; splitrand(p);
105: n = getnum(®s[RT1][1]);
106: if(r==BYTE && (n<-128 || n>127)) goto std;
107: if(r==WORD && (n<-32768 || n>32767)) goto std;
108: p->op = MOV; p->subop = r; p->pop = 0; nchange++;
109: } goto std;
110:
111: case SUB:
112: if ((p->subop&0xF)!=LONG) goto std; cp1=p->code;
113: if (*cp1++!='$') goto std; splitrand(p);
114: #ifdef MOVAFASTER
115: if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */
116: char buf[C2_ASIZE]; cp2=buf; *cp2++='-';
117: cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2;
118: cp1="(fp),"; while (*cp2++= *cp1++); --cp2;
119: cp1=regs[RT3]; while (*cp2++= *cp1++);
120: p->code=copy(buf); p->op = MOVA; p->subop = BYTE; p->pop=0;
121: } else
122: #endif MOVAFASTER
123: if (*cp1++=='-' && 0<=(r=getnum(cp1))) {
124: p->op=ADD; p->pop=0; *--cp1='$'; p->code=cp1;
125: } goto std;
126: case ADD:
127: if ((p->subop&0xF)!=LONG) goto std; cp1=p->code;
128: if (*cp1++!='$') goto std; splitrand(p);
129: if (isstatic(cp1) && (r=isreg(regs[RT2]))>=0 && r<NUSE && uses[r]==p->forw)
130: {
131: /* address comp:
132: ** addl2 $_foo,r0 \ movab _foo[r0],bar
133: ** movl r0,bar /
134: */
135: register struct node *pnext = p->forw;
136: char buf[C2_ASIZE];
137:
138: if (pnext->op == MOV && pnext->subop == LONG)
139: {
140: cp1 = ®s[RT1][1]; cp2 = &buf[0];
141: while (*cp2++ = *cp1++) ; cp2--;
142: splitrand(pnext);
143: if (r == isreg(regs[RT1]))
144: {
145: delnode(p); p = pnext;
146: p->op = MOVA; p->subop = BYTE;
147: p->pop = 0;
148: cp1 = regs[RT1]; *cp2++ = '[';
149: while (*cp2++ = *cp1++) ; cp2--;
150: *cp2++ = ']'; *cp2++ = ',';
151: cp1 = regs[RT2];
152: while (*cp2++ = *cp1++) ;
153: p->code = copy(buf);
154: }
155: }
156: }
157: else
158: #ifdef MOVAFASTER
159: if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */
160: cp2=cp1-1; cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2;
161: cp1="(fp)"; while (*cp2++= *cp1++); *--cp2=',';
162: p->op = MOVA; p->subop = BYTE; p->pop=0;
163: } else
164: #endif MOVAFASTER
165: if (*cp1++=='-' && 0<=(r=getnum(cp1))) {
166: p->op=SUB; p->pop=0; *--cp1='$'; p->code=cp1;
167: }
168: /* fall thru ... */
169: case CASE:
170: default: std:
171: p=bflow(p); break;
172:
173: case MUL:
174: /*
175: ** Change multiplication
176: ** by constant powers of 2 to shifts.
177: */
178: splitrand(p);
179: if (regs[RT1][0] != '$' || regs[RT1][1] == '-') goto std;
180: if ((r = ispow2(getnum(®s[RT1][1]))) <= 0) goto std;
181: /* mull2 $2,x */
182: if(r == 1 && p->subop == U(LONG, OP2)) {
183: strcpy(regs[RT1], regs[RT2]);
184: p->op = ADD; p->pop = 0; newcode(p); nchange++;
185: goto std;
186: }
187: if (p->subop == U(LONG,OP2))
188: strcpy(regs[RT3], regs[RT2]);
189: sprintf(regs[RT1], "$%d", r);
190: p->op = SHL; p->subop = LONG;
191: p->pop = 0; newcode(p); nchange++;
192: goto std;
193:
194: case SHAL:
195: case SHL:
196: {
197: /* bit tests:
198: ** shll A,$1,rC \
199: ** bitl B,rC > jbc A,B,D
200: ** jeql D /
201: **
202: ** address comp:
203: ** shll $1,bar,r0 \ movl bar,r0
204: ** movab _foo[r0] / movaw _foo[r0]
205: **
206: ** shll $2,r0,r0 \ moval _foo[r0]
207: ** movab _foo[r0] /
208: */
209: register struct node *pf;
210: register struct node *pn;
211: register int shfrom, shto;
212: long shcnt;
213: char *regfrom;
214:
215: splitrand(p);
216: if (regs[RT1][0] != '$') {
217: if (isreg(regs[RT1]) < 0) goto std; /* alignment */
218: if (regs[RT2][0] != '$') goto std;
219: if (getnum(®s[RT2][1]) != 1) goto std;
220: r = isreg(regs[RT3]);
221: if (r < 0 || r >= NUSE) goto std;
222: if ((pf = p->forw)->op != BIT && pf->op!=AND) goto std;
223: if (uses[r] && uses[r] != pf) goto std;
224: splitrand(pf);
225: if (r == isreg(regs[RT1])) cp2 = regs[RT2];
226: else if (r == isreg(regs[RT2])) cp2 = regs[RT1];
227: else goto std;
228: if(*cp2 == '$') goto std;
229: if ((pn = pf->forw)->op != CBR) goto std;
230: if (pn->subop != JEQ && pn->subop != JNE) goto std;
231: delnode(p); delnode(pf);
232: pn->subop = (pn->subop == JEQ) ? JBC : JBS;
233: for(cp1=p->code; *cp1++!=',';);
234: while (*cp1++= *cp2++);
235: pn->code = p->code; pn->pop = NULL;
236: uses[r] = NULL;
237: nbj++; nchange++;
238: p = pn; goto std;
239: }
240: if ((shcnt = getnum(®s[RT1][1])) < 1 || shcnt > 2) goto std;
241: if ((shfrom = isreg(regs[RT2])) >= 0)
242: regfrom = copy(regs[RT2]);
243: if ((shto = isreg(regs[RT3])) >= 0 && shto<NUSE)
244: {
245: int regnum;
246:
247: if (uses[shto] != (pf = p->forw)) goto ashadd;
248: if (pf->op != MOVA && pf->op != PUSHA) goto ashadd;
249: if (pf->subop != BYTE) goto ashadd;
250: splitrand(pf);
251: if (!indexa(regs[RT1])) goto std;
252: cp2 = regs[RT1];
253: if(!isstatic(cp2)) goto std;
254: while (*cp2++ != '[') ;
255: if (*cp2++ != 'r' || !isdigit(*cp2)) goto std;
256: regnum = *cp2++ - '0';
257: if (isdigit(*cp2))
258: {
259: if (cp2[1] != ']') goto std;
260: regnum *= 10; regnum += *cp2 - '0';
261: }
262: if (regnum != shto) goto std;
263: if (shfrom >= 0) /* shll $N,r*,r0 */
264: {
265: delnode(p);
266: p = pf;
267: if (shfrom != shto)
268: {
269: uses[shto] = NULL; splitrand(pf);
270: cp2=regs[RT1]; while (*cp2++!='[');
271: cp1=regfrom; while (*cp2++= *cp1++);
272: cp2[-1] = ']'; *cp2 = 0;
273: newcode(pf);
274: }
275: }
276: else
277: {
278: p->op = MOV; splitrand(p);
279: strcpy(regs[RT1], regs[RT2]);
280: strcpy(regs[RT2], regs[RT3]);
281: regs[RT3][0] = '\0';
282: p->pop = 0; newcode(p);
283: }
284: switch (shcnt)
285: {
286: case 1: pf->subop = WORD; break;
287: case 2: pf->subop = LONG; break;
288: }
289: redunm++; nsaddr++; nchange++;
290: }
291: goto std;
292: ashadd:
293: /* at this point, RT2 and RT3 are guaranteed to be simple regs*/
294: if (shcnt == 1) {
295: /*
296: ** quickie:
297: ** shll $1,A,A > addl2 A,A
298: ** shll $1,A,B > addl3 A,A,B
299: */
300: p->op = ADD;
301: strcpy(regs[RT1], regs[RT2]);
302: if(equstr(regs[RT2], regs[RT3])) {
303: p->subop = U(LONG,OP2);
304: regs[RT3][0] = '\0';
305: } else
306: p->subop = U(LONG,OP3);
307: p->pop = 0;
308: newcode(p); nchange++;
309: }
310: goto std;
311: }
312:
313: case SHAR:
314: case SHR:
315: {
316: /* bit tests:
317: ** shrl A,B,rC \
318: ** bitl $1,rC > jbc A,B,D
319: ** jeql D /
320: */
321: register struct node *pf; /* forward node */
322: register struct node *pn; /* next node (after pf) */
323: register int extreg; /* reg extracted to */
324:
325: splitrand(p);
326: if (isreg(regs[RT1]) < 0) goto std; /* alignment */
327: extreg = isreg(regs[RT3]);
328: if (extreg < 0 || extreg >= NUSE) goto std;
329: if ((pf = p->forw)->op != BIT) goto std;
330: if (uses[extreg] && uses[extreg] != pf) goto std;
331: splitrand(pf);
332: if (regs[RT1][0] != '$') goto std;
333: if (getnum(®s[RT1][1]) != 1) goto std;
334: if (extreg != isreg(regs[RT2])) goto std;
335: if ((pn = pf->forw)->op != CBR) goto std;
336: if (pn->subop != JEQ && pn->subop != JNE) goto std;
337: delnode(p); delnode(pf);
338: pn->subop = (pn->subop == JEQ) ? JBC : JBS;
339: for(cp1=p->code; *cp1++!=',';);
340: while (*cp1!=',') cp1++; *cp1='\0';
341: pn->code = p->code; pn->pop = NULL;
342: uses[extreg] = NULL; nbj++;
343: nchange++;
344: p = pn; goto std;
345: }
346:
347: case AND:
348: {
349: /* unsigned conversion:
350: ** cvtbl A,B; andl2 $255,B > movzbl A,B
351: **
352: ** also byte- and word-size fields:
353: ** shrl $(3-n)*8,A,B; andl2 $255,B > movzbl n+A,B
354: ** shrl $(1-n)*16,A,B; andl2 $65535,B > movzwl n+A,B
355: */
356: char src[C2_ASIZE];
357: register f; /* field length */
358: register struct node *pb = p->back; /* backward node */
359:
360: if (p->subop != U(LONG,OP2))
361: goto std;
362: splitrand(p); cp1=regs[RT1];
363: if (*cp1++!='$' || (f=getnum(cp1))!=0xff && f!=0xffff)
364: goto std;
365: f = f==0xff ? 8 : 16;
366: if (pb->op!=CVT && pb->op!=MOVZ && pb->op!=SHAR && pb->op!=SHR)
367: goto std;
368: /* save source of ANDL in 'src' */
369: strcpy(src, regs[RT2]);
370: splitrand(pb);
371: if (!equstr(src,lastrand))
372: goto std;
373: if (pb->op==CVT || pb->op==MOVZ) {
374: if (!(bitsize[pb->subop&0xF]==f
375: && bitsize[pb->subop>>4]>=f)) /* good CVT */
376: goto std;
377: strcpy(src, regs[RT1]);
378: } else {
379: register int boff; /* bit offset */
380:
381: if (regs[RT1][0] != '$') goto std;
382: if ((boff = getnum(®s[RT1][1])) < 0) goto std;
383: if (isreg(regs[RT2])>=0 || !natural(regs[RT2])) goto std;
384: if ((boff & (f-1)) != 0) goto std;
385: boff = (32-boff-f) / 8;
386: if (boff == 0)
387: strcpy(src, regs[RT2]);
388: else
389: sprintf(src, "%d%s%s", boff, regs[RT2][0]=='(' ? "":"+",
390: regs[RT2]);
391: }
392: delnode(pb);
393: p->op = MOVZ;
394: p->subop = U((f==8 ? BYTE : WORD), LONG);
395: sprintf(line,"%s,%s",src,lastrand);
396: p->pop=0;
397: p->code = copy(line);
398: nchange++;
399: goto std;
400: }
401:
402: case CMP:
403: {
404: /* comparison to -63 to -1:
405: ** cmpl r0,$-1 > incl r0
406: ** jeql ...
407: **
408: ** cmpl r0,$-63 > addl2 $63,r0
409: ** jeql ...
410: */
411: register int num;
412: register int reg;
413: register struct node *regp = p->back;
414:
415: if (p->forw->op != CBR) goto std;
416: if (p->forw->subop != JEQ && p->forw->subop != JNE) goto std;
417: splitrand(p);
418: if (strncmp(regs[RT2], "$-", 2) != 0) goto std;
419: reg = r = isreg(regs[RT1]);
420: if (r < 0) goto std;
421: if (r < NUSE && uses[r] != 0) goto std;
422: if (r >= NUSE && regp->op == MOV && p->subop == regp->subop)
423: {
424: if (*regp->code != 'r') goto std;
425: reg = regp->code[1] - '0';
426: if (isdigit(regp->code[2]) || reg >= NUSE || uses[reg])
427: goto std;
428: }
429: if (r >= NUSE) goto std;
430: if (reg != r)
431: sprintf(regs[RT1], "r%d", reg);
432: if ((num = getnum(®s[RT2][2])) <= 0 || num > 63) goto std;
433: if (num == 1)
434: {
435: p->op = INC; regs[RT2][0] = '\0';
436: }
437: else
438: {
439: register char *t;
440:
441: t=regs[RT1];regs[RT1]=regs[RT2];regs[RT2]=t;
442: p->op = ADD; p->subop = U(p->subop, OP2);
443: for (t = ®s[RT1][2]; t[-1] = *t; t++) ;
444: }
445: p->pop = 0; newcode(p);
446: nchange++;
447: goto std;
448: }
449:
450: case JBR: case JMP:
451: clearuse();
452: if (p->subop==RET) {
453: uses[0]=p; regs[0][0]= -1;
454: uses[1]=p; regs[1][0]= -1;
455: break;
456: }
457: if (p->ref==0) goto std; /* jmp (r0) */
458: /* fall through */
459: case CBR:
460: if (p->ref->ref!=0) for (r=NUSE;--r>=0;)
461: if ((1<<r) & (int)p->ref->ref) {uses[r]=p; regs[r][0]= -1;}
462: case EROU: case JSW:
463: case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ;
464: }
465: }
466: for (p= &first; p!=0; p=p->forw)
467: if (p->op==LABEL || p->op==DLABEL) p->ref=0; /* erase our tracks */
468: }
469:
470: rmove()
471: {
472: register struct node *p;
473: register int r, r1;
474:
475: clearreg();
476: for (p=first.forw; p!=0; p = p->forw) {
477: if (debug) {
478: printf("Regs:\n");
479: for (r=0; r<NREG; r++)
480: if (regs[r][0]) {
481: r1=regs[r][0];
482: printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1);
483: }
484: printf("-\n");
485: }
486: switch (p->op) {
487:
488: case CVT:
489: case MOVZ:
490: splitrand(p);
491: repladdr(p);
492: r = isreg(regs[RT1]);
493: r1 = isreg(regs[RT2]);
494: dest(regs[RT2],p->subop, 1);
495: if (r>=0 && r1>=0) {
496: p->op = MOV; p->subop = LONG;
497: p->pop = 0;
498: nchange++;
499: goto case_mov;
500: }
501: if(p->op == CVT) {
502: if (r1>=0) savereg(r1, regs[RT1], p->subop);
503: } else
504: ccloc[0] = 0;
505: break;
506:
507: case MOV:
508: case_mov:
509: splitrand(p);
510: if ((r = findrand(regs[RT1],p->subop)) >= 0) {
511: if (r == isreg(regs[RT2]))
512: if(p->forw->op!=CBR) {
513: delnode(p); redunm++; nchange++; break;
514: } else {
515: p->op=TST; p->pop=0;
516: while(*p->code++ != ',');
517: redunm++; nchange++;
518: goto case_tst;
519: }
520: }
521: repladdr(p);
522: r = isreg(regs[RT1]);
523: r1 = isreg(regs[RT2]);
524: dest(regs[RT2],p->subop, 1);
525: if (r>=0) {
526: if (r1>=0) {
527: if (r == r1 && p->forw->op!=CBR) {
528: delnode(p); redunm++; nchange++; break;
529: }
530: if(regs[r][0])
531: savereg(r1, regs[r]+1, p->subop);
532: } else savereg(r, regs[RT2], p->subop);
533: } else if (r1>=0) savereg(r1, regs[RT1], p->subop);
534: else setcon(regs[RT1], regs[RT2], p->subop);
535: break;
536:
537: /* .rx,.wx or .rx,.rx,.wx */
538: case ADD:
539: case SUB:
540: case AND:
541: case OR:
542: case XOR:
543: case MUL:
544: case DIV:
545: case SHAL:
546: case SHAR:
547: case SHL:
548: case SHR:
549: case ADDA:
550: case SUBA:
551: /* .rx,.wx */
552: case MFPR:
553: case COM:
554: case NEG:
555: splitrand(p);
556: repladdr(p);
557: dest(lastrand,p->subop, p->op!=ADDA && p->op!=SUBA);
558: break;
559:
560: /* .mx or .wx */
561: case CLR:
562: case INC:
563: case DEC:
564: splitrand(p);
565: dest(lastrand,p->subop, 1);
566: if (p->op==CLR)
567: if ((r = isreg(regs[RT1])) >= 0)
568: savereg(r, "$0", p->subop);
569: else
570: setcon("$0", regs[RT1], p->subop);
571: break;
572:
573: /* .rx */
574: case TST:
575: case_tst:
576: case PUSH:
577: splitrand(p);
578: lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */
579: repladdr(p);
580: lastrand=regs[RT1];
581: if (p->op==TST && equstr(lastrand, ccloc+1)
582: && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop))) {
583: delnode(p); nrtst++; nchange++; break;
584: }
585: if (p->op==PUSH && p->subop!=LONG &&
586: (isreg(lastrand)>=0 || *lastrand=='$')) {
587: p->subop = LONG;
588: p->pop = 0;
589: nchange++;
590: }
591: setcc(lastrand,p->subop);
592: break;
593:
594: /* .rx,.rx,.rx */
595: case PROBE:
596: case CASE:
597: /* .rx,.rx */
598: case MTPR:
599: case CALLS:
600: case CALLF:
601: case CMP:
602: case BIT:
603: splitrand(p);
604: /* fool repladdr into doing right number of operands */
605: if (p->op==CASE || p->op==PROBE)
606: lastrand=regs[RT4];
607: else lastrand=regs[RT3];
608: if (p->op==CALLF || p->op==CALLS) clearreg();
609: else repladdr(p);
610: ccloc[0]=0; break;
611:
612: /* .rx,.rx,.wx,.wx */
613: case EDIV:
614: splitrand(p);
615: lastrand = regs[RT3];
616: repladdr(p);
617: dest(regs[RT3], p->subop, 1);
618: dest(regs[RT4], p->subop, 0);
619: break;
620:
621: case CBR:
622: if (p->subop>=JBC) {
623: splitrand(p);
624: lastrand=regs[RT3]; /* 2 operands can be optimized */
625: repladdr(p);
626: ccloc[0] = 0;
627: } else
628: reduncbr(p);
629: break;
630:
631: case JBR:
632: redunbr(p);
633:
634: default:
635: clearreg();
636: }
637: }
638: }
639:
640: char *
641: byondrd(p) register struct node *p; {
642: /* return pointer to register which is "beyond last read/modify operand" */
643: if (OP2==(p->subop>>4)) return(regs[RT3]);
644: switch (p->op) {
645: case MFPR:
646: case PUSHA:
647: case TST: case INC: case DEC: case PUSH: return(regs[RT2]);
648: case CBR: /* must be JBC/JBS */
649: case MTPR: case EDIV:
650: case BIT: case CMP: case CALLS: case CALLF: return(regs[RT3]);
651: case PROBE:
652: case MOVBLK:
653: case CASE: return(regs[RT4]);
654: }
655: return(lastrand);
656: }
657:
658: struct node *
659: bflow(p)
660: register struct node *p;
661: {
662: register char *cp1,*cp2,**preg; register int r;
663:
664: int flow= -1;
665: struct node *olduse=0;
666: splitrand(p);
667: if (p->op!=PUSH && p->op!=EDIV && p->subop && 0<=(r=isreg(lastrand))
668: && r<NUSE && uses[r]==p->forw) {
669: if (equtype(p->subop,regs[r][0])
670: || ((p->op==CVT || p->op==MOVZ)
671: && 0xf®s[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))
672: || p->op==MOVA && compat(LONG, regs[r][0])) {
673: register int r2;
674: if (regs[r][1]!=0) {/* send directly to destination */
675: if (p->op==INC || p->op==DEC) {
676: if (p->op==DEC) p->op=SUB; else p->op=ADD;
677: p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */
678: p->pop=0;
679: cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */
680: cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0;
681: }
682: cp1=regs[r]+1; cp2=lastrand;
683: if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */
684: p->pop=0;
685: p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3];
686: }
687: while (*cp2++= *cp1++);
688: if (p->op==MOVA && p->forw->op==PUSH) {
689: p->op=PUSHA;
690: *regs[RT2]=0; p->pop=0;
691: } else if ((p->op==MOV || p->op==CVT) && p->forw->op==PUSH) {
692: p->op=PUSH; p->subop &= 0xF;
693: *regs[RT2]=0; p->pop=0;
694: }
695: delnode(p->forw);
696: if (0<=(r2=isreg(lastrand)) && r2<NUSE) {
697: uses[r2]=uses[r]; uses[r]=0;
698: }
699: redun3(p);
700: newcode(p); redunm++; flow=r;
701: } else if (p->op==MOV) {
702: /* superfluous fetch */
703: int nmatch;
704: char src[C2_ASIZE];
705: movit:
706: cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++);
707: splitrand(p->forw);
708: if (p->forw->op != INC && p->forw->op != DEC)
709: lastrand=byondrd(p->forw);
710: nmatch=0;
711: for (preg=regs+RT1;*preg!=lastrand;preg++)
712: if (r==isreg(*preg)) {
713: cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch;
714: }
715: if (nmatch==1) {
716: if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) {
717: p->forw->pop=0;
718: p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3];
719: *cp1++='r'; *cp1++=r+'0'; *cp1=0;
720: }
721: delnode(p); p=p->forw;
722: if (0<=(r2=isreg(src)) && r2<NUSE) {
723: uses[r2]=uses[r]; uses[r]=0;
724: }
725: redun3(p);
726: newcode(p); redunm++; flow=r;
727: } else splitrand(p);
728: }
729: } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ)
730: && p->forw->subop&0xf /* if base or index, then forget it */
731: && compat(p->subop,p->forw->subop) && !indexa(cp1=regs[RT1]))
732: goto movit;
733: }
734: /* adjust 'lastrand' past any 'read' or 'modify' operands. */
735: lastrand=byondrd(p);
736: /* a 'write' clobbers the register. */
737: if (0<=(r=isreg(lastrand)) && r<NUSE
738: || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) {
739: /* writing a dead register is useless, but watch side effects */
740: switch (p->op) {
741: case AOBLEQ: case AOBLSS: case EDIV: break;
742: default:
743: if (uses[r]==0) {/* no direct uses, check for use of condition codes */
744: register struct node *q=p;
745: while ((q=nonlab(q->forw))->op==JBR && q->subop==0) q=q->ref; /* cc unused, unchanged */
746: if (q->op!=CBR && q->op!=ADDA && q->op!=SUBA) {/* ... and destroyed */
747: preg=regs+RT1;
748: while (cp1= *preg++) {
749: if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);}
750: if (equstr(cp1,lastrand)) break;
751: }
752: }
753: }
754: flow=r;
755: }
756: }
757: if (0<=(r=flow)) {olduse=uses[r]; uses[r]=0; *(short *)(regs[r])=0;}
758: /* now look for 'read' or 'modify' (read & write) uses */
759: preg=regs+RT1;
760: while (*(cp1= *preg++)) {
761: /* check for r */
762: if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) {
763: uses[r]=p; cp2=regs[r]; *cp2++=p->subop;
764: if((p->op==SHAL || p->op==SHAR || p->op==SHL || p->op==SHR)
765: && cp1==regs[RT1]) cp2[-1]=BYTE;
766: if(p->op==CBR && (p->subop==JBC || p->subop==JBS)) cp2[-1]=LONG;
767: if(p->op==MOVA && cp1==regs[RT2]) cp2[-1]=LONG;
768: if(p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) {
769: if (p->op==PUSH) cp1="-(sp)";
770: else {
771: cp1=regs[RT2];
772: if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0)
773: uses[r]=olduse; /* reincarnation!! */
774: /* as in addl2 r0,r1; movl r1,r0; ret */
775: if (p->op!=MOV) cp1=0;
776: }
777: if (cp1) while (*cp2++= *cp1++);
778: else *cp2=0;
779: } else *cp2=0;
780: continue;
781: }
782: /* check for (r),[r] */
783: do if (*cp1=='(' || *cp1=='[') {/* get register number */
784: char t;
785: cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0;
786: if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) {
787: uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4);
788: }
789: *cp1=t;
790: } while (*++cp1);
791: }
792: /* ediv's 2nd operand is quad */
793: if(p->op==EDIV && 0<=(r=isreg(regs[RT2])) && ++r<NUSE && uses[r]==0) {
794: uses[r]=p; cp2=regs[r]; *cp2++=p->subop;
795: *cp2=0;
796: }
797: #ifdef MOVAFASTER
798: /* pushax or movax possibility? */
799: cp1=regs[RT1];
800: if (*cp1++=='$' && isstatic(cp1)) {
801: if (p->op==MOV && p->subop==LONG) {
802: if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) {
803: cp1=p->code; while (*cp1++!=','); p->code= --cp1;
804: }
805: p->op = MOVA; p->subop = BYTE; ++p->code; p->pop=0;
806: } else if (p->op==PUSH && p->subop==LONG) {
807: p->op = PUSHA; p->subop = BYTE; ++p->code; p->pop=0;
808: } else if (p->op==ADD && p->subop==U(LONG,OP3)
809: && 0<=(r=isreg(regs[RT2]))) {
810: cp1=cp2=p->code; ++cp1;
811: do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='[';
812: do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']';
813: if (!equstr(regs[RT3],"-(sp)")){ p->op = MOVA; p->subop = BYTE;}
814: else {p->op = PUSHA; p->subop = BYTE; *cp2=0;}
815: if (uses[r]==0) {uses[r]=p; regs[r][0]=OPX<<4;}
816: p->pop=0;
817: }
818: }
819: #endif MOVAFASTER
820: return(p);
821: }
822:
823: jumpsw()
824: {
825: register struct node *p, *p1, *pt;
826: register t, nj;
827:
828: t = 0;
829: nj = 0;
830: for (p=first.forw; p!=0; p = p->forw)
831: p->seq = ++t;
832: for (p=first.forw; p!=0; p = p1) {
833: p1 = p->forw;
834: if (p->op == CBR && p1->op==JBR && p->ref && p1->ref
835: && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) {
836: if (p->ref==p1->ref)
837: continue;
838: p->subop = revbr[p->subop];
839: p->pop=0;
840: pt = p1->ref;
841: p1->ref = p->ref;
842: p->ref = pt;
843: t = p1->labno;
844: p1->labno = p->labno;
845: p->labno = t;
846: #ifdef COPYCODE
847: if (p->labno == 0) {
848: pt = (struct node *)p1->code; p1->code = p->code; p->code = (char *)pt;
849: }
850: #endif
851: nrevbr++;
852: nj++;
853: }
854: }
855: return(nj);
856: }
857:
858: addaob()
859: {
860: register struct node *p, *p1, *p2, *p3;
861:
862: for (p = &first; (p1 = p->forw)!=0; p = p1) {
863: if (p->op==INC && p->subop==LONG) {
864: if (p1->op==LABEL && p1->refc==1 && p1->forw->op==CMP && p1->forw->subop==LONG
865: && (p2=p1->forw->forw)->op==CBR && p2->subop==JLE
866: && (p3=p2->ref->back)->op==JBR && p3->subop==0 && p3->ref==p1
867: && p3->forw->op==LABEL && p3->forw==p2->ref) {
868: /* change INC LAB: CMP to LAB: INC CMP */
869: p->back->forw=p1; p1->back=p->back;
870: p->forw=p1->forw; p1->forw->back=p;
871: p->back=p1; p1->forw=p;
872: p1=p->forw;
873: /* adjust beginning value by 1 */
874: p2=alloc(sizeof first); p2->op = DEC; p2->subop = LONG;
875: p2->pop=0;
876: p2->forw=p3; p2->back=p3->back; p3->back->forw=p2;
877: p3->back=p2; p2->code=p->code; p2->labno=0;
878: }
879: if (p1->op==CMP && p1->subop==LONG &&
880: (p2=p1->forw)->op==CBR && p2->forw->op!=CBR) {
881: register char *cp1,*cp2;
882: splitrand(p1); if (!equstr(p->code,regs[RT1])) continue;
883: if (p2->subop==JLE || p2->subop==JLT) {
884: if (p2->subop==JLE) p->op = AOBLEQ; else p->op = AOBLSS; p->subop = 0;
885: cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */
886: cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */
887: p->pop=0; newcode(p);
888: p->labno = p2->labno; delnode(p2); delnode(p1); naob++;
889: }
890: }
891: }
892: }
893: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.