10 * complete R structure
13 for(r=firstr; r!=R; r=r1) {
44 for(r=firstr; r!=R; r=r->link) {
46 if(p->as == AMOVW || p->as == AFMOVF || p->as == AFMOVD)
49 if(p->from.type == p->to.type) {
54 if(subprop(r) && copyprop(r)) {
60 if(p->to.type == D_REG) {
67 if(subprop(r) && copyprop(r)) {
77 * look for MOVB x,R; MOVB R,R
79 for(r=firstr; r!=R; r=r->link) {
88 if(p->to.type != D_REG)
98 if(p1->from.type != D_REG || p1->from.reg != p->to.reg)
100 if(p1->to.type != D_REG || p1->to.reg != p->to.reg)
113 p->from = zprog.from;
115 p->reg = zprog.reg; /**/
126 if(r1 == R || r1->p2link != R)
153 if(a->type == D_CONST)
166 if(a->type == D_REG) {
171 if(a->type == D_FREG)
177 * the idea is to substitute
178 * one register for another
179 * from one MOV to another
181 * ADD b, R0 / no use of R1
183 * would be converted to
187 * hopefully, then the former or latter MOV
188 * will be eliminated by copy propagation.
205 for(r=uniqp(r0); r!=R; r=uniqp(r)) {
235 if(p->to.type == v1->type)
236 if(p->to.reg == v1->reg) {
246 if(p->to.type == v1->type)
247 if(p->to.reg == v1->reg)
251 if(copyau(&p->from, v2) ||
255 if(copysub(&p->from, v1, v2, 0) ||
256 copysub1(p, v1, v2, 0) ||
257 copysub(&p->to, v1, v2, 0))
263 copysub(&p->to, v1, v2, 1);
265 print("gotit: %D->%D\n%P", v1, v2, r->prog);
266 if(p->from.type == v2->type)
270 for(r=uniqs(r); r!=r0; r=uniqs(r)) {
272 copysub(&p->from, v1, v2, 1);
273 copysub1(p, v1, v2, 1);
274 copysub(&p->to, v1, v2, 1);
276 print("%P\n", r->prog);
282 print("%P last\n", r->prog);
287 * The idea is to remove redundant copies.
296 * set v2 return success
310 for(r=firstr; r!=R; r=r->link)
312 return copy1(v1, v2, r0->s1, 0);
315 copy1(Adr *v1, Adr *v2, Reg *r, int f)
322 print("act set; return 1\n");
327 print("copy %D->%D f=%d\n", v1, v2, f);
328 for(; r != R; r = r->s1) {
332 if(!f && uniqp(r) == R) {
335 print("; merge; f=%d", f);
339 case 2: /* rar, cant split */
341 print("; %Drar; return 0\n", v2);
346 print("; %Dset; return 1\n", v2);
349 case 1: /* used, substitute */
350 case 4: /* use and set */
355 print("; %Dused+set and f=%d; return 0\n", v2, f);
357 print("; %Dused and f=%d; return 0\n", v2, f);
360 if(copyu(p, v2, v1)) {
362 print("; sub fail; return 0\n");
366 print("; sub%D/%D", v2, v1);
369 print("; %Dused+set; return 1\n", v2);
376 if(!f && (t == 2 || t == 3 || t == 4)) {
379 print("; %Dset and !f; f=%d", v1, f);
385 if(!copy1(v1, v2, r->s2, f))
393 * 1 if v only used (and substitute),
394 * 2 if read-alter-rewrite
397 * 0 otherwise (not touched)
400 copyu(Prog *p, Adr *v, Adr *s)
411 case ANOP: /* read, write */
427 if(copysub(&p->from, v, s, 1))
429 if(!copyas(&p->to, v))
430 if(copysub(&p->to, v, s, 1))
434 if(copyas(&p->to, v)) {
435 if(copyau(&p->from, v))
439 if(copyau(&p->from, v))
441 if(copyau(&p->to, v))
445 case AADD: /* read read write */
468 if(copysub(&p->from, v, s, 1))
470 if(copysub1(p, v, s, 1))
472 if(!copyas(&p->to, v))
473 if(copysub(&p->to, v, s, 1))
477 if(copyas(&p->to, v)) {
480 if(copyau(&p->from, v))
486 if(copyau(&p->from, v))
490 if(copyau(&p->to, v))
494 case ABA: /* no reference */
528 case ACMP: /* read read */
532 if(copysub(&p->from, v, s, 1))
534 return copysub(&p->to, v, s, 1);
536 if(copyau(&p->from, v))
538 if(copyau(&p->to, v))
542 case AJMP: /* funny */
544 if(copysub(&p->to, v, s, 1))
548 if(copyau(&p->to, v))
552 case ARETURN: /* funny */
556 if(v->type == D_FREG)
557 if(v->reg == FREGRET)
560 case AJMPL: /* funny */
561 if(v->type == D_REG) {
562 if(v->reg <= REGEXT && v->reg > exregoffset)
567 if(v->type == D_FREG) {
568 if(v->reg <= FREGEXT && v->reg > exfregoffset)
573 if(copysub(&p->to, v, s, 1))
577 if(copyau(&p->to, v))
581 case ATEXT: /* funny */
625 * could be set/use depending on
629 copyas(Adr *a, Adr *v)
633 if(a->type == v->type)
640 * either direct or indirect
643 copyau(Adr *a, Adr *v)
649 if(a->type == D_OREG)
656 copyau1(Prog *p, Adr *v)
660 if(p->from.type == v->type || p->to.type == v->type)
661 if(p->reg == v->reg) {
662 if(a2type(p) != v->type)
663 print("botch a2type %P\n", p);
670 * substitute s for v in a
671 * return failure to substitute
674 copysub(Adr *a, Adr *v, Adr *s, int f)
684 copysub1(Prog *p1, Adr *v, Adr *s, int f)