19 if(p->to.type == D_BRANCH)
35 * complete R structure
38 for(r=firstr; r!=R; r=r1) {
67 pc = 0; /* speculating it won't kill */
72 for(r=firstr; r!=R; r=r->link) {
77 if(regtyp(&p->from)) {
82 if(subprop(r) && copyprop(r)) {
97 if(p->as == p1->as && p->to.type == p1->from.type)
104 if(p->from.type != D_CONST || needc(p->link))
106 if(p->from.offset == -1){
111 p->from = zprog.from;
113 else if(p->from.offset == 1){
118 p->from = zprog.from;
123 if(p->from.type != D_CONST || needc(p->link))
125 if(p->from.offset == -1) {
130 p->from = zprog.from;
132 else if(p->from.offset == 1){
137 p->from = zprog.from;
153 p->from = zprog.from;
165 if(r1 == R || r1->p2link != R)
195 if(t >= D_AX && t <= D_DI)
201 * the idea is to substitute
202 * one register for another
203 * from one MOV to another
205 * ADD b, R0 / no use of R1
207 * would be converted to
211 * hopefully, then the former or latter MOV
212 * will be eliminated by copy propagation.
229 for(r=uniqp(r0); r!=R; r=uniqp(r)) {
239 if(p->to.type != D_NONE)
286 if(p->to.type == v1->type)
290 if(copyau(&p->from, v2) ||
293 if(copysub(&p->from, v1, v2, 0) ||
294 copysub(&p->to, v1, v2, 0))
300 copysub(&p->to, v1, v2, 1);
302 print("gotit: %D->%D\n%P", v1, v2, r->prog);
303 if(p->from.type == v2->type)
307 for(r=uniqs(r); r!=r0; r=uniqs(r)) {
309 copysub(&p->from, v1, v2, 1);
310 copysub(&p->to, v1, v2, 1);
312 print("%P\n", r->prog);
318 print("%P last\n", r->prog);
323 * The idea is to remove redundant copies.
332 * set v2 return success
346 for(r=firstr; r!=R; r=r->link)
348 return copy1(v1, v2, r0->s1, 0);
352 copy1(Adr *v1, Adr *v2, Reg *r, int f)
359 print("act set; return 1\n");
364 print("copy %D->%D f=%d\n", v1, v2, f);
365 for(; r != R; r = r->s1) {
369 if(!f && uniqp(r) == R) {
372 print("; merge; f=%d", f);
376 case 2: /* rar, cant split */
378 print("; %D rar; return 0\n", v2);
383 print("; %D set; return 1\n", v2);
386 case 1: /* used, substitute */
387 case 4: /* use and set */
392 print("; %D used+set and f=%d; return 0\n", v2, f);
394 print("; %D used and f=%d; return 0\n", v2, f);
397 if(copyu(p, v2, v1)) {
399 print("; sub fail; return 0\n");
403 print("; sub %D/%D", v2, v1);
406 print("; %D used+set; return 1\n", v2);
413 if(!f && (t == 2 || t == 3 || t == 4)) {
416 print("; %D set and !f; f=%d", v1, f);
422 if(!copy1(v1, v2, r->s2, f))
430 * 1 if v only used (and substitute),
431 * 2 if read-alter-rewrite
434 * 0 otherwise (not touched)
437 copyu(Prog *p, Adr *v, Adr *s)
444 print("unknown op %A\n", p->as);
453 if(copyas(&p->to, v))
457 case ALEAL: /* lhs addr, rhs store */
458 if(copyas(&p->from, v))
462 case ANOP: /* rhs store */
468 if(copyas(&p->to, v)) {
470 return copysub(&p->from, v, s, 1);
471 if(copyau(&p->from, v))
495 if(copyas(&p->to, v))
497 if(copyas(&p->from, v))
498 if(p->from.type == D_CX)
502 case AADDB: /* rhs rar */
568 if(copyas(&p->to, v))
572 case ACMPL: /* read only */
592 if(copysub(&p->from, v, s, 1))
594 return copysub(&p->to, v, s, 1);
596 if(copyau(&p->from, v))
598 if(copyau(&p->to, v))
602 case AJGE: /* no reference */
622 if(p->to.type != D_NONE) {
623 if(copyas(&p->to, v))
641 if(v->type == D_AX || v->type == D_DX)
653 if(v->type == D_DI || v->type == D_SI)
659 if(v->type == D_AX || v->type == D_DI)
668 case AJMP: /* funny */
670 if(copysub(&p->to, v, s, 1))
674 if(copyau(&p->to, v))
678 case ARET: /* funny */
679 if(v->type == REGRET)
685 case ACALL: /* funny */
686 if(REGARG>=0 && v->type == REGARG)
690 if(copysub(&p->to, v, s, 1))
694 if(copyau(&p->to, v))
703 * could be set/use depending on
707 copyas(Adr *a, Adr *v)
709 if(a->type != v->type)
713 if(v->type == D_AUTO || v->type == D_PARAM)
714 if(v->offset == a->offset)
720 * either direct or indirect
723 copyau(Adr *a, Adr *v)
729 if(a->type-D_INDIR == v->type)
731 if(a->index == v->type)
738 * substitute s for v in a
739 * return failure to substitute
742 copysub(Adr *a, Adr *v, Adr *s, int f)
748 if(t >= D_AX && t <= D_DI) {
756 if(a->type == t+D_INDIR) {
757 if(s->type == D_BP && a->index != D_NONE)
758 return 1; /* can't use BP-base with index */
760 a->type = s->type+D_INDIR;