Debian Codegraph @

mip/match.c

raw symbols SCIP
1 /* $Id: match.c,v 1.107 2019/05/01 06:34:19 ragge Exp $ */
2 /*
3 * Copyright (c) 2003 Anders Magnusson ([email protected]).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 *
36 * Redistributions of source code and documentation must retain the above
37 * copyright notice, this list of conditions and the following disclaimer.
38 * Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditionsand the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed or owned by Caldera
44 * International, Inc.
45 * Neither the name of Caldera International, Inc. nor the names of other
46 * contributors may be used to endorse or promote products derived from
47 * this software without specific prior written permission.
48 *
49 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
50 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
54 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
58 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
59 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 */
62
63 #include "pass2.h"
64
65 #ifdef HAVE_STRINGS_H
66 #include <strings.h>
67 #endif
68
69 void setclass(int tmp, int class);
70 int getclass(int tmp);
71
72 #ifdef PCC_DEBUG
73 static char *srtyp[] = { "SRNOPE", "SRDIR", "SROREG", "SRREG" };
74 #endif
75
76 /*
77 * return true if shape is appropriate for the node p
78 *
79 * Return values:
80 * SRNOPE Cannot match this shape.
81 * SRDIR Direct match, may or may not traverse down.
82 * SRREG Will match if put in a regster XXX - kill this?
83 */
84 int
85 tshape(NODE *p, int shape)
86 {
87 int o, mask;
88
89 o = p->n_op;
90
91 #ifdef PCC_DEBUG
92 if (s2debug)
93 printf("tshape(%p, %s) op = %s\n", p, prcook(shape), opst[o]);
94 #endif
95
96 if (shape & SPECIAL) {
97
98 switch (shape) {
99 case SZERO:
100 case SONE:
101 case SMONE:
102 case SSCON:
103 case SCCON:
104 if (o != ICON || p->n_name[0])
105 return SRNOPE;
106 if (getlval(p)== 0 && shape == SZERO)
107 return SRDIR;
108 if (getlval(p) == 1 && shape == SONE)
109 return SRDIR;
110 if (getlval(p) == -1 && shape == SMONE)
111 return SRDIR;
112 if (getlval(p) > -257 && getlval(p) < 256 &&
113 shape == SCCON)
114 return SRDIR;
115 if (getlval(p) > -32769 && getlval(p) < 32768 &&
116 shape == SSCON)
117 return SRDIR;
118 return SRNOPE;
119
120 case SSOREG: /* non-indexed OREG */
121 if (o == OREG && !R2TEST(p->n_rval))
122 return SRDIR;
123 return SRNOPE;
124
125 default:
126 return (special(p, shape));
127 }
128 }
129
130 if (shape & SANY)
131 return SRDIR;
132
133 if ((shape&INTEMP) && shtemp(p)) /* XXX remove? */
134 return SRDIR;
135
136 if ((shape&SWADD) && (o==NAME||o==OREG))
137 if (BYTEOFF(getlval(p)))
138 return SRNOPE;
139
140 switch (o) {
141
142 case NAME:
143 if (shape & SNAME)
144 return SRDIR;
145 break;
146
147 case ICON:
148 case FCON:
149 if (shape & SCON)
150 return SRDIR;
151 break;
152
153 case FLD:
154 if (shape & SFLD)
155 return flshape(p->n_left);
156 break;
157
158 case CCODES:
159 if (shape & SCC)
160 return SRDIR;
161 break;
162
163 case REG:
164 case TEMP:
165 mask = PCLASS(p);
166 if (shape & mask)
167 return SRDIR;
168 break;
169
170 case OREG:
171 if (shape & SOREG)
172 return SRDIR;
173 break;
174
175 case UMUL:
176 #if 0
177 if (shumul(p->n_left) & shape)
178 return SROREG; /* Calls offstar to traverse down */
179 break;
180 #else
181 return shumul(p->n_left, shape);
182 #endif
183
184 }
185 return SRNOPE;
186 }
187
188 /*
189 * does the type t match tword
190 */
191 int
192 ttype(TWORD t, int tword)
193 {
194 if (tword & TANY)
195 return(1);
196
197 #ifdef PCC_DEBUG
198 if (t2debug)
199 printf("ttype(0x%x, 0x%x)\n", t, tword);
200 #endif
201 if (ISPTR(t) && ISFTN(DECREF(t)) && (tword & TFTN)) {
202 /* For funny function pointers */
203 return 1;
204 }
205 if (ISPTR(t) && (tword&TPTRTO)) {
206 do {
207 t = DECREF(t);
208 } while (ISARY(t));
209 /* arrays that are left are usually only
210 * in structure references...
211 */
212 return (ttype(t, tword&(~TPTRTO)));
213 }
214 if (t != BTYPE(t))
215 return (tword & TPOINT); /* TPOINT means not simple! */
216 if (tword & TPTRTO)
217 return(0);
218
219 switch (t) {
220 case CHAR:
221 return( tword & TCHAR );
222 case SHORT:
223 return( tword & TSHORT );
224 case STRTY:
225 case UNIONTY:
226 return( tword & TSTRUCT );
227 case INT:
228 return( tword & TINT );
229 case UNSIGNED:
230 return( tword & TUNSIGNED );
231 case USHORT:
232 return( tword & TUSHORT );
233 case UCHAR:
234 return( tword & TUCHAR );
235 case ULONG:
236 return( tword & TULONG );
237 case LONG:
238 return( tword & TLONG );
239 case LONGLONG:
240 return( tword & TLONGLONG );
241 case ULONGLONG:
242 return( tword & TULONGLONG );
243 case FLOAT:
244 return( tword & TFLOAT );
245 case DOUBLE:
246 return( tword & TDOUBLE );
247 case LDOUBLE:
248 return( tword & TLDOUBLE );
249 }
250
251 return(0);
252 }
253
254 #define FLDSZ(x) UPKFSZ(x)
255 #if TARGET_ENDIAN == TARGET_LE
256 #define FLDSHF(x) UPKFOFF(x)
257 #else
258 #define FLDSHF(x) (SZINT - FLDSZ(x) - UPKFOFF(x))
259 #endif
260
261 /*
262 * generate code by interpreting table entry
263 */
264 void
265 expand(NODE *p, int cookie, char *cp)
266 {
267 CONSZ val;
268
269 #if 0
270 printf("expand\n");
271 fwalk(p, e2print, 0);
272 #endif
273
274 for( ; *cp; ++cp ){
275 switch( *cp ){
276
277 default:
278 putchar(*cp);
279 continue; /* this is the usual case... */
280
281 case 'Z': /* special machine dependent operations */
282 zzzcode( p, *++cp );
283 continue;
284
285 case 'F': /* this line deleted if FOREFF is active */
286 if (cookie & FOREFF) {
287 while (*cp && *cp != '\n')
288 cp++;
289 if (*cp == 0)
290 return;
291 }
292 continue;
293
294 case 'S': /* field size */
295 if (fldexpand(p, cookie, &cp))
296 continue;
297 printf("%d", FLDSZ(p->n_rval));
298 continue;
299
300 case 'H': /* field shift */
301 if (fldexpand(p, cookie, &cp))
302 continue;
303 printf("%d", FLDSHF(p->n_rval));
304 continue;
305
306 case 'M': /* field mask */
307 case 'N': /* complement of field mask */
308 if (fldexpand(p, cookie, &cp))
309 continue;
310 val = 1;
311 val <<= FLDSZ(p->n_rval);
312 --val;
313 val <<= FLDSHF(p->n_rval);
314 adrcon( *cp=='M' ? val : ~val );
315 continue;
316
317 case 'L': /* output special label field */
318 if (*++cp == 'C')
319 printf(LABFMT, p->n_label);
320 else
321 printf(LABFMT, (int)getlval(getlr(p,*cp)));
322 continue;
323
324 case 'O': /* opcode string */
325 #ifdef FINDMOPS
326 if (p->n_op == ASSIGN)
327 hopcode(*++cp, p->n_right->n_op);
328 else
329 #endif
330 hopcode( *++cp, p->n_op );
331 continue;
332
333 case 'B': /* byte offset in word */
334 val = getlval(getlr(p,*++cp));
335 val = BYTEOFF(val);
336 printf( CONFMT, val );
337 continue;
338
339 case 'C': /* for constant value only */
340 conput(stdout, getlr( p, *++cp ) );
341 continue;
342
343 case 'I': /* in instruction */
344 insput( getlr( p, *++cp ) );
345 continue;
346
347 case 'A': /* address of */
348 adrput(stdout, getlr( p, *++cp ) );
349 continue;
350
351 case 'U': /* for upper half of address, only */
352 upput(getlr(p, *++cp), SZLONG);
353 continue;
354
355 }
356
357 }
358
359 }
360
361 NODE resc[NRESC];
362
363 NODE *
364 getlr(NODE *p, int c)
365 {
366 /* return the pointer to the left or right side of p, or p itself,
367 depending on the optype of p */
368
369 switch (c) {
370
371 case '1':
372 case '2':
373 case '3':
374 case 'D':
375 if (c == 'D')
376 c = 0;
377 else
378 c -= '0';
379 if (resc[c].n_op == FREE)
380 comperr("getlr: free node");
381 return &resc[c];
382
383 case 'L':
384 return( optype( p->n_op ) == LTYPE ? p : p->n_left );
385
386 case 'R':
387 return( optype( p->n_op ) != BITYPE ? p : p->n_right );
388
389 }
390 cerror( "bad getlr: %c", c );
391 /* NOTREACHED */
392 return NULL;
393 }
394
395 #ifdef PCC_DEBUG
396 #define F2DEBUG(x) if (f2debug) printf x
397 #define F2WALK(x) if (f2debug) fwalk(x, e2print, 0)
398 #else
399 #define F2DEBUG(x)
400 #define F2WALK(x)
401 #endif
402
403 /*
404 * Convert a node to REG or OREG.
405 * Shape is register class where we want the result.
406 * Returns register class if register nodes.
407 * If w is: (should be shapes)
408 * - SRREG - result in register, call geninsn().
409 * - SROREG - create OREG; call offstar().
410 * - 0 - clear su, walk down.
411 */
412 static int
413 swmatch(NODE *p, int shape, int w)
414 {
415 int rv = 0;
416
417 F2DEBUG(("swmatch: p=%p, shape=%s, w=%s\n", p, prcook(shape), srtyp[w]));
418
419 switch (w) {
420 case SRREG:
421 rv = geninsn(p, shape);
422 break;
423
424 case SROREG:
425 /* should be here only if op == UMUL */
426 if (p->n_op != UMUL && p->n_op != FLD)
427 comperr("swmatch %p", p);
428 if (p->n_op == FLD) {
429 offstar(p->n_left->n_left, shape);
430 p->n_left->n_su = 0;
431 } else
432 offstar(p->n_left, shape);
433 p->n_su = 0;
434 rv = ffs(shape)-1;
435 break;
436
437 case 0:
438 if (optype(p->n_op) == BITYPE)
439 swmatch(p->n_right, 0, 0);
440 if (optype(p->n_op) != LTYPE)
441 swmatch(p->n_left, 0, 0);
442 p->n_su = 0;
443 }
444 return rv;
445
446 }
447
448 /*
449 * Help routines for find*() functions.
450 * If the node will be a REG node and it will be rewritten in the
451 * instruction, ask for it to be put in a register.
452 */
453 static int
454 chcheck(NODE *p, int shape, int rew)
455 {
456 int sh, sha;
457
458 sha = shape;
459 if (shape & SPECIAL)
460 shape = 0;
461
462 switch ((sh = tshape(p, sha))) {
463 case SRNOPE:
464 if (shape & INREGS)
465 sh = SRREG;
466 break;
467
468 case SROREG:
469 case SRDIR:
470 if (rew == 0)
471 break;
472 if (shape & INREGS)
473 sh = SRREG;
474 else
475 sh = SRNOPE;
476 break;
477 }
478 return sh;
479 }
480
481 /*
482 * Check how to walk further down.
483 * Merge with swmatch()?
484 * sh - shape for return value (register class).
485 * p - node (for this leg)
486 * shape - given shape for this leg
487 * cookie - cookie given for parent node
488 * rew -
489 * go - switch key for traversing down
490 * returns register class.
491 */
492 static int
493 shswitch(int sh, NODE *p, int shape, int cookie, int rew, int go)
494 {
495 int lsh;
496
497 F2DEBUG(("shswitch: p=%p, shape=%s, ", p, prcook(shape)));
498 F2DEBUG(("cookie=%s, rew=0x%x, go=%s\n", prcook(cookie), rew, srtyp[go]));
499
500 switch (go) {
501 case SRDIR: /* direct match, just clear su */
502 (void)swmatch(p, 0, 0);
503 break;
504
505 case SROREG: /* call offstar to prepare for OREG conversion */
506 (void)swmatch(p, shape, SROREG);
507 break;
508
509 case SRREG: /* call geninsn() to get value into register */
510 lsh = shape & (FORCC | INREGS);
511 if (rew && cookie != FOREFF)
512 lsh &= (cookie & (FORCC | INREGS));
513 lsh = swmatch(p, lsh, SRREG);
514 if (rew)
515 sh = lsh;
516 break;
517 }
518 return sh;
519 }
520
521 #ifdef NEWNEED
522 char *
523 hasneed(char *w, int need)
524 {
525 if (w)
526 for (; *w; w += NEEDADD(*w)) {
527 if (*w == need)
528 return w;
529 }
530 return 0;
531 }
532
533 char *
534 hasneed2(char *w, int need, int need2)
535 {
536 if (w)
537 for (; *w; w += NEEDADD(*w)) {
538 if (*w == need && w[1] == need2)
539 return w;
540 }
541 return 0;
542 }
543 #endif
544
545 /*
546 * Find the best instruction to evaluate the given tree.
547 * Best is to match both subnodes directly, second-best is if
548 * subnodes must be evaluated into OREGs, thereafter if nodes
549 * must be put into registers.
550 * Whether 2-op instructions or 3-op is preferred is depending on in
551 * which order they are found in the table.
552 * mtchno is set to the count of regs needed for its legs.
553 */
554 int
555 findops(NODE *p, int cookie)
556 {
557 extern int *qtable[];
558 struct optab *q, *qq = NULL;
559 int i, shl, shr, *ixp, sh;
560 int lvl = 10, idx = 0, gol = 0, gor = 0;
561 NODE *l, *r;
562
563 F2DEBUG(("findops node %p (%s)\n", p, prcook(cookie)));
564 F2WALK(p);
565
566 ixp = qtable[p->n_op];
567 l = getlr(p, 'L');
568 r = getlr(p, 'R');
569 for (i = 0; ixp[i] >= 0; i++) {
570 q = &table[ixp[i]];
571
572 F2DEBUG(("findop: ixp %d str %s\n", ixp[i], q->cstring));
573 if (!acceptable(q)) /* target-dependent filter */
574 continue;
575
576 if (ttype(l->n_type, q->ltype) == 0 ||
577 ttype(r->n_type, q->rtype) == 0)
578 continue; /* Types must be correct */
579
580 if ((cookie & q->visit) == 0)
581 continue; /* must get a result */
582
583 F2DEBUG(("findop got types\n"));
584
585 if ((shl = chcheck(l, q->lshape, q->rewrite & RLEFT)) == SRNOPE)
586 continue;
587
588 F2DEBUG(("findop lshape %s\n", srtyp[shl]));
589 F2WALK(l);
590
591 if ((shr = chcheck(r, q->rshape, q->rewrite & RRIGHT)) == SRNOPE)
592 continue;
593
594 F2DEBUG(("findop rshape %s\n", srtyp[shr]));
595 F2WALK(r);
596
597 /* Help register assignment after SSA by preferring */
598 /* 2-op insns instead of 3-ops */
599 if (xssa && (q->rewrite & RLEFT) == 0 &&
600 (q->lshape & (INREGS)) && shl == SRDIR)
601 shl = SRREG;
602
603 #ifdef NEWNEED
604 if (hasneed(q->needs, cNREW) != 0)
605 break;
606 #else
607 if (q->needs & REWRITE)
608 break; /* Done here */
609 #endif
610
611 if (lvl <= (shl + shr))
612 continue;
613 lvl = shl + shr;
614 qq = q;
615 idx = ixp[i];
616 gol = shl;
617 gor = shr;
618 }
619 if (lvl == 10) {
620 F2DEBUG(("findops failed\n"));
621 if (setbin(p))
622 return FRETRY;
623 return FFAIL;
624 }
625
626 F2DEBUG(("findops entry %d(%s,%s)\n", idx, srtyp[gol], srtyp[gor]));
627
628 sh = -1;
629
630 if (cookie == FORCC)
631 cookie = INREGS;
632
633 sh = shswitch(sh, p->n_left, qq->lshape, cookie,
634 qq->rewrite & RLEFT, gol);
635 sh = shswitch(sh, p->n_right, qq->rshape, cookie,
636 qq->rewrite & RRIGHT, gor);
637
638 if (sh == -1) {
639 if (cookie == FOREFF || cookie == FORCC)
640 sh = 0;
641 else
642 sh = ffs(cookie & qq->visit & INREGS)-1;
643 }
644 F2DEBUG(("findops: node %p sh %d (%s)\n", p, sh, prcook(1 << sh)));
645 p->n_su = MKIDX(idx, 0);
646 SCLASS(p->n_su, sh);
647 return sh;
648 }
649
650 /*
651 * Find the best relation op for matching the two trees it has.
652 * This is a sub-version of the function findops() above.
653 * The instruction with the lowest grading is emitted.
654 *
655 * Level assignment for priority:
656 * left right prio
657 * - - -
658 * direct direct 1
659 * direct OREG 2 # make oreg
660 * OREG direct 2 # make oreg
661 * OREG OREG 2 # make both oreg
662 * direct REG 3 # put in reg
663 * OREG REG 3 # put in reg, make oreg
664 * REG direct 3 # put in reg
665 * REG OREG 3 # put in reg, make oreg
666 * REG REG 4 # put both in reg
667 */
668 int
669 relops(NODE *p)
670 {
671 extern int *qtable[];
672 struct optab *q;
673 int i, shl = 0, shr = 0, sh;
674 NODE *l, *r;
675 int *ixp, idx = 0;
676 int lvl = 10, gol = 0, gor = 0;
677
678 F2DEBUG(("relops tree:\n"));
679 F2WALK(p);
680
681 l = getlr(p, 'L');
682 r = getlr(p, 'R');
683 ixp = qtable[p->n_op];
684 for (i = 0; ixp[i] >= 0; i++) {
685 q = &table[ixp[i]];
686
687 F2DEBUG(("relops: ixp %d\n", ixp[i]));
688 if (!acceptable(q)) /* target-dependent filter */
689 continue;
690
691 if (ttype(l->n_type, q->ltype) == 0 ||
692 ttype(r->n_type, q->rtype) == 0)
693 continue; /* Types must be correct */
694
695 F2DEBUG(("relops got types\n"));
696 if ((shl = chcheck(l, q->lshape, 0)) == SRNOPE)
697 continue;
698 F2DEBUG(("relops lshape %d\n", shl));
699 F2WALK(p);
700 if ((shr = chcheck(r, q->rshape, 0)) == SRNOPE)
701 continue;
702 F2DEBUG(("relops rshape %d\n", shr));
703 F2WALK(p);
704 #ifdef NEWNEED
705 if (hasneed(q->needs, cNREW) != 0)
706 break;
707 #else
708 if (q->needs & REWRITE)
709 break; /* Done here */
710 #endif
711
712 if (lvl <= (shl + shr))
713 continue;
714 lvl = shl + shr;
715 idx = ixp[i];
716 gol = shl;
717 gor = shr;
718 }
719 if (lvl == 10) {
720 F2DEBUG(("relops failed\n"));
721 if (setbin(p))
722 return FRETRY;
723 return FFAIL;
724 }
725 F2DEBUG(("relops entry %d(%s %s)\n", idx, srtyp[gol], srtyp[gor]));
726
727 q = &table[idx];
728
729 (void)shswitch(-1, p->n_left, q->lshape, INREGS,
730 q->rewrite & RLEFT, gol);
731
732 (void)shswitch(-1, p->n_right, q->rshape, INREGS,
733 q->rewrite & RRIGHT, gor);
734
735 sh = 0;
736 if (q->rewrite & RLEFT)
737 sh = ffs(q->lshape & INREGS)-1;
738 else if (q->rewrite & RRIGHT)
739 sh = ffs(q->rshape & INREGS)-1;
740
741 F2DEBUG(("relops: node %p\n", p));
742 p->n_su = MKIDX(idx, 0);
743 SCLASS(p->n_su, sh);
744 return 0;
745 }
746
747 /*
748 * Find a matching assign op.
749 *
750 * Level assignment for priority:
751 * left right prio
752 * - - -
753 * direct direct 1
754 * direct REG 2
755 * direct OREG 3
756 * OREG direct 4
757 * OREG REG 5
758 * OREG OREG 6
759 */
760 int
761 findasg(NODE *p, int cookie)
762 {
763 extern int *qtable[];
764 struct optab *q;
765 int i, sh, shl, shr, lvl = 10;
766 NODE *l, *r;
767 int *ixp;
768 struct optab *qq = NULL; /* XXX gcc */
769 int idx = 0, gol = 0, gor = 0;
770
771 shl = shr = 0;
772
773 F2DEBUG(("findasg tree: %s\n", prcook(cookie)));
774 F2WALK(p);
775
776 ixp = qtable[p->n_op];
777 l = getlr(p, 'L');
778 r = getlr(p, 'R');
779 for (i = 0; ixp[i] >= 0; i++) {
780 q = &table[ixp[i]];
781
782 F2DEBUG(("findasg: ixp %d\n", ixp[i]));
783 if (!acceptable(q)) /* target-dependent filter */
784 continue;
785
786 if (ttype(l->n_type, q->ltype) == 0 ||
787 ttype(r->n_type, q->rtype) == 0)
788 continue; /* Types must be correct */
789
790 if ((cookie & q->visit) == 0)
791 continue; /* must get a result */
792
793 F2DEBUG(("findasg got types\n"));
794 #ifdef mach_pdp11 /* XXX - check for other targets too */
795 if (p->n_op == STASG && ISPTR(l->n_type)) {
796 /* Accept lvalue to be in register */
797 /* if struct assignment is given a pointer */
798 if ((shl = chcheck(l, q->lshape,
799 q->rewrite & RLEFT)) == SRNOPE)
800 continue;
801 } else
802 #endif
803 {
804 if ((shl = tshape(l, q->lshape)) == SRNOPE)
805 continue;
806 if (shl == SRREG)
807 continue;
808 }
809
810 F2DEBUG(("findasg lshape %d\n", shl));
811 F2WALK(l);
812
813 if ((shr = chcheck(r, q->rshape, q->rewrite & RRIGHT)) == SRNOPE)
814 continue;
815
816 F2DEBUG(("findasg rshape %d\n", shr));
817 F2WALK(r);
818 #ifdef NEWNEED
819 if (hasneed(q->needs, cNREW) != 0)
820 break;
821 #else
822 if (q->needs & REWRITE)
823 break; /* Done here */
824 #endif
825
826 if (lvl <= (shl + shr))
827 continue;
828
829 lvl = shl + shr;
830 qq = q;
831 idx = ixp[i];
832 gol = shl;
833 gor = shr;
834 }
835
836 if (lvl == 10) {
837 F2DEBUG(("findasg failed\n"));
838 if (setasg(p, cookie))
839 return FRETRY;
840 return FFAIL;
841 }
842 F2DEBUG(("findasg entry %d(%s,%s)\n", idx, srtyp[gol], srtyp[gor]));
843
844 sh = -1;
845 sh = shswitch(sh, p->n_left, qq->lshape, cookie,
846 qq->rewrite & RLEFT, gol);
847
848 sh = shswitch(sh, p->n_right, qq->rshape, cookie,
849 qq->rewrite & RRIGHT, gor);
850
851 #ifdef mach_pdp11 /* XXX all targets? */
852 lvl = 0;
853 if (cookie == FOREFF)
854 lvl = RVEFF, sh = 0;
855 else if (cookie == FORCC)
856 lvl = RVCC, sh = 0;
857 else if (sh == -1) {
858 sh = ffs(cookie & qq->visit & INREGS)-1;
859 #ifdef PCC_DEBUG
860 if (sh == -1)
861 comperr("findasg bad shape");
862 #endif
863 SCLASS(lvl,sh);
864 } else
865 SCLASS(lvl,sh);
866 p->n_su = MKIDX(idx, lvl);
867 #else
868 if (sh == -1) {
869 if (cookie == FOREFF)
870 sh = 0;
871 else
872 sh = ffs(cookie & qq->visit & INREGS)-1;
873 }
874 F2DEBUG(("findasg: node %p class %d\n", p, sh));
875
876 p->n_su = MKIDX(idx, 0);
877 SCLASS(p->n_su, sh);
878 #endif /* mach_pdp11 */
879 #ifdef FINDMOPS
880 p->n_su &= ~ISMOPS;
881 #endif
882 return sh;
883 }
884
885 /*
886 * Search for an UMUL table entry that can turn an indirect node into
887 * a move from an OREG.
888 */
889 int
890 findumul(NODE *p, int cookie)
891 {
892 extern int *qtable[];
893 struct optab *q = NULL; /* XXX gcc */
894 int i, shl = 0, shr = 0, sh;
895 int *ixp;
896
897 F2DEBUG(("findumul p %p (%s)\n", p, prcook(cookie)));
898 F2WALK(p);
899
900 ixp = qtable[p->n_op];
901 for (i = 0; ixp[i] >= 0; i++) {
902 q = &table[ixp[i]];
903
904 F2DEBUG(("findumul: ixp %d\n", ixp[i]));
905 if (!acceptable(q)) /* target-dependent filter */
906 continue;
907
908 if ((q->visit & cookie) == 0)
909 continue; /* wrong registers */
910
911 if (ttype(p->n_type, q->rtype) == 0)
912 continue; /* Types must be correct */
913
914
915 F2DEBUG(("findumul got types, rshape %s\n", prcook(q->rshape)));
916 /*
917 * Try to create an OREG of the node.
918 * Fake left even though it's right node,
919 * to be sure of conversion if going down left.
920 */
921 if ((shl = chcheck(p, q->rshape, 0)) == SRNOPE)
922 continue;
923
924 shr = 0;
925
926 #ifdef NEWNEED
927 if (hasneed(q->needs, cNREW) != 0)
928 break;
929 #else
930 if (q->needs & REWRITE)
931 break; /* Done here */
932 #endif
933
934 F2DEBUG(("findumul got shape %s\n", srtyp[shl]));
935
936 break; /* XXX search better matches */
937 }
938 if (ixp[i] < 0) {
939 F2DEBUG(("findumul failed\n"));
940 if (setuni(p, cookie))
941 return FRETRY;
942 return FFAIL;
943 }
944 F2DEBUG(("findumul entry %d(%s %s)\n", ixp[i], srtyp[shl], srtyp[shr]));
945
946 sh = shswitch(-1, p, q->rshape, cookie, q->rewrite & RLEFT, shl);
947 if (sh == -1)
948 sh = ffs(cookie & q->visit & INREGS)-1;
949
950 F2DEBUG(("findumul: node %p (%s)\n", p, prcook(1 << sh)));
951 p->n_su = MKIDX(ixp[i], 0);
952 SCLASS(p->n_su, sh);
953 F2WALK(p);
954 return sh;
955 }
956
957 /*
958 * Find a leaf type node that puts the value into a register.
959 */
960 int
961 findleaf(NODE *p, int cookie)
962 {
963 extern int *qtable[];
964 struct optab *q = NULL; /* XXX gcc */
965 int i, sh;
966 int *ixp;
967
968 F2DEBUG(("findleaf p %p (%s)\n", p, prcook(cookie)));
969 F2WALK(p);
970
971 ixp = qtable[p->n_op];
972 for (i = 0; ixp[i] >= 0; i++) {
973 q = &table[ixp[i]];
974
975 F2DEBUG(("findleaf: ixp %d\n", ixp[i]));
976 if (!acceptable(q)) /* target-dependent filter */
977 continue;
978 if ((q->visit & cookie) == 0)
979 continue; /* wrong registers */
980
981 if (ttype(p->n_type, q->rtype) == 0 ||
982 ttype(p->n_type, q->ltype) == 0)
983 continue; /* Types must be correct */
984
985 F2DEBUG(("findleaf got types, rshape %s\n", prcook(q->rshape)));
986
987 if (chcheck(p, q->rshape, 0) != SRDIR)
988 continue;
989
990 #ifdef NEWNEED
991 if (hasneed(q->needs, cNREW) != 0)
992 break;
993 #else
994 if (q->needs & REWRITE)
995 break; /* Done here */
996 #endif
997
998 break;
999 }
1000 if (ixp[i] < 0) {
1001 F2DEBUG(("findleaf failed\n"));
1002 if (setuni(p, cookie))
1003 return FRETRY;
1004 return FFAIL;
1005 }
1006 F2DEBUG(("findleaf entry %d\n", ixp[i]));
1007
1008 sh = ffs(cookie & q->visit & INREGS)-1;
1009 F2DEBUG(("findleaf: node %p (%s)\n", p, prcook(1 << sh)));
1010 p->n_su = MKIDX(ixp[i], 0);
1011 SCLASS(p->n_su, sh);
1012 return sh;
1013 }
1014
1015 /*
1016 * Find a UNARY op that satisfy the needs.
1017 * For now, the destination is always a register.
1018 * Both source and dest types must match, but only source (left)
1019 * shape is of interest.
1020 */
1021 int
1022 finduni(NODE *p, int cookie)
1023 {
1024 extern int *qtable[];
1025 struct optab *q;
1026 NODE *l, *r;
1027 int i, shl = 0, num = 4;
1028 int *ixp, idx = 0;
1029 int sh;
1030
1031 F2DEBUG(("finduni tree: %s\n", prcook(cookie)));
1032 F2WALK(p);
1033
1034 l = getlr(p, 'L');
1035 if (p->n_op == CALL || p->n_op == FORTCALL || p->n_op == STCALL)
1036 r = p;
1037 else
1038 r = getlr(p, 'R');
1039 ixp = qtable[p->n_op];
1040 for (i = 0; ixp[i] >= 0; i++) {
1041 q = &table[ixp[i]];
1042
1043 F2DEBUG(("finduni: ixp %d\n", ixp[i]));
1044 if (!acceptable(q)) /* target-dependent filter */
1045 continue;
1046
1047 if (ttype(l->n_type, q->ltype) == 0)
1048 continue; /* Type must be correct */
1049
1050 F2DEBUG(("finduni got left type\n"));
1051 if (ttype(r->n_type, q->rtype) == 0)
1052 continue; /* Type must be correct */
1053
1054 F2DEBUG(("finduni got types\n"));
1055 if ((shl = chcheck(l, q->lshape, q->rewrite & RLEFT)) == SRNOPE)
1056 continue;
1057
1058 F2DEBUG(("finduni got shapes %d\n", shl));
1059
1060 if ((cookie & q->visit) == 0) /* check correct return value */
1061 continue; /* XXX - should check needs */
1062
1063 /* avoid clobbering of longlived regs */
1064 /* let register allocator coalesce */
1065 if ((q->rewrite & RLEFT) && (shl == SRDIR) /* && isreg(l) */)
1066 shl = SRREG;
1067
1068 F2DEBUG(("finduni got cookie\n"));
1069 #ifdef NEWNEED
1070 if (hasneed(q->needs, cNREW) != 0)
1071 break;
1072 #else
1073 if (q->needs & REWRITE)
1074 break; /* Done here */
1075 #endif
1076
1077 if (shl >= num)
1078 continue;
1079 num = shl;
1080 idx = ixp[i];
1081
1082 if (shl == SRDIR)
1083 break;
1084 }
1085
1086 if (num == 4) {
1087 F2DEBUG(("finduni failed\n"));
1088 } else
1089 F2DEBUG(("finduni entry %d(%s)\n", idx, srtyp[num]));
1090
1091 if (num == 4) {
1092 if (setuni(p, cookie))
1093 return FRETRY;
1094 return FFAIL;
1095 }
1096 q = &table[idx];
1097
1098 sh = shswitch(-1, p->n_left, q->lshape, cookie,
1099 q->rewrite & RLEFT, num);
1100 if (sh == -1)
1101 sh = ffs(cookie & q->visit & INREGS)-1;
1102 if (sh == -1)
1103 sh = 0;
1104
1105 F2DEBUG(("finduni: node %p (%s)\n", p, prcook(1 << sh)));
1106 p->n_su = MKIDX(idx, 0);
1107 SCLASS(p->n_su, sh);
1108 return sh;
1109 }
1110
1111 #ifdef FINDMOPS
1112 /*
1113 * Try to find constructs like "a = a + 1;" and match them together
1114 * with instructions like "incl a" or "addl $1,a".
1115 *
1116 * Level assignment for priority:
1117 * left right prio
1118 * - - -
1119 * direct direct 1
1120 * direct REG 2
1121 * direct OREG 3
1122 * OREG direct 4
1123 * OREG REG 5
1124 * OREG OREG 6
1125 */
1126 int
1127 findmops(NODE *p, int cookie)
1128 {
1129 extern int *qtable[];
1130 struct optab *q;
1131 int i, sh, shl, shr, lvl = 10;
1132 NODE *l, *r;
1133 int *ixp;
1134 struct optab *qq = NULL; /* XXX gcc */
1135 int idx = 0, gol = 0, gor = 0;
1136
1137 shl = shr = 0;
1138
1139 F2DEBUG(("findmops tree: %s\n", prcook(cookie)));
1140 F2WALK(p);
1141
1142 l = getlr(p, 'L');
1143 r = getlr(p, 'R');
1144 /* See if this is a usable tree to work with */
1145 /* Currently only check for leaves */
1146 if (optype(r->n_op) != BITYPE || treecmp(l, r->n_left) == 0)
1147 return FFAIL;
1148
1149 F2DEBUG(("findmops is useable\n"));
1150
1151 /* We can try to find a match. Use right op */
1152 ixp = qtable[r->n_op];
1153 l = getlr(r, 'L');
1154 r = getlr(r, 'R');
1155
1156 for (i = 0; ixp[i] >= 0; i++) {
1157 q = &table[ixp[i]];
1158
1159 F2DEBUG(("findmops: ixp %d\n", ixp[i]));
1160 if (!acceptable(q)) /* target-dependent filter */
1161 continue;
1162
1163 if (ttype(l->n_type, q->ltype) == 0 ||
1164 ttype(r->n_type, q->rtype) == 0)
1165 continue; /* Types must be correct */
1166
1167 F2DEBUG(("findmops got types\n"));
1168
1169 switch (cookie) {
1170 case FOREFF:
1171 if ((q->visit & FOREFF) == 0)
1172 continue; /* Not only for side effects */
1173 break;
1174 case FORCC:
1175 if ((q->visit & FORCC) == 0)
1176 continue; /* Not only for side effects */
1177 break;
1178 default:
1179 if ((cookie & q->visit) == 0)
1180 continue; /* Won't match requested shape */
1181 if (((cookie & INREGS & q->lshape) == 0) || !isreg(l))
1182 continue; /* Bad return register */
1183 break;
1184 }
1185 F2DEBUG(("findmops cookie\n"));
1186
1187 /*
1188 * left shape must match left node.
1189 */
1190 if ((shl = tshape(l, q->lshape)) != SRDIR && (shl != SROREG))
1191 continue;
1192
1193 F2DEBUG(("findmops lshape %s\n", srtyp[shl]));
1194 F2WALK(l);
1195
1196 if ((shr = chcheck(r, q->rshape, 0)) == SRNOPE)
1197 continue;
1198
1199 F2DEBUG(("findmops rshape %s\n", srtyp[shr]));
1200
1201 /*
1202 * Only allow RLEFT. XXX
1203 */
1204 if ((q->rewrite & (RLEFT|RRIGHT)) != RLEFT)
1205 continue;
1206
1207 F2DEBUG(("rewrite OK\n"));
1208
1209 F2WALK(r);
1210 #ifdef NEWNEED
1211 if (hasneed(q->needs, cNREW) != 0)
1212 break;
1213 #else
1214 if (q->needs & REWRITE)
1215 break; /* Done here */
1216 #endif
1217
1218 if (lvl <= (shl + shr))
1219 continue;
1220
1221 lvl = shl + shr;
1222 qq = q;
1223 idx = ixp[i];
1224 gol = shl;
1225 gor = shr;
1226 }
1227
1228 if (lvl == 10)
1229 return FFAIL;
1230 F2DEBUG(("findmops entry %d(%s,%s)\n", idx, srtyp[gol], srtyp[gor]));
1231
1232 /*
1233 * Now we're here and have a match. left is semi-direct and
1234 * right may be anything.
1235 */
1236
1237 sh = -1;
1238 sh = shswitch(sh, p->n_left, qq->lshape, cookie,
1239 qq->rewrite & RLEFT, gol);
1240 sh = shswitch(sh, r, qq->rshape, cookie, 0, gor);
1241
1242 if (sh == -1) {
1243 if (cookie & (FOREFF|FORCC))
1244 sh = 0;
1245 else
1246 sh = ffs(cookie & qq->visit & INREGS)-1;
1247 }
1248 F2DEBUG(("findmops done: node %p class %d\n", p, sh));
1249
1250 /* Trickery: Set table index on assign to op instead */
1251 /* gencode() will remove useless nodes */
1252 p->n_su = MKIDX(idx, 0);
1253 p->n_su |= ISMOPS; /* XXX tell gencode to reduce the right tree */
1254 SCLASS(p->n_su, sh);
1255
1256 return sh;
1257 }
1258
1259 /*
1260 * Compare two trees; return 1 if equal and 0 if not.
1261 */
1262 int
1263 treecmp(NODE *p1, NODE *p2)
1264 {
1265 if (p1->n_op != p2->n_op)
1266 return 0;
1267
1268 switch (p1->n_op) {
1269 case SCONV:
1270 case UMUL:
1271 return treecmp(p1->n_left, p2->n_left);
1272
1273 case OREG:
1274 if (getlval(p1) != getlval(p2) || p1->n_rval != p2->n_rval)
1275 return 0;
1276 break;
1277
1278 case NAME:
1279 case ICON:
1280 if (strcmp(p1->n_name, p2->n_name))
1281 return 0;
1282 /* FALLTHROUGH */
1283 if (getlval(p1) != getlval(p2))
1284 return 0;
1285 break;
1286
1287 case TEMP:
1288 #ifdef notyet
1289 /* SSA will put assignment in separate register */
1290 /* Help out by accepting different regs here */
1291 if (xssa)
1292 break;
1293 #endif
1294 case REG:
1295 if (p1->n_rval != p2->n_rval)
1296 return 0;
1297 break;
1298 case LS:
1299 case RS:
1300 case PLUS:
1301 case MINUS:
1302 case MUL:
1303 case DIV:
1304 if (treecmp(p1->n_left, p2->n_left) == 0 ||
1305 treecmp(p1->n_right, p2->n_right) == 0)
1306 return 0;
1307 break;
1308
1309 default:
1310 return 0;
1311 }
1312 return 1;
1313 }
1314 #endif