comparison dmd2/inline.c @ 758:f04dde6e882c

Added initial D2 support, D2 frontend and changes to codegen to make things compile.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:38:48 +0100
parents
children 356e65836fb5
comparison
equal deleted inserted replaced
757:2c730d530c98 758:f04dde6e882c
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())
546 ;
547 else
548 {
549 VarDeclaration *vto;
550
551 vto = new VarDeclaration(vd->loc, vd->type, vd->ident, vd->init);
552 *vto = *vd;
553 vto->parent = ids->parent;
554 vto->csym = NULL;
555 vto->isym = NULL;
556
557 ids->from.push(vd);
558 ids->to.push(vto);
559
560 if (vd->init)
561 {
562 if (vd->init->isVoidInitializer())
563 {
564 vto->init = new VoidInitializer(vd->init->loc);
565 }
566 else
567 {
568 ExpInitializer *ie = vd->init->isExpInitializer();
569 assert(ie);
570 vto->init = new ExpInitializer(ie->loc, ie->exp->doInline(ids));
571 }
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 if (body)
858 body = body->inlineScan(iss);
859 return this;
860 }
861
862
863 #if DMDV2
864 Statement *ForeachRangeStatement::inlineScan(InlineScanState *iss)
865 {
866 lwr = lwr->inlineScan(iss);
867 upr = upr->inlineScan(iss);
868 if (body)
869 body = body->inlineScan(iss);
870 return this;
871 }
872 #endif
873
874
875 Statement *IfStatement::inlineScan(InlineScanState *iss)
876 {
877 condition = condition->inlineScan(iss);
878 if (ifbody)
879 ifbody = ifbody->inlineScan(iss);
880 if (elsebody)
881 elsebody = elsebody->inlineScan(iss);
882 return this;
883 }
884
885
886 Statement *SwitchStatement::inlineScan(InlineScanState *iss)
887 {
888 //printf("SwitchStatement::inlineScan()\n");
889 condition = condition->inlineScan(iss);
890 body = body ? body->inlineScan(iss) : NULL;
891 if (sdefault)
892 sdefault = (DefaultStatement *)sdefault->inlineScan(iss);
893 if (cases)
894 {
895 for (int i = 0; i < cases->dim; i++)
896 { Statement *s;
897
898 s = (Statement *) cases->data[i];
899 cases->data[i] = (void *)s->inlineScan(iss);
900 }
901 }
902 return this;
903 }
904
905
906 Statement *CaseStatement::inlineScan(InlineScanState *iss)
907 {
908 //printf("CaseStatement::inlineScan()\n");
909 exp = exp->inlineScan(iss);
910 if (statement)
911 statement = statement->inlineScan(iss);
912 return this;
913 }
914
915
916 Statement *DefaultStatement::inlineScan(InlineScanState *iss)
917 {
918 if (statement)
919 statement = statement->inlineScan(iss);
920 return this;
921 }
922
923
924 Statement *ReturnStatement::inlineScan(InlineScanState *iss)
925 {
926 //printf("ReturnStatement::inlineScan()\n");
927 if (exp)
928 {
929 exp = exp->inlineScan(iss);
930 }
931 return this;
932 }
933
934
935 Statement *SynchronizedStatement::inlineScan(InlineScanState *iss)
936 {
937 if (exp)
938 exp = exp->inlineScan(iss);
939 if (body)
940 body = body->inlineScan(iss);
941 return this;
942 }
943
944
945 Statement *WithStatement::inlineScan(InlineScanState *iss)
946 {
947 if (exp)
948 exp = exp->inlineScan(iss);
949 if (body)
950 body = body->inlineScan(iss);
951 return this;
952 }
953
954
955 Statement *TryCatchStatement::inlineScan(InlineScanState *iss)
956 {
957 if (body)
958 body = body->inlineScan(iss);
959 if (catches)
960 {
961 for (int i = 0; i < catches->dim; i++)
962 { Catch *c = (Catch *)catches->data[i];
963
964 if (c->handler)
965 c->handler = c->handler->inlineScan(iss);
966 }
967 }
968 return this;
969 }
970
971
972 Statement *TryFinallyStatement::inlineScan(InlineScanState *iss)
973 {
974 if (body)
975 body = body->inlineScan(iss);
976 if (finalbody)
977 finalbody = finalbody->inlineScan(iss);
978 return this;
979 }
980
981
982 Statement *ThrowStatement::inlineScan(InlineScanState *iss)
983 {
984 if (exp)
985 exp = exp->inlineScan(iss);
986 return this;
987 }
988
989
990 Statement *VolatileStatement::inlineScan(InlineScanState *iss)
991 {
992 if (statement)
993 statement = statement->inlineScan(iss);
994 return this;
995 }
996
997
998 Statement *LabelStatement::inlineScan(InlineScanState *iss)
999 {
1000 if (statement)
1001 statement = statement->inlineScan(iss);
1002 return this;
1003 }
1004
1005 /* -------------------------- */
1006
1007 void arrayInlineScan(InlineScanState *iss, Array *arguments)
1008 {
1009 if (arguments)
1010 {
1011 for (int i = 0; i < arguments->dim; i++)
1012 { Expression *e = (Expression *)arguments->data[i];
1013
1014 if (e)
1015 {
1016 e = e->inlineScan(iss);
1017 arguments->data[i] = (void *)e;
1018 }
1019 }
1020 }
1021 }
1022
1023 Expression *Expression::inlineScan(InlineScanState *iss)
1024 {
1025 return this;
1026 }
1027
1028 void scanVar(Dsymbol *s, InlineScanState *iss)
1029 {
1030 VarDeclaration *vd = s->isVarDeclaration();
1031 if (vd)
1032 {
1033 TupleDeclaration *td = vd->toAlias()->isTupleDeclaration();
1034 if (td)
1035 {
1036 for (size_t i = 0; i < td->objects->dim; i++)
1037 { DsymbolExp *se = (DsymbolExp *)td->objects->data[i];
1038 assert(se->op == TOKdsymbol);
1039 scanVar(se->s, iss);
1040 }
1041 }
1042 else
1043 {
1044 // Scan initializer (vd->init)
1045 if (vd->init)
1046 {
1047 ExpInitializer *ie = vd->init->isExpInitializer();
1048
1049 if (ie)
1050 {
1051 ie->exp = ie->exp->inlineScan(iss);
1052 }
1053 }
1054 }
1055 }
1056 }
1057
1058 Expression *DeclarationExp::inlineScan(InlineScanState *iss)
1059 {
1060 //printf("DeclarationExp::inlineScan()\n");
1061 scanVar(declaration, iss);
1062 return this;
1063 }
1064
1065 Expression *UnaExp::inlineScan(InlineScanState *iss)
1066 {
1067 e1 = e1->inlineScan(iss);
1068 return this;
1069 }
1070
1071 Expression *AssertExp::inlineScan(InlineScanState *iss)
1072 {
1073 e1 = e1->inlineScan(iss);
1074 if (msg)
1075 msg = msg->inlineScan(iss);
1076 return this;
1077 }
1078
1079 Expression *BinExp::inlineScan(InlineScanState *iss)
1080 {
1081 e1 = e1->inlineScan(iss);
1082 e2 = e2->inlineScan(iss);
1083 return this;
1084 }
1085
1086
1087 Expression *CallExp::inlineScan(InlineScanState *iss)
1088 { Expression *e = this;
1089
1090 //printf("CallExp::inlineScan()\n");
1091 e1 = e1->inlineScan(iss);
1092 arrayInlineScan(iss, arguments);
1093
1094 if (e1->op == TOKvar)
1095 {
1096 VarExp *ve = (VarExp *)e1;
1097 FuncDeclaration *fd = ve->var->isFuncDeclaration();
1098
1099 if (fd && fd != iss->fd && fd->canInline(0))
1100 {
1101 e = fd->doInline(iss, NULL, arguments);
1102 }
1103 }
1104 else if (e1->op == TOKdotvar)
1105 {
1106 DotVarExp *dve = (DotVarExp *)e1;
1107 FuncDeclaration *fd = dve->var->isFuncDeclaration();
1108
1109 if (fd && fd != iss->fd && fd->canInline(1))
1110 {
1111 if (dve->e1->op == TOKcall &&
1112 dve->e1->type->toBasetype()->ty == Tstruct)
1113 {
1114 /* To create ethis, we'll need to take the address
1115 * of dve->e1, but this won't work if dve->e1 is
1116 * a function call.
1117 */
1118 ;
1119 }
1120 else
1121 e = fd->doInline(iss, dve->e1, arguments);
1122 }
1123 }
1124
1125 return e;
1126 }
1127
1128
1129 Expression *SliceExp::inlineScan(InlineScanState *iss)
1130 {
1131 e1 = e1->inlineScan(iss);
1132 if (lwr)
1133 lwr = lwr->inlineScan(iss);
1134 if (upr)
1135 upr = upr->inlineScan(iss);
1136 return this;
1137 }
1138
1139
1140 Expression *TupleExp::inlineScan(InlineScanState *iss)
1141 { Expression *e = this;
1142
1143 //printf("TupleExp::inlineScan()\n");
1144 arrayInlineScan(iss, exps);
1145
1146 return e;
1147 }
1148
1149
1150 Expression *ArrayLiteralExp::inlineScan(InlineScanState *iss)
1151 { Expression *e = this;
1152
1153 //printf("ArrayLiteralExp::inlineScan()\n");
1154 arrayInlineScan(iss, elements);
1155
1156 return e;
1157 }
1158
1159
1160 Expression *AssocArrayLiteralExp::inlineScan(InlineScanState *iss)
1161 { Expression *e = this;
1162
1163 //printf("AssocArrayLiteralExp::inlineScan()\n");
1164 arrayInlineScan(iss, keys);
1165 arrayInlineScan(iss, values);
1166
1167 return e;
1168 }
1169
1170
1171 Expression *StructLiteralExp::inlineScan(InlineScanState *iss)
1172 { Expression *e = this;
1173
1174 //printf("StructLiteralExp::inlineScan()\n");
1175 arrayInlineScan(iss, elements);
1176
1177 return e;
1178 }
1179
1180
1181 Expression *ArrayExp::inlineScan(InlineScanState *iss)
1182 { Expression *e = this;
1183
1184 //printf("ArrayExp::inlineScan()\n");
1185 e1 = e1->inlineScan(iss);
1186 arrayInlineScan(iss, arguments);
1187
1188 return e;
1189 }
1190
1191
1192 Expression *CondExp::inlineScan(InlineScanState *iss)
1193 {
1194 econd = econd->inlineScan(iss);
1195 e1 = e1->inlineScan(iss);
1196 e2 = e2->inlineScan(iss);
1197 return this;
1198 }
1199
1200
1201 /* ========== =============== */
1202
1203 void FuncDeclaration::inlineScan()
1204 {
1205 InlineScanState iss;
1206
1207 #if LOG
1208 printf("FuncDeclaration::inlineScan('%s')\n", toChars());
1209 #endif
1210 memset(&iss, 0, sizeof(iss));
1211 iss.fd = this;
1212 if (fbody)
1213 {
1214 inlineNest++;
1215 fbody = fbody->inlineScan(&iss);
1216 inlineNest--;
1217 }
1218 }
1219
1220 int FuncDeclaration::canInline(int hasthis, int hdrscan)
1221 {
1222 InlineCostState ics;
1223 int cost;
1224
1225 #define CANINLINE_LOG 0
1226
1227 #if CANINLINE_LOG
1228 printf("FuncDeclaration::canInline(hasthis = %d, '%s')\n", hasthis, toChars());
1229 #endif
1230
1231 if (needThis() && !hasthis)
1232 return 0;
1233
1234 if (inlineNest || (!semanticRun && !hdrscan))
1235 {
1236 #if CANINLINE_LOG
1237 printf("\t1: no, inlineNest = %d, semanticRun = %d\n", inlineNest, semanticRun);
1238 #endif
1239 return 0;
1240 }
1241
1242 switch (inlineStatus)
1243 {
1244 case ILSyes:
1245 #if CANINLINE_LOG
1246 printf("\t1: yes %s\n", toChars());
1247 #endif
1248 return 1;
1249
1250 case ILSno:
1251 #if CANINLINE_LOG
1252 printf("\t1: no %s\n", toChars());
1253 #endif
1254 return 0;
1255
1256 case ILSuninitialized:
1257 break;
1258
1259 default:
1260 assert(0);
1261 }
1262
1263 if (type)
1264 { assert(type->ty == Tfunction);
1265 TypeFunction *tf = (TypeFunction *)(type);
1266 if (tf->varargs == 1) // no variadic parameter lists
1267 goto Lno;
1268
1269 /* Don't inline a function that returns non-void, but has
1270 * no return expression.
1271 */
1272 if (tf->next && tf->next->ty != Tvoid &&
1273 !(hasReturnExp & 1) &&
1274 !hdrscan)
1275 goto Lno;
1276 }
1277 else
1278 { CtorDeclaration *ctor = isCtorDeclaration();
1279
1280 if (ctor && ctor->varargs == 1)
1281 goto Lno;
1282 }
1283
1284 if (
1285 !fbody ||
1286 !hdrscan &&
1287 (
1288 #if 0
1289 isCtorDeclaration() || // cannot because need to convert:
1290 // return;
1291 // to:
1292 // return this;
1293 #endif
1294 isSynchronized() ||
1295 isImportedSymbol() ||
1296 #if DMDV2
1297 closureVars.dim || // no nested references to this frame
1298 #else
1299 nestedFrameRef || // no nested references to this frame
1300 #endif
1301 (isVirtual() && !isFinal())
1302 ))
1303 {
1304 goto Lno;
1305 }
1306
1307 /* If any parameters are Tsarray's (which are passed by reference)
1308 * or out parameters (also passed by reference), don't do inlining.
1309 */
1310 if (parameters)
1311 {
1312 for (int i = 0; i < parameters->dim; i++)
1313 {
1314 VarDeclaration *v = (VarDeclaration *)parameters->data[i];
1315 if (v->isOut() || v->isRef() || v->type->toBasetype()->ty == Tsarray)
1316 goto Lno;
1317 }
1318 }
1319
1320 memset(&ics, 0, sizeof(ics));
1321 ics.hasthis = hasthis;
1322 ics.fd = this;
1323 ics.hdrscan = hdrscan;
1324 cost = fbody->inlineCost(&ics);
1325 #if CANINLINE_LOG
1326 printf("cost = %d\n", cost);
1327 #endif
1328 if (cost >= COST_MAX)
1329 goto Lno;
1330
1331 if (!hdrscan) // Don't scan recursively for header content scan
1332 inlineScan();
1333
1334 Lyes:
1335 if (!hdrscan) // Don't modify inlineStatus for header content scan
1336 inlineStatus = ILSyes;
1337 #if CANINLINE_LOG
1338 printf("\t2: yes %s\n", toChars());
1339 #endif
1340 return 1;
1341
1342 Lno:
1343 if (!hdrscan) // Don't modify inlineStatus for header content scan
1344 inlineStatus = ILSno;
1345 #if CANINLINE_LOG
1346 printf("\t2: no %s\n", toChars());
1347 #endif
1348 return 0;
1349 }
1350
1351 Expression *FuncDeclaration::doInline(InlineScanState *iss, Expression *ethis, Array *arguments)
1352 {
1353 InlineDoState ids;
1354 DeclarationExp *de;
1355 Expression *e = NULL;
1356
1357 #if LOG
1358 printf("FuncDeclaration::doInline('%s')\n", toChars());
1359 #endif
1360
1361 memset(&ids, 0, sizeof(ids));
1362 ids.parent = iss->fd;
1363
1364 // Set up vthis
1365 if (ethis)
1366 {
1367 VarDeclaration *vthis;
1368 ExpInitializer *ei;
1369 VarExp *ve;
1370
1371 if (ethis->type->ty != Tclass && ethis->type->ty != Tpointer)
1372 {
1373 ethis = ethis->addressOf(NULL);
1374 }
1375
1376 ei = new ExpInitializer(ethis->loc, ethis);
1377
1378 vthis = new VarDeclaration(ethis->loc, ethis->type, Id::This, ei);
1379 vthis->storage_class = STCin;
1380 vthis->linkage = LINKd;
1381 vthis->parent = iss->fd;
1382
1383 ve = new VarExp(vthis->loc, vthis);
1384 ve->type = vthis->type;
1385
1386 ei->exp = new AssignExp(vthis->loc, ve, ethis);
1387 ei->exp->type = ve->type;
1388
1389 ids.vthis = vthis;
1390 }
1391
1392 // Set up parameters
1393 if (ethis)
1394 {
1395 e = new DeclarationExp(0, ids.vthis);
1396 e->type = Type::tvoid;
1397 }
1398
1399 if (arguments && arguments->dim)
1400 {
1401 assert(parameters->dim == arguments->dim);
1402
1403 for (int i = 0; i < arguments->dim; i++)
1404 {
1405 VarDeclaration *vfrom = (VarDeclaration *)parameters->data[i];
1406 VarDeclaration *vto;
1407 Expression *arg = (Expression *)arguments->data[i];
1408 ExpInitializer *ei;
1409 VarExp *ve;
1410
1411 ei = new ExpInitializer(arg->loc, arg);
1412
1413 vto = new VarDeclaration(vfrom->loc, vfrom->type, vfrom->ident, ei);
1414 vto->storage_class |= vfrom->storage_class & (STCin | STCout | STClazy | STCref);
1415 vto->linkage = vfrom->linkage;
1416 vto->parent = iss->fd;
1417 //printf("vto = '%s', vto->storage_class = x%x\n", vto->toChars(), vto->storage_class);
1418 //printf("vto->parent = '%s'\n", iss->fd->toChars());
1419
1420 ve = new VarExp(vto->loc, vto);
1421 //ve->type = vto->type;
1422 ve->type = arg->type;
1423
1424 ei->exp = new AssignExp(vto->loc, ve, arg);
1425 ei->exp->type = ve->type;
1426 //ve->type->print();
1427 //arg->type->print();
1428 //ei->exp->print();
1429
1430 ids.from.push(vfrom);
1431 ids.to.push(vto);
1432
1433 de = new DeclarationExp(0, vto);
1434 de->type = Type::tvoid;
1435
1436 e = Expression::combine(e, de);
1437 }
1438 }
1439
1440 inlineNest++;
1441 Expression *eb = fbody->doInline(&ids);
1442 inlineNest--;
1443 return Expression::combine(e, eb);
1444 }
1445
1446
1447