Debian Codegraph @

cc/ccom/inline.c

raw symbols SCIP
1 /* $Id: inline.c,v 1.66 2016/09/26 16:45:43 ragge Exp $ */
2 /*
3 * Copyright (c) 2003, 2008 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27
28 #include "pass1.h"
29
30 #include <stdarg.h>
31
32 /*
33 * Simple description of how the inlining works:
34 * A function found with the keyword "inline" is always saved.
35 * If it also has the keyword "extern" it is written out thereafter.
36 * If it has the keyword "static" it will be written out if it is referenced.
37 * inlining will only be done if -xinline is given, and only if it is
38 * possible to inline the function.
39 */
40 static void printip(struct interpass *pole);
41
42 struct ntds {
43 int temp;
44 TWORD type;
45 union dimfun *df;
46 struct attr *attr;
47 };
48
49 /*
50 * ilink from ipole points to the next struct in the list of functions.
51 */
52 static struct istat {
53 SLIST_ENTRY(istat) link;
54 struct symtab *sp;
55 int flags;
56 #define CANINL 1 /* function is possible to inline */
57 #define WRITTEN 2 /* function is written out */
58 #define REFD 4 /* Referenced but not yet written out */
59 struct ntds *nt;/* Array of arg temp type data */
60 int nargs; /* number of args in array */
61 int retval; /* number of return temporary, if any */
62 struct interpass shead;
63 } *cifun;
64
65 static SLIST_HEAD(, istat) ipole = { NULL, &ipole.q_forw };
66 static int nlabs, svclass;
67
68 #define IP_REF (MAXIP+1)
69 #ifdef PCC_DEBUG
70 #define SDEBUG(x) if (sdebug) printf x
71 #else
72 #define SDEBUG(x)
73 #endif
74
75 int isinlining;
76 int inlstatcnt;
77
78 #define SZSI sizeof(struct istat)
79 int istatsz = SZSI;
80 #define ialloc() memset(permalloc(SZSI), 0, SZSI); inlstatcnt++
81
82 /*
83 * Get prolog/epilog for a function.
84 */
85 static struct interpass_prolog *
86 getprol(struct istat *is, int type)
87 {
88 struct interpass *ip;
89
90 DLIST_FOREACH(ip, &is->shead, qelem)
91 if (ip->type == type)
92 return (struct interpass_prolog *)ip;
93 cerror("getprol: %d not found", type);
94 return 0; /* XXX */
95 }
96
97 static struct istat *
98 findfun(struct symtab *sp)
99 {
100 struct istat *is;
101
102 SLIST_FOREACH(is, &ipole, link)
103 if (is->sp == sp)
104 return is;
105 return NULL;
106 }
107
108 static void
109 refnode(struct symtab *sp)
110 {
111 struct interpass *ip;
112
113 SDEBUG(("refnode(%s)\n", sp->sname));
114
115 ip = permalloc(sizeof(*ip));
116 ip->type = IP_REF;
117 ip->ip_name = (char *)sp;
118 inline_addarg(ip);
119 }
120
121 /*
122 * Save attributes permanent.
123 */
124 static struct attr *
125 inapcopy(struct attr *ap)
126 {
127 struct attr *nap = attr_dup(ap);
128
129 if (ap->next)
130 nap->next = inapcopy(ap->next);
131 return nap;
132 }
133
134 /*
135 * Copy a tree onto the permanent heap to save for inline.
136 */
137 static NODE *
138 intcopy(NODE *p)
139 {
140 NODE *q = permalloc(sizeof(NODE));
141 int o = coptype(p->n_op); /* XXX pass2 optype? */
142
143 *q = *p;
144 if (nlabs > 1 && (p->n_op == REG || p->n_op == OREG) &&
145 regno(p) == FPREG)
146 SLIST_FIRST(&ipole)->flags &= ~CANINL; /* no stack refs */
147 if (q->n_ap)
148 q->n_ap = inapcopy(q->n_ap);
149 if (q->n_op == NAME || q->n_op == ICON ||
150 q->n_op == XASM || q->n_op == XARG) {
151 if (*q->n_name)
152 q->n_name = xstrdup(q->n_name); /* XXX permstrdup */
153 else
154 q->n_name = "";
155 }
156 if (o == BITYPE)
157 q->n_right = intcopy(q->n_right);
158 if (o != LTYPE)
159 q->n_left = intcopy(q->n_left);
160 return q;
161 }
162
163 void
164 inline_addarg(struct interpass *ip)
165 {
166 struct interpass_prolog *ipp;
167 NODE *q;
168 static int g = 0;
169 extern P1ND *cftnod;
170
171 SDEBUG(("inline_addarg(%p)\n", ip));
172 DLIST_INSERT_BEFORE(&cifun->shead, ip, qelem);
173 switch (ip->type) {
174 case IP_ASM:
175 ip->ip_asm = xstrdup(ip->ip_asm);
176 break;
177 case IP_DEFLAB:
178 nlabs++;
179 break;
180 case IP_NODE:
181 q = ip->ip_node;
182 ip->ip_node = intcopy(ip->ip_node);
183 tfree(q);
184 break;
185 case IP_EPILOG:
186 ipp = (struct interpass_prolog *)ip;
187 if (ipp->ip_labels[0])
188 uerror("no computed goto in inlined functions");
189 ipp->ip_labels = &g;
190 break;
191 }
192 if (cftnod)
193 cifun->retval = regno(cftnod);
194 }
195
196 /*
197 * Called to setup for inlining of a new function.
198 */
199 void
200 inline_start(struct symtab *sp, int class)
201 {
202 struct istat *is;
203
204 SDEBUG(("inline_start(\"%s\")\n", sp->sname));
205
206 if (isinlining)
207 cerror("already inlining function");
208
209 svclass = class;
210 if ((is = findfun(sp)) != 0) {
211 if (!DLIST_ISEMPTY(&is->shead, qelem))
212 uerror("inline function already defined");
213 } else {
214 is = ialloc();
215 is->sp = sp;
216 SLIST_INSERT_FIRST(&ipole, is, link);
217 DLIST_INIT(&is->shead, qelem);
218 }
219 cifun = is;
220 nlabs = 0;
221 isinlining++;
222 }
223
224 /*
225 * End of an inline function. In C99 an inline function declared "extern"
226 * should also have external linkage and are therefore printed out.
227 *
228 * Gcc inline syntax is a mess, see matrix below on emitting functions:
229 * without extern
230 * -std= - gnu89 gnu99
231 * gcc 3.3.5: ja ja ja
232 * gcc 4.1.3: ja ja ja
233 * gcc 4.3.1 ja ja nej
234 *
235 * with extern
236 * gcc 3.3.5: nej nej nej
237 * gcc 4.1.3: nej nej nej
238 * gcc 4.3.1 nej nej ja
239 *
240 * The above is only true if extern is given on the same line as the
241 * function declaration. If given as a separate definition it do not count.
242 *
243 * The attribute gnu_inline sets gnu89 behaviour.
244 * Since pcc mimics gcc 4.3.1 that is the behaviour we emulate.
245 */
246 void
247 inline_end(void)
248 {
249 struct symtab *sp = cifun->sp;
250
251 SDEBUG(("inline_end()\n"));
252
253 if (sdebug)printip(&cifun->shead);
254 isinlining = 0;
255
256 if (xgnu89 && svclass == SNULL)
257 sp->sclass = EXTERN;
258
259 #ifdef GCC_COMPAT
260 if (sp->sclass != STATIC &&
261 (attr_find(sp->sap, GCC_ATYP_GNU_INLINE) || xgnu89)) {
262 if (sp->sclass == EXTDEF)
263 sp->sclass = EXTERN;
264 else
265 sp->sclass = EXTDEF;
266 }
267 #endif
268
269 if (sp->sclass == EXTDEF) {
270 cifun->flags |= REFD;
271 inline_prtout();
272 }
273 }
274
275 /*
276 * Called when an inline function is found, to be sure that it will
277 * be written out.
278 * The function may not be defined when inline_ref() is called.
279 */
280 void
281 inline_ref(struct symtab *sp)
282 {
283 struct istat *w;
284
285 SDEBUG(("inline_ref(\"%s\")\n", sp->sname));
286 if (sp->sclass == SNULL)
287 return; /* only inline, no references */
288 if (isinlining) {
289 refnode(sp);
290 } else {
291 SLIST_FOREACH(w,&ipole, link) {
292 if (w->sp != sp)
293 continue;
294 w->flags |= REFD;
295 return;
296 }
297 /* function not yet defined, print out when found */
298 w = ialloc();
299 w->sp = sp;
300 w->flags |= REFD;
301 SLIST_INSERT_FIRST(&ipole, w, link);
302 DLIST_INIT(&w->shead, qelem);
303 }
304 }
305
306 static void
307 puto(struct istat *w)
308 {
309 struct interpass_prolog *ipp, *epp, *pp;
310 struct interpass *ip, *nip;
311 extern int crslab;
312 int lbloff = 0;
313
314 /* Copy the saved function and print it out */
315 ipp = 0; /* XXX data flow analysis */
316 DLIST_FOREACH(ip, &w->shead, qelem) {
317 switch (ip->type) {
318 case IP_EPILOG:
319 case IP_PROLOG:
320 if (ip->type == IP_PROLOG) {
321 ipp = (struct interpass_prolog *)ip;
322 /* fix label offsets */
323 lbloff = crslab - ipp->ip_lblnum;
324 } else {
325 epp = (struct interpass_prolog *)ip;
326 crslab += (epp->ip_lblnum - ipp->ip_lblnum);
327 }
328 pp = xmalloc(sizeof(struct interpass_prolog));
329 memcpy(pp, ip, sizeof(struct interpass_prolog));
330 pp->ip_lblnum += lbloff;
331 #ifdef PCC_DEBUG
332 if (ip->type == IP_EPILOG && crslab != pp->ip_lblnum)
333 cerror("puto: %d != %d", crslab, pp->ip_lblnum);
334 #endif
335 pass2_compile((struct interpass *)pp);
336 break;
337
338 case IP_REF:
339 inline_ref((struct symtab *)ip->ip_name);
340 break;
341
342 default:
343 nip = xmalloc(sizeof(struct interpass));
344 *nip = *ip;
345 if (nip->type == IP_NODE) {
346 NODE *p;
347
348 p = nip->ip_node = tcopy(nip->ip_node);
349 if (p->n_op == GOTO)
350 slval(p->n_left,
351 glval(p->n_left) + lbloff);
352 else if (p->n_op == CBRANCH)
353 slval(p->n_right,
354 glval(p->n_right) + lbloff);
355 } else if (nip->type == IP_DEFLAB)
356 nip->ip_lbl += lbloff;
357 pass2_compile(nip);
358 break;
359 }
360 }
361 w->flags |= WRITTEN;
362 }
363
364 /*
365 * printout functions that are referenced.
366 */
367 void
368 inline_prtout(void)
369 {
370 struct istat *w;
371 int gotone = 0;
372
373 SLIST_FOREACH(w, &ipole, link) {
374 if ((w->flags & (REFD|WRITTEN)) == REFD &&
375 !DLIST_ISEMPTY(&w->shead, qelem)) {
376 locctr(PROG, w->sp);
377 defloc(w->sp);
378 puto(w);
379 w->flags |= WRITTEN;
380 gotone++;
381 }
382 }
383 if (gotone)
384 inline_prtout();
385 }
386
387 #if 1
388 static void
389 printip(struct interpass *pole)
390 {
391 static char *foo[] = {
392 0, "NODE", "PROLOG", "STKOFF", "EPILOG", "DEFLAB", "DEFNAM", "ASM" };
393 struct interpass *ip;
394 struct interpass_prolog *ipplg, *epplg;
395
396 DLIST_FOREACH(ip, pole, qelem) {
397 if (ip->type > MAXIP)
398 printf("IP(%d) (%p): ", ip->type, ip);
399 else
400 printf("%s (%p): ", foo[ip->type], ip);
401 switch (ip->type) {
402 case IP_NODE: printf("\n");
403 #ifdef PCC_DEBUG
404 #ifndef TWOPASS
405 { extern void e2print(NODE *p, int down, int *a, int *b);
406 fwalk(ip->ip_node, e2print, 0); break; }
407 #endif
408 #endif
409 case IP_PROLOG:
410 ipplg = (struct interpass_prolog *)ip;
411 printf("%s %s autos %d mintemp %d minlbl %d\n",
412 ipplg->ipp_name, ipplg->ipp_vis ? "(local)" : "",
413 ipplg->ipp_autos,
414 ipplg->ip_tmpnum, ipplg->ip_lblnum);
415 break;
416 case IP_EPILOG:
417 epplg = (struct interpass_prolog *)ip;
418 printf("%s %s autos %d mintemp %d minlbl %d\n",
419 epplg->ipp_name, epplg->ipp_vis ? "(local)" : "",
420 epplg->ipp_autos,
421 epplg->ip_tmpnum, epplg->ip_lblnum);
422 break;
423 case IP_DEFLAB: printf(LABFMT "\n", ip->ip_lbl); break;
424 case IP_DEFNAM: printf("\n"); break;
425 case IP_ASM: printf("%s", ip->ip_asm); break;
426 default:
427 break;
428 }
429 }
430 }
431 #endif
432
433 static int toff;
434
435 static P1ND *
436 mnode(struct ntds *nt, P1ND *p)
437 {
438 P1ND *q;
439 int num = nt->temp + toff;
440
441 if (p->n_op == CM) {
442 q = p->n_right;
443 q = tempnode(num, nt->type, nt->df, nt->attr);
444 nt--;
445 p->n_right = buildtree(ASSIGN, q, p->n_right);
446 p->n_left = mnode(nt, p->n_left);
447 p->n_op = COMOP;
448 } else {
449 p = pconvert(p);
450 q = tempnode(num, nt->type, nt->df, nt->attr);
451 p = buildtree(ASSIGN, q, p);
452 }
453 return p;
454 }
455
456 static void
457 rtmps(NODE *p, void *arg)
458 {
459 if (p->n_op == TEMP)
460 regno(p) += toff;
461 }
462
463 /*
464 * Inline a function. Returns the return value.
465 * There are two major things that must be converted when
466 * inlining a function:
467 * - Label numbers must be updated with an offset.
468 * - The stack block must be relocated (add to REG or OREG).
469 * - Temporaries should be updated (but no must)
470 *
471 * Extra tricky: The call is P1ND, nut the resulting tree is already NODE...
472 */
473 P1ND *
474 inlinetree(struct symtab *sp, P1ND *f, P1ND *ap)
475 {
476 extern int crslab, tvaloff;
477 struct istat *is = findfun(sp);
478 struct interpass *ip, *ipf, *ipl;
479 struct interpass_prolog *ipp, *ipe;
480 int lmin, l0, l1, l2, gainl, n;
481 NODE *pp;
482 P1ND *p, *rp;
483
484 if (is == NULL || nerrors) {
485 inline_ref(sp); /* prototype of not yet declared inline ftn */
486 return NULL;
487 }
488
489 SDEBUG(("inlinetree(%p,%p) OK %d\n", f, ap, is->flags & CANINL));
490
491 #ifdef GCC_COMPAT
492 gainl = attr_find(sp->sap, GCC_ATYP_ALW_INL) != NULL;
493 #else
494 gainl = 0;
495 #endif
496
497 n = nerrors;
498 if ((is->flags & CANINL) == 0 && gainl)
499 werror("cannot inline but always_inline");
500 nerrors = n;
501
502 if ((is->flags & CANINL) == 0 || (xinline == 0 && gainl == 0)) {
503 if (is->sp->sclass == STATIC || is->sp->sclass == USTATIC)
504 inline_ref(sp);
505 return NULL;
506 }
507
508 if (isinlining && cifun->sp == sp) {
509 /* Do not try to inline ourselves */
510 inline_ref(sp);
511 return NULL;
512 }
513
514 #ifdef mach_i386
515 if (kflag) {
516 is->flags |= REFD; /* if static inline, emit */
517 return NULL; /* XXX cannot handle hidden ebx arg */
518 }
519 #endif
520
521 /* emit jumps to surround inline function */
522 branch(l0 = getlab());
523 plabel(l1 = getlab());
524 l2 = getlab();
525 SDEBUG(("branch labels %d,%d,%d\n", l0, l1, l2));
526
527 /* From here it is NODE */
528
529 ipp = getprol(is, IP_PROLOG);
530 ipe = getprol(is, IP_EPILOG);
531
532 /* Fix label & temp offsets */
533
534 SDEBUG(("pre-offsets crslab %d tvaloff %d\n", crslab, tvaloff));
535 lmin = crslab - ipp->ip_lblnum;
536 crslab += (ipe->ip_lblnum - ipp->ip_lblnum) + 2;
537 toff = tvaloff - ipp->ip_tmpnum;
538 tvaloff += (ipe->ip_tmpnum - ipp->ip_tmpnum) + 1;
539 SDEBUG(("offsets crslab %d lmin %d tvaloff %d toff %d\n",
540 crslab, lmin, tvaloff, toff));
541
542 /* traverse until first real label */
543 n = 0;
544 DLIST_FOREACH(ipf, &is->shead, qelem) {
545 if (ipf->type == IP_REF)
546 inline_ref((struct symtab *)ipf->ip_name);
547 if (ipf->type == IP_DEFLAB && n++ == 1)
548 break;
549 }
550
551 /* traverse backwards to last label */
552 DLIST_FOREACH_REVERSE(ipl, &is->shead, qelem) {
553 if (ipl->type == IP_REF)
554 inline_ref((struct symtab *)ipl->ip_name);
555 if (ipl->type == IP_DEFLAB)
556 break;
557 }
558
559 /* So, walk over all statements and emit them */
560 for (ip = ipf; ip != ipl; ip = DLIST_NEXT(ip, qelem)) {
561 switch (ip->type) {
562 case IP_NODE:
563 pp = tcopy(ip->ip_node);
564 if (pp->n_op == GOTO)
565 slval(pp->n_left, glval(pp->n_left) + lmin);
566 else if (pp->n_op == CBRANCH)
567 slval(pp->n_right, glval(pp->n_right) + lmin);
568 walkf(pp, rtmps, 0);
569 #ifdef PCC_DEBUG
570 #ifndef TWOPASS
571 if (sdebug) {
572 extern void e2print(NODE *p, int down, int *a, int *b);
573 printf("converted node\n");
574 fwalk(ip->ip_node, e2print, 0);
575 fwalk(pp, e2print, 0);
576 }
577 #endif
578 #endif
579 send_passt(IP_NODE, pp);
580 break;
581
582 case IP_DEFLAB:
583 SDEBUG(("converted label %d to %d\n",
584 ip->ip_lbl, ip->ip_lbl + lmin));
585 send_passt(IP_DEFLAB, ip->ip_lbl + lmin);
586 break;
587
588 case IP_ASM:
589 send_passt(IP_ASM, ip->ip_asm);
590 break;
591
592 case IP_REF:
593 inline_ref((struct symtab *)ip->ip_name);
594 break;
595
596 default:
597 cerror("bad inline stmt %d", ip->type);
598 }
599 }
600 SDEBUG(("last label %d to %d\n", ip->ip_lbl, ip->ip_lbl + lmin));
601 send_passt(IP_DEFLAB, ip->ip_lbl + lmin);
602
603 branch(l2);
604 plabel(l0);
605
606 /* Here we are P1ND again */
607 rp = block(GOTO, bcon(l1), NULL, INT, 0, 0);
608 if (is->retval)
609 p = tempnode(is->retval + toff, DECREF(sp->stype),
610 sp->sdf, sp->sap);
611 else
612 p = xbcon(0, NULL, DECREF(sp->stype));
613 rp = buildtree(COMOP, rp, p);
614
615 if (is->nargs) {
616 p = mnode(&is->nt[is->nargs-1], ap);
617 rp = buildtree(COMOP, p, rp);
618 }
619
620 p1tfree(f);
621 return rp;
622 }
623
624 void
625 inline_args(struct symtab **sp, int nargs)
626 {
627 union arglist *al;
628 struct istat *cf;
629 TWORD t;
630 int i;
631
632 SDEBUG(("inline_args\n"));
633 cf = cifun;
634 /*
635 * First handle arguments. We currently do not inline anything if:
636 * - function has varargs
637 * - function args are volatile, checked if no temp node is asg'd.
638 */
639 /* XXX - this is ugly, invent something better */
640 if (cf->sp->sdf->dfun == NULL)
641 return; /* no prototype */
642 for (al = cf->sp->sdf->dfun; al->type != TNULL; al++) {
643 t = al->type;
644 if (t == TELLIPSIS)
645 return; /* cannot inline */
646 if (ISSOU(BTYPE(t)))
647 al++;
648 for (; t > BTMASK; t = DECREF(t))
649 if (ISARY(t) || ISFTN(t))
650 al++;
651 }
652
653 if (nargs) {
654 for (i = 0; i < nargs; i++)
655 if ((sp[i]->sflags & STNODE) == 0)
656 return; /* not temporary */
657 cf->nt = permalloc(sizeof(struct ntds)*nargs);
658 for (i = 0; i < nargs; i++) {
659 cf->nt[i].temp = sp[i]->soffset;
660 cf->nt[i].type = sp[i]->stype;
661 cf->nt[i].df = sp[i]->sdf;
662 cf->nt[i].attr = sp[i]->sap;
663 }
664 }
665 cf->nargs = nargs;
666 cf->flags |= CANINL;
667 }