-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathinlinecost.d
521 lines (477 loc) · 14.6 KB
/
inlinecost.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
* Compute the cost of inlining a function call by counting expressions.
*
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/inlinecost.d, _inlinecost.d)
* Documentation: https://dlang.org/phobos/dmd_inlinecost.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/inlinecost.d
*/
module dmd.inlinecost;
import core.stdc.stdio;
import core.stdc.string;
import dmd.aggregate;
import dmd.arraytypes;
import dmd.astenums;
import dmd.attrib;
import dmd.dclass;
import dmd.declaration;
import dmd.dmodule;
import dmd.dscope;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.expression;
import dmd.func;
import dmd.globals;
import dmd.id;
import dmd.identifier;
import dmd.init;
import dmd.mtype;
import dmd.opover;
import dmd.statement;
import dmd.tokens;
import dmd.visitor;
import dmd.visitor.postorder;
enum COST_MAX = 250;
private enum STATEMENT_COST = 0x1000;
private enum STATEMENT_COST_MAX = 250 * STATEMENT_COST;
// STATEMENT_COST be power of 2 and greater than COST_MAX
static assert((STATEMENT_COST & (STATEMENT_COST - 1)) == 0);
static assert(STATEMENT_COST > COST_MAX);
/*********************************
* Determine if too expensive to inline.
* Params:
* cost = cost of inlining
* Returns:
* true if too costly
*/
bool tooCostly(int cost) pure nothrow @safe
{
return ((cost & (STATEMENT_COST - 1)) >= COST_MAX);
}
/*********************************
* Determine cost of inlining Expression
* Params:
* e = Expression to determine cost of
* Returns:
* cost of inlining e
*/
int inlineCostExpression(Expression e)
{
scope InlineCostVisitor icv = new InlineCostVisitor(false, true, true, null);
icv.expressionInlineCost(e);
return icv.cost;
}
/*********************************
* Determine cost of inlining function
* Params:
* fd = function to determine cost of
* hasthis = if the function call has explicit 'this' expression
* hdrscan = if generating a header file
* Returns:
* cost of inlining fd
*/
int inlineCostFunction(FuncDeclaration fd, bool hasthis, bool hdrscan)
{
scope InlineCostVisitor icv = new InlineCostVisitor(hasthis, hdrscan, false, fd);
fd.fbody.accept(icv);
return icv.cost;
}
/**
* Indicates if a nested aggregate prevents or not a function to be inlined.
* It's used to compute the cost but also to avoid a copy of the aggregate
* while the inliner processes.
*
* Params:
* e = the declaration expression that may represent an aggregate.
*
* Returns: `null` if `e` is not an aggregate or if it is an aggregate that
* doesn't permit inlining, and the aggregate otherwise.
*/
AggregateDeclaration isInlinableNestedAggregate(DeclarationExp e)
{
AggregateDeclaration result;
if (e.declaration.isAnonymous() && e.declaration.isAttribDeclaration)
{
AttribDeclaration ad = e.declaration.isAttribDeclaration;
if (ad.decl.length == 1)
{
if ((result = (*ad.decl)[0].isAggregateDeclaration) !is null)
{
// classes would have to be destroyed
if (auto cdecl = result.isClassDeclaration)
return null;
// if it's a struct: must not have dtor
StructDeclaration sdecl = result.isStructDeclaration;
if (sdecl && (sdecl.fieldDtor || sdecl.dtor))
return null;
// the aggregate must be static
UnionDeclaration udecl = result.isUnionDeclaration;
if ((sdecl || udecl) && !(result.storage_class & STC.static_))
return null;
return result;
}
}
}
else if ((result = e.declaration.isStructDeclaration) !is null)
{
return result;
}
else if ((result = e.declaration.isUnionDeclaration) !is null)
{
return result;
}
return null;
}
private:
/***********************************************************
* Compute cost of inlining.
*
* Walk trees to determine if inlining can be done, and if so,
* if it is too complex to be worth inlining or not.
*/
extern (C++) final class InlineCostVisitor : Visitor
{
alias visit = Visitor.visit;
public:
int nested;
bool hasthis;
bool hdrscan; // if inline scan for 'header' content
bool allowAlloca;
FuncDeclaration fd;
int cost; // zero start for subsequent AST
extern (D) this() scope @safe
{
}
extern (D) this(bool hasthis, bool hdrscan, bool allowAlloca, FuncDeclaration fd) scope @safe
{
this.hasthis = hasthis;
this.hdrscan = hdrscan;
this.allowAlloca = allowAlloca;
this.fd = fd;
}
extern (D) this(InlineCostVisitor icv) scope @safe
{
nested = icv.nested;
hasthis = icv.hasthis;
hdrscan = icv.hdrscan;
allowAlloca = icv.allowAlloca;
fd = icv.fd;
}
override void visit(Statement s)
{
//printf("Statement.inlineCost = %d\n", COST_MAX);
//printf("%p\n", s.isScopeStatement());
//printf("%s\n", s.toChars());
cost += COST_MAX; // default is we can't inline it
}
override void visit(ExpStatement s)
{
expressionInlineCost(s.exp);
}
override void visit(CompoundStatement s)
{
scope InlineCostVisitor icv = new InlineCostVisitor(this);
foreach (i; 0 .. s.statements.length)
{
if (Statement s2 = (*s.statements)[i])
{
/* Specifically allow:
* if (condition)
* return exp1;
* return exp2;
*/
IfStatement ifs;
Statement s3;
if ((ifs = s2.isIfStatement()) !is null &&
ifs.ifbody &&
ifs.ifbody.endsWithReturnStatement() &&
!ifs.elsebody &&
i + 1 < s.statements.length &&
(s3 = (*s.statements)[i + 1]) !is null &&
s3.endsWithReturnStatement()
)
{
if (ifs.param) // if variables are declared
{
cost = COST_MAX;
return;
}
expressionInlineCost(ifs.condition);
ifs.ifbody.accept(this);
s3.accept(this);
}
else
s2.accept(icv);
if (tooCostly(icv.cost))
break;
}
}
cost += icv.cost;
}
override void visit(UnrolledLoopStatement s)
{
scope InlineCostVisitor icv = new InlineCostVisitor(this);
foreach (s2; *s.statements)
{
if (s2)
{
s2.accept(icv);
if (tooCostly(icv.cost))
break;
}
}
cost += icv.cost;
}
override void visit(ScopeStatement s)
{
cost++;
if (s.statement)
s.statement.accept(this);
}
override void visit(IfStatement s)
{
/* Can't declare variables inside ?: expressions, so
* we cannot inline if a variable is declared.
*/
if (s.param)
{
cost = COST_MAX;
return;
}
expressionInlineCost(s.condition);
if (s.isIfCtfeBlock())
{
cost = COST_MAX;
return;
}
/* Specifically allow:
* if (condition)
* return exp1;
* else
* return exp2;
* Otherwise, we can't handle return statements nested in if's.
*/
if (s.elsebody && s.ifbody && s.ifbody.endsWithReturnStatement() && s.elsebody.endsWithReturnStatement())
{
s.ifbody.accept(this);
s.elsebody.accept(this);
//printf("cost = %d\n", cost);
}
else
{
nested += 1;
if (s.ifbody)
s.ifbody.accept(this);
if (s.elsebody)
s.elsebody.accept(this);
nested -= 1;
}
//printf("IfStatement.inlineCost = %d\n", cost);
}
override void visit(ReturnStatement s)
{
// Can't handle return statements nested in if's
if (nested)
{
cost = COST_MAX;
}
else
{
expressionInlineCost(s.exp);
}
}
override void visit(ImportStatement s)
{
}
override void visit(ForStatement s)
{
cost += STATEMENT_COST;
if (s._init)
s._init.accept(this);
if (s.condition)
s.condition.accept(this);
if (s.increment)
s.increment.accept(this);
if (s._body)
s._body.accept(this);
//printf("ForStatement: inlineCost = %d\n", cost);
}
override void visit(ThrowStatement s)
{
cost += STATEMENT_COST;
s.exp.accept(this);
}
/* -------------------------- */
void expressionInlineCost(Expression e)
{
//printf("expressionInlineCost()\n");
//e.print();
if (e)
{
extern (C++) final class LambdaInlineCost : StoppableVisitor
{
alias visit = typeof(super).visit;
InlineCostVisitor icv;
public:
extern (D) this(InlineCostVisitor icv) @safe
{
this.icv = icv;
}
override void visit(Expression e)
{
e.accept(icv);
stop = icv.cost >= COST_MAX;
}
}
scope InlineCostVisitor icv = new InlineCostVisitor(this);
scope LambdaInlineCost lic = new LambdaInlineCost(icv);
walkPostorder(e, lic);
cost += icv.cost;
}
}
override void visit(Expression e)
{
cost++;
}
override void visit(VarExp e)
{
//printf("VarExp.inlineCost3() %s\n", toChars());
Type tb = e.type.toBasetype();
if (auto ts = tb.isTypeStruct())
{
StructDeclaration sd = ts.sym;
if (sd.isNested())
{
/* An inner struct will be nested inside another function hierarchy than where
* we're inlining into, so don't inline it.
* At least not until we figure out how to 'move' the struct to be nested
* locally. Example:
* struct S(alias pred) { void unused_func(); }
* void abc() { int w; S!(w) m; }
* void bar() { abc(); }
*/
cost = COST_MAX;
return;
}
}
FuncDeclaration fd = e.var.isFuncDeclaration();
if (fd && fd.isNested()) // https://issues.dlang.org/show_bug.cgi?id=7199 for test case
cost = COST_MAX;
else
cost++;
}
override void visit(ThisExp e)
{
//printf("ThisExp.inlineCost3() %s\n", toChars());
if (!fd)
{
cost = COST_MAX;
return;
}
if (!hdrscan)
{
if (fd.isNested() || !hasthis)
{
cost = COST_MAX;
return;
}
}
cost++;
}
override void visit(StructLiteralExp e)
{
//printf("StructLiteralExp.inlineCost3() %s\n", toChars());
if (e.sd.isNested())
cost = COST_MAX;
else
cost++;
}
override void visit(NewExp e)
{
//printf("NewExp.inlineCost3() %s\n", e.toChars());
AggregateDeclaration ad = isAggregate(e.newtype);
if (ad && ad.isNested() || e.placement)
cost = COST_MAX;
else
cost++;
}
override void visit(FuncExp e)
{
//printf("FuncExp.inlineCost3()\n");
// Right now, this makes the function be output to the .obj file twice.
cost = COST_MAX;
}
override void visit(DelegateExp e)
{
//printf("DelegateExp.inlineCost3()\n");
cost = COST_MAX;
}
override void visit(DeclarationExp e)
{
//printf("DeclarationExp.inlineCost3()\n");
if (auto vd = e.declaration.isVarDeclaration())
{
if (auto td = vd.toAlias().isTupleDeclaration())
{
cost = COST_MAX; // finish DeclarationExp.doInlineAs
return;
}
if (!hdrscan && vd.isDataseg())
{
cost = COST_MAX;
return;
}
if (vd.edtor)
{
// if destructor required
// needs work to make this work
cost = COST_MAX;
return;
}
// Scan initializer (vd.init)
if (vd._init)
{
if (auto ie = vd._init.isExpInitializer())
{
expressionInlineCost(ie.exp);
}
}
++cost;
}
// aggregates are accepted under certain circumstances
if (isInlinableNestedAggregate(e))
{
cost++;
return;
}
// These can contain functions, which when copied, get output twice.
if (e.declaration.isStructDeclaration() ||
e.declaration.isClassDeclaration() ||
e.declaration.isFuncDeclaration() ||
e.declaration.isAttribDeclaration() ||
e.declaration.isTemplateMixin())
{
cost = COST_MAX;
return;
}
//printf("DeclarationExp.inlineCost3('%s')\n", toChars());
}
override void visit(CallExp e)
{
//printf("CallExp.inlineCost3() %s\n", toChars());
// in LDC, we only use the inliner for default arguments
static if (IN_LLVM)
cost++;
// https://issues.dlang.org/show_bug.cgi?id=3500
// super.func() calls must be devirtualized, and the inliner
// can't handle that at present.
else if (e.e1.op == EXP.dotVariable && (cast(DotVarExp)e.e1).e1.op == EXP.super_)
cost = COST_MAX;
else if (e.f && e.f.ident == Id.__alloca && e.f._linkage == LINK.c && !allowAlloca)
cost = COST_MAX; // inlining alloca may cause stack overflows
else
cost++;
}
}