24 if(p->to.type == D_BRANCH)
41 if(p->as != ANOP || p->from.type != D_NONE || p->to.type != D_NONE)
59 * complete R structure
62 for(r=firstr; r!=R; r=r1) {
91 pc = 0; /* speculating it won't kill */
96 for(r=firstr; r!=R; r=r->link) {
105 if(regtyp(&p->from)) {
111 if(subprop(r) && copyprop(r)) {
119 r1 = rnops(uniqs(r));
122 if(p1->as == AMOVLQZX && p1->from.type == p->to.type && p1->to.type == p->to.type){
134 r1 = rnops(uniqs(r));
137 if(p->to.type != p1->from.type)
139 if((p->as == AMOVBLZX && p1->as == AMOVBQZX)
140 || (p->as == AMOVWLZX && p1->as == AMOVWQZX)
141 || (p->as == AMOVBLSX && p1->as == AMOVBQSX && p1->to.type == p->to.type)
142 || (p->as == AMOVWLSX && p1->as == AMOVWQSX && p1->to.type == p->to.type)) {
147 if(p->as == p1->as) {
162 r1 = rnops(uniqs(r));
165 if(p->as == p1->as && p->to.type == p1->from.type){
176 if(p->from.type != D_CONST || needc(p->link))
178 if(p->from.offset == -1){
181 else if(p->as == AADDL)
185 p->from = zprog.from;
187 else if(p->from.offset == 1){
190 else if(p->as == AADDL)
194 p->from = zprog.from;
201 if(p->from.type != D_CONST || needc(p->link))
203 if(p->from.offset == -1) {
206 else if(p->as == ASUBL)
210 p->from = zprog.from;
212 else if(p->from.offset == 1){
215 else if(p->as == ASUBL)
219 p->from = zprog.from;
225 if(p->to.type != D_CONST || p->to.offset != 0 || regtyp(&p->from) == 0)
227 if(p->link == P || (p->link->as != AJEQ && p->link->as != AJNE))
230 while(r1 != R && r1->prog->as == ANOP)
232 if(r1 == R || r1->prog->to.type != p->from.type)
240 /* shift doesnt affect ZF when shift count is zero */
241 if(p1->from.type != D_CONST || p1->from.offset == 0)
271 /* shift doesnt affect ZF when shift count is zero */
272 if(p1->from.type != D_CONST || p1->from.offset == 0)
290 p->from = zprog.from;
302 if(r1 == R || r1->p2link != R)
332 if(t >= D_AX && t <= D_R15)
334 if(t >= D_X0 && t <= D_X0+15)
340 * the idea is to substitute
341 * one register for another
342 * from one MOV to another
344 * ADD b, R0 / no use of R1
346 * would be converted to
350 * hopefully, then the former or latter MOV
351 * will be eliminated by copy propagation.
368 for(r=uniqp(r0); r!=R; r=uniqp(r)) {
379 if(p->to.type != D_NONE)
445 * MOV R2, R1 / no use for R1
447 if(p->to.type == v1->type && p->from.type == v2->type){
448 copysub(&p->from, v2, v1, 1);
471 if(p->to.type == v1->type)
475 if(copyau(&p->from, v2) ||
478 if(copysub(&p->from, v1, v2, 0) ||
479 copysub(&p->to, v1, v2, 0))
485 copysub(&p->to, v1, v2, 1);
487 print("gotit: %D->%D\n%P", v1, v2, r->prog);
488 if(p->from.type == v2->type)
492 for(r=uniqs(r); r!=r0; r=uniqs(r)) {
494 copysub(&p->from, v1, v2, 1);
495 copysub(&p->to, v1, v2, 1);
497 print("%P\n", r->prog);
503 print("%P last\n", r->prog);
508 * The idea is to remove redundant copies.
517 * set v2 return success
531 for(r=firstr; r!=R; r=r->link)
533 return copy1(v1, v2, r0->s1, 0);
537 copy1(Adr *v1, Adr *v2, Reg *r, int f)
544 print("act set; return 1\n");
549 print("copy %D->%D f=%d\n", v1, v2, f);
550 for(; r != R; r = r->s1) {
554 if(!f && uniqp(r) == R) {
557 print("; merge; f=%d", f);
561 case 2: /* rar, cant split */
563 print("; %D rar; return 0\n", v2);
568 print("; %D set; return 1\n", v2);
571 case 1: /* used, substitute */
572 case 4: /* use and set */
577 print("; %D used+set and f=%d; return 0\n", v2, f);
579 print("; %D used and f=%d; return 0\n", v2, f);
582 if(copyu(p, v2, v1)) {
584 print("; sub fail; return 0\n");
588 print("; sub %D/%D", v2, v1);
591 print("; %D used+set; return 1\n", v2);
598 if(!f && (t == 2 || t == 3 || t == 4)) {
601 print("; %D set and !f; f=%d", v1, f);
607 if(!copy1(v1, v2, r->s2, f))
615 * 1 if v only used (and substitute),
616 * 2 if read-alter-rewrite
619 * 0 otherwise (not touched)
622 copyu(Prog *p, Adr *v, Adr *s)
629 print("unknown op %A\n", p->as);
630 /* SBBL; ADCL; FLD1; SAHF */
642 if(copyas(&p->to, v))
646 case ALEAL: /* lhs addr, rhs store */
648 if(copyas(&p->from, v))
652 case ANOP: /* rhs store */
683 if(copyas(&p->to, v)) {
685 return copysub(&p->from, v, s, 1);
686 if(copyau(&p->from, v))
716 if(copyas(&p->to, v))
718 if(copyas(&p->from, v))
719 if(p->from.type == D_CX)
723 case AADDB: /* rhs rar */
771 if(copyas(&p->to, v))
775 case ACMPL: /* read only */
786 if(copysub(&p->from, v, s, 1))
788 return copysub(&p->to, v, s, 1);
790 if(copyau(&p->from, v))
792 if(copyau(&p->to, v))
796 case AJGE: /* no reference */
817 if(p->to.type != D_NONE) {
818 if(copyas(&p->to, v))
840 if(v->type == D_AX || v->type == D_DX)
848 if(v->type == D_CX || v->type == D_DI || v->type == D_SI)
852 case AJMP: /* funny */
854 if(copysub(&p->to, v, s, 1))
858 if(copyau(&p->to, v))
862 case ARET: /* funny */
863 if(v->type == REGRET || v->type == FREGRET)
869 case ACALL: /* funny */
870 if(REGEXT && v->type <= REGEXT && v->type > exregoffset)
872 if(REGARG && v->type == REGARG)
876 if(copysub(&p->to, v, s, 1))
880 if(copyau(&p->to, v))
884 case ATEXT: /* funny */
885 if(REGARG && v->type == REGARG)
894 * could be set/use depending on
898 copyas(Adr *a, Adr *v)
900 if(a->type != v->type)
904 if(v->type == D_AUTO || v->type == D_PARAM)
905 if(v->offset == a->offset)
911 * either direct or indirect
914 copyau(Adr *a, Adr *v)
920 if(a->type-D_INDIR == v->type)
922 if(a->index == v->type)
929 * substitute s for v in a
930 * return failure to substitute
933 copysub(Adr *a, Adr *v, Adr *s, int f)
939 if(t >= D_AX && t <= D_R15 || t >= D_X0 && t <= D_X0+15) {
947 if(a->type == t+D_INDIR) {
948 if((s->type == D_BP || s->type == D_R13) && a->index != D_NONE)
949 return 1; /* can't use BP-base with index */
951 a->type = s->type+D_INDIR;