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 || p->as == AROR) {
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)
254 if(a->type == D_FREG)
260 * the idea is to substitute
261 * one register for another
262 * from one MOV to another
264 * ADD b, R0 / no use of R1
266 * would be converted to
270 * hopefully, then the former or latter MOV
271 * will be eliminated by copy propagation.
288 for(r=uniqp(r0); r!=R; r=uniqp(r)) {
323 if(p->to.type == v1->type)
324 if(p->to.reg == v1->reg) {
334 if(p->to.type == v1->type)
335 if(p->to.reg == v1->reg)
341 if((p->from.type == D_CONST && (p->from.offset&t)) ||
342 (p->to.type == D_CONST && (p->to.offset&t)))
346 if(copyau(&p->from, v2) ||
350 if(copysub(&p->from, v1, v2, 0) ||
351 copysub1(p, v1, v2, 0) ||
352 copysub(&p->to, v1, v2, 0))
358 copysub(&p->to, v1, v2, 1);
360 print("gotit: %D->%D\n%P", v1, v2, r->prog);
361 if(p->from.type == v2->type)
365 for(r=uniqs(r); r!=r0; r=uniqs(r)) {
367 copysub(&p->from, v1, v2, 1);
368 copysub1(p, v1, v2, 1);
369 copysub(&p->to, v1, v2, 1);
371 print("%P\n", r->prog);
377 print("%P last\n", r->prog);
382 * The idea is to remove redundant copies.
391 * set v2 return success
405 for(r=firstr; r!=R; r=r->link)
407 return copy1(v1, v2, r0->s1, 0);
411 copy1(Adr *v1, Adr *v2, Reg *r, int f)
418 print("act set; return 1\n");
423 print("copy %D->%D f=%d\n", v1, v2, f);
424 for(; r != R; r = r->s1) {
428 if(!f && uniqp(r) == R) {
431 print("; merge; f=%d", f);
435 case 2: /* rar, cant split */
437 print("; %Drar; return 0\n", v2);
442 print("; %Dset; return 1\n", v2);
445 case 1: /* used, substitute */
446 case 4: /* use and set */
451 print("; %Dused+set and f=%d; return 0\n", v2, f);
453 print("; %Dused and f=%d; return 0\n", v2, f);
456 if(copyu(p, v2, v1)) {
458 print("; sub fail; return 0\n");
462 print("; sub%D/%D", v2, v1);
465 print("; %Dused+set; return 1\n", v2);
472 if(!f && (t == 2 || t == 3 || t == 4)) {
475 print("; %Dset and !f; f=%d", v1, f);
481 if(!copy1(v1, v2, r->s2, f))
488 * The idea is to remove redundant constants.
490 * ($c1->v2 s/$c1/v1)*
492 * The v1->v2 should be eliminated by copy propagation.
495 constprop(Adr *c1, Adr *v1, Reg *r)
500 print("constprop %D->%D\n", c1, v1);
501 for(; r != R; r = r->s1) {
507 print("; merge; return\n");
510 if(p->as == AMOVW && copyas(&p->from, c1)) {
512 print("; sub%D/%D", &p->from, v1);
514 } else if(copyu(p, v1, A) > 1) {
516 print("; %Dset; return\n", v1);
522 constprop(c1, v1, r->s2);
528 * .. (not use w, not set x y w)
529 * AXXX w,a,b (a != w)
532 * ----------- changed to
537 #define FAIL(msg) { if(debug['H']) print("\t%s; FAILURE\n", msg); return 0; }
547 if(p->to.type != D_REG)
548 FAIL("BOTCH: result not reg");
551 if(p->reg != NREG && p->reg != p->to.reg) {
556 print("shiftprop\n%P", p);
559 /* find first use of shift result; abort if shift operands or result are changed */
568 switch(copyu(p1, &p->to, A)) {
569 case 0: /* not used or set */
570 if((p->from.type == D_REG && copyu(p1, &p->from, A) > 1) ||
571 (a.type == D_REG && copyu(p1, &a, A) > 1))
572 FAIL("args modified");
574 case 3: /* set, not used */
575 FAIL("BOTCH: noref");
579 /* check whether substitution can be done */
592 if(p1->reg == n || (p1->reg == NREG && p1->to.type == D_REG && p1->to.reg == n)) {
593 if(p1->from.type != D_REG)
595 p1->reg = p1->from.reg;
619 if(p1->reg == NREG && p1->to.reg == n)
620 FAIL("shift result used twice");
622 if(p1->from.type == D_SHIFT)
623 FAIL("shift result used in shift");
624 if(p1->from.type != D_REG || p1->from.reg != n)
625 FAIL("BOTCH: where is it used?");
628 /* check whether shift result is used subsequently */
634 FAIL("inconclusive");
638 switch(copyu(p1, &p->to, A)) {
639 case 0: /* not used or set */
641 case 3: /* set, not used */
648 /* make the substitution */
649 p2->from.type = D_SHIFT;
654 switch(p->from.type){
656 o |= (p->from.offset&0x1f)<<7;
659 o |= (1<<4) | (p->from.reg<<8);
678 print("\t=>%P\tSUCCEED\n", p2);
683 findpre(Reg *r, Adr *v)
687 for(r1=uniqp(r); r1!=R; r=r1,r1=uniqp(r)) {
690 switch(copyu(r1->prog, v, A)) {
692 case 2: /* read-alter-rewrite */
695 case 4: /* set and used */
703 findinc(Reg *r, Reg *r2, Adr *v)
709 for(r1=uniqs(r); r1!=R && r1!=r2; r=r1,r1=uniqs(r)) {
712 switch(copyu(r1->prog, v, A)) {
713 case 0: /* not touched */
715 case 4: /* set and used */
718 if(p->from.type == D_CONST)
719 if(p->from.offset > -4096 && p->from.offset < 4096)
729 nochange(Reg *r, Reg *r2, Prog *p)
737 if(p->reg != NREG && p->reg != p->to.reg) {
741 switch(p->from.type) {
744 a[n++].reg = p->from.offset&0xf;
747 a[n++].reg = p->from.reg;
751 for(; r!=R && r!=r2; r=uniqs(r)) {
754 if(copyu(p, &a[i], A) > 1)
761 findu1(Reg *r, Adr *v)
763 for(; r != R; r = r->s1) {
767 switch(copyu(r->prog, v, A)) {
769 case 2: /* read-alter-rewrite */
770 case 4: /* set and used */
776 if (findu1(r->s2, v))
783 finduse(Reg *r, Adr *v)
787 for(r1=firstr; r1!=R; r1=r1->link)
793 xtramodes(Reg *r, Adr *a)
800 if(debug['h'] && p->as == AMOVB && p->from.type == D_OREG) /* byte load */
807 if(p1->to.type == D_REG && p1->to.reg == v.reg)
810 if(p1->from.type == D_REG ||
811 (p1->from.type == D_SHIFT && (p1->from.offset&(1<<4)) == 0 &&
812 (p->as != AMOVB || (a == &p->from && (p1->from.offset&~0xf) == 0))) ||
813 (p1->from.type == D_CONST &&
814 p1->from.offset > -4096 && p1->from.offset < 4096))
815 if(nochange(uniqs(r1), r, p1)) {
816 if(a != &p->from || v.reg != p->to.reg)
817 if (finduse(r->s1, &v)) {
818 if(p1->reg == NREG || p1->reg == v.reg)
823 switch (p1->from.type) {
825 /* register offset */
827 a->offset = p1->from.reg;
830 /* scaled register offset */
833 /* immediate offset */
834 a->offset = p1->from.offset;
844 if(p1->from.type == D_REG)
845 if((r2 = findinc(r1, r, &p1->from)) != R) {
846 for(r3=uniqs(r2); r3->prog->as==ANOP; r3=uniqs(r3))
852 a->offset = p1->from.offset;
854 if(!finduse(r, &r1->prog->to))
863 if(a != &p->from || a->reg != p->to.reg)
864 if((r1 = findinc(r, R, &v)) != R) {
867 a->offset = p1->from.offset;
877 * 1 if v only used (and substitute),
878 * 2 if read-alter-rewrite
881 * 0 otherwise (not touched)
884 copyu(Prog *p, Adr *v, Adr *s)
900 if(copyau(&p->to, v))
901 return (p->as == AMULAL || p->as == AMULALU) ? 2 : 3;
902 if(p->from.reg == v->reg || p->reg == v->reg){
903 if(s != A && !copyau(&p->to, s) && p->from.reg != s->reg && p->reg != s->reg){
904 if(p->from.reg == v->reg)
905 p->from.reg = s->reg;
916 if(p->from.type == D_CONST) { /* read reglist, read/rar */
918 if(p->from.offset&(1<<v->reg))
920 if(copysub(&p->to, v, s, 1))
924 if(copyau(&p->to, v)) {
929 if(p->from.offset&(1<<v->reg))
931 } else { /* read/rar, write reglist */
933 if(p->to.offset&(1<<v->reg))
935 if(copysub(&p->from, v, s, 1))
939 if(copyau(&p->from, v)) {
942 if(p->to.offset&(1<<v->reg))
946 if(p->to.offset&(1<<v->reg))
951 case ANOP: /* read, write */
963 if(p->scond&(C_WBIT|C_PBIT))
964 if(v->type == D_REG) {
965 if(p->from.type == D_OREG || p->from.type == D_SHIFT) {
966 if(p->from.reg == v->reg)
969 if(p->to.reg == v->reg)
974 if(copysub(&p->from, v, s, 1))
976 if(!copyas(&p->to, v))
977 if(copysub(&p->to, v, s, 1))
981 if(copyas(&p->to, v)) {
982 if(copyau(&p->from, v))
986 if(copyau(&p->from, v))
988 if(copyau(&p->to, v))
993 case AADD: /* read, read, write */
1022 if(copysub(&p->from, v, s, 1))
1024 if(copysub1(p, v, s, 1))
1026 if(!copyas(&p->to, v))
1027 if(copysub(&p->to, v, s, 1))
1031 if(copyas(&p->to, v)) {
1034 if(copyau(&p->from, v))
1040 if(copyau(&p->from, v))
1044 if(copyau(&p->to, v))
1048 case ABEQ: /* read, read */
1065 if(copysub(&p->from, v, s, 1))
1067 return copysub1(p, v, s, 1);
1069 if(copyau(&p->from, v))
1075 case AB: /* funny */
1077 if(copysub(&p->to, v, s, 1))
1081 if(copyau(&p->to, v))
1085 case ARET: /* funny */
1086 if(v->type == D_REG)
1087 if(v->reg == REGRET)
1089 if(v->type == D_FREG)
1090 if(v->reg == FREGRET)
1093 case ABL: /* funny */
1094 if(v->type == D_REG) {
1095 if(v->reg <= REGEXT && v->reg > exregoffset)
1097 if(v->reg == (uchar)REGARG)
1100 if(v->type == D_FREG)
1101 if(v->reg <= FREGEXT && v->reg > exfregoffset)
1105 if(copysub(&p->to, v, s, 1))
1109 if(copyau(&p->to, v))
1113 case ATEXT: /* funny */
1114 if(v->type == D_REG)
1115 if(v->reg == (uchar)REGARG)
1164 * could be set/use depending on
1168 copyas(Adr *a, Adr *v)
1172 if(a->type == v->type)
1173 if(a->reg == v->reg)
1175 } else if(v->type == D_CONST) { /* for constprop */
1176 if(a->type == v->type)
1177 if(a->name == v->name)
1178 if(a->sym == v->sym)
1179 if(a->reg == v->reg)
1180 if(a->offset == v->offset)
1187 * either direct or indirect
1190 copyau(Adr *a, Adr *v)
1195 if(v->type == D_REG) {
1196 if(a->type == D_OREG) {
1197 if(a->reg == v->reg)
1199 } else if(a->type == D_SHIFT) {
1200 if((a->offset&0xf) == v->reg)
1202 if((a->offset&(1<<4)) && (a->offset>>8) == v->reg)
1204 } else if(a->type == D_REGREG) {
1205 if(a->reg == v->reg || a->offset == v->reg)
1213 copyau1(Prog *p, Adr *v)
1217 if(a2type(p) == v->type)
1218 if(p->reg == v->reg) {
1219 if(a2type(p) != v->type)
1220 print("botch a2type %P\n", p);
1228 * substitute s for v in a
1229 * return failure to substitute
1232 copysub(Adr *a, Adr *v, Adr *s, int f)
1237 if(a->type == D_SHIFT) {
1238 if((a->offset&0xf) == v->reg)
1239 a->offset = (a->offset&~0xf)|s->reg;
1240 if((a->offset&(1<<4)) && (a->offset>>8) == v->reg)
1241 a->offset = (a->offset&~(0xf<<8))|(s->reg<<8);
1242 } else if(a->type == D_REGREG) {
1243 if(a->offset == v->reg)
1245 if(a->reg == v->reg)
1254 copysub1(Prog *p1, Adr *v, Adr *s, int f)
1269 { ABEQ, ABNE, 0x0, 0x1, },
1270 { ABNE, ABEQ, 0x1, 0x0, },
1271 { ABCS, ABCC, 0x2, 0x3, },
1272 { ABHS, ABLO, 0x2, 0x3, },
1273 { ABCC, ABCS, 0x3, 0x2, },
1274 { ABLO, ABHS, 0x3, 0x2, },
1275 { ABMI, ABPL, 0x4, 0x5, },
1276 { ABPL, ABMI, 0x5, 0x4, },
1277 { ABVS, ABVC, 0x6, 0x7, },
1278 { ABVC, ABVS, 0x7, 0x6, },
1279 { ABHI, ABLS, 0x8, 0x9, },
1280 { ABLS, ABHI, 0x9, 0x8, },
1281 { ABGE, ABLT, 0xA, 0xB, },
1282 { ABLT, ABGE, 0xB, 0xA, },
1283 { ABGT, ABLE, 0xC, 0xD, },
1284 { ABLE, ABGT, 0xD, 0xC, },
1313 return (ABEQ <= p->as) && (p->as <= ABLE);
1325 || p->as == AHISTORY
1327 || p->as == ASIGNAME
1339 * Depends on an analysis of the encodings performed by 5l.
1340 * These seem to be all of the opcodes that lead to the "S" bit
1341 * being set in the instruction encodings.
1343 * C_SBIT may also have been set explicitly in p->scond.
1346 modifiescpsr(Prog *p)
1348 return (p->scond&C_SBIT)
1363 * Find the maximal chain of instructions starting with r which could
1364 * be executed conditionally
1367 joinsplit(Reg *r, Joininfo *j)
1373 if (r->p2 && (r->p1 || r->p2->p2link)) {
1377 if (r->s1 && r->s2) {
1382 if (r->prog->as != ANOP)
1384 if (!r->s1 && !r->s2) {
1392 switch(r->prog->as){
1397 /* emulated by 5l, doesnt handle conditionals */
1401 if (modifiescpsr(r->prog)) {
1406 } while (j->len < 4);
1421 applypred(Reg *rstart, Joininfo *j, int cond, int branch)
1428 if (cond == Truecond)
1429 pred = predinfo[rstart->prog->as - ABEQ].scond;
1431 pred = predinfo[rstart->prog->as - ABEQ].notscond;
1433 for (r = j->start; ; r = successor(r)) {
1434 if (r->prog->as == AB) {
1435 if (r != j->last || branch == Delbranch)
1438 if (cond == Truecond)
1439 r->prog->as = predinfo[rstart->prog->as - ABEQ].opcode;
1441 r->prog->as = predinfo[rstart->prog->as - ABEQ].notopcode;
1444 else if (predicable(r->prog))
1445 r->prog->scond = (r->prog->scond&~C_SCOND)|pred;
1446 if (r->s1 != r->link) {
1462 for(r=firstr; r!=R; r=r->link) {
1463 if (isbranch(r->prog)) {
1464 t1 = joinsplit(r->s1, &j1);
1465 t2 = joinsplit(r->s2, &j2);
1466 if(j1.last->link != j2.start)
1468 if(j1.end == j2.end)
1469 if((t1 == Branch && (t2 == Join || t2 == Setcond)) ||
1470 (t2 == Join && (t1 == Join || t1 == Setcond))) {
1471 applypred(r, &j1, Falsecond, Delbranch);
1472 applypred(r, &j2, Truecond, Delbranch);
1476 if(t1 == End || t1 == Branch) {
1477 applypred(r, &j1, Falsecond, Keepbranch);