comparison dmd/inline.c @ 1:c53b6e3fe49a trunk

[svn r5] Initial commit. Most things are very rough.
author lindquist
date Sat, 01 Sep 2007 21:43:27 +0200
parents
children 70d6113eeb8c
comparison
equal deleted inserted replaced
0:a9e71648e74d 1:c53b6e3fe49a
1
2 // Copyright (c) 1999-2007 by Digital Mars
3 // All Rights Reserved
4 // written by Walter Bright
5 // http://www.digitalmars.com
6 // License for redistribution is by either the Artistic License
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
8 // See the included readme.txt for details.
9
10 // Routines to perform function inlining
11
12 #define LOG 0
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <assert.h>
17
18 #include "id.h"
19 #include "init.h"
20 #include "declaration.h"
21 #include "aggregate.h"
22 #include "expression.h"
23 #include "statement.h"
24 #include "mtype.h"
25
26 /* ========== Compute cost of inlining =============== */
27
28 /* Walk trees to determine if inlining can be done, and if so,
29 * if it is too complex to be worth inlining or not.
30 */
31
32 struct InlineCostState
33 {
34 int nested;
35 int hasthis;
36 int hdrscan; // !=0 if inline scan for 'header' content
37 FuncDeclaration *fd;
38 };
39
40 const int COST_MAX = 250;
41
42 int Statement::inlineCost(InlineCostState *ics)
43 {
44 return COST_MAX; // default is we can't inline it
45 }
46
47 int ExpStatement::inlineCost(InlineCostState *ics)
48 {
49 return exp ? exp->inlineCost(ics) : 0;
50 }
51
52 int CompoundStatement::inlineCost(InlineCostState *ics)
53 { int cost = 0;
54
55 for (size_t i = 0; i < statements->dim; i++)
56 { Statement *s = (Statement *) statements->data[i];
57 if (s)
58 {
59 cost += s->inlineCost(ics);
60 if (cost >= COST_MAX)
61 break;
62 }
63 }
64 return cost;
65 }
66
67 int UnrolledLoopStatement::inlineCost(InlineCostState *ics)
68 { int cost = 0;
69
70 for (size_t i = 0; i < statements->dim; i++)
71 { Statement *s = (Statement *) statements->data[i];
72 if (s)
73 {
74 cost += s->inlineCost(ics);
75 if (cost >= COST_MAX)
76 break;
77 }
78 }
79 return cost;
80 }
81
82 int IfStatement::inlineCost(InlineCostState *ics)
83 {
84 int cost;
85
86 /* Can't declare variables inside ?: expressions, so
87 * we cannot inline if a variable is declared.
88 */
89 if (arg)
90 return COST_MAX;
91
92 cost = condition->inlineCost(ics);
93
94 /* Specifically allow:
95 * if (condition)
96 * return exp1;
97 * else
98 * return exp2;
99 * Otherwise, we can't handle return statements nested in if's.
100 */
101
102 if (elsebody && ifbody &&
103 ifbody->isReturnStatement() &&
104 elsebody->isReturnStatement())
105 {
106 cost += ifbody->inlineCost(ics);
107 cost += elsebody->inlineCost(ics);
108 //printf("cost = %d\n", cost);
109 }
110 else
111 {
112 ics->nested += 1;
113 if (ifbody)
114 cost += ifbody->inlineCost(ics);
115 if (elsebody)
116 cost += elsebody->inlineCost(ics);
117 ics->nested -= 1;
118 }
119 return cost;
120 }
121
122 int ReturnStatement::inlineCost(InlineCostState *ics)
123 {
124 // Can't handle return statements nested in if's
125 if (ics->nested)
126 return COST_MAX;
127 return exp ? exp->inlineCost(ics) : 0;
128 }
129
130 /* -------------------------- */
131
132 int arrayInlineCost(InlineCostState *ics, Array *arguments)
133 { int cost = 0;
134
135 if (arguments)
136 {
137 for (int i = 0; i < arguments->dim; i++)
138 { Expression *e = (Expression *)arguments->data[i];
139
140 if (e)
141 cost += e->inlineCost(ics);
142 }
143 }
144 return cost;
145 }
146
147 int Expression::inlineCost(InlineCostState *ics)
148 {
149 return 1;
150 }
151
152 int VarExp::inlineCost(InlineCostState *ics)
153 {
154 //printf("VarExp::inlineCost() %s\n", toChars());
155 return 1;
156 }
157
158 int ThisExp::inlineCost(InlineCostState *ics)
159 {
160 FuncDeclaration *fd = ics->fd;
161 if (!ics->hdrscan)
162 if (fd->isNested() || !ics->hasthis)
163 return COST_MAX;
164 return 1;
165 }
166
167 int SuperExp::inlineCost(InlineCostState *ics)
168 {
169 FuncDeclaration *fd = ics->fd;
170 if (!ics->hdrscan)
171 if (fd->isNested() || !ics->hasthis)
172 return COST_MAX;
173 return 1;
174 }
175
176 int TupleExp::inlineCost(InlineCostState *ics)
177 {
178 return 1 + arrayInlineCost(ics, exps);
179 }
180
181 int ArrayLiteralExp::inlineCost(InlineCostState *ics)
182 {
183 return 1 + arrayInlineCost(ics, elements);
184 }
185
186 int AssocArrayLiteralExp::inlineCost(InlineCostState *ics)
187 {
188 return 1 + arrayInlineCost(ics, keys) + arrayInlineCost(ics, values);
189 }
190
191 int StructLiteralExp::inlineCost(InlineCostState *ics)
192 {
193 return 1 + arrayInlineCost(ics, elements);
194 }
195
196 int FuncExp::inlineCost(InlineCostState *ics)
197 {
198 // Right now, this makes the function be output to the .obj file twice.
199 return COST_MAX;
200 }
201
202 int DelegateExp::inlineCost(InlineCostState *ics)
203 {
204 return COST_MAX;
205 }
206
207 int DeclarationExp::inlineCost(InlineCostState *ics)
208 { int cost = 0;
209 VarDeclaration *vd;
210
211 //printf("DeclarationExp::inlineCost()\n");
212 vd = declaration->isVarDeclaration();
213 if (vd)
214 {
215 TupleDeclaration *td = vd->toAlias()->isTupleDeclaration();
216 if (td)
217 {
218 #if 1
219 return COST_MAX; // finish DeclarationExp::doInline
220 #else
221 for (size_t i = 0; i < td->objects->dim; i++)
222 { Object *o = (Object *)td->objects->data[i];
223 if (o->dyncast() != DYNCAST_EXPRESSION)
224 return COST_MAX;
225 Expression *eo = (Expression *)o;
226 if (eo->op != TOKdsymbol)
227 return COST_MAX;
228 }
229 return td->objects->dim;
230 #endif
231 }
232 if (!ics->hdrscan && vd->isDataseg())
233 return COST_MAX;
234 cost += 1;
235
236 // Scan initializer (vd->init)
237 if (vd->init)
238 {
239 ExpInitializer *ie = vd->init->isExpInitializer();
240
241 if (ie)
242 {
243 cost += ie->exp->inlineCost(ics);
244 }
245 }
246 }
247
248 // These can contain functions, which when copied, get output twice.
249 if (declaration->isStructDeclaration() ||
250 declaration->isClassDeclaration() ||
251 declaration->isFuncDeclaration() ||
252 declaration->isTypedefDeclaration() ||
253 declaration->isTemplateMixin())
254 return COST_MAX;
255
256 //printf("DeclarationExp::inlineCost('%s')\n", toChars());
257 return cost;
258 }
259
260 int UnaExp::inlineCost(InlineCostState *ics)
261 {
262 return 1 + e1->inlineCost(ics);
263 }
264
265 int AssertExp::inlineCost(InlineCostState *ics)
266 {
267 return 1 + e1->inlineCost(ics) + (msg ? msg->inlineCost(ics) : 0);
268 }
269
270 int BinExp::inlineCost(InlineCostState *ics)
271 {
272 return 1 + e1->inlineCost(ics) + e2->inlineCost(ics);
273 }
274
275 int CallExp::inlineCost(InlineCostState *ics)
276 {
277 return 1 + e1->inlineCost(ics) + arrayInlineCost(ics, arguments);
278 }
279
280 int SliceExp::inlineCost(InlineCostState *ics)
281 { int cost;
282
283 cost = 1 + e1->inlineCost(ics);
284 if (lwr)
285 cost += lwr->inlineCost(ics);
286 if (upr)
287 cost += upr->inlineCost(ics);
288 return cost;
289 }
290
291 int ArrayExp::inlineCost(InlineCostState *ics)
292 {
293 return 1 + e1->inlineCost(ics) + arrayInlineCost(ics, arguments);
294 }
295
296
297 int CondExp::inlineCost(InlineCostState *ics)
298 {
299 return 1 +
300 e1->inlineCost(ics) +
301 e2->inlineCost(ics) +
302 econd->inlineCost(ics);
303 }
304
305
306 /* ======================== Perform the inlining ============================== */
307
308 /* Inlining is done by:
309 * o Converting to an Expression
310 * o Copying the trees of the function to be inlined
311 * o Renaming the variables
312 */
313
314 struct InlineDoState
315 {
316 VarDeclaration *vthis;
317 Array from; // old Dsymbols
318 Array to; // parallel array of new Dsymbols
319 Dsymbol *parent; // new parent
320 };
321
322 Expression *Statement::doInline(InlineDoState *ids)
323 {
324 assert(0);
325 return NULL; // default is we can't inline it
326 }
327
328 Expression *ExpStatement::doInline(InlineDoState *ids)
329 {
330 #if LOG
331 if (exp) printf("ExpStatement::doInline() '%s'\n", exp->toChars());
332 #endif
333 return exp ? exp->doInline(ids) : NULL;
334 }
335
336 Expression *CompoundStatement::doInline(InlineDoState *ids)
337 {
338 Expression *e = NULL;
339
340 //printf("CompoundStatement::doInline() %d\n", statements->dim);
341 for (size_t i = 0; i < statements->dim; i++)
342 { Statement *s = (Statement *) statements->data[i];
343 if (s)
344 {
345 Expression *e2 = s->doInline(ids);
346 e = Expression::combine(e, e2);
347 if (s->isReturnStatement())
348 break;
349
350 /* Check for:
351 * if (condition)
352 * return exp1;
353 * else
354 * return exp2;
355 */
356 IfStatement *ifs = s->isIfStatement();
357 if (ifs && ifs->elsebody && ifs->ifbody &&
358 ifs->ifbody->isReturnStatement() &&
359 ifs->elsebody->isReturnStatement()
360 )
361 break;
362
363 }
364 }
365 return e;
366 }
367
368 Expression *UnrolledLoopStatement::doInline(InlineDoState *ids)
369 {
370 Expression *e = NULL;
371
372 //printf("UnrolledLoopStatement::doInline() %d\n", statements->dim);
373 for (size_t i = 0; i < statements->dim; i++)
374 { Statement *s = (Statement *) statements->data[i];
375 if (s)
376 {
377 Expression *e2 = s->doInline(ids);
378 e = Expression::combine(e, e2);
379 if (s->isReturnStatement())
380 break;
381 }
382 }
383 return e;
384 }
385
386 Expression *IfStatement::doInline(InlineDoState *ids)
387 {
388 Expression *econd;
389 Expression *e1;
390 Expression *e2;
391 Expression *e;
392
393 assert(!arg);
394 econd = condition->doInline(ids);
395 assert(econd);
396 if (ifbody)
397 e1 = ifbody->doInline(ids);
398 else
399 e1 = NULL;
400 if (elsebody)
401 e2 = elsebody->doInline(ids);
402 else
403 e2 = NULL;
404 if (e1 && e2)
405 {
406 e = new CondExp(econd->loc, econd, e1, e2);
407 e->type = e1->type;
408 }
409 else if (e1)
410 {
411 e = new AndAndExp(econd->loc, econd, e1);
412 e->type = Type::tvoid;
413 }
414 else if (e2)
415 {
416 e = new OrOrExp(econd->loc, econd, e2);
417 e->type = Type::tvoid;
418 }
419 else
420 {
421 e = econd;
422 }
423 return e;
424 }
425
426 Expression *ReturnStatement::doInline(InlineDoState *ids)
427 {
428 //printf("ReturnStatement::doInline() '%s'\n", exp ? exp->toChars() : "");
429 return exp ? exp->doInline(ids) : 0;
430 }
431
432 /* --------------------------------------------------------------- */
433
434 /******************************
435 * Perform doInline() on an array of Expressions.
436 */
437
438 Expressions *arrayExpressiondoInline(Expressions *a, InlineDoState *ids)
439 { Expressions *newa = NULL;
440
441 if (a)
442 {
443 newa = new Expressions();
444 newa->setDim(a->dim);
445
446 for (int i = 0; i < a->dim; i++)
447 { Expression *e = (Expression *)a->data[i];
448
449 if (e)
450 {
451 e = e->doInline(ids);
452 newa->data[i] = (void *)e;
453 }
454 }
455 }
456 return newa;
457 }
458
459 Expression *Expression::doInline(InlineDoState *ids)
460 {
461 //printf("Expression::doInline(%s): %s\n", Token::toChars(op), toChars());
462 return copy();
463 }
464
465 Expression *SymOffExp::doInline(InlineDoState *ids)
466 {
467 int i;
468
469 //printf("SymOffExp::doInline(%s)\n", toChars());
470 for (i = 0; i < ids->from.dim; i++)
471 {
472 if (var == (Declaration *)ids->from.data[i])
473 {
474 SymOffExp *se = (SymOffExp *)copy();
475
476 se->var = (Declaration *)ids->to.data[i];
477 return se;
478 }
479 }
480 return this;
481 }
482
483 Expression *VarExp::doInline(InlineDoState *ids)
484 {
485 int i;
486
487 //printf("VarExp::doInline(%s)\n", toChars());
488 for (i = 0; i < ids->from.dim; i++)
489 {
490 if (var == (Declaration *)ids->from.data[i])
491 {
492 VarExp *ve = (VarExp *)copy();
493
494 ve->var = (Declaration *)ids->to.data[i];
495 return ve;
496 }
497 }
498 return this;
499 }
500
501 Expression *ThisExp::doInline(InlineDoState *ids)
502 {
503 //if (!ids->vthis)
504 //error("no 'this' when inlining %s", ids->parent->toChars());
505 if (!ids->vthis)
506 {
507 return this;
508 }
509
510 VarExp *ve = new VarExp(loc, ids->vthis);
511 ve->type = type;
512 return ve;
513 }
514
515 Expression *SuperExp::doInline(InlineDoState *ids)
516 {
517 assert(ids->vthis);
518
519 VarExp *ve = new VarExp(loc, ids->vthis);
520 ve->type = type;
521 return ve;
522 }
523
524 Expression *DeclarationExp::doInline(InlineDoState *ids)
525 { DeclarationExp *de = (DeclarationExp *)copy();
526 VarDeclaration *vd;
527
528 //printf("DeclarationExp::doInline(%s)\n", toChars());
529 vd = declaration->isVarDeclaration();
530 if (vd)
531 {
532 #if 0
533 // Need to figure this out before inlining can work for tuples
534 TupleDeclaration *td = vd->toAlias()->isTupleDeclaration();
535 if (td)
536 {
537 for (size_t i = 0; i < td->objects->dim; i++)
538 { DsymbolExp *se = (DsymbolExp *)td->objects->data[i];
539 assert(se->op == TOKdsymbol);
540 se->s;
541 }
542 return st->objects->dim;
543 }
544 #endif
545 if (vd->isStatic() || vd->isConst())
546 ;
547 else
548 {
549 ExpInitializer *ie;
550 ExpInitializer *ieto;
551 VarDeclaration *vto;
552
553 vto = new VarDeclaration(vd->loc, vd->type, vd->ident, vd->init);
554 *vto = *vd;
555 vto->parent = ids->parent;
556 vto->csym = NULL;
557 vto->isym = NULL;
558
559 ids->from.push(vd);
560 ids->to.push(vto);
561
562 if (vd->init->isVoidInitializer())
563 {
564 vto->init = new VoidInitializer(vd->init->loc);
565 }
566 else
567 {
568 ie = vd->init->isExpInitializer();
569 assert(ie);
570 ieto = new ExpInitializer(ie->loc, ie->exp->doInline(ids));
571 vto->init = ieto;
572 }
573 de->declaration = (Dsymbol *) (void *)vto;
574 }
575 }
576 /* This needs work, like DeclarationExp::toElem(), if we are
577 * to handle TemplateMixin's. For now, we just don't inline them.
578 */
579 return de;
580 }
581
582 Expression *NewExp::doInline(InlineDoState *ids)
583 {
584 //printf("NewExp::doInline(): %s\n", toChars());
585 NewExp *ne = (NewExp *)copy();
586
587 if (thisexp)
588 ne->thisexp = thisexp->doInline(ids);
589 ne->newargs = arrayExpressiondoInline(ne->newargs, ids);
590 ne->arguments = arrayExpressiondoInline(ne->arguments, ids);
591 return ne;
592 }
593
594 Expression *UnaExp::doInline(InlineDoState *ids)
595 {
596 UnaExp *ue = (UnaExp *)copy();
597
598 ue->e1 = e1->doInline(ids);
599 return ue;
600 }
601
602 Expression *AssertExp::doInline(InlineDoState *ids)
603 {
604 AssertExp *ae = (AssertExp *)copy();
605
606 ae->e1 = e1->doInline(ids);
607 if (msg)
608 ae->msg = msg->doInline(ids);
609 return ae;
610 }
611
612 Expression *BinExp::doInline(InlineDoState *ids)
613 {
614 BinExp *be = (BinExp *)copy();
615
616 be->e1 = e1->doInline(ids);
617 be->e2 = e2->doInline(ids);
618 return be;
619 }
620
621 Expression *CallExp::doInline(InlineDoState *ids)
622 {
623 CallExp *ce;
624
625 ce = (CallExp *)copy();
626 ce->e1 = e1->doInline(ids);
627 ce->arguments = arrayExpressiondoInline(arguments, ids);
628 return ce;
629 }
630
631
632 Expression *IndexExp::doInline(InlineDoState *ids)
633 {
634 IndexExp *are = (IndexExp *)copy();
635
636 are->e1 = e1->doInline(ids);
637
638 if (lengthVar)
639 { //printf("lengthVar\n");
640 VarDeclaration *vd = lengthVar;
641 ExpInitializer *ie;
642 ExpInitializer *ieto;
643 VarDeclaration *vto;
644
645 vto = new VarDeclaration(vd->loc, vd->type, vd->ident, vd->init);
646 *vto = *vd;
647 vto->parent = ids->parent;
648 vto->csym = NULL;
649 vto->isym = NULL;
650
651 ids->from.push(vd);
652 ids->to.push(vto);
653
654 if (vd->init)
655 {
656 ie = vd->init->isExpInitializer();
657 assert(ie);
658 ieto = new ExpInitializer(ie->loc, ie->exp->doInline(ids));
659 vto->init = ieto;
660 }
661
662 are->lengthVar = (VarDeclaration *) (void *)vto;
663 }
664 are->e2 = e2->doInline(ids);
665 return are;
666 }
667
668
669 Expression *SliceExp::doInline(InlineDoState *ids)
670 {
671 SliceExp *are = (SliceExp *)copy();
672
673 are->e1 = e1->doInline(ids);
674
675 if (lengthVar)
676 { //printf("lengthVar\n");
677 VarDeclaration *vd = lengthVar;
678 ExpInitializer *ie;
679 ExpInitializer *ieto;
680 VarDeclaration *vto;
681
682 vto = new VarDeclaration(vd->loc, vd->type, vd->ident, vd->init);
683 *vto = *vd;
684 vto->parent = ids->parent;
685 vto->csym = NULL;
686 vto->isym = NULL;
687
688 ids->from.push(vd);
689 ids->to.push(vto);
690
691 if (vd->init)
692 {
693 ie = vd->init->isExpInitializer();
694 assert(ie);
695 ieto = new ExpInitializer(ie->loc, ie->exp->doInline(ids));
696 vto->init = ieto;
697 }
698
699 are->lengthVar = (VarDeclaration *) (void *)vto;
700 }
701 if (lwr)
702 are->lwr = lwr->doInline(ids);
703 if (upr)
704 are->upr = upr->doInline(ids);
705 return are;
706 }
707
708
709 Expression *TupleExp::doInline(InlineDoState *ids)
710 {
711 TupleExp *ce;
712
713 ce = (TupleExp *)copy();
714 ce->exps = arrayExpressiondoInline(exps, ids);
715 return ce;
716 }
717
718
719 Expression *ArrayLiteralExp::doInline(InlineDoState *ids)
720 {
721 ArrayLiteralExp *ce;
722
723 ce = (ArrayLiteralExp *)copy();
724 ce->elements = arrayExpressiondoInline(elements, ids);
725 return ce;
726 }
727
728
729 Expression *AssocArrayLiteralExp::doInline(InlineDoState *ids)
730 {
731 AssocArrayLiteralExp *ce;
732
733 ce = (AssocArrayLiteralExp *)copy();
734 ce->keys = arrayExpressiondoInline(keys, ids);
735 ce->values = arrayExpressiondoInline(values, ids);
736 return ce;
737 }
738
739
740 Expression *StructLiteralExp::doInline(InlineDoState *ids)
741 {
742 StructLiteralExp *ce;
743
744 ce = (StructLiteralExp *)copy();
745 ce->elements = arrayExpressiondoInline(elements, ids);
746 return ce;
747 }
748
749
750 Expression *ArrayExp::doInline(InlineDoState *ids)
751 {
752 ArrayExp *ce;
753
754 ce = (ArrayExp *)copy();
755 ce->e1 = e1->doInline(ids);
756 ce->arguments = arrayExpressiondoInline(arguments, ids);
757 return ce;
758 }
759
760
761 Expression *CondExp::doInline(InlineDoState *ids)
762 {
763 CondExp *ce = (CondExp *)copy();
764
765 ce->econd = econd->doInline(ids);
766 ce->e1 = e1->doInline(ids);
767 ce->e2 = e2->doInline(ids);
768 return ce;
769 }
770
771
772 /* ========== Walk the parse trees, and inline expand functions ============= */
773
774 /* Walk the trees, looking for functions to inline.
775 * Inline any that can be.
776 */
777
778 struct InlineScanState
779 {
780 FuncDeclaration *fd; // function being scanned
781 };
782
783 Statement *Statement::inlineScan(InlineScanState *iss)
784 {
785 return this;
786 }
787
788 Statement *ExpStatement::inlineScan(InlineScanState *iss)
789 {
790 #if LOG
791 printf("ExpStatement::inlineScan(%s)\n", toChars());
792 #endif
793 if (exp)
794 exp = exp->inlineScan(iss);
795 return this;
796 }
797
798 Statement *CompoundStatement::inlineScan(InlineScanState *iss)
799 {
800 for (size_t i = 0; i < statements->dim; i++)
801 { Statement *s = (Statement *) statements->data[i];
802 if (s)
803 statements->data[i] = (void *)s->inlineScan(iss);
804 }
805 return this;
806 }
807
808 Statement *UnrolledLoopStatement::inlineScan(InlineScanState *iss)
809 {
810 for (size_t i = 0; i < statements->dim; i++)
811 { Statement *s = (Statement *) statements->data[i];
812 if (s)
813 statements->data[i] = (void *)s->inlineScan(iss);
814 }
815 return this;
816 }
817
818 Statement *ScopeStatement::inlineScan(InlineScanState *iss)
819 {
820 if (statement)
821 statement = statement->inlineScan(iss);
822 return this;
823 }
824
825 Statement *WhileStatement::inlineScan(InlineScanState *iss)
826 {
827 condition = condition->inlineScan(iss);
828 body = body ? body->inlineScan(iss) : NULL;
829 return this;
830 }
831
832
833 Statement *DoStatement::inlineScan(InlineScanState *iss)
834 {
835 body = body ? body->inlineScan(iss) : NULL;
836 condition = condition->inlineScan(iss);
837 return this;
838 }
839
840
841 Statement *ForStatement::inlineScan(InlineScanState *iss)
842 {
843 if (init)
844 init = init->inlineScan(iss);
845 if (condition)
846 condition = condition->inlineScan(iss);
847 if (increment)
848 increment = increment->inlineScan(iss);
849 body = body->inlineScan(iss);
850 return this;
851 }
852
853
854 Statement *ForeachStatement::inlineScan(InlineScanState *iss)
855 {
856 aggr = aggr->inlineScan(iss);
857 body = body->inlineScan(iss);
858 return this;
859 }
860
861
862 #if V2
863 Statement *ForeachRangeStatement::inlineScan(InlineScanState *iss)
864 {
865 lwr = lwr->inlineScan(iss);
866 upr = upr->inlineScan(iss);
867 body = body->inlineScan(iss);
868 return this;
869 }
870 #endif
871
872
873 Statement *IfStatement::inlineScan(InlineScanState *iss)
874 {
875 condition = condition->inlineScan(iss);
876 if (ifbody)
877 ifbody = ifbody->inlineScan(iss);
878 if (elsebody)
879 elsebody = elsebody->inlineScan(iss);
880 return this;
881 }
882
883
884 Statement *SwitchStatement::inlineScan(InlineScanState *iss)
885 {
886 //printf("SwitchStatement::inlineScan()\n");
887 condition = condition->inlineScan(iss);
888 body = body ? body->inlineScan(iss) : NULL;
889 if (sdefault)
890 sdefault = (DefaultStatement *)sdefault->inlineScan(iss);
891 if (cases)
892 {
893 for (int i = 0; i < cases->dim; i++)
894 { Statement *s;
895
896 s = (Statement *) cases->data[i];
897 cases->data[i] = (void *)s->inlineScan(iss);
898 }
899 }
900 return this;
901 }
902
903
904 Statement *CaseStatement::inlineScan(InlineScanState *iss)
905 {
906 //printf("CaseStatement::inlineScan()\n");
907 exp = exp->inlineScan(iss);
908 if (statement)
909 statement = statement->inlineScan(iss);
910 return this;
911 }
912
913
914 Statement *DefaultStatement::inlineScan(InlineScanState *iss)
915 {
916 if (statement)
917 statement = statement->inlineScan(iss);
918 return this;
919 }
920
921
922 Statement *ReturnStatement::inlineScan(InlineScanState *iss)
923 {
924 if (exp)
925 exp = exp->inlineScan(iss);
926 return this;
927 }
928
929
930 Statement *SynchronizedStatement::inlineScan(InlineScanState *iss)
931 {
932 if (exp)
933 exp = exp->inlineScan(iss);
934 if (body)
935 body = body->inlineScan(iss);
936 return this;
937 }
938
939
940 Statement *WithStatement::inlineScan(InlineScanState *iss)
941 {
942 if (exp)
943 exp = exp->inlineScan(iss);
944 if (body)
945 body = body->inlineScan(iss);
946 return this;
947 }
948
949
950 Statement *TryCatchStatement::inlineScan(InlineScanState *iss)
951 {
952 if (body)
953 body = body->inlineScan(iss);
954 if (catches)
955 {
956 for (int i = 0; i < catches->dim; i++)
957 { Catch *c = (Catch *)catches->data[i];
958
959 if (c->handler)
960 c->handler = c->handler->inlineScan(iss);
961 }
962 }
963 return this;
964 }
965
966
967 Statement *TryFinallyStatement::inlineScan(InlineScanState *iss)
968 {
969 if (body)
970 body = body->inlineScan(iss);
971 if (finalbody)
972 finalbody = finalbody->inlineScan(iss);
973 return this;
974 }
975
976
977 Statement *ThrowStatement::inlineScan(InlineScanState *iss)
978 {
979 if (exp)
980 exp = exp->inlineScan(iss);
981 return this;
982 }
983
984
985 Statement *VolatileStatement::inlineScan(InlineScanState *iss)
986 {
987 if (statement)
988 statement = statement->inlineScan(iss);
989 return this;
990 }
991
992
993 Statement *LabelStatement::inlineScan(InlineScanState *iss)
994 {
995 if (statement)
996 statement = statement->inlineScan(iss);
997 return this;
998 }
999
1000 /* -------------------------- */
1001
1002 void arrayInlineScan(InlineScanState *iss, Array *arguments)
1003 {
1004 if (arguments)
1005 {
1006 for (int i = 0; i < arguments->dim; i++)
1007 { Expression *e = (Expression *)arguments->data[i];
1008
1009 if (e)
1010 {
1011 e = e->inlineScan(iss);
1012 arguments->data[i] = (void *)e;
1013 }
1014 }
1015 }
1016 }
1017
1018 Expression *Expression::inlineScan(InlineScanState *iss)
1019 {
1020 return this;
1021 }
1022
1023 void scanVar(Dsymbol *s, InlineScanState *iss)
1024 {
1025 VarDeclaration *vd = s->isVarDeclaration();
1026 if (vd)
1027 {
1028 TupleDeclaration *td = vd->toAlias()->isTupleDeclaration();
1029 if (td)
1030 {
1031 for (size_t i = 0; i < td->objects->dim; i++)
1032 { DsymbolExp *se = (DsymbolExp *)td->objects->data[i];
1033 assert(se->op == TOKdsymbol);
1034 scanVar(se->s, iss);
1035 }
1036 }
1037 else
1038 {
1039 // Scan initializer (vd->init)
1040 if (vd->init)
1041 {
1042 ExpInitializer *ie = vd->init->isExpInitializer();
1043
1044 if (ie)
1045 {
1046 ie->exp = ie->exp->inlineScan(iss);
1047 }
1048 }
1049 }
1050 }
1051 }
1052
1053 Expression *DeclarationExp::inlineScan(InlineScanState *iss)
1054 {
1055 //printf("DeclarationExp::inlineScan()\n");
1056 scanVar(declaration, iss);
1057 return this;
1058 }
1059
1060 Expression *UnaExp::inlineScan(InlineScanState *iss)
1061 {
1062 e1 = e1->inlineScan(iss);
1063 return this;
1064 }
1065
1066 Expression *AssertExp::inlineScan(InlineScanState *iss)
1067 {
1068 e1 = e1->inlineScan(iss);
1069 if (msg)
1070 msg = msg->inlineScan(iss);
1071 return this;
1072 }
1073
1074 Expression *BinExp::inlineScan(InlineScanState *iss)
1075 {
1076 e1 = e1->inlineScan(iss);
1077 e2 = e2->inlineScan(iss);
1078 return this;
1079 }
1080
1081
1082 Expression *CallExp::inlineScan(InlineScanState *iss)
1083 { Expression *e = this;
1084
1085 //printf("CallExp::inlineScan()\n");
1086 e1 = e1->inlineScan(iss);
1087 arrayInlineScan(iss, arguments);
1088
1089 if (e1->op == TOKvar)
1090 {
1091 VarExp *ve = (VarExp *)e1;
1092 FuncDeclaration *fd = ve->var->isFuncDeclaration();
1093
1094 if (fd && fd != iss->fd && fd->canInline(0))
1095 {
1096 e = fd->doInline(iss, NULL, arguments);
1097 }
1098 }
1099 else if (e1->op == TOKdotvar)
1100 {
1101 DotVarExp *dve = (DotVarExp *)e1;
1102 FuncDeclaration *fd = dve->var->isFuncDeclaration();
1103
1104 if (fd && fd != iss->fd && fd->canInline(1))
1105 {
1106 if (dve->e1->op == TOKcall &&
1107 dve->e1->type->toBasetype()->ty == Tstruct)
1108 {
1109 /* To create ethis, we'll need to take the address
1110 * of dve->e1, but this won't work if dve->e1 is
1111 * a function call.
1112 */
1113 ;
1114 }
1115 else
1116 e = fd->doInline(iss, dve->e1, arguments);
1117 }
1118 }
1119
1120 return e;
1121 }
1122
1123
1124 Expression *SliceExp::inlineScan(InlineScanState *iss)
1125 {
1126 e1 = e1->inlineScan(iss);
1127 if (lwr)
1128 lwr = lwr->inlineScan(iss);
1129 if (upr)
1130 upr = upr->inlineScan(iss);
1131 return this;
1132 }
1133
1134
1135 Expression *TupleExp::inlineScan(InlineScanState *iss)
1136 { Expression *e = this;
1137
1138 //printf("TupleExp::inlineScan()\n");
1139 arrayInlineScan(iss, exps);
1140
1141 return e;
1142 }
1143
1144
1145 Expression *ArrayLiteralExp::inlineScan(InlineScanState *iss)
1146 { Expression *e = this;
1147
1148 //printf("ArrayLiteralExp::inlineScan()\n");
1149 arrayInlineScan(iss, elements);
1150
1151 return e;
1152 }
1153
1154
1155 Expression *AssocArrayLiteralExp::inlineScan(InlineScanState *iss)
1156 { Expression *e = this;
1157
1158 //printf("AssocArrayLiteralExp::inlineScan()\n");
1159 arrayInlineScan(iss, keys);
1160 arrayInlineScan(iss, values);
1161
1162 return e;
1163 }
1164
1165
1166 Expression *StructLiteralExp::inlineScan(InlineScanState *iss)
1167 { Expression *e = this;
1168
1169 //printf("StructLiteralExp::inlineScan()\n");
1170 arrayInlineScan(iss, elements);
1171
1172 return e;
1173 }
1174
1175
1176 Expression *ArrayExp::inlineScan(InlineScanState *iss)
1177 { Expression *e = this;
1178
1179 //printf("ArrayExp::inlineScan()\n");
1180 e1 = e1->inlineScan(iss);
1181 arrayInlineScan(iss, arguments);
1182
1183 return e;
1184 }
1185
1186
1187 Expression *CondExp::inlineScan(InlineScanState *iss)
1188 {
1189 econd = econd->inlineScan(iss);
1190 e1 = e1->inlineScan(iss);
1191 e2 = e2->inlineScan(iss);
1192 return this;
1193 }
1194
1195
1196 /* ========== =============== */
1197
1198 void FuncDeclaration::inlineScan()
1199 {
1200 InlineScanState iss;
1201
1202 #if LOG
1203 printf("FuncDeclaration::inlineScan('%s')\n", toChars());
1204 #endif
1205 memset(&iss, 0, sizeof(iss));
1206 iss.fd = this;
1207 if (fbody)
1208 {
1209 inlineNest++;
1210 fbody = fbody->inlineScan(&iss);
1211 inlineNest--;
1212 }
1213 }
1214
1215 int FuncDeclaration::canInline(int hasthis, int hdrscan)
1216 {
1217 InlineCostState ics;
1218 int cost;
1219
1220 #define CANINLINE_LOG 0
1221
1222 #if CANINLINE_LOG
1223 printf("FuncDeclaration::canInline(hasthis = %d, '%s')\n", hasthis, toChars());
1224 #endif
1225
1226 if (needThis() && !hasthis)
1227 return 0;
1228
1229 if (inlineNest || (!semanticRun && !hdrscan))
1230 {
1231 #if CANINLINE_LOG
1232 printf("\t1: no, inlineNest = %d, semanticRun = %d\n", inlineNest, semanticRun);
1233 #endif
1234 return 0;
1235 }
1236
1237 switch (inlineStatus)
1238 {
1239 case ILSyes:
1240 #if CANINLINE_LOG
1241 printf("\tyes\n");
1242 #endif
1243 return 1;
1244
1245 case ILSno:
1246 #if CANINLINE_LOG
1247 printf("\t2: no\n");
1248 #endif
1249 return 0;
1250
1251 case ILSuninitialized:
1252 break;
1253
1254 default:
1255 assert(0);
1256 }
1257
1258 if (type)
1259 { assert(type->ty == Tfunction);
1260 TypeFunction *tf = (TypeFunction *)(type);
1261 if (tf->varargs == 1) // no variadic parameter lists
1262 goto Lno;
1263
1264 /* Don't inline a function that returns non-void, but has
1265 * no return expression.
1266 */
1267 if (tf->next && tf->next->ty != Tvoid &&
1268 !(hasReturnExp & 1) &&
1269 !hdrscan)
1270 goto Lno;
1271 }
1272 else
1273 { CtorDeclaration *ctor = isCtorDeclaration();
1274
1275 if (ctor && ctor->varargs == 1)
1276 goto Lno;
1277 }
1278
1279 if (
1280 !fbody ||
1281 !hdrscan &&
1282 (
1283 #if 0
1284 isCtorDeclaration() || // cannot because need to convert:
1285 // return;
1286 // to:
1287 // return this;
1288 #endif
1289 isSynchronized() ||
1290 isImportedSymbol() ||
1291 nestedFrameRef || // no nested references to this frame
1292 (isVirtual() && !isFinal())
1293 ))
1294 {
1295 goto Lno;
1296 }
1297
1298 /* If any parameters are Tsarray's (which are passed by reference)
1299 * or out parameters (also passed by reference), don't do inlining.
1300 */
1301 if (parameters)
1302 {
1303 for (int i = 0; i < parameters->dim; i++)
1304 {
1305 VarDeclaration *v = (VarDeclaration *)parameters->data[i];
1306 if (v->isOut() || v->isRef() || v->type->toBasetype()->ty == Tsarray)
1307 goto Lno;
1308 }
1309 }
1310
1311 memset(&ics, 0, sizeof(ics));
1312 ics.hasthis = hasthis;
1313 ics.fd = this;
1314 ics.hdrscan = hdrscan;
1315 cost = fbody->inlineCost(&ics);
1316 #if CANINLINE_LOG
1317 printf("cost = %d\n", cost);
1318 #endif
1319 if (cost >= COST_MAX)
1320 goto Lno;
1321
1322 if (!hdrscan) // Don't scan recursively for header content scan
1323 inlineScan();
1324
1325 Lyes:
1326 if (!hdrscan) // Don't modify inlineStatus for header content scan
1327 inlineStatus = ILSyes;
1328 #if CANINLINE_LOG
1329 printf("\tyes\n");
1330 #endif
1331 return 1;
1332
1333 Lno:
1334 if (!hdrscan) // Don't modify inlineStatus for header content scan
1335 inlineStatus = ILSno;
1336 #if CANINLINE_LOG
1337 printf("\tno\n");
1338 #endif
1339 return 0;
1340 }
1341
1342 Expression *FuncDeclaration::doInline(InlineScanState *iss, Expression *ethis, Array *arguments)
1343 {
1344 InlineDoState ids;
1345 DeclarationExp *de;
1346 Expression *e = NULL;
1347
1348 #if LOG
1349 printf("FuncDeclaration::doInline('%s')\n", toChars());
1350 #endif
1351
1352 memset(&ids, 0, sizeof(ids));
1353 ids.parent = iss->fd;
1354
1355 // Set up vthis
1356 if (ethis)
1357 {
1358 VarDeclaration *vthis;
1359 ExpInitializer *ei;
1360 VarExp *ve;
1361
1362 if (ethis->type->ty != Tclass && ethis->type->ty != Tpointer)
1363 {
1364 ethis = ethis->addressOf(NULL);
1365 }
1366
1367 ei = new ExpInitializer(ethis->loc, ethis);
1368
1369 vthis = new VarDeclaration(ethis->loc, ethis->type, Id::This, ei);
1370 vthis->storage_class = STCin;
1371 vthis->linkage = LINKd;
1372 vthis->parent = iss->fd;
1373
1374 ve = new VarExp(vthis->loc, vthis);
1375 ve->type = vthis->type;
1376
1377 ei->exp = new AssignExp(vthis->loc, ve, ethis);
1378 ei->exp->type = ve->type;
1379
1380 ids.vthis = vthis;
1381 }
1382
1383 // Set up parameters
1384 if (ethis)
1385 {
1386 e = new DeclarationExp(0, ids.vthis);
1387 e->type = Type::tvoid;
1388 }
1389
1390 if (arguments && arguments->dim)
1391 {
1392 assert(parameters->dim == arguments->dim);
1393
1394 for (int i = 0; i < arguments->dim; i++)
1395 {
1396 VarDeclaration *vfrom = (VarDeclaration *)parameters->data[i];
1397 VarDeclaration *vto;
1398 Expression *arg = (Expression *)arguments->data[i];
1399 ExpInitializer *ei;
1400 VarExp *ve;
1401
1402 ei = new ExpInitializer(arg->loc, arg);
1403
1404 vto = new VarDeclaration(vfrom->loc, vfrom->type, vfrom->ident, ei);
1405 vto->storage_class |= vfrom->storage_class & (STCin | STCout | STClazy | STCref);
1406 vto->linkage = vfrom->linkage;
1407 vto->parent = iss->fd;
1408 //printf("vto = '%s', vto->storage_class = x%x\n", vto->toChars(), vto->storage_class);
1409 //printf("vto->parent = '%s'\n", iss->fd->toChars());
1410
1411 ve = new VarExp(vto->loc, vto);
1412 //ve->type = vto->type;
1413 ve->type = arg->type;
1414
1415 ei->exp = new AssignExp(vto->loc, ve, arg);
1416 ei->exp->type = ve->type;
1417 //ve->type->print();
1418 //arg->type->print();
1419 //ei->exp->print();
1420
1421 ids.from.push(vfrom);
1422 ids.to.push(vto);
1423
1424 de = new DeclarationExp(0, vto);
1425 de->type = Type::tvoid;
1426
1427 e = Expression::combine(e, de);
1428 }
1429 }
1430
1431 inlineNest++;
1432 Expression *eb = fbody->doInline(&ids);
1433 inlineNest--;
1434 //eb->type->print();
1435 //eb->print();
1436 //eb->dump(0);
1437 return Expression::combine(e, eb);
1438 }
1439
1440
1441