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>
This commit is contained in:
John Stiles 2021-03-17 15:18:09 -04:00 committed by Skia Commit-Bot
parent 94c6b30728
commit 28257dba55

View File

@ -586,17 +586,17 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
ExpressionArray& arguments = call->arguments(); ExpressionArray& arguments = call->arguments();
const int offset = call->fOffset; const int offset = call->fOffset;
const FunctionDefinition& function = *call->function().definition(); const FunctionDefinition& function = *call->function().definition();
const Block& body = function.body()->as<Block>();
const ReturnComplexity returnComplexity = GetReturnComplexity(function); const ReturnComplexity returnComplexity = GetReturnComplexity(function);
InlinedCall inlinedCall; StatementArray inlineStatements;
StatementArray inlinedBlockStmts; int expectedStmtCount = 1 + // Inline marker
inlinedBlockStmts.reserve_back(1 + // Inline marker 1 + // Result variable
1 + // Result variable arguments.size() + // Function argument temp-vars
arguments.size() + // Function arguments (passing in) body.children().size(); // Inlined code
arguments.size() + // Function arguments (copy out-params back)
1); // Block for 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; std::unique_ptr<Expression> resultExpr;
if (returnComplexity > ReturnComplexity::kSingleSafeReturn && if (returnComplexity > ReturnComplexity::kSingleSafeReturn &&
@ -609,7 +609,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
&function.declaration().returnType(), &function.declaration().returnType(),
symbolTable.get(), Modifiers{}, symbolTable.get(), Modifiers{},
caller->isBuiltin(), &noInitialValue); 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); resultExpr = std::make_unique<VariableReference>(/*offset=*/-1, var.fVarSymbol);
} }
@ -621,7 +621,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
const Variable* param = function.declaration().parameters()[i]; const Variable* param = function.declaration().parameters()[i];
if (Analysis::IsTrivialExpression(*arguments[i])) { if (Analysis::IsTrivialExpression(*arguments[i])) {
// ... and isn't written to within the inline function... // ... 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. // ... we don't need to copy it at all! We can just use the existing expression.
varMap[param] = arguments[i]->clone(); varMap[param] = arguments[i]->clone();
continue; continue;
@ -630,23 +630,22 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
InlineVariable var = this->makeInlineVariable(param->name(), &arguments[i]->type(), InlineVariable var = this->makeInlineVariable(param->name(), &arguments[i]->type(),
symbolTable.get(), param->modifiers(), symbolTable.get(), param->modifiers(),
caller->isBuiltin(), &arguments[i]); 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); 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()) { for (const std::unique_ptr<Statement>& stmt : body.children()) {
inlineStatements->push_back(this->inlineStatement(offset, &varMap, symbolTable.get(), inlineStatements.push_back(this->inlineStatement(offset, &varMap, symbolTable.get(),
&resultExpr, returnComplexity, *stmt, &resultExpr, returnComplexity, *stmt,
caller->isBuiltin())); 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 // 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. // 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); /*symbols=*/nullptr, /*isScope=*/false);
if (resultExpr) { if (resultExpr) {