Debian Codegraph @

mip/pass2.h

raw symbols SCIP
1 /* $Id: pass2.h,v 1.144 2017/03/11 09:22:09 ragge Exp $ */
2 /*
3 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * Redistributions of source code and documentation must retain the above
10 * copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditionsand the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed or owned by Caldera
17 * International, Inc.
18 * Neither the name of Caldera International, Inc. nor the names of other
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35 #include <sys/types.h>
36
37 #ifndef MKEXT
38 #include "external.h"
39 #else
40 typedef unsigned int bittype; /* XXX - for basicblock */
41 #define BIT2BYTE(a) (((a) + 31) / 32)
42 #endif
43 #include "manifest.h"
44
45 /* cookies, used as arguments to codgen */
46 #define FOREFF 01 /* compute for effects only */
47 #define INAREG 02 /* compute into a register */
48 #define INBREG 04 /* compute into a register */
49 #define INCREG 010 /* compute into a register */
50 #define INDREG 020 /* compute into a register */
51 #define INREGS (INAREG|INBREG|INCREG|INDREG)
52 #define FORCC 040 /* compute for condition codes only */
53 #define QUIET 0100 /* tell geninsn() to not complain if fail */
54 #define INTEMP 010000 /* compute into a temporary location */
55 #define FORREW 040000 /* search the table for a rewrite rule */
56 #define INEREG 0x10000 /* compute into a register, > 16 bits */
57 #define INFREG 0x20000 /* compute into a register, > 16 bits */
58 #define INGREG 0x40000 /* compute into a register, > 16 bits */
59
60 /*
61 * OP descriptors,
62 * the ASG operator may be used on some of these
63 */
64 #define OPSIMP 010000 /* +, -, &, |, ^ */
65 #define OPCOMM 010002 /* +, &, |, ^ */
66 #define OPMUL 010004 /* *, / */
67 #define OPDIV 010006 /* /, % */
68 #define OPUNARY 010010 /* unary ops */
69 #define OPLEAF 010012 /* leaves */
70 #define OPANY 010014 /* any op... */
71 #define OPLOG 010016 /* logical ops */
72 #define OPFLOAT 010020 /* +, -, *, or / (for floats) */
73 #define OPSHFT 010022 /* <<, >> */
74 #define OPLTYPE 010024 /* leaf type nodes (e.g, NAME, ICON, etc.) */
75
76 /* shapes */
77 #define SANY 01 /* same as FOREFF */
78 #define SAREG 02 /* same as INAREG */
79 #define SBREG 04 /* same as INBREG */
80 #define SCREG 010 /* same as INCREG */
81 #define SDREG 020 /* same as INDREG */
82 #define SCC 040 /* same as FORCC */
83 #define SNAME 0100
84 #define SCON 0200
85 #define SFLD 0400
86 #define SOREG 01000
87 #define STARNM 02000
88 #define STARREG 04000
89 #define SWADD 040000
90 #define SPECIAL 0100000
91 #define SZERO SPECIAL
92 #define SONE (SPECIAL|1)
93 #define SMONE (SPECIAL|2)
94 #define SCCON (SPECIAL|3) /* -256 <= constant < 256 */
95 #define SSCON (SPECIAL|4) /* -32768 <= constant < 32768 */
96 #define SSOREG (SPECIAL|5) /* non-indexed OREG */
97 #define MAXSPECIAL (SPECIAL|5)
98 #define SEREG 0x10000 /* same as INEREG */
99 #define SFREG 0x20000 /* same as INFREG */
100 #define SGREG 0x40000 /* same as INGREG */
101
102 /* These are used in rstatus[] in conjunction with SxREG */
103 #define TEMPREG 01000
104 #define PERMREG 02000
105
106 /* tshape() return values */
107 #define SRNOPE 0 /* Cannot match any shape */
108 #define SRDIR 1 /* Direct match */
109 #define SROREG 2 /* Can convert into OREG */
110 #define SRREG 3 /* Must put into REG */
111
112 /* find*() return values */
113 #define FRETRY -2
114 #define FFAIL -1
115
116 /* INTEMP is carefully not conflicting with shapes */
117
118 /* types */
119 #define TCHAR 01 /* char */
120 #define TSHORT 02 /* short */
121 #define TINT 04 /* int */
122 #define TLONG 010 /* long */
123 #define TFLOAT 020 /* float */
124 #define TDOUBLE 040 /* double */
125 #define TPOINT 0100 /* pointer to something */
126 #define TUCHAR 0200 /* unsigned char */
127 #define TUSHORT 0400 /* unsigned short */
128 #define TUNSIGNED 01000 /* unsigned int */
129 #define TULONG 02000 /* unsigned long */
130 #define TPTRTO 04000 /* pointer to one of the above */
131 #define TANY 010000 /* matches anything within reason */
132 #define TSTRUCT 020000 /* structure or union */
133 #define TLONGLONG 040000 /* long long */
134 #define TULONGLONG 0100000 /* unsigned long long */
135 #define TLDOUBLE 0200000 /* long double; exceeds 16 bit */
136 #define TFTN 0400000 /* function pointer; exceeds 16 bit */
137
138 /* reclamation cookies */
139 #define RNULL 0 /* clobber result */
140 #define RLEFT 01
141 #define RRIGHT 02
142 #define RESC1 04
143 #define RESC2 010
144 #define RESC3 020
145 #define RDEST 040
146 #define RESCC 04000
147 #define RNOP 010000 /* DANGER: can cause loops.. */
148
149 #ifdef NEWNEED
150 enum { cNREG = 1, cNTL, cNTR, cNSL, cNSR, cNL, cNR, cNOL, cNOR,
151 cNEVER, cNRES, cNTEMP, cNREW };
152 #define NEEDS(...) (char []){ __VA_ARGS__, 0 }
153 #define NEEDADD(x) (x == cNREG ? 3 : x < cNSL ? 1 : 2)
154 #define NREG(cl,num) cNREG, CLASS ## cl, num
155 #define NTL cNTL
156 #define NTR cNTR
157 #define NSL(cl) cNSL, CLASS ## cl
158 #define NSR(cl) cNSR, CLASS ## cl
159 #define NLEFT(reg) cNL, reg
160 #define NRIGHT(reg) cNR, reg
161 #define NOLEFT(reg) cNOL, reg
162 #define NORIGHT(reg) cNOR, reg
163 #define NEVER(reg) cNEVER, reg
164 #define NRES(reg) cNRES, reg
165 #define NTEMP(num) cNTEMP, num
166 #define NREWRITE cNREW, 0
167 #else
168 /* needs */
169 #define NASL 0x0001 /* may share left register */
170 #define NASR 0x0002 /* may share right register */
171 #define NAREG 0x0004
172 #define NACOUNT 0x000c
173 #define NBSL 0x0010
174 #define NBSR 0x0020
175 #define NBREG 0x0040
176 #define NBCOUNT 0x00c0
177 #define NCSL 0x0100
178 #define NCSR 0x0200
179 #define NCREG 0x0400
180 #define NCCOUNT 0x0c00
181 #define NTEMP 0x1000
182 #define NTMASK 0x3000
183 #define NSPECIAL 0x4000 /* need special register treatment */
184 #define REWRITE 0x8000
185 #define NDSL 0x00010000 /* Above 16 bit */
186 #define NDSR 0x00020000 /* Above 16 bit */
187 #define NDREG 0x00040000 /* Above 16 bit */
188 #define NDCOUNT 0x000c0000
189 #define NESL 0x00100000 /* Above 16 bit */
190 #define NESR 0x00200000 /* Above 16 bit */
191 #define NEREG 0x00400000 /* Above 16 bit */
192 #define NECOUNT 0x00c00000
193 #define NFSL 0x01000000 /* Above 16 bit */
194 #define NFSR 0x02000000 /* Above 16 bit */
195 #define NFREG 0x04000000 /* Above 16 bit */
196 #define NFCOUNT 0x0c000000
197 #define NGSL 0x10000000 /* Above 16 bit */
198 #define NGSR 0x20000000 /* Above 16 bit */
199 #undef NGREG /* XXX - linux exposes NGREG to public */
200 #define NGREG 0x40000000 /* Above 16 bit */
201 #define NGCOUNT 0xc0000000
202
203 /* special treatment */
204 #define NLEFT (0001) /* left leg register (moveadd) */
205 #define NOLEFT (0002) /* avoid regs for left (addedge) */
206 #define NRIGHT (0004) /* right leg register */
207 #define NORIGHT (0010) /* avoid reg for right */
208 #define NEVER (0020) /* registers trashed (addalledges) */
209 #define NRES (0040) /* result register (moveadd) */
210 #define NMOVTO (0100) /* move between classes */
211 #endif
212
213 #define MUSTDO 010000 /* force register requirements */
214 #define NOPREF 020000 /* no preference for register assignment */
215
216 #define isreg(p) (p->n_op == REG || p->n_op == TEMP)
217
218 extern int fregs;
219
220 /* code tables */
221 extern struct optab {
222 int op;
223 int visit;
224 int lshape;
225 int ltype;
226 int rshape;
227 int rtype;
228 #ifdef NEWNEED
229 char *needs;
230 #else
231 int needs;
232 #endif
233 int rewrite;
234 char *cstring;
235 } table[];
236
237 #ifndef NEWNEED
238 /* Special needs for register allocations */
239 struct rspecial {
240 int op, num;
241 #if 0
242 int left; /* left leg register */
243 int noleft; /* avoid regs for left */
244 int right; /* right leg register */
245 int noright; /* avoid right leg register */
246 int *rmask; /* array of destroyed registers */
247 int res; /* Result ends up here */
248 // void (*rew)(struct optab *, NODE *); /* special rewrite */
249 #endif
250 };
251 #endif
252
253 struct p2env;
254 #define NRESC 4
255 extern NODE resc[];
256 extern int p2autooff, p2maxautooff;
257
258 extern NODE
259 *talloc(void),
260 *eread(void),
261 *mklnode(int, CONSZ, int, TWORD),
262 *mkbinode(int, NODE *, NODE *, TWORD),
263 *mkunode(int, NODE *, int, TWORD),
264 *getlr(NODE *p, int);
265
266 void eoftn(struct interpass_prolog *);
267 void prologue(struct interpass_prolog *);
268 void e2print(NODE *p, int down, int *a, int *b);
269 void myoptim(struct interpass *);
270 void cbgen(int op, int label);
271 int match(NODE *p, int cookie);
272 int acceptable(struct optab *);
273 int special(NODE *, int);
274 int setasg(NODE *, int);
275 int setuni(NODE *, int);
276 int sucomp(NODE *);
277 int nsucomp(NODE *);
278 int setorder(NODE *);
279 int geninsn(NODE *, int cookie);
280 void adrput(FILE *, NODE *);
281 void comperr(char *str, ...);
282 void genregs(NODE *p);
283 void ngenregs(struct p2env *);
284 NODE *store(NODE *);
285 struct interpass *ipnode(NODE *);
286 void deflab(int);
287 void rmove(int, int, TWORD);
288 int rspecial(struct optab *, int);
289 void printip(struct interpass *pole);
290 int findops(NODE *p, int);
291 int findasg(NODE *p, int);
292 int finduni(NODE *p, int);
293 int findumul(NODE *p, int);
294 int findleaf(NODE *p, int);
295 int relops(NODE *p);
296 #ifdef FINDMOPS
297 int findmops(NODE *p, int);
298 int treecmp(NODE *p1, NODE *p2);
299 #endif
300 void offstar(NODE *p, int shape);
301 int gclass(TWORD);
302 void lastcall(NODE *);
303 void myreader(struct interpass *pole);
304 int oregok(NODE *p, int sharp);
305 void myormake(NODE *);
306 int *livecall(NODE *);
307 void prtreg(NODE *);
308 char *prcook(int);
309 int myxasm(struct interpass *ip, NODE *p);
310 int xasmcode(char *s);
311 int freetemp(int k);
312 NODE *storenode(TWORD t, int k);
313 void storemod(NODE *, int k, int reg);
314 int rewfld(NODE *p);
315 void canon(NODE *);
316 void mycanon(NODE *);
317 void oreg2(NODE *p, void *);
318 int shumul(NODE *p, int);
319 NODE *deluseless(NODE *p);
320 int getlab2(void);
321 int tshape(NODE *, int);
322 void conput(FILE *, NODE *);
323 int shtemp(NODE *p);
324 int ttype(TWORD t, int tword);
325 void expand(NODE *, int, char *);
326 void hopcode(int, int);
327 void adrcon(CONSZ);
328 void zzzcode(NODE *, int);
329 void insput(NODE *);
330 void upput(NODE *, int);
331 int tlen(NODE *p);
332 int setbin(NODE *);
333 int notoff(TWORD, int, CONSZ, char *);
334 int fldexpand(NODE *, int, char **);
335 int flshape(NODE *p);
336 #ifdef NEWNEED
337 int ncnt(char *);
338 char *hasneed(char *, int);
339 char *hasneed2(char *, int, int);
340 #else
341 struct rspecial *nspecial(struct optab *q);
342 int ncnt(int needs);
343 #endif
344 void mainp2(void);
345
346 extern char *rnames[];
347
348 extern int classmask(int), tclassmask(int);
349 extern void cmapinit(void);
350 extern int aliasmap(int adjclass, int regnum);
351 extern int regK[];
352 #define CLASSA 1
353 #define CLASSB 2
354 #define CLASSC 3
355 #define CLASSD 4
356 #define CLASSE 5
357 #define CLASSF 6
358 #define CLASSG 7
359
360 /* used when parsing xasm codes */
361 #define XASMVAL(x) ((x) & 0377) /* get val from codeword */
362 #define XASMVAL1(x) (((x) >> 16) & 0377) /* get val from codeword */
363 #define XASMVAL2(x) (((x) >> 24) & 0377) /* get val from codeword */
364 #define XASMASG 0x100 /* = */
365 #define XASMCONSTR 0x200 /* & */
366 #define XASMINOUT 0x400 /* + */
367 #define XASMALL (XASMASG|XASMCONSTR|XASMINOUT)
368 #define XASMISINP(cw) (((cw) & XASMASG) == 0) /* input operand */
369 #define XASMISOUT(cw) ((cw) & (XASMASG|XASMINOUT)) /* output operand */
370
371 /* routines to handle double indirection */
372 #ifdef R2REGS
373 void makeor2(NODE *p, NODE *q, int, int);
374 int base(NODE *);
375 int offset(NODE *p, int);
376 #endif
377
378 extern int lineno;
379 extern int ndebug;
380 extern int b2debug, c2debug, e2debug, f2debug, g2debug, o2debug;
381 extern int r2debug, s2debug, t2debug, u2debug, x2debug;
382
383 extern int dope[]; /* a vector containing operator information */
384 extern char *opst[]; /* a vector containing names for ops */
385
386 #ifdef PCC_DEBUG
387
388 static inline int
389 optype(int o)
390 {
391 if (o >= MAXOP+1)
392 cerror("optype");
393 return (dope[o]&TYFLG);
394 }
395
396 static inline int
397 asgop(int o)
398 {
399 if (o >= MAXOP+1)
400 cerror("asgop");
401 return (dope[o]&ASGFLG);
402 }
403
404 static inline int
405 logop(int o)
406 {
407 if (o >= MAXOP+1)
408 cerror("logop");
409 return (dope[o]&LOGFLG);
410 }
411
412 static inline int
413 callop(int o)
414 {
415 if (o >= MAXOP+1)
416 cerror("callop");
417 return (dope[o]&CALLFLG);
418 }
419
420 #else
421
422 #define optype(o) (dope[o]&TYFLG)
423 #define asgop(o) (dope[o]&ASGFLG)
424 #define logop(o) (dope[o]&LOGFLG)
425 #define callop(o) (dope[o]&CALLFLG)
426
427 #endif
428
429 /* macros for doing double indexing */
430 #define R2PACK(x,y,z) (0200*((x)+1)+y+040000*z)
431 #define R2UPK1(x) ((((x)>>7)-1)&0177)
432 #define R2UPK2(x) ((x)&0177)
433 #define R2UPK3(x) (x>>14)
434 #define R2TEST(x) ((x)>=0200)
435
436 /*
437 * Layout of findops() return value:
438 * bit 0 whether left shall go into a register.
439 * bit 1 whether right shall go into a register.
440 * bit 2 entry is only used for side effects.
441 * bit 3 if condition codes are used.
442 *
443 * These values should be synced with FOREFF/FORCC.
444 */
445 #define ISMOPS 001
446 #define RREG 002
447 #define RVEFF 004
448 #define RVCC 010
449 #define DORIGHT 020
450 #define SCLASS(v,x) ((v) |= ((x) << 5))
451 #define TCLASS(x) (((x) >> 5) & 7)
452 #define TBSH 8
453 #define TBLIDX(idx) ((idx) >> TBSH)
454 #define MKIDX(tbl,mod) (((tbl) << TBSH) | (mod))
455
456 #ifndef BREGS
457 #define BREGS 0
458 #define TBREGS 0
459 #endif
460 #define REGBIT(x) (1 << (x))
461 #ifndef PERMTYPE
462 #define PERMTYPE(a) (INT)
463 #endif
464
465 /* Flags for the dataflow code */
466 /* do the live/dead analysis */
467 #define DO_LIVEDEAD 0x01
468 /* compute avail expressions */
469 #define DO_AVAILEXPR 0x02
470 /* Do an update on the live/dead. One variable only */
471 #define DO_UPDATELD 0x04
472 /* Do an update on available expressions, one variable has changed */
473 #define DO_UPDATEEX 0x08
474
475 void emit(struct interpass *);
476 void optimize(struct p2env *);
477
478 struct basicblock {
479 DLIST_ENTRY(basicblock) bbelem;
480 SLIST_HEAD(, cfgnode) parents; /* CFG - parents to this node */
481 SLIST_HEAD(, cfgnode) child; /* Children, usually max 2 of them */
482 int bbnum; /* this basic block number */
483 unsigned int dfnum; /* DFS-number */
484 unsigned int dfparent; /* Parent in DFS */
485 unsigned int semi;
486 unsigned int ancestor;
487 unsigned int idom;
488 unsigned int samedom;
489 bittype *bucket;
490 bittype *df;
491 bittype *dfchildren;
492 bittype *Aorig;
493 bittype *Aphi;
494 SLIST_HEAD(, phiinfo) phi;
495
496 bittype *gen, *killed, *in, *out; /* Liveness analysis */
497
498 struct interpass *first; /* first element of basic block */
499 struct interpass *last; /* last element of basic block */
500 };
501
502 struct labelinfo {
503 struct basicblock **arr;
504 int size;
505 unsigned int low;
506 };
507
508 struct bblockinfo {
509 int size;
510 struct basicblock **arr;
511 };
512
513 struct varinfo {
514 struct pvarinfo **arr;
515 SLIST_HEAD(, varstack) *stack;
516 int size;
517 int low;
518 };
519
520 struct pvarinfo {
521 struct pvarinfo *next;
522 struct basicblock *bb;
523 TWORD n_type;
524 };
525
526 struct varstack {
527 SLIST_ENTRY(varstack) varstackelem;
528 int tmpregno;
529 };
530
531
532 struct cfgnode {
533 SLIST_ENTRY(cfgnode) cfgelem;
534 SLIST_ENTRY(cfgnode) chld;
535 struct basicblock *bblock;
536 };
537
538 struct phiinfo {
539 SLIST_ENTRY(phiinfo) phielem;
540 int tmpregno;
541 int newtmpregno;
542 TWORD n_type;
543 int size;
544 int *intmpregno;
545 };
546
547 /*
548 * Description of the pass2 environment.
549 * There will be only one of these structs. It is used to keep
550 * all state descriptions during the compilation of a function
551 * in one place.
552 */
553 struct p2env {
554 struct interpass ipole; /* all statements */
555 struct interpass_prolog *ipp, *epp; /* quick references */
556 struct bblockinfo bbinfo;
557 struct labelinfo labinfo;
558 struct basicblock bblocks;
559 int nbblocks;
560 #define NIPPREGS BIT2BYTE(MAXREGS)/sizeof(bittype)
561 bittype p_regs[NIPPREGS]; /* Bitmask of registers to save */
562 };
563
564 extern struct p2env p2env;
565
566 /*
567 * C compiler second pass extra defines.
568 */
569 #define PHI (MAXOP + 1) /* Used in SSA trees */
570
571 enum {
572 ATTR_P2_FIRST = ATTR_MI_MAX + 1,
573 ATTR_P2STRUCT,
574 #ifdef ATTR_P2_TARGET
575 ATTR_P2_TARGET,
576 #endif
577 ATTR_P2_MAX
578 };