Skip to content

Commit

Permalink
eliminate redundant inline scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Sep 25, 2015
1 parent 51e7592 commit 550fbd6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/func.d
Expand Up @@ -393,6 +393,7 @@ enum FUNCFLAGsafetyInprocess = 2; // working on determining safety
enum FUNCFLAGnothrowInprocess = 4; // working on determining nothrow
enum FUNCFLAGnogcInprocess = 8; // working on determining @nogc
enum FUNCFLAGreturnInprocess = 0x10; // working on inferring 'return' for parameters
enum FUNCFLAGinlineScanned = 0x20; // function has been scanned for inline possibilities

/***********************************************************
*/
Expand Down
45 changes: 34 additions & 11 deletions src/inline.d
Expand Up @@ -1179,6 +1179,7 @@ public:
// are used to pass the result from 'visit' back to 'inlineScan'
Statement result;
Expression eresult;
bool again;

extern (D) this()
{
Expand Down Expand Up @@ -1209,7 +1210,7 @@ public:
FuncDeclaration fd = ve.var.isFuncDeclaration();
if (fd && fd != parent && canInline(fd, 0, 0, 1))
{
expandInline(fd, parent, null, null, ce.arguments, &result);
expandInline(fd, parent, null, null, ce.arguments, &result, again);
}
}
}
Expand Down Expand Up @@ -1494,7 +1495,7 @@ public:
FuncDeclaration fd = ve.var.isFuncDeclaration();
if (fd && fd != parent && canInline(fd, 0, 0, 0))
{
Expression ex = expandInline(fd, parent, eret, null, e.arguments, null);
Expression ex = expandInline(fd, parent, eret, null, e.arguments, null, again);
if (ex)
{
eresult = ex;
Expand All @@ -1518,7 +1519,7 @@ public:
}
else
{
Expression ex = expandInline(fd, parent, eret, dve.e1, e.arguments, null);
Expression ex = expandInline(fd, parent, eret, dve.e1, e.arguments, null, again);
if (ex)
{
eresult = ex;
Expand Down Expand Up @@ -1619,17 +1620,26 @@ public:
{
printf("FuncDeclaration::inlineScan('%s')\n", fd.toPrettyChars());
}
if (fd.isUnitTestDeclaration() && !global.params.useUnitTests)
if (fd.isUnitTestDeclaration() && !global.params.useUnitTests ||
fd.flags & FUNCFLAGinlineScanned)
return;
FuncDeclaration oldparent = parent;
parent = fd;
if (fd.fbody && !fd.naked)
{
fd.inlineNest++;
inlineScan(&fd.fbody);
fd.inlineNest--;
auto againsave = again;
auto parentsave = parent;
parent = fd;
do
{
again = false;
fd.inlineNest++;
fd.flags |= FUNCFLAGinlineScanned;
inlineScan(&fd.fbody);
fd.inlineNest--;
}
while (again);
again = againsave;
parent = parentsave;
}
parent = oldparent;
}

override void visit(AttribDeclaration d)
Expand Down Expand Up @@ -1874,10 +1884,13 @@ public void inlineScanModule(Module m)
* arguments = arguments passed to fd
* ps = if expanding to a statement, this is where the statement is written to
* (and ignore the return value)
* again = if true, then fd can be inline scanned again because there may be
* more opportunities for inlining
* Returns:
* Expression it expanded to
*/
Expression expandInline(FuncDeclaration fd, FuncDeclaration parent, Expression eret, Expression ethis, Expressions* arguments, Statement* ps)
Expression expandInline(FuncDeclaration fd, FuncDeclaration parent, Expression eret,
Expression ethis, Expressions* arguments, Statement* ps, out bool again)
{
InlineDoState ids;
Expression e = null;
Expand Down Expand Up @@ -2022,6 +2035,16 @@ Expression expandInline(FuncDeclaration fd, FuncDeclaration parent, Expression e
auto de = new DeclarationExp(Loc(), vto);
de.type = Type.tvoid;
e = Expression.combine(e, de);

/* If function pointer or delegate parameters are present,
* inline scan again because if they are initialized to a symbol,
* any calls to the fp or dg can be inlined.
*/
if (vfrom.type.ty == Tdelegate ||
vfrom.type.ty == Tpointer && vfrom.type.nextOf().ty == Tfunction)
{
again = true;
}
}
}
if (ps)
Expand Down

0 comments on commit 550fbd6

Please sign in to comment.