Code cleanup: Simplify redundant code in Inliner.
Due to ongoing simplifications, the code had two variables that always
referred to the same object (inlineStatements, inlinedBlockStmts) and
reserve_back was called on it twice. The amount of space being reserved
was also wrong, as it accounted for out-param writebacks, which are no
longer supported.
We now only have one name for it, and reserve all the space we need the
first time.
Change-Id: If7b52ff8cef5bc7d4610384b8362f8a6a420f2ac
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/385937
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
diff --git a/src/sksl/SkSLInliner.cpp b/src/sksl/SkSLInliner.cpp
index b5c0a94..dc714eb 100644
--- a/src/sksl/SkSLInliner.cpp
+++ b/src/sksl/SkSLInliner.cpp
@@ -586,17 +586,17 @@
ExpressionArray& arguments = call->arguments();
const int offset = call->fOffset;
const FunctionDefinition& function = *call->function().definition();
+ const Block& body = function.body()->as<Block>();
const ReturnComplexity returnComplexity = GetReturnComplexity(function);
- InlinedCall inlinedCall;
- StatementArray inlinedBlockStmts;
- inlinedBlockStmts.reserve_back(1 + // Inline marker
- 1 + // Result variable
- arguments.size() + // Function arguments (passing in)
- arguments.size() + // Function arguments (copy out-params back)
- 1); // Block for inlined code
+ StatementArray inlineStatements;
+ int expectedStmtCount = 1 + // Inline marker
+ 1 + // Result variable
+ arguments.size() + // Function argument temp-vars
+ body.children().size(); // Inlined code
- inlinedBlockStmts.push_back(InlineMarker::Make(&call->function()));
+ inlineStatements.reserve_back(expectedStmtCount);
+ inlineStatements.push_back(InlineMarker::Make(&call->function()));
std::unique_ptr<Expression> resultExpr;
if (returnComplexity > ReturnComplexity::kSingleSafeReturn &&
@@ -609,7 +609,7 @@
&function.declaration().returnType(),
symbolTable.get(), Modifiers{},
caller->isBuiltin(), &noInitialValue);
- inlinedBlockStmts.push_back(std::move(var.fVarDecl));
+ inlineStatements.push_back(std::move(var.fVarDecl));
resultExpr = std::make_unique<VariableReference>(/*offset=*/-1, var.fVarSymbol);
}
@@ -621,7 +621,7 @@
const Variable* param = function.declaration().parameters()[i];
if (Analysis::IsTrivialExpression(*arguments[i])) {
// ... and isn't written to within the inline function...
- if (!Analysis::StatementWritesToVariable(*function.body(), *param)) {
+ if (!Analysis::StatementWritesToVariable(body, *param)) {
// ... we don't need to copy it at all! We can just use the existing expression.
varMap[param] = arguments[i]->clone();
continue;
@@ -630,23 +630,22 @@
InlineVariable var = this->makeInlineVariable(param->name(), &arguments[i]->type(),
symbolTable.get(), param->modifiers(),
caller->isBuiltin(), &arguments[i]);
- inlinedBlockStmts.push_back(std::move(var.fVarDecl));
+ inlineStatements.push_back(std::move(var.fVarDecl));
varMap[param] = std::make_unique<VariableReference>(/*offset=*/-1, var.fVarSymbol);
}
- const Block& body = function.body()->as<Block>();
- StatementArray* inlineStatements = &inlinedBlockStmts;
-
- inlineStatements->reserve_back(body.children().size());
for (const std::unique_ptr<Statement>& stmt : body.children()) {
- inlineStatements->push_back(this->inlineStatement(offset, &varMap, symbolTable.get(),
- &resultExpr, returnComplexity, *stmt,
- caller->isBuiltin()));
+ inlineStatements.push_back(this->inlineStatement(offset, &varMap, symbolTable.get(),
+ &resultExpr, returnComplexity, *stmt,
+ caller->isBuiltin()));
}
+ SkASSERT(inlineStatements.count() <= expectedStmtCount);
+
// Wrap all of the generated statements in a block. We need a real Block here, so we can't use
// MakeUnscoped. This is because we need to add another child statement to the Block later.
- inlinedCall.fInlinedBody = Block::Make(offset, std::move(inlinedBlockStmts),
+ InlinedCall inlinedCall;
+ inlinedCall.fInlinedBody = Block::Make(offset, std::move(inlineStatements),
/*symbols=*/nullptr, /*isScope=*/false);
if (resultExpr) {