3 int xtramodes(Reg*, Adr*);
12 * complete R structure
15 for(r=firstr; r!=R; r=r1) {
46 for(r=firstr; r!=R; r=r->link) {
48 if(p->as == ASLL || p->as == ASRL || p->as == ASRA) {
50 * elide shift into D_SHIFT operand of subsequent instruction
57 if(p->as == AMOVW || p->as == AMOVF || p->as == AMOVD)
59 if(p->from.type == D_CONST)
60 constprop(&p->from, &p->to, r->s1);
61 else if(regtyp(&p->from))
62 if(p->from.type == p->to.type) {
67 if(subprop(r) && copyprop(r)) {
77 * look for MOVB x,R; MOVB R,R
79 for(r=firstr; r!=R; r=r->link) {
86 * EOR -1,x,y => MVN x,y
88 if(p->from.type == D_CONST && p->from.offset == -1) {
94 p->from.reg = p->to.reg;
102 if(p->to.type != D_REG)
112 if(p1->from.type != D_REG || p1->from.reg != p->to.reg)
114 if(p1->to.type != D_REG || p1->to.reg != p->to.reg)
119 for(r=firstr; r!=R; r=r->link) {
125 if(p->from.type == D_OREG && p->from.offset == 0)
126 xtramodes(r, &p->from);
127 else if(p->to.type == D_OREG && p->to.offset == 0)
128 xtramodes(r, &p->to);
134 * elide CMP $0,x if calculation of x can set condition codes
136 if(p->from.type != D_CONST || p->from.offset != 0)
166 while (r1 != R && r1->prog->as == ANOP);
170 if(p1->to.type != D_REG)
172 if(p1->to.reg != p->reg)
173 if(!(p1->as == AMOVW && p1->from.type == D_REG && p1->from.reg == p->reg))
179 if(p1->from.type != D_REG)
211 p->scond = zprog.scond;
212 p->from = zprog.from;
214 p->reg = zprog.reg; /**/
225 if(r1 == R || r1->p2link != R)
255 if(a->type == D_FREG)
261 * the idea is to substitute
262 * one register for another
263 * from one MOV to another
265 * ADD b, R0 / no use of R1
267 * would be converted to
271 * hopefully, then the former or latter MOV
272 * will be eliminated by copy propagation.
289 for(r=uniqp(r0); r!=R; r=uniqp(r)) {
322 if(p->to.type == v1->type)
323 if(p->to.reg == v1->reg) {
333 if(p->to.type == v1->type)
334 if(p->to.reg == v1->reg)
340 if((p->from.type == D_CONST && (p->from.offset&t)) ||
341 (p->to.type == D_CONST && (p->to.offset&t)))
345 if(copyau(&p->from, v2) ||
349 if(copysub(&p->from, v1, v2, 0) ||
350 copysub1(p, v1, v2, 0) ||
351 copysub(&p->to, v1, v2, 0))
357 copysub(&p->to, v1, v2, 1);
359 print("gotit: %D->%D\n%P", v1, v2, r->prog);
360 if(p->from.type == v2->type)
364 for(r=uniqs(r); r!=r0; r=uniqs(r)) {
366 copysub(&p->from, v1, v2, 1);
367 copysub1(p, v1, v2, 1);
368 copysub(&p->to, v1, v2, 1);
370 print("%P\n", r->prog);
376 print("%P last\n", r->prog);
381 * The idea is to remove redundant copies.
390 * set v2 return success
404 for(r=firstr; r!=R; r=r->link)
406 return copy1(v1, v2, r0->s1, 0);
410 copy1(Adr *v1, Adr *v2, Reg *r, int f)
417 print("act set; return 1\n");
422 print("copy %D->%D f=%d\n", v1, v2, f);
423 for(; r != R; r = r->s1) {
427 if(!f && uniqp(r) == R) {
430 print("; merge; f=%d", f);
434 case 2: /* rar, cant split */
436 print("; %Drar; return 0\n", v2);
441 print("; %Dset; return 1\n", v2);
444 case 1: /* used, substitute */
445 case 4: /* use and set */
450 print("; %Dused+set and f=%d; return 0\n", v2, f);
452 print("; %Dused and f=%d; return 0\n", v2, f);
455 if(copyu(p, v2, v1)) {
457 print("; sub fail; return 0\n");
461 print("; sub%D/%D", v2, v1);
464 print("; %Dused+set; return 1\n", v2);
471 if(!f && (t == 2 || t == 3 || t == 4)) {
474 print("; %Dset and !f; f=%d", v1, f);
480 if(!copy1(v1, v2, r->s2, f))
487 * The idea is to remove redundant constants.
489 * ($c1->v2 s/$c1/v1)*
491 * The v1->v2 should be eliminated by copy propagation.
494 constprop(Adr *c1, Adr *v1, Reg *r)
499 print("constprop %D->%D\n", c1, v1);
500 for(; r != R; r = r->s1) {
506 print("; merge; return\n");
509 if(p->as == AMOVW && copyas(&p->from, c1)) {
511 print("; sub%D/%D", &p->from, v1);
513 } else if(copyu(p, v1, A) > 1) {
515 print("; %Dset; return\n", v1);
521 constprop(c1, v1, r->s2);
527 * .. (not use w, not set x y w)
528 * AXXX w,a,b (a != w)
531 * ----------- changed to
536 #define FAIL(msg) { if(debug['H']) print("\t%s; FAILURE\n", msg); return 0; }
546 if(p->to.type != D_REG)
547 FAIL("BOTCH: result not reg");
550 if(p->reg != NREG && p->reg != p->to.reg) {
555 print("shiftprop\n%P", p);
558 /* find first use of shift result; abort if shift operands or result are changed */
567 switch(copyu(p1, &p->to, A)) {
568 case 0: /* not used or set */
569 if((p->from.type == D_REG && copyu(p1, &p->from, A) > 1) ||
570 (a.type == D_REG && copyu(p1, &a, A) > 1))
571 FAIL("args modified");
573 case 3: /* set, not used */
574 FAIL("BOTCH: noref");
578 /* check whether substitution can be done */
591 if(p1->reg == n || (p1->reg == NREG && p1->to.type == D_REG && p1->to.reg == n)) {
592 if(p1->from.type != D_REG)
594 p1->reg = p1->from.reg;
618 if(p1->reg == NREG && p1->to.reg == n)
619 FAIL("shift result used twice");
621 if(p1->from.type == D_SHIFT)
622 FAIL("shift result used in shift");
623 if(p1->from.type != D_REG || p1->from.reg != n)
624 FAIL("BOTCH: where is it used?");
627 /* check whether shift result is used subsequently */
633 FAIL("inconclusive");
637 switch(copyu(p1, &p->to, A)) {
638 case 0: /* not used or set */
640 case 3: /* set, not used */
647 /* make the substitution */
648 p2->from.type = D_SHIFT;
653 switch(p->from.type){
655 o |= (p->from.offset&0x1f)<<7;
658 o |= (1<<4) | (p->from.reg<<8);
674 print("\t=>%P\tSUCCEED\n", p2);
679 findpre(Reg *r, Adr *v)
683 for(r1=uniqp(r); r1!=R; r=r1,r1=uniqp(r)) {
686 switch(copyu(r1->prog, v, A)) {
688 case 2: /* read-alter-rewrite */
691 case 4: /* set and used */
699 findinc(Reg *r, Reg *r2, Adr *v)
705 for(r1=uniqs(r); r1!=R && r1!=r2; r=r1,r1=uniqs(r)) {
708 switch(copyu(r1->prog, v, A)) {
709 case 0: /* not touched */
711 case 4: /* set and used */
714 if(p->from.type == D_CONST)
715 if(p->from.offset > -4096 && p->from.offset < 4096)
725 nochange(Reg *r, Reg *r2, Prog *p)
733 if(p->reg != NREG && p->reg != p->to.reg) {
737 switch(p->from.type) {
740 a[n++].reg = p->from.offset&0xf;
743 a[n++].reg = p->from.reg;
747 for(; r!=R && r!=r2; r=uniqs(r)) {
750 if(copyu(p, &a[i], A) > 1)
757 findu1(Reg *r, Adr *v)
759 for(; r != R; r = r->s1) {
763 switch(copyu(r->prog, v, A)) {
765 case 2: /* read-alter-rewrite */
766 case 4: /* set and used */
772 if (findu1(r->s2, v))
779 finduse(Reg *r, Adr *v)
783 for(r1=firstr; r1!=R; r1=r1->link)
789 xtramodes(Reg *r, Adr *a)
796 if(debug['h'] && p->as == AMOVB && p->from.type == D_OREG) /* byte load */
803 if(p1->to.type == D_REG && p1->to.reg == v.reg)
806 if(p1->from.type == D_REG ||
807 (p1->from.type == D_SHIFT && (p1->from.offset&(1<<4)) == 0 &&
808 (p->as != AMOVB || (a == &p->from && (p1->from.offset&~0xf) == 0))) ||
809 (p1->from.type == D_CONST &&
810 p1->from.offset > -4096 && p1->from.offset < 4096))
811 if(nochange(uniqs(r1), r, p1)) {
812 if(a != &p->from || v.reg != p->to.reg)
813 if (finduse(r->s1, &v)) {
814 if(p1->reg == NREG || p1->reg == v.reg)
819 switch (p1->from.type) {
821 /* register offset */
823 a->offset = p1->from.reg;
826 /* scaled register offset */
829 /* immediate offset */
830 a->offset = p1->from.offset;
840 if(p1->from.type == D_REG)
841 if((r2 = findinc(r1, r, &p1->from)) != R) {
842 for(r3=uniqs(r2); r3->prog->as==ANOP; r3=uniqs(r3))
848 a->offset = p1->from.offset;
850 if(!finduse(r, &r1->prog->to))
859 if(a != &p->from || a->reg != p->to.reg)
860 if((r1 = findinc(r, R, &v)) != R) {
863 a->offset = p1->from.offset;
873 * 1 if v only used (and substitute),
874 * 2 if read-alter-rewrite
877 * 0 otherwise (not touched)
880 copyu(Prog *p, Adr *v, Adr *s)
893 if(p->from.type == D_CONST) { /* read reglist, read/rar */
895 if(p->from.offset&(1<<v->reg))
897 if(copysub(&p->to, v, s, 1))
901 if(copyau(&p->to, v)) {
906 if(p->from.offset&(1<<v->reg))
908 } else { /* read/rar, write reglist */
910 if(p->to.offset&(1<<v->reg))
912 if(copysub(&p->from, v, s, 1))
916 if(copyau(&p->from, v)) {
919 if(p->to.offset&(1<<v->reg))
923 if(p->to.offset&(1<<v->reg))
928 case ANOP: /* read, write */
940 if(p->scond&(C_WBIT|C_PBIT))
941 if(v->type == D_REG) {
942 if(p->from.type == D_OREG || p->from.type == D_SHIFT) {
943 if(p->from.reg == v->reg)
946 if(p->to.reg == v->reg)
951 if(copysub(&p->from, v, s, 1))
953 if(!copyas(&p->to, v))
954 if(copysub(&p->to, v, s, 1))
958 if(copyas(&p->to, v)) {
959 if(copyau(&p->from, v))
963 if(copyau(&p->from, v))
965 if(copyau(&p->to, v))
970 case AADD: /* read, read, write */
997 if(copysub(&p->from, v, s, 1))
999 if(copysub1(p, v, s, 1))
1001 if(!copyas(&p->to, v))
1002 if(copysub(&p->to, v, s, 1))
1006 if(copyas(&p->to, v)) {
1009 if(copyau(&p->from, v))
1015 if(copyau(&p->from, v))
1019 if(copyau(&p->to, v))
1023 case ABEQ: /* read, read */
1040 if(copysub(&p->from, v, s, 1))
1042 return copysub1(p, v, s, 1);
1044 if(copyau(&p->from, v))
1050 case AB: /* funny */
1052 if(copysub(&p->to, v, s, 1))
1056 if(copyau(&p->to, v))
1060 case ARET: /* funny */
1061 if(v->type == D_REG)
1062 if(v->reg == REGRET)
1064 if(v->type == D_FREG)
1065 if(v->reg == FREGRET)
1068 case ABL: /* funny */
1069 if(v->type == D_REG) {
1070 if(v->reg <= REGEXT && v->reg > exregoffset)
1072 if(v->reg == REGARG)
1075 if(v->type == D_FREG)
1076 if(v->reg <= FREGEXT && v->reg > exfregoffset)
1080 if(copysub(&p->to, v, s, 1))
1084 if(copyau(&p->to, v))
1088 case ATEXT: /* funny */
1089 if(v->type == D_REG)
1090 if(v->reg == REGARG)
1137 * could be set/use depending on
1141 copyas(Adr *a, Adr *v)
1145 if(a->type == v->type)
1146 if(a->reg == v->reg)
1148 } else if(v->type == D_CONST) { /* for constprop */
1149 if(a->type == v->type)
1150 if(a->name == v->name)
1151 if(a->sym == v->sym)
1152 if(a->reg == v->reg)
1153 if(a->offset == v->offset)
1160 * either direct or indirect
1163 copyau(Adr *a, Adr *v)
1168 if(v->type == D_REG) {
1169 if(a->type == D_OREG) {
1170 if(v->reg == a->reg)
1172 } else if(a->type == D_SHIFT) {
1173 if((a->offset&0xf) == v->reg)
1175 if((a->offset&(1<<4)) && (a->offset>>8) == v->reg)
1183 copyau1(Prog *p, Adr *v)
1187 if(a2type(p) == v->type)
1188 if(p->reg == v->reg) {
1189 if(a2type(p) != v->type)
1190 print("botch a2type %P\n", p);
1198 * substitute s for v in a
1199 * return failure to substitute
1202 copysub(Adr *a, Adr *v, Adr *s, int f)
1207 if(a->type == D_SHIFT) {
1208 if((a->offset&0xf) == v->reg)
1209 a->offset = (a->offset&~0xf)|s->reg;
1210 if((a->offset&(1<<4)) && (a->offset>>8) == v->reg)
1211 a->offset = (a->offset&~(0xf<<8))|(s->reg<<8);
1219 copysub1(Prog *p1, Adr *v, Adr *s, int f)
1234 { ABEQ, ABNE, 0x0, 0x1, },
1235 { ABNE, ABEQ, 0x1, 0x0, },
1236 { ABCS, ABCC, 0x2, 0x3, },
1237 { ABHS, ABLO, 0x2, 0x3, },
1238 { ABCC, ABCS, 0x3, 0x2, },
1239 { ABLO, ABHS, 0x3, 0x2, },
1240 { ABMI, ABPL, 0x4, 0x5, },
1241 { ABPL, ABMI, 0x5, 0x4, },
1242 { ABVS, ABVC, 0x6, 0x7, },
1243 { ABVC, ABVS, 0x7, 0x6, },
1244 { ABHI, ABLS, 0x8, 0x9, },
1245 { ABLS, ABHI, 0x9, 0x8, },
1246 { ABGE, ABLT, 0xA, 0xB, },
1247 { ABLT, ABGE, 0xB, 0xA, },
1248 { ABGT, ABLE, 0xC, 0xD, },
1249 { ABLE, ABGT, 0xD, 0xC, },
1278 return (ABEQ <= p->as) && (p->as <= ABLE);
1290 || p->as == AHISTORY
1292 || p->as == ASIGNAME
1304 * Depends on an analysis of the encodings performed by 5l.
1305 * These seem to be all of the opcodes that lead to the "S" bit
1306 * being set in the instruction encodings.
1308 * C_SBIT may also have been set explicitly in p->scond.
1311 modifiescpsr(Prog *p)
1313 return (p->scond&C_SBIT)
1328 * Find the maximal chain of instructions starting with r which could
1329 * be executed conditionally
1332 joinsplit(Reg *r, Joininfo *j)
1338 if (r->p2 && (r->p1 || r->p2->p2link)) {
1342 if (r->s1 && r->s2) {
1347 if (r->prog->as != ANOP)
1349 if (!r->s1 && !r->s2) {
1357 if (modifiescpsr(r->prog)) {
1362 } while (j->len < 4);
1377 applypred(Reg *rstart, Joininfo *j, int cond, int branch)
1384 if (cond == Truecond)
1385 pred = predinfo[rstart->prog->as - ABEQ].scond;
1387 pred = predinfo[rstart->prog->as - ABEQ].notscond;
1389 for (r = j->start; ; r = successor(r)) {
1390 if (r->prog->as == AB) {
1391 if (r != j->last || branch == Delbranch)
1394 if (cond == Truecond)
1395 r->prog->as = predinfo[rstart->prog->as - ABEQ].opcode;
1397 r->prog->as = predinfo[rstart->prog->as - ABEQ].notopcode;
1400 else if (predicable(r->prog))
1401 r->prog->scond = (r->prog->scond&~C_SCOND)|pred;
1402 if (r->s1 != r->link) {
1418 for(r=firstr; r!=R; r=r->link) {
1419 if (isbranch(r->prog)) {
1420 t1 = joinsplit(r->s1, &j1);
1421 t2 = joinsplit(r->s2, &j2);
1422 if(j1.last->link != j2.start)
1424 if(j1.end == j2.end)
1425 if((t1 == Branch && (t2 == Join || t2 == Setcond)) ||
1426 (t2 == Join && (t1 == Join || t1 == Setcond))) {
1427 applypred(r, &j1, Falsecond, Delbranch);
1428 applypred(r, &j2, Truecond, Delbranch);
1432 if(t1 == End || t1 == Branch) {
1433 applypred(r, &j1, Falsecond, Keepbranch);