blob: babd1e8aca8aa03db9a655ae124c5d5a70b6e6af [file] [log] [blame]
John Stiles44e96be2020-08-31 13:16:04 -04001/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "src/sksl/SkSLInliner.h"
9
John Stiles2d7973a2020-10-02 15:01:03 -040010#include <limits.h>
John Stiles44e96be2020-08-31 13:16:04 -040011#include <memory>
12#include <unordered_set>
13
14#include "src/sksl/SkSLAnalysis.h"
15#include "src/sksl/ir/SkSLBinaryExpression.h"
16#include "src/sksl/ir/SkSLBoolLiteral.h"
17#include "src/sksl/ir/SkSLBreakStatement.h"
18#include "src/sksl/ir/SkSLConstructor.h"
19#include "src/sksl/ir/SkSLContinueStatement.h"
20#include "src/sksl/ir/SkSLDiscardStatement.h"
21#include "src/sksl/ir/SkSLDoStatement.h"
22#include "src/sksl/ir/SkSLEnum.h"
23#include "src/sksl/ir/SkSLExpressionStatement.h"
24#include "src/sksl/ir/SkSLExternalFunctionCall.h"
25#include "src/sksl/ir/SkSLExternalValueReference.h"
26#include "src/sksl/ir/SkSLField.h"
27#include "src/sksl/ir/SkSLFieldAccess.h"
28#include "src/sksl/ir/SkSLFloatLiteral.h"
29#include "src/sksl/ir/SkSLForStatement.h"
30#include "src/sksl/ir/SkSLFunctionCall.h"
31#include "src/sksl/ir/SkSLFunctionDeclaration.h"
32#include "src/sksl/ir/SkSLFunctionDefinition.h"
33#include "src/sksl/ir/SkSLFunctionReference.h"
34#include "src/sksl/ir/SkSLIfStatement.h"
35#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040036#include "src/sksl/ir/SkSLInlineMarker.h"
John Stiles44e96be2020-08-31 13:16:04 -040037#include "src/sksl/ir/SkSLIntLiteral.h"
38#include "src/sksl/ir/SkSLInterfaceBlock.h"
39#include "src/sksl/ir/SkSLLayout.h"
40#include "src/sksl/ir/SkSLNop.h"
41#include "src/sksl/ir/SkSLNullLiteral.h"
42#include "src/sksl/ir/SkSLPostfixExpression.h"
43#include "src/sksl/ir/SkSLPrefixExpression.h"
44#include "src/sksl/ir/SkSLReturnStatement.h"
45#include "src/sksl/ir/SkSLSetting.h"
46#include "src/sksl/ir/SkSLSwitchCase.h"
47#include "src/sksl/ir/SkSLSwitchStatement.h"
48#include "src/sksl/ir/SkSLSwizzle.h"
49#include "src/sksl/ir/SkSLTernaryExpression.h"
50#include "src/sksl/ir/SkSLUnresolvedFunction.h"
51#include "src/sksl/ir/SkSLVarDeclarations.h"
John Stiles44e96be2020-08-31 13:16:04 -040052#include "src/sksl/ir/SkSLVariable.h"
53#include "src/sksl/ir/SkSLVariableReference.h"
54#include "src/sksl/ir/SkSLWhileStatement.h"
55
56namespace SkSL {
57namespace {
58
John Stiles44dff4f2020-09-21 12:28:01 -040059static bool contains_returns_above_limit(const FunctionDefinition& funcDef, int limit) {
60 class CountReturnsWithLimit : public ProgramVisitor {
John Stiles44e96be2020-08-31 13:16:04 -040061 public:
John Stiles44dff4f2020-09-21 12:28:01 -040062 CountReturnsWithLimit(const FunctionDefinition& funcDef, int limit) : fLimit(limit) {
John Stiles44e96be2020-08-31 13:16:04 -040063 this->visitProgramElement(funcDef);
64 }
65
66 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -040067 switch (stmt.kind()) {
68 case Statement::Kind::kReturn:
John Stiles44e96be2020-08-31 13:16:04 -040069 ++fNumReturns;
John Stiles44dff4f2020-09-21 12:28:01 -040070 return (fNumReturns > fLimit) || INHERITED::visitStatement(stmt);
John Stiles44e96be2020-08-31 13:16:04 -040071
72 default:
John Stiles93442622020-09-11 12:11:27 -040073 return INHERITED::visitStatement(stmt);
John Stiles44e96be2020-08-31 13:16:04 -040074 }
75 }
76
77 int fNumReturns = 0;
John Stiles44dff4f2020-09-21 12:28:01 -040078 int fLimit = 0;
John Stiles44e96be2020-08-31 13:16:04 -040079 using INHERITED = ProgramVisitor;
80 };
81
John Stiles44dff4f2020-09-21 12:28:01 -040082 return CountReturnsWithLimit{funcDef, limit}.fNumReturns > limit;
John Stiles44e96be2020-08-31 13:16:04 -040083}
84
85static int count_returns_at_end_of_control_flow(const FunctionDefinition& funcDef) {
86 class CountReturnsAtEndOfControlFlow : public ProgramVisitor {
87 public:
88 CountReturnsAtEndOfControlFlow(const FunctionDefinition& funcDef) {
89 this->visitProgramElement(funcDef);
90 }
91
92 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -040093 switch (stmt.kind()) {
94 case Statement::Kind::kBlock: {
John Stiles44e96be2020-08-31 13:16:04 -040095 // Check only the last statement of a block.
Ethan Nicholas7bd60432020-09-25 14:31:59 -040096 const auto& block = stmt.as<Block>();
97 return block.children().size() &&
98 this->visitStatement(*block.children().back());
John Stiles44e96be2020-08-31 13:16:04 -040099 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400100 case Statement::Kind::kSwitch:
101 case Statement::Kind::kWhile:
102 case Statement::Kind::kDo:
103 case Statement::Kind::kFor:
John Stiles44e96be2020-08-31 13:16:04 -0400104 // Don't introspect switches or loop structures at all.
105 return false;
106
Ethan Nicholase6592142020-09-08 10:22:09 -0400107 case Statement::Kind::kReturn:
John Stiles44e96be2020-08-31 13:16:04 -0400108 ++fNumReturns;
109 [[fallthrough]];
110
111 default:
John Stiles93442622020-09-11 12:11:27 -0400112 return INHERITED::visitStatement(stmt);
John Stiles44e96be2020-08-31 13:16:04 -0400113 }
114 }
115
116 int fNumReturns = 0;
117 using INHERITED = ProgramVisitor;
118 };
119
120 return CountReturnsAtEndOfControlFlow{funcDef}.fNumReturns;
121}
122
123static int count_returns_in_breakable_constructs(const FunctionDefinition& funcDef) {
124 class CountReturnsInBreakableConstructs : public ProgramVisitor {
125 public:
126 CountReturnsInBreakableConstructs(const FunctionDefinition& funcDef) {
127 this->visitProgramElement(funcDef);
128 }
129
130 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -0400131 switch (stmt.kind()) {
132 case Statement::Kind::kSwitch:
133 case Statement::Kind::kWhile:
134 case Statement::Kind::kDo:
135 case Statement::Kind::kFor: {
John Stiles44e96be2020-08-31 13:16:04 -0400136 ++fInsideBreakableConstruct;
John Stiles93442622020-09-11 12:11:27 -0400137 bool result = INHERITED::visitStatement(stmt);
John Stiles44e96be2020-08-31 13:16:04 -0400138 --fInsideBreakableConstruct;
139 return result;
140 }
141
Ethan Nicholase6592142020-09-08 10:22:09 -0400142 case Statement::Kind::kReturn:
John Stiles44e96be2020-08-31 13:16:04 -0400143 fNumReturns += (fInsideBreakableConstruct > 0) ? 1 : 0;
144 [[fallthrough]];
145
146 default:
John Stiles93442622020-09-11 12:11:27 -0400147 return INHERITED::visitStatement(stmt);
John Stiles44e96be2020-08-31 13:16:04 -0400148 }
149 }
150
151 int fNumReturns = 0;
152 int fInsideBreakableConstruct = 0;
153 using INHERITED = ProgramVisitor;
154 };
155
156 return CountReturnsInBreakableConstructs{funcDef}.fNumReturns;
157}
158
159static bool has_early_return(const FunctionDefinition& funcDef) {
John Stiles44e96be2020-08-31 13:16:04 -0400160 int returnsAtEndOfControlFlow = count_returns_at_end_of_control_flow(funcDef);
John Stiles44dff4f2020-09-21 12:28:01 -0400161 return contains_returns_above_limit(funcDef, returnsAtEndOfControlFlow);
John Stiles44e96be2020-08-31 13:16:04 -0400162}
163
John Stiles991b09d2020-09-10 13:33:40 -0400164static bool contains_recursive_call(const FunctionDeclaration& funcDecl) {
165 class ContainsRecursiveCall : public ProgramVisitor {
166 public:
167 bool visit(const FunctionDeclaration& funcDecl) {
168 fFuncDecl = &funcDecl;
Ethan Nicholased84b732020-10-08 11:45:44 -0400169 return funcDecl.definition() ? this->visitProgramElement(*funcDecl.definition())
170 : false;
John Stiles991b09d2020-09-10 13:33:40 -0400171 }
172
173 bool visitExpression(const Expression& expr) override {
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400174 if (expr.is<FunctionCall>() && expr.as<FunctionCall>().function().matches(*fFuncDecl)) {
John Stiles991b09d2020-09-10 13:33:40 -0400175 return true;
176 }
177 return INHERITED::visitExpression(expr);
178 }
179
180 bool visitStatement(const Statement& stmt) override {
Ethan Nicholasceb62142020-10-09 16:51:18 -0400181 if (stmt.is<InlineMarker>() &&
182 stmt.as<InlineMarker>().function().matches(*fFuncDecl)) {
John Stiles991b09d2020-09-10 13:33:40 -0400183 return true;
184 }
185 return INHERITED::visitStatement(stmt);
186 }
187
188 const FunctionDeclaration* fFuncDecl;
189 using INHERITED = ProgramVisitor;
190 };
191
192 return ContainsRecursiveCall{}.visit(funcDecl);
193}
194
John Stiles44e96be2020-08-31 13:16:04 -0400195static const Type* copy_if_needed(const Type* src, SymbolTable& symbolTable) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400196 if (src->typeKind() == Type::TypeKind::kArray) {
Ethan Nicholase2c49992020-10-05 11:49:11 -0400197 return symbolTable.takeOwnershipOfSymbol(std::make_unique<Type>(src->name(),
198 src->typeKind(),
199 src->componentType(),
200 src->columns()));
John Stiles44e96be2020-08-31 13:16:04 -0400201 }
202 return src;
203}
204
John Stiles6d696082020-10-01 10:18:54 -0400205static std::unique_ptr<Statement>* find_parent_statement(
206 const std::vector<std::unique_ptr<Statement>*>& stmtStack) {
John Stiles915a38c2020-09-14 09:38:13 -0400207 SkASSERT(!stmtStack.empty());
208
209 // Walk the statement stack from back to front, ignoring the last element (which is the
210 // enclosing statement).
211 auto iter = stmtStack.rbegin();
212 ++iter;
213
214 // Anything counts as a parent statement other than a scopeless Block.
215 for (; iter != stmtStack.rend(); ++iter) {
John Stiles6d696082020-10-01 10:18:54 -0400216 std::unique_ptr<Statement>* stmt = *iter;
217 if (!(*stmt)->is<Block>() || (*stmt)->as<Block>().isScope()) {
John Stiles915a38c2020-09-14 09:38:13 -0400218 return stmt;
219 }
220 }
221
222 // There wasn't any parent statement to be found.
223 return nullptr;
224}
225
John Stilese41b4ee2020-09-28 12:28:16 -0400226std::unique_ptr<Expression> clone_with_ref_kind(const Expression& expr,
227 VariableReference::RefKind refKind) {
228 std::unique_ptr<Expression> clone = expr.clone();
John Stiles70b82422020-09-30 10:55:12 -0400229 class SetRefKindInExpression : public ProgramWriter {
John Stilese41b4ee2020-09-28 12:28:16 -0400230 public:
231 SetRefKindInExpression(VariableReference::RefKind refKind) : fRefKind(refKind) {}
John Stiles70b82422020-09-30 10:55:12 -0400232 bool visitExpression(Expression& expr) override {
John Stilese41b4ee2020-09-28 12:28:16 -0400233 if (expr.is<VariableReference>()) {
John Stiles70b82422020-09-30 10:55:12 -0400234 expr.as<VariableReference>().setRefKind(fRefKind);
John Stilese41b4ee2020-09-28 12:28:16 -0400235 }
236 return INHERITED::visitExpression(expr);
237 }
238
239 private:
240 VariableReference::RefKind fRefKind;
241
John Stiles70b82422020-09-30 10:55:12 -0400242 using INHERITED = ProgramWriter;
John Stilese41b4ee2020-09-28 12:28:16 -0400243 };
244
245 SetRefKindInExpression{refKind}.visitExpression(*clone);
246 return clone;
247}
248
John Stiles44733aa2020-09-29 17:42:23 -0400249bool is_trivial_argument(const Expression& argument) {
250 return argument.is<VariableReference>() ||
251 (argument.is<Swizzle>() && is_trivial_argument(*argument.as<Swizzle>().fBase)) ||
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400252 (argument.is<FieldAccess>() &&
253 is_trivial_argument(*argument.as<FieldAccess>().base())) ||
John Stiles80ccdbd2020-09-30 11:58:16 -0400254 (argument.is<Constructor>() &&
255 argument.as<Constructor>().arguments().size() == 1 &&
256 is_trivial_argument(*argument.as<Constructor>().arguments().front())) ||
John Stiles44733aa2020-09-29 17:42:23 -0400257 (argument.is<IndexExpression>() &&
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400258 argument.as<IndexExpression>().index()->is<IntLiteral>() &&
259 is_trivial_argument(*argument.as<IndexExpression>().base()));
John Stiles44733aa2020-09-29 17:42:23 -0400260}
261
John Stiles44e96be2020-08-31 13:16:04 -0400262} // namespace
263
John Stilesb61ee902020-09-21 12:26:59 -0400264void Inliner::ensureScopedBlocks(Statement* inlinedBody, Statement* parentStmt) {
265 // No changes necessary if this statement isn't actually a block.
266 if (!inlinedBody || !inlinedBody->is<Block>()) {
267 return;
268 }
269
270 // No changes necessary if the parent statement doesn't require a scope.
271 if (!parentStmt || !(parentStmt->is<IfStatement>() || parentStmt->is<ForStatement>() ||
272 parentStmt->is<DoStatement>() || parentStmt->is<WhileStatement>())) {
273 return;
274 }
275
276 Block& block = inlinedBody->as<Block>();
277
278 // The inliner will create inlined function bodies as a Block containing multiple statements,
279 // but no scope. Normally, this is fine, but if this block is used as the statement for a
280 // do/for/if/while, this isn't actually possible to represent textually; a scope must be added
281 // for the generated code to match the intent. In the case of Blocks nested inside other Blocks,
282 // we add the scope to the outermost block if needed. Zero-statement blocks have similar
283 // issues--if we don't represent the Block textually somehow, we run the risk of accidentally
284 // absorbing the following statement into our loop--so we also add a scope to these.
285 for (Block* nestedBlock = &block;; ) {
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400286 if (nestedBlock->isScope()) {
John Stilesb61ee902020-09-21 12:26:59 -0400287 // We found an explicit scope; all is well.
288 return;
289 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400290 if (nestedBlock->children().size() != 1) {
John Stilesb61ee902020-09-21 12:26:59 -0400291 // We found a block with multiple (or zero) statements, but no scope? Let's add a scope
292 // to the outermost block.
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400293 block.setIsScope(true);
John Stilesb61ee902020-09-21 12:26:59 -0400294 return;
295 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400296 if (!nestedBlock->children()[0]->is<Block>()) {
John Stilesb61ee902020-09-21 12:26:59 -0400297 // This block has exactly one thing inside, and it's not another block. No need to scope
298 // it.
299 return;
300 }
301 // We have to go deeper.
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400302 nestedBlock = &nestedBlock->children()[0]->as<Block>();
John Stilesb61ee902020-09-21 12:26:59 -0400303 }
304}
305
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400306void Inliner::reset(const Context* context, ModifiersPool* modifiers,
307 const Program::Settings* settings) {
308 fContext = context;
309 fModifiers = modifiers;
310 fSettings = settings;
John Stiles44e96be2020-08-31 13:16:04 -0400311 fInlineVarCounter = 0;
312}
313
John Stilesc75abb82020-09-14 18:24:12 -0400314String Inliner::uniqueNameForInlineVar(const String& baseName, SymbolTable* symbolTable) {
315 // If the base name starts with an underscore, like "_coords", we can't append another
316 // underscore, because OpenGL disallows two consecutive underscores anywhere in the string. But
317 // in the general case, using the underscore as a splitter reads nicely enough that it's worth
318 // putting in this special case.
319 const char* splitter = baseName.startsWith("_") ? "" : "_";
320
321 // Append a unique numeric prefix to avoid name overlap. Check the symbol table to make sure
322 // we're not reusing an existing name. (Note that within a single compilation pass, this check
323 // isn't fully comprehensive, as code isn't always generated in top-to-bottom order.)
324 String uniqueName;
325 for (;;) {
326 uniqueName = String::printf("_%d%s%s", fInlineVarCounter++, splitter, baseName.c_str());
327 StringFragment frag{uniqueName.data(), uniqueName.length()};
328 if ((*symbolTable)[frag] == nullptr) {
329 break;
330 }
331 }
332
333 return uniqueName;
334}
335
John Stiles44e96be2020-08-31 13:16:04 -0400336std::unique_ptr<Expression> Inliner::inlineExpression(int offset,
337 VariableRewriteMap* varMap,
338 const Expression& expression) {
339 auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> {
340 if (e) {
341 return this->inlineExpression(offset, varMap, *e);
342 }
343 return nullptr;
344 };
345 auto argList = [&](const std::vector<std::unique_ptr<Expression>>& originalArgs)
346 -> std::vector<std::unique_ptr<Expression>> {
347 std::vector<std::unique_ptr<Expression>> args;
348 args.reserve(originalArgs.size());
349 for (const std::unique_ptr<Expression>& arg : originalArgs) {
350 args.push_back(expr(arg));
351 }
352 return args;
353 };
354
Ethan Nicholase6592142020-09-08 10:22:09 -0400355 switch (expression.kind()) {
356 case Expression::Kind::kBinary: {
John Stiles44e96be2020-08-31 13:16:04 -0400357 const BinaryExpression& b = expression.as<BinaryExpression>();
358 return std::make_unique<BinaryExpression>(offset,
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400359 expr(b.leftPointer()),
360 b.getOperator(),
361 expr(b.rightPointer()),
Ethan Nicholas30d30222020-09-11 12:27:26 -0400362 &b.type());
John Stiles44e96be2020-08-31 13:16:04 -0400363 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400364 case Expression::Kind::kBoolLiteral:
365 case Expression::Kind::kIntLiteral:
366 case Expression::Kind::kFloatLiteral:
367 case Expression::Kind::kNullLiteral:
John Stiles44e96be2020-08-31 13:16:04 -0400368 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400369 case Expression::Kind::kConstructor: {
John Stiles44e96be2020-08-31 13:16:04 -0400370 const Constructor& constructor = expression.as<Constructor>();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400371 return std::make_unique<Constructor>(offset, &constructor.type(),
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400372 argList(constructor.arguments()));
John Stiles44e96be2020-08-31 13:16:04 -0400373 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400374 case Expression::Kind::kExternalFunctionCall: {
John Stiles44e96be2020-08-31 13:16:04 -0400375 const ExternalFunctionCall& externalCall = expression.as<ExternalFunctionCall>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400376 return std::make_unique<ExternalFunctionCall>(offset, &externalCall.function(),
Ethan Nicholas6e86ec92020-09-30 14:29:56 -0400377 argList(externalCall.arguments()));
John Stiles44e96be2020-08-31 13:16:04 -0400378 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400379 case Expression::Kind::kExternalValue:
John Stiles44e96be2020-08-31 13:16:04 -0400380 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400381 case Expression::Kind::kFieldAccess: {
John Stiles44e96be2020-08-31 13:16:04 -0400382 const FieldAccess& f = expression.as<FieldAccess>();
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400383 return std::make_unique<FieldAccess>(expr(f.base()), f.fieldIndex(), f.ownerKind());
John Stiles44e96be2020-08-31 13:16:04 -0400384 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400385 case Expression::Kind::kFunctionCall: {
John Stiles44e96be2020-08-31 13:16:04 -0400386 const FunctionCall& funcCall = expression.as<FunctionCall>();
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400387 return std::make_unique<FunctionCall>(offset, &funcCall.type(), &funcCall.function(),
388 argList(funcCall.arguments()));
John Stiles44e96be2020-08-31 13:16:04 -0400389 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400390 case Expression::Kind::kFunctionReference:
Brian Osman2b3b35f2020-09-08 09:17:36 -0400391 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400392 case Expression::Kind::kIndex: {
John Stiles44e96be2020-08-31 13:16:04 -0400393 const IndexExpression& idx = expression.as<IndexExpression>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400394 return std::make_unique<IndexExpression>(*fContext, expr(idx.base()),
395 expr(idx.index()));
John Stiles44e96be2020-08-31 13:16:04 -0400396 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400397 case Expression::Kind::kPrefix: {
John Stiles44e96be2020-08-31 13:16:04 -0400398 const PrefixExpression& p = expression.as<PrefixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400399 return std::make_unique<PrefixExpression>(p.getOperator(), expr(p.operand()));
John Stiles44e96be2020-08-31 13:16:04 -0400400 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400401 case Expression::Kind::kPostfix: {
John Stiles44e96be2020-08-31 13:16:04 -0400402 const PostfixExpression& p = expression.as<PostfixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400403 return std::make_unique<PostfixExpression>(expr(p.operand()), p.getOperator());
John Stiles44e96be2020-08-31 13:16:04 -0400404 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400405 case Expression::Kind::kSetting:
John Stiles44e96be2020-08-31 13:16:04 -0400406 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400407 case Expression::Kind::kSwizzle: {
John Stiles44e96be2020-08-31 13:16:04 -0400408 const Swizzle& s = expression.as<Swizzle>();
409 return std::make_unique<Swizzle>(*fContext, expr(s.fBase), s.fComponents);
410 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400411 case Expression::Kind::kTernary: {
John Stiles44e96be2020-08-31 13:16:04 -0400412 const TernaryExpression& t = expression.as<TernaryExpression>();
Ethan Nicholasdd218162020-10-08 05:48:01 -0400413 return std::make_unique<TernaryExpression>(offset, expr(t.test()),
414 expr(t.ifTrue()), expr(t.ifFalse()));
John Stiles44e96be2020-08-31 13:16:04 -0400415 }
Brian Osman83ba9302020-09-11 13:33:46 -0400416 case Expression::Kind::kTypeReference:
417 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400418 case Expression::Kind::kVariableReference: {
John Stiles44e96be2020-08-31 13:16:04 -0400419 const VariableReference& v = expression.as<VariableReference>();
Ethan Nicholas78686922020-10-08 06:46:27 -0400420 auto varMapIter = varMap->find(v.variable());
John Stilese41b4ee2020-09-28 12:28:16 -0400421 if (varMapIter != varMap->end()) {
Ethan Nicholas78686922020-10-08 06:46:27 -0400422 return clone_with_ref_kind(*varMapIter->second, v.refKind());
John Stiles44e96be2020-08-31 13:16:04 -0400423 }
424 return v.clone();
425 }
426 default:
427 SkASSERT(false);
428 return nullptr;
429 }
430}
431
432std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
433 VariableRewriteMap* varMap,
434 SymbolTable* symbolTableForStatement,
John Stilese41b4ee2020-09-28 12:28:16 -0400435 const Expression* resultExpr,
John Stiles44e96be2020-08-31 13:16:04 -0400436 bool haveEarlyReturns,
Brian Osman3887a012020-09-30 13:22:27 -0400437 const Statement& statement,
438 bool isBuiltinCode) {
John Stiles44e96be2020-08-31 13:16:04 -0400439 auto stmt = [&](const std::unique_ptr<Statement>& s) -> std::unique_ptr<Statement> {
440 if (s) {
John Stilesa5f3c312020-09-22 12:05:16 -0400441 return this->inlineStatement(offset, varMap, symbolTableForStatement, resultExpr,
Brian Osman3887a012020-09-30 13:22:27 -0400442 haveEarlyReturns, *s, isBuiltinCode);
John Stiles44e96be2020-08-31 13:16:04 -0400443 }
444 return nullptr;
445 };
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400446 auto blockStmts = [&](const Block& block) {
447 std::vector<std::unique_ptr<Statement>> result;
448 for (const std::unique_ptr<Statement>& child : block.children()) {
449 result.push_back(stmt(child));
450 }
451 return result;
452 };
John Stiles44e96be2020-08-31 13:16:04 -0400453 auto stmts = [&](const std::vector<std::unique_ptr<Statement>>& ss) {
454 std::vector<std::unique_ptr<Statement>> result;
455 for (const auto& s : ss) {
456 result.push_back(stmt(s));
457 }
458 return result;
459 };
460 auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> {
461 if (e) {
462 return this->inlineExpression(offset, varMap, *e);
463 }
464 return nullptr;
465 };
Ethan Nicholase6592142020-09-08 10:22:09 -0400466 switch (statement.kind()) {
467 case Statement::Kind::kBlock: {
John Stiles44e96be2020-08-31 13:16:04 -0400468 const Block& b = statement.as<Block>();
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400469 return std::make_unique<Block>(offset, blockStmts(b), b.symbolTable(), b.isScope());
John Stiles44e96be2020-08-31 13:16:04 -0400470 }
471
Ethan Nicholase6592142020-09-08 10:22:09 -0400472 case Statement::Kind::kBreak:
473 case Statement::Kind::kContinue:
474 case Statement::Kind::kDiscard:
John Stiles44e96be2020-08-31 13:16:04 -0400475 return statement.clone();
476
Ethan Nicholase6592142020-09-08 10:22:09 -0400477 case Statement::Kind::kDo: {
John Stiles44e96be2020-08-31 13:16:04 -0400478 const DoStatement& d = statement.as<DoStatement>();
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400479 return std::make_unique<DoStatement>(offset, stmt(d.statement()), expr(d.test()));
John Stiles44e96be2020-08-31 13:16:04 -0400480 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400481 case Statement::Kind::kExpression: {
John Stiles44e96be2020-08-31 13:16:04 -0400482 const ExpressionStatement& e = statement.as<ExpressionStatement>();
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400483 return std::make_unique<ExpressionStatement>(expr(e.expression()));
John Stiles44e96be2020-08-31 13:16:04 -0400484 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400485 case Statement::Kind::kFor: {
John Stiles44e96be2020-08-31 13:16:04 -0400486 const ForStatement& f = statement.as<ForStatement>();
487 // need to ensure initializer is evaluated first so that we've already remapped its
488 // declarations by the time we evaluate test & next
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400489 std::unique_ptr<Statement> initializer = stmt(f.initializer());
490 return std::make_unique<ForStatement>(offset, std::move(initializer), expr(f.test()),
491 expr(f.next()), stmt(f.statement()), f.symbols());
John Stiles44e96be2020-08-31 13:16:04 -0400492 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400493 case Statement::Kind::kIf: {
John Stiles44e96be2020-08-31 13:16:04 -0400494 const IfStatement& i = statement.as<IfStatement>();
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400495 return std::make_unique<IfStatement>(offset, i.isStatic(), expr(i.test()),
496 stmt(i.ifTrue()), stmt(i.ifFalse()));
John Stiles44e96be2020-08-31 13:16:04 -0400497 }
John Stiles98c1f822020-09-09 14:18:53 -0400498 case Statement::Kind::kInlineMarker:
Ethan Nicholase6592142020-09-08 10:22:09 -0400499 case Statement::Kind::kNop:
John Stiles44e96be2020-08-31 13:16:04 -0400500 return statement.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400501 case Statement::Kind::kReturn: {
John Stiles44e96be2020-08-31 13:16:04 -0400502 const ReturnStatement& r = statement.as<ReturnStatement>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400503 if (r.expression()) {
John Stilese41b4ee2020-09-28 12:28:16 -0400504 SkASSERT(resultExpr);
John Stilesa5f3c312020-09-22 12:05:16 -0400505 auto assignment =
506 std::make_unique<ExpressionStatement>(std::make_unique<BinaryExpression>(
507 offset,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400508 clone_with_ref_kind(*resultExpr,
509 VariableReference::RefKind::kWrite),
John Stilesa5f3c312020-09-22 12:05:16 -0400510 Token::Kind::TK_EQ,
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400511 expr(r.expression()),
John Stilese41b4ee2020-09-28 12:28:16 -0400512 &resultExpr->type()));
John Stiles44e96be2020-08-31 13:16:04 -0400513 if (haveEarlyReturns) {
514 std::vector<std::unique_ptr<Statement>> block;
515 block.push_back(std::move(assignment));
516 block.emplace_back(new BreakStatement(offset));
517 return std::make_unique<Block>(offset, std::move(block), /*symbols=*/nullptr,
518 /*isScope=*/true);
519 } else {
520 return std::move(assignment);
521 }
522 } else {
523 if (haveEarlyReturns) {
524 return std::make_unique<BreakStatement>(offset);
525 } else {
526 return std::make_unique<Nop>();
527 }
528 }
529 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400530 case Statement::Kind::kSwitch: {
John Stiles44e96be2020-08-31 13:16:04 -0400531 const SwitchStatement& ss = statement.as<SwitchStatement>();
532 std::vector<std::unique_ptr<SwitchCase>> cases;
533 for (const auto& sc : ss.fCases) {
534 cases.emplace_back(new SwitchCase(offset, expr(sc->fValue),
535 stmts(sc->fStatements)));
536 }
537 return std::make_unique<SwitchStatement>(offset, ss.fIsStatic, expr(ss.fValue),
538 std::move(cases), ss.fSymbols);
539 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400540 case Statement::Kind::kVarDeclaration: {
John Stiles44e96be2020-08-31 13:16:04 -0400541 const VarDeclaration& decl = statement.as<VarDeclaration>();
542 std::vector<std::unique_ptr<Expression>> sizes;
543 for (const auto& size : decl.fSizes) {
544 sizes.push_back(expr(size));
545 }
546 std::unique_ptr<Expression> initialValue = expr(decl.fValue);
547 const Variable* old = decl.fVar;
John Stilesc75abb82020-09-14 18:24:12 -0400548 // We assign unique names to inlined variables--scopes hide most of the problems in this
549 // regard, but see `InlinerAvoidsVariableNameOverlap` for a counterexample where unique
550 // names are important.
551 auto name = std::make_unique<String>(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400552 this->uniqueNameForInlineVar(String(old->name()), symbolTableForStatement));
John Stiles44e96be2020-08-31 13:16:04 -0400553 const String* namePtr = symbolTableForStatement->takeOwnershipOfString(std::move(name));
Brian Osmanc0213602020-10-06 14:43:32 -0400554 const Type* baseTypePtr = copy_if_needed(&decl.fBaseType, *symbolTableForStatement);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400555 const Type* typePtr = copy_if_needed(&old->type(), *symbolTableForStatement);
John Stiles44e96be2020-08-31 13:16:04 -0400556 const Variable* clone = symbolTableForStatement->takeOwnershipOfSymbol(
557 std::make_unique<Variable>(offset,
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400558 old->modifiersHandle(),
John Stiles44e96be2020-08-31 13:16:04 -0400559 namePtr->c_str(),
Ethan Nicholas30d30222020-09-11 12:27:26 -0400560 typePtr,
Brian Osman3887a012020-09-30 13:22:27 -0400561 isBuiltinCode,
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400562 old->storage(),
John Stiles44e96be2020-08-31 13:16:04 -0400563 initialValue.get()));
John Stilese41b4ee2020-09-28 12:28:16 -0400564 (*varMap)[old] = std::make_unique<VariableReference>(offset, clone);
Brian Osmanc0213602020-10-06 14:43:32 -0400565 return std::make_unique<VarDeclaration>(clone, baseTypePtr, std::move(sizes),
John Stiles44e96be2020-08-31 13:16:04 -0400566 std::move(initialValue));
567 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400568 case Statement::Kind::kWhile: {
John Stiles44e96be2020-08-31 13:16:04 -0400569 const WhileStatement& w = statement.as<WhileStatement>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400570 return std::make_unique<WhileStatement>(offset, expr(w.test()), stmt(w.statement()));
John Stiles44e96be2020-08-31 13:16:04 -0400571 }
572 default:
573 SkASSERT(false);
574 return nullptr;
575 }
576}
577
John Stiles6eadf132020-09-08 10:16:10 -0400578Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
Brian Osman3887a012020-09-30 13:22:27 -0400579 SymbolTable* symbolTableForCall,
580 const FunctionDeclaration* caller) {
John Stiles44e96be2020-08-31 13:16:04 -0400581 // Inlining is more complicated here than in a typical compiler, because we have to have a
582 // high-level IR and can't just drop statements into the middle of an expression or even use
583 // gotos.
584 //
585 // Since we can't insert statements into an expression, we run the inline function as extra
586 // statements before the statement we're currently processing, relying on a lack of execution
587 // order guarantees. Since we can't use gotos (which are normally used to replace return
588 // statements), we wrap the whole function in a loop and use break statements to jump to the
589 // end.
590 SkASSERT(fSettings);
591 SkASSERT(fContext);
592 SkASSERT(call);
Ethan Nicholased84b732020-10-08 11:45:44 -0400593 SkASSERT(this->isSafeToInline(call->function().definition()));
John Stiles44e96be2020-08-31 13:16:04 -0400594
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400595 std::vector<std::unique_ptr<Expression>>& arguments = call->arguments();
John Stiles6eadf132020-09-08 10:16:10 -0400596 const int offset = call->fOffset;
Ethan Nicholased84b732020-10-08 11:45:44 -0400597 const FunctionDefinition& function = *call->function().definition();
John Stiles6eadf132020-09-08 10:16:10 -0400598 const bool hasEarlyReturn = has_early_return(function);
599
John Stiles44e96be2020-08-31 13:16:04 -0400600 InlinedCall inlinedCall;
John Stiles6eadf132020-09-08 10:16:10 -0400601 inlinedCall.fInlinedBody = std::make_unique<Block>(offset,
602 std::vector<std::unique_ptr<Statement>>{},
603 /*symbols=*/nullptr,
604 /*isScope=*/false);
John Stiles98c1f822020-09-09 14:18:53 -0400605
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400606 Block& inlinedBody = *inlinedCall.fInlinedBody;
607 inlinedBody.children().reserve(1 + // Inline marker
608 1 + // Result variable
609 arguments.size() + // Function arguments (passing in)
John Stilese41b4ee2020-09-28 12:28:16 -0400610 arguments.size() + // Function arguments (copy out-params back)
611 1); // Inlined code (Block or do-while loop)
John Stiles98c1f822020-09-09 14:18:53 -0400612
Ethan Nicholasceb62142020-10-09 16:51:18 -0400613 inlinedBody.children().push_back(std::make_unique<InlineMarker>(&call->function()));
John Stiles44e96be2020-08-31 13:16:04 -0400614
John Stilese41b4ee2020-09-28 12:28:16 -0400615 auto makeInlineVar =
616 [&](const String& baseName, const Type* type, Modifiers modifiers,
617 std::unique_ptr<Expression>* initialValue) -> std::unique_ptr<Expression> {
John Stilesa003e812020-09-11 09:43:49 -0400618 // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so
619 // replace them if they ever appear here. If this happens, we likely forgot to coerce a type
620 // somewhere during compilation.
621 if (type == fContext->fFloatLiteral_Type.get()) {
John Stilesd2be5c52020-09-11 14:58:06 -0400622 SkDEBUGFAIL("found a $floatLiteral type while inlining");
John Stilesa003e812020-09-11 09:43:49 -0400623 type = fContext->fFloat_Type.get();
624 } else if (type == fContext->fIntLiteral_Type.get()) {
John Stilesd2be5c52020-09-11 14:58:06 -0400625 SkDEBUGFAIL("found an $intLiteral type while inlining");
John Stilesa003e812020-09-11 09:43:49 -0400626 type = fContext->fInt_Type.get();
627 }
628
John Stilesc75abb82020-09-14 18:24:12 -0400629 // Provide our new variable with a unique name, and add it to our symbol table.
630 String uniqueName = this->uniqueNameForInlineVar(baseName, symbolTableForCall);
John Stilescf936f92020-08-31 17:18:45 -0400631 const String* namePtr = symbolTableForCall->takeOwnershipOfString(
632 std::make_unique<String>(std::move(uniqueName)));
John Stiles44e96be2020-08-31 13:16:04 -0400633 StringFragment nameFrag{namePtr->c_str(), namePtr->length()};
634
635 // Add our new variable to the symbol table.
John Stilesb8cc6652020-10-08 09:12:07 -0400636 const Variable* variableSymbol = symbolTableForCall->add(std::make_unique<Variable>(
637 /*offset=*/-1, fModifiers->handle(Modifiers()),
Ethan Nicholased84b732020-10-08 11:45:44 -0400638 nameFrag, type, caller->isBuiltin(),
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400639 Variable::Storage::kLocal, initialValue->get()));
John Stiles44e96be2020-08-31 13:16:04 -0400640
641 // Prepare the variable declaration (taking extra care with `out` params to not clobber any
642 // initial value).
Brian Osmanc0213602020-10-06 14:43:32 -0400643 std::unique_ptr<Statement> variable;
John Stiles44e96be2020-08-31 13:16:04 -0400644 if (initialValue && (modifiers.fFlags & Modifiers::kOut_Flag)) {
Brian Osmanc0213602020-10-06 14:43:32 -0400645 variable = std::make_unique<VarDeclaration>(
646 variableSymbol, type, /*sizes=*/std::vector<std::unique_ptr<Expression>>{},
647 (*initialValue)->clone());
John Stiles44e96be2020-08-31 13:16:04 -0400648 } else {
Brian Osmanc0213602020-10-06 14:43:32 -0400649 variable = std::make_unique<VarDeclaration>(
650 variableSymbol, type, /*sizes=*/std::vector<std::unique_ptr<Expression>>{},
651 std::move(*initialValue));
John Stiles44e96be2020-08-31 13:16:04 -0400652 }
653
654 // Add the new variable-declaration statement to our block of extra statements.
Brian Osmanc0213602020-10-06 14:43:32 -0400655 inlinedBody.children().push_back(std::move(variable));
John Stiles44e96be2020-08-31 13:16:04 -0400656
John Stilese41b4ee2020-09-28 12:28:16 -0400657 return std::make_unique<VariableReference>(offset, variableSymbol);
John Stiles44e96be2020-08-31 13:16:04 -0400658 };
659
660 // Create a variable to hold the result in the extra statements (excepting void).
John Stilese41b4ee2020-09-28 12:28:16 -0400661 std::unique_ptr<Expression> resultExpr;
Ethan Nicholased84b732020-10-08 11:45:44 -0400662 if (function.fDeclaration.returnType() != *fContext->fVoid_Type) {
John Stiles44e96be2020-08-31 13:16:04 -0400663 std::unique_ptr<Expression> noInitialValue;
Ethan Nicholase2c49992020-10-05 11:49:11 -0400664 resultExpr = makeInlineVar(String(function.fDeclaration.name()),
Ethan Nicholased84b732020-10-08 11:45:44 -0400665 &function.fDeclaration.returnType(),
John Stilese41b4ee2020-09-28 12:28:16 -0400666 Modifiers{}, &noInitialValue);
667 }
John Stiles44e96be2020-08-31 13:16:04 -0400668
669 // Create variables in the extra statements to hold the arguments, and assign the arguments to
670 // them.
671 VariableRewriteMap varMap;
John Stilese41b4ee2020-09-28 12:28:16 -0400672 std::vector<int> argsToCopyBack;
John Stiles44e96be2020-08-31 13:16:04 -0400673 for (int i = 0; i < (int) arguments.size(); ++i) {
Ethan Nicholased84b732020-10-08 11:45:44 -0400674 const Variable* param = function.fDeclaration.parameters()[i];
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400675 bool isOutParam = param->modifiers().fFlags & Modifiers::kOut_Flag;
John Stiles44e96be2020-08-31 13:16:04 -0400676
John Stiles44733aa2020-09-29 17:42:23 -0400677 // If this argument can be inlined trivially (e.g. a swizzle, or a constant array index)...
678 if (is_trivial_argument(*arguments[i])) {
John Stilese41b4ee2020-09-28 12:28:16 -0400679 // ... and it's an `out` param, or it isn't written to within the inline function...
680 if (isOutParam || !Analysis::StatementWritesToVariable(*function.fBody, *param)) {
John Stilesf201af82020-09-29 16:57:55 -0400681 // ... we don't need to copy it at all! We can just use the existing expression.
682 varMap[param] = arguments[i]->clone();
John Stiles44e96be2020-08-31 13:16:04 -0400683 continue;
684 }
685 }
686
John Stilese41b4ee2020-09-28 12:28:16 -0400687 if (isOutParam) {
688 argsToCopyBack.push_back(i);
689 }
690
Ethan Nicholase2c49992020-10-05 11:49:11 -0400691 varMap[param] = makeInlineVar(String(param->name()), &arguments[i]->type(),
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400692 param->modifiers(), &arguments[i]);
John Stiles44e96be2020-08-31 13:16:04 -0400693 }
694
695 const Block& body = function.fBody->as<Block>();
John Stiles44e96be2020-08-31 13:16:04 -0400696 auto inlineBlock = std::make_unique<Block>(offset, std::vector<std::unique_ptr<Statement>>{});
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400697 inlineBlock->children().reserve(body.children().size());
698 for (const std::unique_ptr<Statement>& stmt : body.children()) {
Brian Osman3887a012020-09-30 13:22:27 -0400699 inlineBlock->children().push_back(this->inlineStatement(offset, &varMap, symbolTableForCall,
700 resultExpr.get(), hasEarlyReturn,
Ethan Nicholased84b732020-10-08 11:45:44 -0400701 *stmt, caller->isBuiltin()));
John Stiles44e96be2020-08-31 13:16:04 -0400702 }
703 if (hasEarlyReturn) {
704 // Since we output to backends that don't have a goto statement (which would normally be
705 // used to perform an early return), we fake it by wrapping the function in a
706 // do { } while (false); and then use break statements to jump to the end in order to
707 // emulate a goto.
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400708 inlinedBody.children().push_back(std::make_unique<DoStatement>(
John Stiles44e96be2020-08-31 13:16:04 -0400709 /*offset=*/-1,
710 std::move(inlineBlock),
711 std::make_unique<BoolLiteral>(*fContext, offset, /*value=*/false)));
712 } else {
John Stiles6eadf132020-09-08 10:16:10 -0400713 // No early returns, so we can just dump the code in. We still need to keep the block so we
714 // don't get name conflicts with locals.
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400715 inlinedBody.children().push_back(std::move(inlineBlock));
John Stiles44e96be2020-08-31 13:16:04 -0400716 }
717
John Stilese41b4ee2020-09-28 12:28:16 -0400718 // Copy back the values of `out` parameters into their real destinations.
719 for (int i : argsToCopyBack) {
Ethan Nicholased84b732020-10-08 11:45:44 -0400720 const Variable* p = function.fDeclaration.parameters()[i];
John Stilese41b4ee2020-09-28 12:28:16 -0400721 SkASSERT(varMap.find(p) != varMap.end());
722 inlinedBody.children().push_back(
723 std::make_unique<ExpressionStatement>(std::make_unique<BinaryExpression>(
724 offset,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400725 clone_with_ref_kind(*arguments[i], VariableReference::RefKind::kWrite),
John Stilese41b4ee2020-09-28 12:28:16 -0400726 Token::Kind::TK_EQ,
727 std::move(varMap[p]),
728 &arguments[i]->type())));
John Stiles44e96be2020-08-31 13:16:04 -0400729 }
730
John Stilese41b4ee2020-09-28 12:28:16 -0400731 if (resultExpr != nullptr) {
732 // Return our result variable as our replacement expression.
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400733 SkASSERT(resultExpr->as<VariableReference>().refKind() ==
734 VariableReference::RefKind::kRead);
John Stilese41b4ee2020-09-28 12:28:16 -0400735 inlinedCall.fReplacementExpr = std::move(resultExpr);
John Stiles44e96be2020-08-31 13:16:04 -0400736 } else {
737 // It's a void function, so it doesn't actually result in anything, but we have to return
738 // something non-null as a standin.
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400739 inlinedCall.fReplacementExpr = std::make_unique<BoolLiteral>(*fContext,
740 offset,
John Stiles44e96be2020-08-31 13:16:04 -0400741 /*value=*/false);
742 }
743
John Stiles44e96be2020-08-31 13:16:04 -0400744 return inlinedCall;
745}
746
John Stiles2d7973a2020-10-02 15:01:03 -0400747bool Inliner::isSafeToInline(const FunctionDefinition* functionDef) {
John Stiles44e96be2020-08-31 13:16:04 -0400748 SkASSERT(fSettings);
749
John Stiles2d7973a2020-10-02 15:01:03 -0400750 if (functionDef == nullptr) {
John Stiles44e96be2020-08-31 13:16:04 -0400751 // Can't inline something if we don't actually have its definition.
752 return false;
753 }
John Stiles2d7973a2020-10-02 15:01:03 -0400754
John Stiles44e96be2020-08-31 13:16:04 -0400755 if (!fSettings->fCaps || !fSettings->fCaps->canUseDoLoops()) {
756 // We don't have do-while loops. We use do-while loops to simulate early returns, so we
757 // can't inline functions that have an early return.
John Stiles2d7973a2020-10-02 15:01:03 -0400758 bool hasEarlyReturn = has_early_return(*functionDef);
John Stiles44e96be2020-08-31 13:16:04 -0400759
760 // If we didn't detect an early return, there shouldn't be any returns in breakable
761 // constructs either.
John Stiles2d7973a2020-10-02 15:01:03 -0400762 SkASSERT(hasEarlyReturn || count_returns_in_breakable_constructs(*functionDef) == 0);
John Stiles44e96be2020-08-31 13:16:04 -0400763 return !hasEarlyReturn;
764 }
765 // We have do-while loops, but we don't have any mechanism to simulate early returns within a
766 // breakable construct (switch/for/do/while), so we can't inline if there's a return inside one.
John Stiles2d7973a2020-10-02 15:01:03 -0400767 bool hasReturnInBreakableConstruct = (count_returns_in_breakable_constructs(*functionDef) > 0);
John Stiles44e96be2020-08-31 13:16:04 -0400768
769 // If we detected returns in breakable constructs, we should also detect an early return.
John Stiles2d7973a2020-10-02 15:01:03 -0400770 SkASSERT(!hasReturnInBreakableConstruct || has_early_return(*functionDef));
John Stiles44e96be2020-08-31 13:16:04 -0400771 return !hasReturnInBreakableConstruct;
772}
773
John Stiles2d7973a2020-10-02 15:01:03 -0400774// A candidate function for inlining, containing everything that `inlineCall` needs.
775struct InlineCandidate {
776 SymbolTable* fSymbols; // the SymbolTable of the candidate
777 std::unique_ptr<Statement>* fParentStmt; // the parent Statement of the enclosing stmt
778 std::unique_ptr<Statement>* fEnclosingStmt; // the Statement containing the candidate
779 std::unique_ptr<Expression>* fCandidateExpr; // the candidate FunctionCall to be inlined
780 FunctionDefinition* fEnclosingFunction; // the Function containing the candidate
781 bool fIsLargeFunction; // does candidate exceed the inline threshold?
782};
John Stiles93442622020-09-11 12:11:27 -0400783
John Stiles2d7973a2020-10-02 15:01:03 -0400784struct InlineCandidateList {
785 std::vector<InlineCandidate> fCandidates;
786};
787
788class InlineCandidateAnalyzer {
John Stiles70957c82020-10-02 16:42:10 -0400789public:
790 // A list of all the inlining candidates we found during analysis.
791 InlineCandidateList* fCandidateList;
John Stiles2d7973a2020-10-02 15:01:03 -0400792
John Stiles70957c82020-10-02 16:42:10 -0400793 // A stack of the symbol tables; since most nodes don't have one, expected to be shallower than
794 // the enclosing-statement stack.
795 std::vector<SymbolTable*> fSymbolTableStack;
796 // A stack of "enclosing" statements--these would be suitable for the inliner to use for adding
797 // new instructions. Not all statements are suitable (e.g. a for-loop's initializer). The
798 // inliner might replace a statement with a block containing the statement.
799 std::vector<std::unique_ptr<Statement>*> fEnclosingStmtStack;
800 // The function that we're currently processing (i.e. inlining into).
801 FunctionDefinition* fEnclosingFunction = nullptr;
John Stiles93442622020-09-11 12:11:27 -0400802
John Stiles70957c82020-10-02 16:42:10 -0400803 void visit(Program& program, InlineCandidateList* candidateList) {
804 fCandidateList = candidateList;
805 fSymbolTableStack.push_back(program.fSymbols.get());
John Stiles93442622020-09-11 12:11:27 -0400806
Brian Osman1179fcf2020-10-08 16:04:40 -0400807 for (const auto& pe : program.elements()) {
808 this->visitProgramElement(pe.get());
John Stiles93442622020-09-11 12:11:27 -0400809 }
810
John Stiles70957c82020-10-02 16:42:10 -0400811 fSymbolTableStack.pop_back();
812 fCandidateList = nullptr;
813 }
814
815 void visitProgramElement(ProgramElement* pe) {
816 switch (pe->kind()) {
817 case ProgramElement::Kind::kFunction: {
818 FunctionDefinition& funcDef = pe->as<FunctionDefinition>();
819 fEnclosingFunction = &funcDef;
820 this->visitStatement(&funcDef.fBody);
821 break;
John Stiles93442622020-09-11 12:11:27 -0400822 }
John Stiles70957c82020-10-02 16:42:10 -0400823 default:
824 // The inliner can't operate outside of a function's scope.
825 break;
826 }
827 }
828
829 void visitStatement(std::unique_ptr<Statement>* stmt,
830 bool isViableAsEnclosingStatement = true) {
831 if (!*stmt) {
832 return;
John Stiles93442622020-09-11 12:11:27 -0400833 }
834
John Stiles70957c82020-10-02 16:42:10 -0400835 size_t oldEnclosingStmtStackSize = fEnclosingStmtStack.size();
836 size_t oldSymbolStackSize = fSymbolTableStack.size();
John Stiles93442622020-09-11 12:11:27 -0400837
John Stiles70957c82020-10-02 16:42:10 -0400838 if (isViableAsEnclosingStatement) {
839 fEnclosingStmtStack.push_back(stmt);
John Stiles93442622020-09-11 12:11:27 -0400840 }
841
John Stiles70957c82020-10-02 16:42:10 -0400842 switch ((*stmt)->kind()) {
843 case Statement::Kind::kBreak:
844 case Statement::Kind::kContinue:
845 case Statement::Kind::kDiscard:
846 case Statement::Kind::kInlineMarker:
847 case Statement::Kind::kNop:
848 break;
849
850 case Statement::Kind::kBlock: {
851 Block& block = (*stmt)->as<Block>();
852 if (block.symbolTable()) {
853 fSymbolTableStack.push_back(block.symbolTable().get());
854 }
855
856 for (std::unique_ptr<Statement>& stmt : block.children()) {
857 this->visitStatement(&stmt);
858 }
859 break;
John Stiles93442622020-09-11 12:11:27 -0400860 }
John Stiles70957c82020-10-02 16:42:10 -0400861 case Statement::Kind::kDo: {
862 DoStatement& doStmt = (*stmt)->as<DoStatement>();
863 // The loop body is a candidate for inlining.
864 this->visitStatement(&doStmt.statement());
865 // The inliner isn't smart enough to inline the test-expression for a do-while
866 // loop at this time. There are two limitations:
867 // - We would need to insert the inlined-body block at the very end of the do-
868 // statement's inner fStatement. We don't support that today, but it's doable.
869 // - We cannot inline the test expression if the loop uses `continue` anywhere; that
870 // would skip over the inlined block that evaluates the test expression. There
871 // isn't a good fix for this--any workaround would be more complex than the cost
872 // of a function call. However, loops that don't use `continue` would still be
873 // viable candidates for inlining.
874 break;
John Stiles93442622020-09-11 12:11:27 -0400875 }
John Stiles70957c82020-10-02 16:42:10 -0400876 case Statement::Kind::kExpression: {
877 ExpressionStatement& expr = (*stmt)->as<ExpressionStatement>();
878 this->visitExpression(&expr.expression());
879 break;
880 }
881 case Statement::Kind::kFor: {
882 ForStatement& forStmt = (*stmt)->as<ForStatement>();
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400883 if (forStmt.symbols()) {
884 fSymbolTableStack.push_back(forStmt.symbols().get());
John Stiles70957c82020-10-02 16:42:10 -0400885 }
886
887 // The initializer and loop body are candidates for inlining.
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400888 this->visitStatement(&forStmt.initializer(),
John Stiles70957c82020-10-02 16:42:10 -0400889 /*isViableAsEnclosingStatement=*/false);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400890 this->visitStatement(&forStmt.statement());
John Stiles70957c82020-10-02 16:42:10 -0400891
892 // The inliner isn't smart enough to inline the test- or increment-expressions
893 // of a for loop loop at this time. There are a handful of limitations:
894 // - We would need to insert the test-expression block at the very beginning of the
895 // for-loop's inner fStatement, and the increment-expression block at the very
896 // end. We don't support that today, but it's doable.
897 // - The for-loop's built-in test-expression would need to be dropped entirely,
898 // and the loop would be halted via a break statement at the end of the inlined
899 // test-expression. This is again something we don't support today, but it could
900 // be implemented.
901 // - We cannot inline the increment-expression if the loop uses `continue` anywhere;
902 // that would skip over the inlined block that evaluates the increment expression.
903 // There isn't a good fix for this--any workaround would be more complex than the
904 // cost of a function call. However, loops that don't use `continue` would still
905 // be viable candidates for increment-expression inlining.
906 break;
907 }
908 case Statement::Kind::kIf: {
909 IfStatement& ifStmt = (*stmt)->as<IfStatement>();
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400910 this->visitExpression(&ifStmt.test());
911 this->visitStatement(&ifStmt.ifTrue());
912 this->visitStatement(&ifStmt.ifFalse());
John Stiles70957c82020-10-02 16:42:10 -0400913 break;
914 }
915 case Statement::Kind::kReturn: {
916 ReturnStatement& returnStmt = (*stmt)->as<ReturnStatement>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400917 this->visitExpression(&returnStmt.expression());
John Stiles70957c82020-10-02 16:42:10 -0400918 break;
919 }
920 case Statement::Kind::kSwitch: {
921 SwitchStatement& switchStmt = (*stmt)->as<SwitchStatement>();
922 if (switchStmt.fSymbols) {
923 fSymbolTableStack.push_back(switchStmt.fSymbols.get());
924 }
925
926 this->visitExpression(&switchStmt.fValue);
927 for (std::unique_ptr<SwitchCase>& switchCase : switchStmt.fCases) {
928 // The switch-case's fValue cannot be a FunctionCall; skip it.
929 for (std::unique_ptr<Statement>& caseBlock : switchCase->fStatements) {
930 this->visitStatement(&caseBlock);
931 }
932 }
933 break;
934 }
935 case Statement::Kind::kVarDeclaration: {
936 VarDeclaration& varDeclStmt = (*stmt)->as<VarDeclaration>();
937 // Don't need to scan the declaration's sizes; those are always IntLiterals.
938 this->visitExpression(&varDeclStmt.fValue);
939 break;
940 }
John Stiles70957c82020-10-02 16:42:10 -0400941 case Statement::Kind::kWhile: {
942 WhileStatement& whileStmt = (*stmt)->as<WhileStatement>();
943 // The loop body is a candidate for inlining.
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400944 this->visitStatement(&whileStmt.statement());
John Stiles70957c82020-10-02 16:42:10 -0400945 // The inliner isn't smart enough to inline the test-expression for a while loop at
946 // this time. There are two limitations:
947 // - We would need to insert the inlined-body block at the very beginning of the
948 // while loop's inner fStatement. We don't support that today, but it's doable.
949 // - The while-loop's built-in test-expression would need to be replaced with a
950 // `true` BoolLiteral, and the loop would be halted via a break statement at the
951 // end of the inlined test-expression. This is again something we don't support
952 // today, but it could be implemented.
953 break;
954 }
955 default:
956 SkUNREACHABLE;
John Stiles93442622020-09-11 12:11:27 -0400957 }
958
John Stiles70957c82020-10-02 16:42:10 -0400959 // Pop our symbol and enclosing-statement stacks.
960 fSymbolTableStack.resize(oldSymbolStackSize);
961 fEnclosingStmtStack.resize(oldEnclosingStmtStackSize);
962 }
963
964 void visitExpression(std::unique_ptr<Expression>* expr) {
965 if (!*expr) {
966 return;
John Stiles93442622020-09-11 12:11:27 -0400967 }
John Stiles70957c82020-10-02 16:42:10 -0400968
969 switch ((*expr)->kind()) {
970 case Expression::Kind::kBoolLiteral:
971 case Expression::Kind::kDefined:
972 case Expression::Kind::kExternalValue:
973 case Expression::Kind::kFieldAccess:
974 case Expression::Kind::kFloatLiteral:
975 case Expression::Kind::kFunctionReference:
976 case Expression::Kind::kIntLiteral:
977 case Expression::Kind::kNullLiteral:
978 case Expression::Kind::kSetting:
979 case Expression::Kind::kTypeReference:
980 case Expression::Kind::kVariableReference:
981 // Nothing to scan here.
982 break;
983
984 case Expression::Kind::kBinary: {
985 BinaryExpression& binaryExpr = (*expr)->as<BinaryExpression>();
986 this->visitExpression(&binaryExpr.leftPointer());
987
988 // Logical-and and logical-or binary expressions do not inline the right side,
989 // because that would invalidate short-circuiting. That is, when evaluating
990 // expressions like these:
991 // (false && x()) // always false
992 // (true || y()) // always true
993 // It is illegal for side-effects from x() or y() to occur. The simplest way to
994 // enforce that rule is to avoid inlining the right side entirely. However, it is
995 // safe for other types of binary expression to inline both sides.
996 Token::Kind op = binaryExpr.getOperator();
997 bool shortCircuitable = (op == Token::Kind::TK_LOGICALAND ||
998 op == Token::Kind::TK_LOGICALOR);
999 if (!shortCircuitable) {
1000 this->visitExpression(&binaryExpr.rightPointer());
1001 }
1002 break;
1003 }
1004 case Expression::Kind::kConstructor: {
1005 Constructor& constructorExpr = (*expr)->as<Constructor>();
1006 for (std::unique_ptr<Expression>& arg : constructorExpr.arguments()) {
1007 this->visitExpression(&arg);
1008 }
1009 break;
1010 }
1011 case Expression::Kind::kExternalFunctionCall: {
1012 ExternalFunctionCall& funcCallExpr = (*expr)->as<ExternalFunctionCall>();
1013 for (std::unique_ptr<Expression>& arg : funcCallExpr.arguments()) {
1014 this->visitExpression(&arg);
1015 }
1016 break;
1017 }
1018 case Expression::Kind::kFunctionCall: {
1019 FunctionCall& funcCallExpr = (*expr)->as<FunctionCall>();
Ethan Nicholas0dec9922020-10-05 15:51:52 -04001020 for (std::unique_ptr<Expression>& arg : funcCallExpr.arguments()) {
John Stiles70957c82020-10-02 16:42:10 -04001021 this->visitExpression(&arg);
1022 }
1023 this->addInlineCandidate(expr);
1024 break;
1025 }
1026 case Expression::Kind::kIndex:{
1027 IndexExpression& indexExpr = (*expr)->as<IndexExpression>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -04001028 this->visitExpression(&indexExpr.base());
1029 this->visitExpression(&indexExpr.index());
John Stiles70957c82020-10-02 16:42:10 -04001030 break;
1031 }
1032 case Expression::Kind::kPostfix: {
1033 PostfixExpression& postfixExpr = (*expr)->as<PostfixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -04001034 this->visitExpression(&postfixExpr.operand());
John Stiles70957c82020-10-02 16:42:10 -04001035 break;
1036 }
1037 case Expression::Kind::kPrefix: {
1038 PrefixExpression& prefixExpr = (*expr)->as<PrefixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -04001039 this->visitExpression(&prefixExpr.operand());
John Stiles70957c82020-10-02 16:42:10 -04001040 break;
1041 }
1042 case Expression::Kind::kSwizzle: {
1043 Swizzle& swizzleExpr = (*expr)->as<Swizzle>();
1044 this->visitExpression(&swizzleExpr.fBase);
1045 break;
1046 }
1047 case Expression::Kind::kTernary: {
1048 TernaryExpression& ternaryExpr = (*expr)->as<TernaryExpression>();
1049 // The test expression is a candidate for inlining.
Ethan Nicholasdd218162020-10-08 05:48:01 -04001050 this->visitExpression(&ternaryExpr.test());
John Stiles70957c82020-10-02 16:42:10 -04001051 // The true- and false-expressions cannot be inlined, because we are only allowed to
1052 // evaluate one side.
1053 break;
1054 }
1055 default:
1056 SkUNREACHABLE;
1057 }
1058 }
1059
1060 void addInlineCandidate(std::unique_ptr<Expression>* candidate) {
1061 fCandidateList->fCandidates.push_back(
1062 InlineCandidate{fSymbolTableStack.back(),
1063 find_parent_statement(fEnclosingStmtStack),
1064 fEnclosingStmtStack.back(),
1065 candidate,
1066 fEnclosingFunction,
1067 /*isLargeFunction=*/false});
1068 }
John Stiles2d7973a2020-10-02 15:01:03 -04001069};
John Stiles93442622020-09-11 12:11:27 -04001070
John Stiles2d7973a2020-10-02 15:01:03 -04001071bool Inliner::candidateCanBeInlined(const InlineCandidate& candidate, InlinabilityCache* cache) {
Ethan Nicholas0dec9922020-10-05 15:51:52 -04001072 const FunctionDeclaration& funcDecl =
1073 (*candidate.fCandidateExpr)->as<FunctionCall>().function();
John Stiles915a38c2020-09-14 09:38:13 -04001074
John Stiles2d7973a2020-10-02 15:01:03 -04001075 auto [iter, wasInserted] = cache->insert({&funcDecl, false});
1076 if (wasInserted) {
1077 // Recursion is forbidden here to avoid an infinite death spiral of inlining.
Ethan Nicholased84b732020-10-08 11:45:44 -04001078 iter->second = this->isSafeToInline(funcDecl.definition()) &&
John Stiles2d7973a2020-10-02 15:01:03 -04001079 !contains_recursive_call(funcDecl);
John Stiles93442622020-09-11 12:11:27 -04001080 }
1081
John Stiles2d7973a2020-10-02 15:01:03 -04001082 return iter->second;
1083}
1084
1085bool Inliner::isLargeFunction(const FunctionDefinition* functionDef) {
1086 return Analysis::NodeCountExceeds(*functionDef, fSettings->fInlineThreshold);
1087}
1088
1089bool Inliner::isLargeFunction(const InlineCandidate& candidate, LargeFunctionCache* cache) {
Ethan Nicholas0dec9922020-10-05 15:51:52 -04001090 const FunctionDeclaration& funcDecl =
1091 (*candidate.fCandidateExpr)->as<FunctionCall>().function();
John Stiles2d7973a2020-10-02 15:01:03 -04001092
1093 auto [iter, wasInserted] = cache->insert({&funcDecl, false});
1094 if (wasInserted) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001095 iter->second = this->isLargeFunction(funcDecl.definition());
John Stiles2d7973a2020-10-02 15:01:03 -04001096 }
1097
1098 return iter->second;
1099}
1100
1101void Inliner::buildCandidateList(Program& program, InlineCandidateList* candidateList) {
1102 // This is structured much like a ProgramVisitor, but does not actually use ProgramVisitor.
1103 // The analyzer needs to keep track of the `unique_ptr<T>*` of statements and expressions so
1104 // that they can later be replaced, and ProgramVisitor does not provide this; it only provides a
1105 // `const T&`.
1106 InlineCandidateAnalyzer analyzer;
1107 analyzer.visit(program, candidateList);
1108
1109 // Remove candidates that are not safe to inline.
1110 std::vector<InlineCandidate>& candidates = candidateList->fCandidates;
1111 InlinabilityCache cache;
1112 candidates.erase(std::remove_if(candidates.begin(),
1113 candidates.end(),
1114 [&](const InlineCandidate& candidate) {
1115 return !this->candidateCanBeInlined(candidate, &cache);
1116 }),
1117 candidates.end());
1118
1119 // Determine whether each candidate function exceeds our inlining size threshold or not. These
1120 // can still be valid candidates if they are only called one time, so we don't remove them from
1121 // the candidate list, but they will not be inlined if they're called more than once.
1122 LargeFunctionCache largeFunctionCache;
1123 for (InlineCandidate& candidate : candidates) {
1124 candidate.fIsLargeFunction = this->isLargeFunction(candidate, &largeFunctionCache);
1125 }
1126}
1127
1128bool Inliner::analyze(Program& program) {
1129 InlineCandidateList candidateList;
1130 this->buildCandidateList(program, &candidateList);
1131
John Stiles915a38c2020-09-14 09:38:13 -04001132 // Inline the candidates where we've determined that it's safe to do so.
1133 std::unordered_set<const std::unique_ptr<Statement>*> enclosingStmtSet;
1134 bool madeChanges = false;
John Stiles2d7973a2020-10-02 15:01:03 -04001135 for (const InlineCandidate& candidate : candidateList.fCandidates) {
John Stiles915a38c2020-09-14 09:38:13 -04001136 FunctionCall& funcCall = (*candidate.fCandidateExpr)->as<FunctionCall>();
Ethan Nicholas0dec9922020-10-05 15:51:52 -04001137 const FunctionDeclaration* funcDecl = &funcCall.function();
John Stiles915a38c2020-09-14 09:38:13 -04001138
John Stiles2d7973a2020-10-02 15:01:03 -04001139 // If the function is large, not marked `inline`, and is called more than once, it's a bad
1140 // idea to inline it.
1141 if (candidate.fIsLargeFunction &&
Ethan Nicholased84b732020-10-08 11:45:44 -04001142 !(funcDecl->modifiers().fFlags & Modifiers::kInline_Flag) &&
1143 funcDecl->callCount() > 1) {
John Stiles915a38c2020-09-14 09:38:13 -04001144 continue;
1145 }
1146
1147 // Inlining two expressions using the same enclosing statement in the same inlining pass
1148 // does not work properly. If this happens, skip it; we'll get it in the next pass.
1149 auto [unusedIter, inserted] = enclosingStmtSet.insert(candidate.fEnclosingStmt);
1150 if (!inserted) {
1151 continue;
1152 }
1153
1154 // Convert the function call to its inlined equivalent.
Brian Osman3887a012020-09-30 13:22:27 -04001155 InlinedCall inlinedCall = this->inlineCall(&funcCall, candidate.fSymbols,
1156 &candidate.fEnclosingFunction->fDeclaration);
John Stiles915a38c2020-09-14 09:38:13 -04001157 if (inlinedCall.fInlinedBody) {
1158 // Ensure that the inlined body has a scope if it needs one.
John Stiles6d696082020-10-01 10:18:54 -04001159 this->ensureScopedBlocks(inlinedCall.fInlinedBody.get(), candidate.fParentStmt->get());
John Stiles915a38c2020-09-14 09:38:13 -04001160
1161 // Move the enclosing statement to the end of the unscoped Block containing the inlined
1162 // function, then replace the enclosing statement with that Block.
1163 // Before:
1164 // fInlinedBody = Block{ stmt1, stmt2, stmt3 }
1165 // fEnclosingStmt = stmt4
1166 // After:
1167 // fInlinedBody = null
1168 // fEnclosingStmt = Block{ stmt1, stmt2, stmt3, stmt4 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001169 inlinedCall.fInlinedBody->children().push_back(std::move(*candidate.fEnclosingStmt));
John Stiles915a38c2020-09-14 09:38:13 -04001170 *candidate.fEnclosingStmt = std::move(inlinedCall.fInlinedBody);
1171 }
1172
1173 // Replace the candidate function call with our replacement expression.
1174 *candidate.fCandidateExpr = std::move(inlinedCall.fReplacementExpr);
1175 madeChanges = true;
1176
1177 // Note that nothing was destroyed except for the FunctionCall. All other nodes should
1178 // remain valid.
1179 }
1180
1181 return madeChanges;
John Stiles93442622020-09-11 12:11:27 -04001182}
1183
John Stiles44e96be2020-08-31 13:16:04 -04001184} // namespace SkSL