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