blob: 42b86bbe7fc3fa65c7110ccc88f98efa0a9b9135 [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 {
181 if (stmt.is<InlineMarker>() && stmt.as<InlineMarker>().fFuncDecl->matches(*fFuncDecl)) {
182 return true;
183 }
184 return INHERITED::visitStatement(stmt);
185 }
186
187 const FunctionDeclaration* fFuncDecl;
188 using INHERITED = ProgramVisitor;
189 };
190
191 return ContainsRecursiveCall{}.visit(funcDecl);
192}
193
John Stiles44e96be2020-08-31 13:16:04 -0400194static const Type* copy_if_needed(const Type* src, SymbolTable& symbolTable) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400195 if (src->typeKind() == Type::TypeKind::kArray) {
Ethan Nicholase2c49992020-10-05 11:49:11 -0400196 return symbolTable.takeOwnershipOfSymbol(std::make_unique<Type>(src->name(),
197 src->typeKind(),
198 src->componentType(),
199 src->columns()));
John Stiles44e96be2020-08-31 13:16:04 -0400200 }
201 return src;
202}
203
John Stiles6d696082020-10-01 10:18:54 -0400204static std::unique_ptr<Statement>* find_parent_statement(
205 const std::vector<std::unique_ptr<Statement>*>& stmtStack) {
John Stiles915a38c2020-09-14 09:38:13 -0400206 SkASSERT(!stmtStack.empty());
207
208 // Walk the statement stack from back to front, ignoring the last element (which is the
209 // enclosing statement).
210 auto iter = stmtStack.rbegin();
211 ++iter;
212
213 // Anything counts as a parent statement other than a scopeless Block.
214 for (; iter != stmtStack.rend(); ++iter) {
John Stiles6d696082020-10-01 10:18:54 -0400215 std::unique_ptr<Statement>* stmt = *iter;
216 if (!(*stmt)->is<Block>() || (*stmt)->as<Block>().isScope()) {
John Stiles915a38c2020-09-14 09:38:13 -0400217 return stmt;
218 }
219 }
220
221 // There wasn't any parent statement to be found.
222 return nullptr;
223}
224
John Stilese41b4ee2020-09-28 12:28:16 -0400225std::unique_ptr<Expression> clone_with_ref_kind(const Expression& expr,
226 VariableReference::RefKind refKind) {
227 std::unique_ptr<Expression> clone = expr.clone();
John Stiles70b82422020-09-30 10:55:12 -0400228 class SetRefKindInExpression : public ProgramWriter {
John Stilese41b4ee2020-09-28 12:28:16 -0400229 public:
230 SetRefKindInExpression(VariableReference::RefKind refKind) : fRefKind(refKind) {}
John Stiles70b82422020-09-30 10:55:12 -0400231 bool visitExpression(Expression& expr) override {
John Stilese41b4ee2020-09-28 12:28:16 -0400232 if (expr.is<VariableReference>()) {
John Stiles70b82422020-09-30 10:55:12 -0400233 expr.as<VariableReference>().setRefKind(fRefKind);
John Stilese41b4ee2020-09-28 12:28:16 -0400234 }
235 return INHERITED::visitExpression(expr);
236 }
237
238 private:
239 VariableReference::RefKind fRefKind;
240
John Stiles70b82422020-09-30 10:55:12 -0400241 using INHERITED = ProgramWriter;
John Stilese41b4ee2020-09-28 12:28:16 -0400242 };
243
244 SetRefKindInExpression{refKind}.visitExpression(*clone);
245 return clone;
246}
247
John Stiles44733aa2020-09-29 17:42:23 -0400248bool is_trivial_argument(const Expression& argument) {
249 return argument.is<VariableReference>() ||
250 (argument.is<Swizzle>() && is_trivial_argument(*argument.as<Swizzle>().fBase)) ||
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400251 (argument.is<FieldAccess>() &&
252 is_trivial_argument(*argument.as<FieldAccess>().base())) ||
John Stiles80ccdbd2020-09-30 11:58:16 -0400253 (argument.is<Constructor>() &&
254 argument.as<Constructor>().arguments().size() == 1 &&
255 is_trivial_argument(*argument.as<Constructor>().arguments().front())) ||
John Stiles44733aa2020-09-29 17:42:23 -0400256 (argument.is<IndexExpression>() &&
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400257 argument.as<IndexExpression>().index()->is<IntLiteral>() &&
258 is_trivial_argument(*argument.as<IndexExpression>().base()));
John Stiles44733aa2020-09-29 17:42:23 -0400259}
260
John Stiles44e96be2020-08-31 13:16:04 -0400261} // namespace
262
John Stilesb61ee902020-09-21 12:26:59 -0400263void Inliner::ensureScopedBlocks(Statement* inlinedBody, Statement* parentStmt) {
264 // No changes necessary if this statement isn't actually a block.
265 if (!inlinedBody || !inlinedBody->is<Block>()) {
266 return;
267 }
268
269 // No changes necessary if the parent statement doesn't require a scope.
270 if (!parentStmt || !(parentStmt->is<IfStatement>() || parentStmt->is<ForStatement>() ||
271 parentStmt->is<DoStatement>() || parentStmt->is<WhileStatement>())) {
272 return;
273 }
274
275 Block& block = inlinedBody->as<Block>();
276
277 // The inliner will create inlined function bodies as a Block containing multiple statements,
278 // but no scope. Normally, this is fine, but if this block is used as the statement for a
279 // do/for/if/while, this isn't actually possible to represent textually; a scope must be added
280 // for the generated code to match the intent. In the case of Blocks nested inside other Blocks,
281 // we add the scope to the outermost block if needed. Zero-statement blocks have similar
282 // issues--if we don't represent the Block textually somehow, we run the risk of accidentally
283 // absorbing the following statement into our loop--so we also add a scope to these.
284 for (Block* nestedBlock = &block;; ) {
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400285 if (nestedBlock->isScope()) {
John Stilesb61ee902020-09-21 12:26:59 -0400286 // We found an explicit scope; all is well.
287 return;
288 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400289 if (nestedBlock->children().size() != 1) {
John Stilesb61ee902020-09-21 12:26:59 -0400290 // We found a block with multiple (or zero) statements, but no scope? Let's add a scope
291 // to the outermost block.
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400292 block.setIsScope(true);
John Stilesb61ee902020-09-21 12:26:59 -0400293 return;
294 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400295 if (!nestedBlock->children()[0]->is<Block>()) {
John Stilesb61ee902020-09-21 12:26:59 -0400296 // This block has exactly one thing inside, and it's not another block. No need to scope
297 // it.
298 return;
299 }
300 // We have to go deeper.
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400301 nestedBlock = &nestedBlock->children()[0]->as<Block>();
John Stilesb61ee902020-09-21 12:26:59 -0400302 }
303}
304
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400305void Inliner::reset(const Context* context, ModifiersPool* modifiers,
306 const Program::Settings* settings) {
307 fContext = context;
308 fModifiers = modifiers;
309 fSettings = settings;
John Stiles44e96be2020-08-31 13:16:04 -0400310 fInlineVarCounter = 0;
311}
312
John Stilesc75abb82020-09-14 18:24:12 -0400313String Inliner::uniqueNameForInlineVar(const String& baseName, SymbolTable* symbolTable) {
314 // If the base name starts with an underscore, like "_coords", we can't append another
315 // underscore, because OpenGL disallows two consecutive underscores anywhere in the string. But
316 // in the general case, using the underscore as a splitter reads nicely enough that it's worth
317 // putting in this special case.
318 const char* splitter = baseName.startsWith("_") ? "" : "_";
319
320 // Append a unique numeric prefix to avoid name overlap. Check the symbol table to make sure
321 // we're not reusing an existing name. (Note that within a single compilation pass, this check
322 // isn't fully comprehensive, as code isn't always generated in top-to-bottom order.)
323 String uniqueName;
324 for (;;) {
325 uniqueName = String::printf("_%d%s%s", fInlineVarCounter++, splitter, baseName.c_str());
326 StringFragment frag{uniqueName.data(), uniqueName.length()};
327 if ((*symbolTable)[frag] == nullptr) {
328 break;
329 }
330 }
331
332 return uniqueName;
333}
334
John Stiles44e96be2020-08-31 13:16:04 -0400335std::unique_ptr<Expression> Inliner::inlineExpression(int offset,
336 VariableRewriteMap* varMap,
337 const Expression& expression) {
338 auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> {
339 if (e) {
340 return this->inlineExpression(offset, varMap, *e);
341 }
342 return nullptr;
343 };
344 auto argList = [&](const std::vector<std::unique_ptr<Expression>>& originalArgs)
345 -> std::vector<std::unique_ptr<Expression>> {
346 std::vector<std::unique_ptr<Expression>> args;
347 args.reserve(originalArgs.size());
348 for (const std::unique_ptr<Expression>& arg : originalArgs) {
349 args.push_back(expr(arg));
350 }
351 return args;
352 };
353
Ethan Nicholase6592142020-09-08 10:22:09 -0400354 switch (expression.kind()) {
355 case Expression::Kind::kBinary: {
John Stiles44e96be2020-08-31 13:16:04 -0400356 const BinaryExpression& b = expression.as<BinaryExpression>();
357 return std::make_unique<BinaryExpression>(offset,
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400358 expr(b.leftPointer()),
359 b.getOperator(),
360 expr(b.rightPointer()),
Ethan Nicholas30d30222020-09-11 12:27:26 -0400361 &b.type());
John Stiles44e96be2020-08-31 13:16:04 -0400362 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400363 case Expression::Kind::kBoolLiteral:
364 case Expression::Kind::kIntLiteral:
365 case Expression::Kind::kFloatLiteral:
366 case Expression::Kind::kNullLiteral:
John Stiles44e96be2020-08-31 13:16:04 -0400367 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400368 case Expression::Kind::kConstructor: {
John Stiles44e96be2020-08-31 13:16:04 -0400369 const Constructor& constructor = expression.as<Constructor>();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400370 return std::make_unique<Constructor>(offset, &constructor.type(),
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400371 argList(constructor.arguments()));
John Stiles44e96be2020-08-31 13:16:04 -0400372 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400373 case Expression::Kind::kExternalFunctionCall: {
John Stiles44e96be2020-08-31 13:16:04 -0400374 const ExternalFunctionCall& externalCall = expression.as<ExternalFunctionCall>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400375 return std::make_unique<ExternalFunctionCall>(offset, &externalCall.function(),
Ethan Nicholas6e86ec92020-09-30 14:29:56 -0400376 argList(externalCall.arguments()));
John Stiles44e96be2020-08-31 13:16:04 -0400377 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400378 case Expression::Kind::kExternalValue:
John Stiles44e96be2020-08-31 13:16:04 -0400379 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400380 case Expression::Kind::kFieldAccess: {
John Stiles44e96be2020-08-31 13:16:04 -0400381 const FieldAccess& f = expression.as<FieldAccess>();
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400382 return std::make_unique<FieldAccess>(expr(f.base()), f.fieldIndex(), f.ownerKind());
John Stiles44e96be2020-08-31 13:16:04 -0400383 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400384 case Expression::Kind::kFunctionCall: {
John Stiles44e96be2020-08-31 13:16:04 -0400385 const FunctionCall& funcCall = expression.as<FunctionCall>();
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400386 return std::make_unique<FunctionCall>(offset, &funcCall.type(), &funcCall.function(),
387 argList(funcCall.arguments()));
John Stiles44e96be2020-08-31 13:16:04 -0400388 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400389 case Expression::Kind::kFunctionReference:
Brian Osman2b3b35f2020-09-08 09:17:36 -0400390 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400391 case Expression::Kind::kIndex: {
John Stiles44e96be2020-08-31 13:16:04 -0400392 const IndexExpression& idx = expression.as<IndexExpression>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400393 return std::make_unique<IndexExpression>(*fContext, expr(idx.base()),
394 expr(idx.index()));
John Stiles44e96be2020-08-31 13:16:04 -0400395 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400396 case Expression::Kind::kPrefix: {
John Stiles44e96be2020-08-31 13:16:04 -0400397 const PrefixExpression& p = expression.as<PrefixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400398 return std::make_unique<PrefixExpression>(p.getOperator(), expr(p.operand()));
John Stiles44e96be2020-08-31 13:16:04 -0400399 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400400 case Expression::Kind::kPostfix: {
John Stiles44e96be2020-08-31 13:16:04 -0400401 const PostfixExpression& p = expression.as<PostfixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400402 return std::make_unique<PostfixExpression>(expr(p.operand()), p.getOperator());
John Stiles44e96be2020-08-31 13:16:04 -0400403 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400404 case Expression::Kind::kSetting:
John Stiles44e96be2020-08-31 13:16:04 -0400405 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400406 case Expression::Kind::kSwizzle: {
John Stiles44e96be2020-08-31 13:16:04 -0400407 const Swizzle& s = expression.as<Swizzle>();
408 return std::make_unique<Swizzle>(*fContext, expr(s.fBase), s.fComponents);
409 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400410 case Expression::Kind::kTernary: {
John Stiles44e96be2020-08-31 13:16:04 -0400411 const TernaryExpression& t = expression.as<TernaryExpression>();
Ethan Nicholasdd218162020-10-08 05:48:01 -0400412 return std::make_unique<TernaryExpression>(offset, expr(t.test()),
413 expr(t.ifTrue()), expr(t.ifFalse()));
John Stiles44e96be2020-08-31 13:16:04 -0400414 }
Brian Osman83ba9302020-09-11 13:33:46 -0400415 case Expression::Kind::kTypeReference:
416 return expression.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400417 case Expression::Kind::kVariableReference: {
John Stiles44e96be2020-08-31 13:16:04 -0400418 const VariableReference& v = expression.as<VariableReference>();
Ethan Nicholas78686922020-10-08 06:46:27 -0400419 auto varMapIter = varMap->find(v.variable());
John Stilese41b4ee2020-09-28 12:28:16 -0400420 if (varMapIter != varMap->end()) {
Ethan Nicholas78686922020-10-08 06:46:27 -0400421 return clone_with_ref_kind(*varMapIter->second, v.refKind());
John Stiles44e96be2020-08-31 13:16:04 -0400422 }
423 return v.clone();
424 }
425 default:
426 SkASSERT(false);
427 return nullptr;
428 }
429}
430
431std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
432 VariableRewriteMap* varMap,
433 SymbolTable* symbolTableForStatement,
John Stilese41b4ee2020-09-28 12:28:16 -0400434 const Expression* resultExpr,
John Stiles44e96be2020-08-31 13:16:04 -0400435 bool haveEarlyReturns,
Brian Osman3887a012020-09-30 13:22:27 -0400436 const Statement& statement,
437 bool isBuiltinCode) {
John Stiles44e96be2020-08-31 13:16:04 -0400438 auto stmt = [&](const std::unique_ptr<Statement>& s) -> std::unique_ptr<Statement> {
439 if (s) {
John Stilesa5f3c312020-09-22 12:05:16 -0400440 return this->inlineStatement(offset, varMap, symbolTableForStatement, resultExpr,
Brian Osman3887a012020-09-30 13:22:27 -0400441 haveEarlyReturns, *s, isBuiltinCode);
John Stiles44e96be2020-08-31 13:16:04 -0400442 }
443 return nullptr;
444 };
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400445 auto blockStmts = [&](const Block& block) {
446 std::vector<std::unique_ptr<Statement>> result;
447 for (const std::unique_ptr<Statement>& child : block.children()) {
448 result.push_back(stmt(child));
449 }
450 return result;
451 };
John Stiles44e96be2020-08-31 13:16:04 -0400452 auto stmts = [&](const std::vector<std::unique_ptr<Statement>>& ss) {
453 std::vector<std::unique_ptr<Statement>> result;
454 for (const auto& s : ss) {
455 result.push_back(stmt(s));
456 }
457 return result;
458 };
459 auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> {
460 if (e) {
461 return this->inlineExpression(offset, varMap, *e);
462 }
463 return nullptr;
464 };
Ethan Nicholase6592142020-09-08 10:22:09 -0400465 switch (statement.kind()) {
466 case Statement::Kind::kBlock: {
John Stiles44e96be2020-08-31 13:16:04 -0400467 const Block& b = statement.as<Block>();
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400468 return std::make_unique<Block>(offset, blockStmts(b), b.symbolTable(), b.isScope());
John Stiles44e96be2020-08-31 13:16:04 -0400469 }
470
Ethan Nicholase6592142020-09-08 10:22:09 -0400471 case Statement::Kind::kBreak:
472 case Statement::Kind::kContinue:
473 case Statement::Kind::kDiscard:
John Stiles44e96be2020-08-31 13:16:04 -0400474 return statement.clone();
475
Ethan Nicholase6592142020-09-08 10:22:09 -0400476 case Statement::Kind::kDo: {
John Stiles44e96be2020-08-31 13:16:04 -0400477 const DoStatement& d = statement.as<DoStatement>();
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400478 return std::make_unique<DoStatement>(offset, stmt(d.statement()), expr(d.test()));
John Stiles44e96be2020-08-31 13:16:04 -0400479 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400480 case Statement::Kind::kExpression: {
John Stiles44e96be2020-08-31 13:16:04 -0400481 const ExpressionStatement& e = statement.as<ExpressionStatement>();
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400482 return std::make_unique<ExpressionStatement>(expr(e.expression()));
John Stiles44e96be2020-08-31 13:16:04 -0400483 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400484 case Statement::Kind::kFor: {
John Stiles44e96be2020-08-31 13:16:04 -0400485 const ForStatement& f = statement.as<ForStatement>();
486 // need to ensure initializer is evaluated first so that we've already remapped its
487 // declarations by the time we evaluate test & next
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400488 std::unique_ptr<Statement> initializer = stmt(f.initializer());
489 return std::make_unique<ForStatement>(offset, std::move(initializer), expr(f.test()),
490 expr(f.next()), stmt(f.statement()), f.symbols());
John Stiles44e96be2020-08-31 13:16:04 -0400491 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400492 case Statement::Kind::kIf: {
John Stiles44e96be2020-08-31 13:16:04 -0400493 const IfStatement& i = statement.as<IfStatement>();
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400494 return std::make_unique<IfStatement>(offset, i.isStatic(), expr(i.test()),
495 stmt(i.ifTrue()), stmt(i.ifFalse()));
John Stiles44e96be2020-08-31 13:16:04 -0400496 }
John Stiles98c1f822020-09-09 14:18:53 -0400497 case Statement::Kind::kInlineMarker:
Ethan Nicholase6592142020-09-08 10:22:09 -0400498 case Statement::Kind::kNop:
John Stiles44e96be2020-08-31 13:16:04 -0400499 return statement.clone();
Ethan Nicholase6592142020-09-08 10:22:09 -0400500 case Statement::Kind::kReturn: {
John Stiles44e96be2020-08-31 13:16:04 -0400501 const ReturnStatement& r = statement.as<ReturnStatement>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400502 if (r.expression()) {
John Stilese41b4ee2020-09-28 12:28:16 -0400503 SkASSERT(resultExpr);
John Stilesa5f3c312020-09-22 12:05:16 -0400504 auto assignment =
505 std::make_unique<ExpressionStatement>(std::make_unique<BinaryExpression>(
506 offset,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400507 clone_with_ref_kind(*resultExpr,
508 VariableReference::RefKind::kWrite),
John Stilesa5f3c312020-09-22 12:05:16 -0400509 Token::Kind::TK_EQ,
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400510 expr(r.expression()),
John Stilese41b4ee2020-09-28 12:28:16 -0400511 &resultExpr->type()));
John Stiles44e96be2020-08-31 13:16:04 -0400512 if (haveEarlyReturns) {
513 std::vector<std::unique_ptr<Statement>> block;
514 block.push_back(std::move(assignment));
515 block.emplace_back(new BreakStatement(offset));
516 return std::make_unique<Block>(offset, std::move(block), /*symbols=*/nullptr,
517 /*isScope=*/true);
518 } else {
519 return std::move(assignment);
520 }
521 } else {
522 if (haveEarlyReturns) {
523 return std::make_unique<BreakStatement>(offset);
524 } else {
525 return std::make_unique<Nop>();
526 }
527 }
528 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400529 case Statement::Kind::kSwitch: {
John Stiles44e96be2020-08-31 13:16:04 -0400530 const SwitchStatement& ss = statement.as<SwitchStatement>();
531 std::vector<std::unique_ptr<SwitchCase>> cases;
532 for (const auto& sc : ss.fCases) {
533 cases.emplace_back(new SwitchCase(offset, expr(sc->fValue),
534 stmts(sc->fStatements)));
535 }
536 return std::make_unique<SwitchStatement>(offset, ss.fIsStatic, expr(ss.fValue),
537 std::move(cases), ss.fSymbols);
538 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400539 case Statement::Kind::kVarDeclaration: {
John Stiles44e96be2020-08-31 13:16:04 -0400540 const VarDeclaration& decl = statement.as<VarDeclaration>();
541 std::vector<std::unique_ptr<Expression>> sizes;
542 for (const auto& size : decl.fSizes) {
543 sizes.push_back(expr(size));
544 }
545 std::unique_ptr<Expression> initialValue = expr(decl.fValue);
546 const Variable* old = decl.fVar;
John Stilesc75abb82020-09-14 18:24:12 -0400547 // We assign unique names to inlined variables--scopes hide most of the problems in this
548 // regard, but see `InlinerAvoidsVariableNameOverlap` for a counterexample where unique
549 // names are important.
550 auto name = std::make_unique<String>(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400551 this->uniqueNameForInlineVar(String(old->name()), symbolTableForStatement));
John Stiles44e96be2020-08-31 13:16:04 -0400552 const String* namePtr = symbolTableForStatement->takeOwnershipOfString(std::move(name));
Brian Osmanc0213602020-10-06 14:43:32 -0400553 const Type* baseTypePtr = copy_if_needed(&decl.fBaseType, *symbolTableForStatement);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400554 const Type* typePtr = copy_if_needed(&old->type(), *symbolTableForStatement);
John Stiles44e96be2020-08-31 13:16:04 -0400555 const Variable* clone = symbolTableForStatement->takeOwnershipOfSymbol(
556 std::make_unique<Variable>(offset,
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400557 old->modifiersHandle(),
John Stiles44e96be2020-08-31 13:16:04 -0400558 namePtr->c_str(),
Ethan Nicholas30d30222020-09-11 12:27:26 -0400559 typePtr,
Brian Osman3887a012020-09-30 13:22:27 -0400560 isBuiltinCode,
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400561 old->storage(),
John Stiles44e96be2020-08-31 13:16:04 -0400562 initialValue.get()));
John Stilese41b4ee2020-09-28 12:28:16 -0400563 (*varMap)[old] = std::make_unique<VariableReference>(offset, clone);
Brian Osmanc0213602020-10-06 14:43:32 -0400564 return std::make_unique<VarDeclaration>(clone, baseTypePtr, std::move(sizes),
John Stiles44e96be2020-08-31 13:16:04 -0400565 std::move(initialValue));
566 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400567 case Statement::Kind::kWhile: {
John Stiles44e96be2020-08-31 13:16:04 -0400568 const WhileStatement& w = statement.as<WhileStatement>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400569 return std::make_unique<WhileStatement>(offset, expr(w.test()), stmt(w.statement()));
John Stiles44e96be2020-08-31 13:16:04 -0400570 }
571 default:
572 SkASSERT(false);
573 return nullptr;
574 }
575}
576
John Stiles6eadf132020-09-08 10:16:10 -0400577Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
Brian Osman3887a012020-09-30 13:22:27 -0400578 SymbolTable* symbolTableForCall,
579 const FunctionDeclaration* caller) {
John Stiles44e96be2020-08-31 13:16:04 -0400580 // Inlining is more complicated here than in a typical compiler, because we have to have a
581 // high-level IR and can't just drop statements into the middle of an expression or even use
582 // gotos.
583 //
584 // Since we can't insert statements into an expression, we run the inline function as extra
585 // statements before the statement we're currently processing, relying on a lack of execution
586 // order guarantees. Since we can't use gotos (which are normally used to replace return
587 // statements), we wrap the whole function in a loop and use break statements to jump to the
588 // end.
589 SkASSERT(fSettings);
590 SkASSERT(fContext);
591 SkASSERT(call);
Ethan Nicholased84b732020-10-08 11:45:44 -0400592 SkASSERT(this->isSafeToInline(call->function().definition()));
John Stiles44e96be2020-08-31 13:16:04 -0400593
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400594 std::vector<std::unique_ptr<Expression>>& arguments = call->arguments();
John Stiles6eadf132020-09-08 10:16:10 -0400595 const int offset = call->fOffset;
Ethan Nicholased84b732020-10-08 11:45:44 -0400596 const FunctionDefinition& function = *call->function().definition();
John Stiles6eadf132020-09-08 10:16:10 -0400597 const bool hasEarlyReturn = has_early_return(function);
598
John Stiles44e96be2020-08-31 13:16:04 -0400599 InlinedCall inlinedCall;
John Stiles6eadf132020-09-08 10:16:10 -0400600 inlinedCall.fInlinedBody = std::make_unique<Block>(offset,
601 std::vector<std::unique_ptr<Statement>>{},
602 /*symbols=*/nullptr,
603 /*isScope=*/false);
John Stiles98c1f822020-09-09 14:18:53 -0400604
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400605 Block& inlinedBody = *inlinedCall.fInlinedBody;
606 inlinedBody.children().reserve(1 + // Inline marker
607 1 + // Result variable
608 arguments.size() + // Function arguments (passing in)
John Stilese41b4ee2020-09-28 12:28:16 -0400609 arguments.size() + // Function arguments (copy out-params back)
610 1); // Inlined code (Block or do-while loop)
John Stiles98c1f822020-09-09 14:18:53 -0400611
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400612 inlinedBody.children().push_back(std::make_unique<InlineMarker>(call->function()));
John Stiles44e96be2020-08-31 13:16:04 -0400613
John Stilese41b4ee2020-09-28 12:28:16 -0400614 auto makeInlineVar =
615 [&](const String& baseName, const Type* type, Modifiers modifiers,
616 std::unique_ptr<Expression>* initialValue) -> std::unique_ptr<Expression> {
John Stilesa003e812020-09-11 09:43:49 -0400617 // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so
618 // replace them if they ever appear here. If this happens, we likely forgot to coerce a type
619 // somewhere during compilation.
620 if (type == fContext->fFloatLiteral_Type.get()) {
John Stilesd2be5c52020-09-11 14:58:06 -0400621 SkDEBUGFAIL("found a $floatLiteral type while inlining");
John Stilesa003e812020-09-11 09:43:49 -0400622 type = fContext->fFloat_Type.get();
623 } else if (type == fContext->fIntLiteral_Type.get()) {
John Stilesd2be5c52020-09-11 14:58:06 -0400624 SkDEBUGFAIL("found an $intLiteral type while inlining");
John Stilesa003e812020-09-11 09:43:49 -0400625 type = fContext->fInt_Type.get();
626 }
627
John Stilesc75abb82020-09-14 18:24:12 -0400628 // Provide our new variable with a unique name, and add it to our symbol table.
629 String uniqueName = this->uniqueNameForInlineVar(baseName, symbolTableForCall);
John Stilescf936f92020-08-31 17:18:45 -0400630 const String* namePtr = symbolTableForCall->takeOwnershipOfString(
631 std::make_unique<String>(std::move(uniqueName)));
John Stiles44e96be2020-08-31 13:16:04 -0400632 StringFragment nameFrag{namePtr->c_str(), namePtr->length()};
633
634 // Add our new variable to the symbol table.
John Stilesb8cc6652020-10-08 09:12:07 -0400635 const Variable* variableSymbol = symbolTableForCall->add(std::make_unique<Variable>(
636 /*offset=*/-1, fModifiers->handle(Modifiers()),
Ethan Nicholased84b732020-10-08 11:45:44 -0400637 nameFrag, type, caller->isBuiltin(),
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400638 Variable::Storage::kLocal, initialValue->get()));
John Stiles44e96be2020-08-31 13:16:04 -0400639
640 // Prepare the variable declaration (taking extra care with `out` params to not clobber any
641 // initial value).
Brian Osmanc0213602020-10-06 14:43:32 -0400642 std::unique_ptr<Statement> variable;
John Stiles44e96be2020-08-31 13:16:04 -0400643 if (initialValue && (modifiers.fFlags & Modifiers::kOut_Flag)) {
Brian Osmanc0213602020-10-06 14:43:32 -0400644 variable = std::make_unique<VarDeclaration>(
645 variableSymbol, type, /*sizes=*/std::vector<std::unique_ptr<Expression>>{},
646 (*initialValue)->clone());
John Stiles44e96be2020-08-31 13:16:04 -0400647 } else {
Brian Osmanc0213602020-10-06 14:43:32 -0400648 variable = std::make_unique<VarDeclaration>(
649 variableSymbol, type, /*sizes=*/std::vector<std::unique_ptr<Expression>>{},
650 std::move(*initialValue));
John Stiles44e96be2020-08-31 13:16:04 -0400651 }
652
653 // Add the new variable-declaration statement to our block of extra statements.
Brian Osmanc0213602020-10-06 14:43:32 -0400654 inlinedBody.children().push_back(std::move(variable));
John Stiles44e96be2020-08-31 13:16:04 -0400655
John Stilese41b4ee2020-09-28 12:28:16 -0400656 return std::make_unique<VariableReference>(offset, variableSymbol);
John Stiles44e96be2020-08-31 13:16:04 -0400657 };
658
659 // Create a variable to hold the result in the extra statements (excepting void).
John Stilese41b4ee2020-09-28 12:28:16 -0400660 std::unique_ptr<Expression> resultExpr;
Ethan Nicholased84b732020-10-08 11:45:44 -0400661 if (function.fDeclaration.returnType() != *fContext->fVoid_Type) {
John Stiles44e96be2020-08-31 13:16:04 -0400662 std::unique_ptr<Expression> noInitialValue;
Ethan Nicholase2c49992020-10-05 11:49:11 -0400663 resultExpr = makeInlineVar(String(function.fDeclaration.name()),
Ethan Nicholased84b732020-10-08 11:45:44 -0400664 &function.fDeclaration.returnType(),
John Stilese41b4ee2020-09-28 12:28:16 -0400665 Modifiers{}, &noInitialValue);
666 }
John Stiles44e96be2020-08-31 13:16:04 -0400667
668 // Create variables in the extra statements to hold the arguments, and assign the arguments to
669 // them.
670 VariableRewriteMap varMap;
John Stilese41b4ee2020-09-28 12:28:16 -0400671 std::vector<int> argsToCopyBack;
John Stiles44e96be2020-08-31 13:16:04 -0400672 for (int i = 0; i < (int) arguments.size(); ++i) {
Ethan Nicholased84b732020-10-08 11:45:44 -0400673 const Variable* param = function.fDeclaration.parameters()[i];
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400674 bool isOutParam = param->modifiers().fFlags & Modifiers::kOut_Flag;
John Stiles44e96be2020-08-31 13:16:04 -0400675
John Stiles44733aa2020-09-29 17:42:23 -0400676 // If this argument can be inlined trivially (e.g. a swizzle, or a constant array index)...
677 if (is_trivial_argument(*arguments[i])) {
John Stilese41b4ee2020-09-28 12:28:16 -0400678 // ... and it's an `out` param, or it isn't written to within the inline function...
679 if (isOutParam || !Analysis::StatementWritesToVariable(*function.fBody, *param)) {
John Stilesf201af82020-09-29 16:57:55 -0400680 // ... we don't need to copy it at all! We can just use the existing expression.
681 varMap[param] = arguments[i]->clone();
John Stiles44e96be2020-08-31 13:16:04 -0400682 continue;
683 }
684 }
685
John Stilese41b4ee2020-09-28 12:28:16 -0400686 if (isOutParam) {
687 argsToCopyBack.push_back(i);
688 }
689
Ethan Nicholase2c49992020-10-05 11:49:11 -0400690 varMap[param] = makeInlineVar(String(param->name()), &arguments[i]->type(),
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400691 param->modifiers(), &arguments[i]);
John Stiles44e96be2020-08-31 13:16:04 -0400692 }
693
694 const Block& body = function.fBody->as<Block>();
John Stiles44e96be2020-08-31 13:16:04 -0400695 auto inlineBlock = std::make_unique<Block>(offset, std::vector<std::unique_ptr<Statement>>{});
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400696 inlineBlock->children().reserve(body.children().size());
697 for (const std::unique_ptr<Statement>& stmt : body.children()) {
Brian Osman3887a012020-09-30 13:22:27 -0400698 inlineBlock->children().push_back(this->inlineStatement(offset, &varMap, symbolTableForCall,
699 resultExpr.get(), hasEarlyReturn,
Ethan Nicholased84b732020-10-08 11:45:44 -0400700 *stmt, caller->isBuiltin()));
John Stiles44e96be2020-08-31 13:16:04 -0400701 }
702 if (hasEarlyReturn) {
703 // Since we output to backends that don't have a goto statement (which would normally be
704 // used to perform an early return), we fake it by wrapping the function in a
705 // do { } while (false); and then use break statements to jump to the end in order to
706 // emulate a goto.
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400707 inlinedBody.children().push_back(std::make_unique<DoStatement>(
John Stiles44e96be2020-08-31 13:16:04 -0400708 /*offset=*/-1,
709 std::move(inlineBlock),
710 std::make_unique<BoolLiteral>(*fContext, offset, /*value=*/false)));
711 } else {
John Stiles6eadf132020-09-08 10:16:10 -0400712 // No early returns, so we can just dump the code in. We still need to keep the block so we
713 // don't get name conflicts with locals.
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400714 inlinedBody.children().push_back(std::move(inlineBlock));
John Stiles44e96be2020-08-31 13:16:04 -0400715 }
716
John Stilese41b4ee2020-09-28 12:28:16 -0400717 // Copy back the values of `out` parameters into their real destinations.
718 for (int i : argsToCopyBack) {
Ethan Nicholased84b732020-10-08 11:45:44 -0400719 const Variable* p = function.fDeclaration.parameters()[i];
John Stilese41b4ee2020-09-28 12:28:16 -0400720 SkASSERT(varMap.find(p) != varMap.end());
721 inlinedBody.children().push_back(
722 std::make_unique<ExpressionStatement>(std::make_unique<BinaryExpression>(
723 offset,
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400724 clone_with_ref_kind(*arguments[i], VariableReference::RefKind::kWrite),
John Stilese41b4ee2020-09-28 12:28:16 -0400725 Token::Kind::TK_EQ,
726 std::move(varMap[p]),
727 &arguments[i]->type())));
John Stiles44e96be2020-08-31 13:16:04 -0400728 }
729
John Stilese41b4ee2020-09-28 12:28:16 -0400730 if (resultExpr != nullptr) {
731 // Return our result variable as our replacement expression.
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400732 SkASSERT(resultExpr->as<VariableReference>().refKind() ==
733 VariableReference::RefKind::kRead);
John Stilese41b4ee2020-09-28 12:28:16 -0400734 inlinedCall.fReplacementExpr = std::move(resultExpr);
John Stiles44e96be2020-08-31 13:16:04 -0400735 } else {
736 // It's a void function, so it doesn't actually result in anything, but we have to return
737 // something non-null as a standin.
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400738 inlinedCall.fReplacementExpr = std::make_unique<BoolLiteral>(*fContext,
739 offset,
John Stiles44e96be2020-08-31 13:16:04 -0400740 /*value=*/false);
741 }
742
John Stiles44e96be2020-08-31 13:16:04 -0400743 return inlinedCall;
744}
745
John Stiles2d7973a2020-10-02 15:01:03 -0400746bool Inliner::isSafeToInline(const FunctionDefinition* functionDef) {
John Stiles44e96be2020-08-31 13:16:04 -0400747 SkASSERT(fSettings);
748
John Stiles2d7973a2020-10-02 15:01:03 -0400749 if (functionDef == nullptr) {
John Stiles44e96be2020-08-31 13:16:04 -0400750 // Can't inline something if we don't actually have its definition.
751 return false;
752 }
John Stiles2d7973a2020-10-02 15:01:03 -0400753
John Stiles44e96be2020-08-31 13:16:04 -0400754 if (!fSettings->fCaps || !fSettings->fCaps->canUseDoLoops()) {
755 // We don't have do-while loops. We use do-while loops to simulate early returns, so we
756 // can't inline functions that have an early return.
John Stiles2d7973a2020-10-02 15:01:03 -0400757 bool hasEarlyReturn = has_early_return(*functionDef);
John Stiles44e96be2020-08-31 13:16:04 -0400758
759 // If we didn't detect an early return, there shouldn't be any returns in breakable
760 // constructs either.
John Stiles2d7973a2020-10-02 15:01:03 -0400761 SkASSERT(hasEarlyReturn || count_returns_in_breakable_constructs(*functionDef) == 0);
John Stiles44e96be2020-08-31 13:16:04 -0400762 return !hasEarlyReturn;
763 }
764 // We have do-while loops, but we don't have any mechanism to simulate early returns within a
765 // 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 -0400766 bool hasReturnInBreakableConstruct = (count_returns_in_breakable_constructs(*functionDef) > 0);
John Stiles44e96be2020-08-31 13:16:04 -0400767
768 // If we detected returns in breakable constructs, we should also detect an early return.
John Stiles2d7973a2020-10-02 15:01:03 -0400769 SkASSERT(!hasReturnInBreakableConstruct || has_early_return(*functionDef));
John Stiles44e96be2020-08-31 13:16:04 -0400770 return !hasReturnInBreakableConstruct;
771}
772
John Stiles2d7973a2020-10-02 15:01:03 -0400773// A candidate function for inlining, containing everything that `inlineCall` needs.
774struct InlineCandidate {
775 SymbolTable* fSymbols; // the SymbolTable of the candidate
776 std::unique_ptr<Statement>* fParentStmt; // the parent Statement of the enclosing stmt
777 std::unique_ptr<Statement>* fEnclosingStmt; // the Statement containing the candidate
778 std::unique_ptr<Expression>* fCandidateExpr; // the candidate FunctionCall to be inlined
779 FunctionDefinition* fEnclosingFunction; // the Function containing the candidate
780 bool fIsLargeFunction; // does candidate exceed the inline threshold?
781};
John Stiles93442622020-09-11 12:11:27 -0400782
John Stiles2d7973a2020-10-02 15:01:03 -0400783struct InlineCandidateList {
784 std::vector<InlineCandidate> fCandidates;
785};
786
787class InlineCandidateAnalyzer {
John Stiles70957c82020-10-02 16:42:10 -0400788public:
789 // A list of all the inlining candidates we found during analysis.
790 InlineCandidateList* fCandidateList;
John Stiles2d7973a2020-10-02 15:01:03 -0400791
John Stiles70957c82020-10-02 16:42:10 -0400792 // A stack of the symbol tables; since most nodes don't have one, expected to be shallower than
793 // the enclosing-statement stack.
794 std::vector<SymbolTable*> fSymbolTableStack;
795 // A stack of "enclosing" statements--these would be suitable for the inliner to use for adding
796 // new instructions. Not all statements are suitable (e.g. a for-loop's initializer). The
797 // inliner might replace a statement with a block containing the statement.
798 std::vector<std::unique_ptr<Statement>*> fEnclosingStmtStack;
799 // The function that we're currently processing (i.e. inlining into).
800 FunctionDefinition* fEnclosingFunction = nullptr;
John Stiles93442622020-09-11 12:11:27 -0400801
John Stiles70957c82020-10-02 16:42:10 -0400802 void visit(Program& program, InlineCandidateList* candidateList) {
803 fCandidateList = candidateList;
804 fSymbolTableStack.push_back(program.fSymbols.get());
John Stiles93442622020-09-11 12:11:27 -0400805
Brian Osman1179fcf2020-10-08 16:04:40 -0400806 for (const auto& pe : program.elements()) {
807 this->visitProgramElement(pe.get());
John Stiles93442622020-09-11 12:11:27 -0400808 }
809
John Stiles70957c82020-10-02 16:42:10 -0400810 fSymbolTableStack.pop_back();
811 fCandidateList = nullptr;
812 }
813
814 void visitProgramElement(ProgramElement* pe) {
815 switch (pe->kind()) {
816 case ProgramElement::Kind::kFunction: {
817 FunctionDefinition& funcDef = pe->as<FunctionDefinition>();
818 fEnclosingFunction = &funcDef;
819 this->visitStatement(&funcDef.fBody);
820 break;
John Stiles93442622020-09-11 12:11:27 -0400821 }
John Stiles70957c82020-10-02 16:42:10 -0400822 default:
823 // The inliner can't operate outside of a function's scope.
824 break;
825 }
826 }
827
828 void visitStatement(std::unique_ptr<Statement>* stmt,
829 bool isViableAsEnclosingStatement = true) {
830 if (!*stmt) {
831 return;
John Stiles93442622020-09-11 12:11:27 -0400832 }
833
John Stiles70957c82020-10-02 16:42:10 -0400834 size_t oldEnclosingStmtStackSize = fEnclosingStmtStack.size();
835 size_t oldSymbolStackSize = fSymbolTableStack.size();
John Stiles93442622020-09-11 12:11:27 -0400836
John Stiles70957c82020-10-02 16:42:10 -0400837 if (isViableAsEnclosingStatement) {
838 fEnclosingStmtStack.push_back(stmt);
John Stiles93442622020-09-11 12:11:27 -0400839 }
840
John Stiles70957c82020-10-02 16:42:10 -0400841 switch ((*stmt)->kind()) {
842 case Statement::Kind::kBreak:
843 case Statement::Kind::kContinue:
844 case Statement::Kind::kDiscard:
845 case Statement::Kind::kInlineMarker:
846 case Statement::Kind::kNop:
847 break;
848
849 case Statement::Kind::kBlock: {
850 Block& block = (*stmt)->as<Block>();
851 if (block.symbolTable()) {
852 fSymbolTableStack.push_back(block.symbolTable().get());
853 }
854
855 for (std::unique_ptr<Statement>& stmt : block.children()) {
856 this->visitStatement(&stmt);
857 }
858 break;
John Stiles93442622020-09-11 12:11:27 -0400859 }
John Stiles70957c82020-10-02 16:42:10 -0400860 case Statement::Kind::kDo: {
861 DoStatement& doStmt = (*stmt)->as<DoStatement>();
862 // The loop body is a candidate for inlining.
863 this->visitStatement(&doStmt.statement());
864 // The inliner isn't smart enough to inline the test-expression for a do-while
865 // loop at this time. There are two limitations:
866 // - We would need to insert the inlined-body block at the very end of the do-
867 // statement's inner fStatement. We don't support that today, but it's doable.
868 // - We cannot inline the test expression if the loop uses `continue` anywhere; that
869 // would skip over the inlined block that evaluates the test expression. There
870 // isn't a good fix for this--any workaround would be more complex than the cost
871 // of a function call. However, loops that don't use `continue` would still be
872 // viable candidates for inlining.
873 break;
John Stiles93442622020-09-11 12:11:27 -0400874 }
John Stiles70957c82020-10-02 16:42:10 -0400875 case Statement::Kind::kExpression: {
876 ExpressionStatement& expr = (*stmt)->as<ExpressionStatement>();
877 this->visitExpression(&expr.expression());
878 break;
879 }
880 case Statement::Kind::kFor: {
881 ForStatement& forStmt = (*stmt)->as<ForStatement>();
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400882 if (forStmt.symbols()) {
883 fSymbolTableStack.push_back(forStmt.symbols().get());
John Stiles70957c82020-10-02 16:42:10 -0400884 }
885
886 // The initializer and loop body are candidates for inlining.
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400887 this->visitStatement(&forStmt.initializer(),
John Stiles70957c82020-10-02 16:42:10 -0400888 /*isViableAsEnclosingStatement=*/false);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400889 this->visitStatement(&forStmt.statement());
John Stiles70957c82020-10-02 16:42:10 -0400890
891 // The inliner isn't smart enough to inline the test- or increment-expressions
892 // of a for loop loop at this time. There are a handful of limitations:
893 // - We would need to insert the test-expression block at the very beginning of the
894 // for-loop's inner fStatement, and the increment-expression block at the very
895 // end. We don't support that today, but it's doable.
896 // - The for-loop's built-in test-expression would need to be dropped entirely,
897 // and the loop would be halted via a break statement at the end of the inlined
898 // test-expression. This is again something we don't support today, but it could
899 // be implemented.
900 // - We cannot inline the increment-expression if the loop uses `continue` anywhere;
901 // that would skip over the inlined block that evaluates the increment expression.
902 // There isn't a good fix for this--any workaround would be more complex than the
903 // cost of a function call. However, loops that don't use `continue` would still
904 // be viable candidates for increment-expression inlining.
905 break;
906 }
907 case Statement::Kind::kIf: {
908 IfStatement& ifStmt = (*stmt)->as<IfStatement>();
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400909 this->visitExpression(&ifStmt.test());
910 this->visitStatement(&ifStmt.ifTrue());
911 this->visitStatement(&ifStmt.ifFalse());
John Stiles70957c82020-10-02 16:42:10 -0400912 break;
913 }
914 case Statement::Kind::kReturn: {
915 ReturnStatement& returnStmt = (*stmt)->as<ReturnStatement>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400916 this->visitExpression(&returnStmt.expression());
John Stiles70957c82020-10-02 16:42:10 -0400917 break;
918 }
919 case Statement::Kind::kSwitch: {
920 SwitchStatement& switchStmt = (*stmt)->as<SwitchStatement>();
921 if (switchStmt.fSymbols) {
922 fSymbolTableStack.push_back(switchStmt.fSymbols.get());
923 }
924
925 this->visitExpression(&switchStmt.fValue);
926 for (std::unique_ptr<SwitchCase>& switchCase : switchStmt.fCases) {
927 // The switch-case's fValue cannot be a FunctionCall; skip it.
928 for (std::unique_ptr<Statement>& caseBlock : switchCase->fStatements) {
929 this->visitStatement(&caseBlock);
930 }
931 }
932 break;
933 }
934 case Statement::Kind::kVarDeclaration: {
935 VarDeclaration& varDeclStmt = (*stmt)->as<VarDeclaration>();
936 // Don't need to scan the declaration's sizes; those are always IntLiterals.
937 this->visitExpression(&varDeclStmt.fValue);
938 break;
939 }
John Stiles70957c82020-10-02 16:42:10 -0400940 case Statement::Kind::kWhile: {
941 WhileStatement& whileStmt = (*stmt)->as<WhileStatement>();
942 // The loop body is a candidate for inlining.
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400943 this->visitStatement(&whileStmt.statement());
John Stiles70957c82020-10-02 16:42:10 -0400944 // The inliner isn't smart enough to inline the test-expression for a while loop at
945 // this time. There are two limitations:
946 // - We would need to insert the inlined-body block at the very beginning of the
947 // while loop's inner fStatement. We don't support that today, but it's doable.
948 // - The while-loop's built-in test-expression would need to be replaced with a
949 // `true` BoolLiteral, and the loop would be halted via a break statement at the
950 // end of the inlined test-expression. This is again something we don't support
951 // today, but it could be implemented.
952 break;
953 }
954 default:
955 SkUNREACHABLE;
John Stiles93442622020-09-11 12:11:27 -0400956 }
957
John Stiles70957c82020-10-02 16:42:10 -0400958 // Pop our symbol and enclosing-statement stacks.
959 fSymbolTableStack.resize(oldSymbolStackSize);
960 fEnclosingStmtStack.resize(oldEnclosingStmtStackSize);
961 }
962
963 void visitExpression(std::unique_ptr<Expression>* expr) {
964 if (!*expr) {
965 return;
John Stiles93442622020-09-11 12:11:27 -0400966 }
John Stiles70957c82020-10-02 16:42:10 -0400967
968 switch ((*expr)->kind()) {
969 case Expression::Kind::kBoolLiteral:
970 case Expression::Kind::kDefined:
971 case Expression::Kind::kExternalValue:
972 case Expression::Kind::kFieldAccess:
973 case Expression::Kind::kFloatLiteral:
974 case Expression::Kind::kFunctionReference:
975 case Expression::Kind::kIntLiteral:
976 case Expression::Kind::kNullLiteral:
977 case Expression::Kind::kSetting:
978 case Expression::Kind::kTypeReference:
979 case Expression::Kind::kVariableReference:
980 // Nothing to scan here.
981 break;
982
983 case Expression::Kind::kBinary: {
984 BinaryExpression& binaryExpr = (*expr)->as<BinaryExpression>();
985 this->visitExpression(&binaryExpr.leftPointer());
986
987 // Logical-and and logical-or binary expressions do not inline the right side,
988 // because that would invalidate short-circuiting. That is, when evaluating
989 // expressions like these:
990 // (false && x()) // always false
991 // (true || y()) // always true
992 // It is illegal for side-effects from x() or y() to occur. The simplest way to
993 // enforce that rule is to avoid inlining the right side entirely. However, it is
994 // safe for other types of binary expression to inline both sides.
995 Token::Kind op = binaryExpr.getOperator();
996 bool shortCircuitable = (op == Token::Kind::TK_LOGICALAND ||
997 op == Token::Kind::TK_LOGICALOR);
998 if (!shortCircuitable) {
999 this->visitExpression(&binaryExpr.rightPointer());
1000 }
1001 break;
1002 }
1003 case Expression::Kind::kConstructor: {
1004 Constructor& constructorExpr = (*expr)->as<Constructor>();
1005 for (std::unique_ptr<Expression>& arg : constructorExpr.arguments()) {
1006 this->visitExpression(&arg);
1007 }
1008 break;
1009 }
1010 case Expression::Kind::kExternalFunctionCall: {
1011 ExternalFunctionCall& funcCallExpr = (*expr)->as<ExternalFunctionCall>();
1012 for (std::unique_ptr<Expression>& arg : funcCallExpr.arguments()) {
1013 this->visitExpression(&arg);
1014 }
1015 break;
1016 }
1017 case Expression::Kind::kFunctionCall: {
1018 FunctionCall& funcCallExpr = (*expr)->as<FunctionCall>();
Ethan Nicholas0dec9922020-10-05 15:51:52 -04001019 for (std::unique_ptr<Expression>& arg : funcCallExpr.arguments()) {
John Stiles70957c82020-10-02 16:42:10 -04001020 this->visitExpression(&arg);
1021 }
1022 this->addInlineCandidate(expr);
1023 break;
1024 }
1025 case Expression::Kind::kIndex:{
1026 IndexExpression& indexExpr = (*expr)->as<IndexExpression>();
Ethan Nicholas2a4952d2020-10-08 15:35:56 -04001027 this->visitExpression(&indexExpr.base());
1028 this->visitExpression(&indexExpr.index());
John Stiles70957c82020-10-02 16:42:10 -04001029 break;
1030 }
1031 case Expression::Kind::kPostfix: {
1032 PostfixExpression& postfixExpr = (*expr)->as<PostfixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -04001033 this->visitExpression(&postfixExpr.operand());
John Stiles70957c82020-10-02 16:42:10 -04001034 break;
1035 }
1036 case Expression::Kind::kPrefix: {
1037 PrefixExpression& prefixExpr = (*expr)->as<PrefixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -04001038 this->visitExpression(&prefixExpr.operand());
John Stiles70957c82020-10-02 16:42:10 -04001039 break;
1040 }
1041 case Expression::Kind::kSwizzle: {
1042 Swizzle& swizzleExpr = (*expr)->as<Swizzle>();
1043 this->visitExpression(&swizzleExpr.fBase);
1044 break;
1045 }
1046 case Expression::Kind::kTernary: {
1047 TernaryExpression& ternaryExpr = (*expr)->as<TernaryExpression>();
1048 // The test expression is a candidate for inlining.
Ethan Nicholasdd218162020-10-08 05:48:01 -04001049 this->visitExpression(&ternaryExpr.test());
John Stiles70957c82020-10-02 16:42:10 -04001050 // The true- and false-expressions cannot be inlined, because we are only allowed to
1051 // evaluate one side.
1052 break;
1053 }
1054 default:
1055 SkUNREACHABLE;
1056 }
1057 }
1058
1059 void addInlineCandidate(std::unique_ptr<Expression>* candidate) {
1060 fCandidateList->fCandidates.push_back(
1061 InlineCandidate{fSymbolTableStack.back(),
1062 find_parent_statement(fEnclosingStmtStack),
1063 fEnclosingStmtStack.back(),
1064 candidate,
1065 fEnclosingFunction,
1066 /*isLargeFunction=*/false});
1067 }
John Stiles2d7973a2020-10-02 15:01:03 -04001068};
John Stiles93442622020-09-11 12:11:27 -04001069
John Stiles2d7973a2020-10-02 15:01:03 -04001070bool Inliner::candidateCanBeInlined(const InlineCandidate& candidate, InlinabilityCache* cache) {
Ethan Nicholas0dec9922020-10-05 15:51:52 -04001071 const FunctionDeclaration& funcDecl =
1072 (*candidate.fCandidateExpr)->as<FunctionCall>().function();
John Stiles915a38c2020-09-14 09:38:13 -04001073
John Stiles2d7973a2020-10-02 15:01:03 -04001074 auto [iter, wasInserted] = cache->insert({&funcDecl, false});
1075 if (wasInserted) {
1076 // Recursion is forbidden here to avoid an infinite death spiral of inlining.
Ethan Nicholased84b732020-10-08 11:45:44 -04001077 iter->second = this->isSafeToInline(funcDecl.definition()) &&
John Stiles2d7973a2020-10-02 15:01:03 -04001078 !contains_recursive_call(funcDecl);
John Stiles93442622020-09-11 12:11:27 -04001079 }
1080
John Stiles2d7973a2020-10-02 15:01:03 -04001081 return iter->second;
1082}
1083
1084bool Inliner::isLargeFunction(const FunctionDefinition* functionDef) {
1085 return Analysis::NodeCountExceeds(*functionDef, fSettings->fInlineThreshold);
1086}
1087
1088bool Inliner::isLargeFunction(const InlineCandidate& candidate, LargeFunctionCache* cache) {
Ethan Nicholas0dec9922020-10-05 15:51:52 -04001089 const FunctionDeclaration& funcDecl =
1090 (*candidate.fCandidateExpr)->as<FunctionCall>().function();
John Stiles2d7973a2020-10-02 15:01:03 -04001091
1092 auto [iter, wasInserted] = cache->insert({&funcDecl, false});
1093 if (wasInserted) {
Ethan Nicholased84b732020-10-08 11:45:44 -04001094 iter->second = this->isLargeFunction(funcDecl.definition());
John Stiles2d7973a2020-10-02 15:01:03 -04001095 }
1096
1097 return iter->second;
1098}
1099
1100void Inliner::buildCandidateList(Program& program, InlineCandidateList* candidateList) {
1101 // This is structured much like a ProgramVisitor, but does not actually use ProgramVisitor.
1102 // The analyzer needs to keep track of the `unique_ptr<T>*` of statements and expressions so
1103 // that they can later be replaced, and ProgramVisitor does not provide this; it only provides a
1104 // `const T&`.
1105 InlineCandidateAnalyzer analyzer;
1106 analyzer.visit(program, candidateList);
1107
1108 // Remove candidates that are not safe to inline.
1109 std::vector<InlineCandidate>& candidates = candidateList->fCandidates;
1110 InlinabilityCache cache;
1111 candidates.erase(std::remove_if(candidates.begin(),
1112 candidates.end(),
1113 [&](const InlineCandidate& candidate) {
1114 return !this->candidateCanBeInlined(candidate, &cache);
1115 }),
1116 candidates.end());
1117
1118 // Determine whether each candidate function exceeds our inlining size threshold or not. These
1119 // can still be valid candidates if they are only called one time, so we don't remove them from
1120 // the candidate list, but they will not be inlined if they're called more than once.
1121 LargeFunctionCache largeFunctionCache;
1122 for (InlineCandidate& candidate : candidates) {
1123 candidate.fIsLargeFunction = this->isLargeFunction(candidate, &largeFunctionCache);
1124 }
1125}
1126
1127bool Inliner::analyze(Program& program) {
1128 InlineCandidateList candidateList;
1129 this->buildCandidateList(program, &candidateList);
1130
John Stiles915a38c2020-09-14 09:38:13 -04001131 // Inline the candidates where we've determined that it's safe to do so.
1132 std::unordered_set<const std::unique_ptr<Statement>*> enclosingStmtSet;
1133 bool madeChanges = false;
John Stiles2d7973a2020-10-02 15:01:03 -04001134 for (const InlineCandidate& candidate : candidateList.fCandidates) {
John Stiles915a38c2020-09-14 09:38:13 -04001135 FunctionCall& funcCall = (*candidate.fCandidateExpr)->as<FunctionCall>();
Ethan Nicholas0dec9922020-10-05 15:51:52 -04001136 const FunctionDeclaration* funcDecl = &funcCall.function();
John Stiles915a38c2020-09-14 09:38:13 -04001137
John Stiles2d7973a2020-10-02 15:01:03 -04001138 // If the function is large, not marked `inline`, and is called more than once, it's a bad
1139 // idea to inline it.
1140 if (candidate.fIsLargeFunction &&
Ethan Nicholased84b732020-10-08 11:45:44 -04001141 !(funcDecl->modifiers().fFlags & Modifiers::kInline_Flag) &&
1142 funcDecl->callCount() > 1) {
John Stiles915a38c2020-09-14 09:38:13 -04001143 continue;
1144 }
1145
1146 // Inlining two expressions using the same enclosing statement in the same inlining pass
1147 // does not work properly. If this happens, skip it; we'll get it in the next pass.
1148 auto [unusedIter, inserted] = enclosingStmtSet.insert(candidate.fEnclosingStmt);
1149 if (!inserted) {
1150 continue;
1151 }
1152
1153 // Convert the function call to its inlined equivalent.
Brian Osman3887a012020-09-30 13:22:27 -04001154 InlinedCall inlinedCall = this->inlineCall(&funcCall, candidate.fSymbols,
1155 &candidate.fEnclosingFunction->fDeclaration);
John Stiles915a38c2020-09-14 09:38:13 -04001156 if (inlinedCall.fInlinedBody) {
1157 // Ensure that the inlined body has a scope if it needs one.
John Stiles6d696082020-10-01 10:18:54 -04001158 this->ensureScopedBlocks(inlinedCall.fInlinedBody.get(), candidate.fParentStmt->get());
John Stiles915a38c2020-09-14 09:38:13 -04001159
1160 // Move the enclosing statement to the end of the unscoped Block containing the inlined
1161 // function, then replace the enclosing statement with that Block.
1162 // Before:
1163 // fInlinedBody = Block{ stmt1, stmt2, stmt3 }
1164 // fEnclosingStmt = stmt4
1165 // After:
1166 // fInlinedBody = null
1167 // fEnclosingStmt = Block{ stmt1, stmt2, stmt3, stmt4 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001168 inlinedCall.fInlinedBody->children().push_back(std::move(*candidate.fEnclosingStmt));
John Stiles915a38c2020-09-14 09:38:13 -04001169 *candidate.fEnclosingStmt = std::move(inlinedCall.fInlinedBody);
1170 }
1171
1172 // Replace the candidate function call with our replacement expression.
1173 *candidate.fCandidateExpr = std::move(inlinedCall.fReplacementExpr);
1174 madeChanges = true;
1175
1176 // Note that nothing was destroyed except for the FunctionCall. All other nodes should
1177 // remain valid.
1178 }
1179
1180 return madeChanges;
John Stiles93442622020-09-11 12:11:27 -04001181}
1182
John Stiles44e96be2020-08-31 13:16:04 -04001183} // namespace SkSL