John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 10 | #include "limits.h" |
| 11 | #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 Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 36 | #include "src/sksl/ir/SkSLInlineMarker.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 37 | #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" |
| 52 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 53 | #include "src/sksl/ir/SkSLVariable.h" |
| 54 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 55 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 56 | |
| 57 | namespace SkSL { |
| 58 | namespace { |
| 59 | |
John Stiles | 44dff4f | 2020-09-21 12:28:01 -0400 | [diff] [blame] | 60 | static bool contains_returns_above_limit(const FunctionDefinition& funcDef, int limit) { |
| 61 | class CountReturnsWithLimit : public ProgramVisitor { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 62 | public: |
John Stiles | 44dff4f | 2020-09-21 12:28:01 -0400 | [diff] [blame] | 63 | CountReturnsWithLimit(const FunctionDefinition& funcDef, int limit) : fLimit(limit) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 64 | this->visitProgramElement(funcDef); |
| 65 | } |
| 66 | |
| 67 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 68 | switch (stmt.kind()) { |
| 69 | case Statement::Kind::kReturn: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 70 | ++fNumReturns; |
John Stiles | 44dff4f | 2020-09-21 12:28:01 -0400 | [diff] [blame] | 71 | return (fNumReturns > fLimit) || INHERITED::visitStatement(stmt); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 72 | |
| 73 | default: |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 74 | return INHERITED::visitStatement(stmt); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | int fNumReturns = 0; |
John Stiles | 44dff4f | 2020-09-21 12:28:01 -0400 | [diff] [blame] | 79 | int fLimit = 0; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 80 | using INHERITED = ProgramVisitor; |
| 81 | }; |
| 82 | |
John Stiles | 44dff4f | 2020-09-21 12:28:01 -0400 | [diff] [blame] | 83 | return CountReturnsWithLimit{funcDef, limit}.fNumReturns > limit; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static int count_returns_at_end_of_control_flow(const FunctionDefinition& funcDef) { |
| 87 | class CountReturnsAtEndOfControlFlow : public ProgramVisitor { |
| 88 | public: |
| 89 | CountReturnsAtEndOfControlFlow(const FunctionDefinition& funcDef) { |
| 90 | this->visitProgramElement(funcDef); |
| 91 | } |
| 92 | |
| 93 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 94 | switch (stmt.kind()) { |
| 95 | case Statement::Kind::kBlock: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 96 | // Check only the last statement of a block. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 97 | const auto& block = stmt.as<Block>(); |
| 98 | return block.children().size() && |
| 99 | this->visitStatement(*block.children().back()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 100 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 101 | case Statement::Kind::kSwitch: |
| 102 | case Statement::Kind::kWhile: |
| 103 | case Statement::Kind::kDo: |
| 104 | case Statement::Kind::kFor: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 105 | // Don't introspect switches or loop structures at all. |
| 106 | return false; |
| 107 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 108 | case Statement::Kind::kReturn: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 109 | ++fNumReturns; |
| 110 | [[fallthrough]]; |
| 111 | |
| 112 | default: |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 113 | return INHERITED::visitStatement(stmt); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | int fNumReturns = 0; |
| 118 | using INHERITED = ProgramVisitor; |
| 119 | }; |
| 120 | |
| 121 | return CountReturnsAtEndOfControlFlow{funcDef}.fNumReturns; |
| 122 | } |
| 123 | |
| 124 | static int count_returns_in_breakable_constructs(const FunctionDefinition& funcDef) { |
| 125 | class CountReturnsInBreakableConstructs : public ProgramVisitor { |
| 126 | public: |
| 127 | CountReturnsInBreakableConstructs(const FunctionDefinition& funcDef) { |
| 128 | this->visitProgramElement(funcDef); |
| 129 | } |
| 130 | |
| 131 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 132 | switch (stmt.kind()) { |
| 133 | case Statement::Kind::kSwitch: |
| 134 | case Statement::Kind::kWhile: |
| 135 | case Statement::Kind::kDo: |
| 136 | case Statement::Kind::kFor: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 137 | ++fInsideBreakableConstruct; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 138 | bool result = INHERITED::visitStatement(stmt); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 139 | --fInsideBreakableConstruct; |
| 140 | return result; |
| 141 | } |
| 142 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 143 | case Statement::Kind::kReturn: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 144 | fNumReturns += (fInsideBreakableConstruct > 0) ? 1 : 0; |
| 145 | [[fallthrough]]; |
| 146 | |
| 147 | default: |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 148 | return INHERITED::visitStatement(stmt); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | int fNumReturns = 0; |
| 153 | int fInsideBreakableConstruct = 0; |
| 154 | using INHERITED = ProgramVisitor; |
| 155 | }; |
| 156 | |
| 157 | return CountReturnsInBreakableConstructs{funcDef}.fNumReturns; |
| 158 | } |
| 159 | |
| 160 | static bool has_early_return(const FunctionDefinition& funcDef) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 161 | int returnsAtEndOfControlFlow = count_returns_at_end_of_control_flow(funcDef); |
John Stiles | 44dff4f | 2020-09-21 12:28:01 -0400 | [diff] [blame] | 162 | return contains_returns_above_limit(funcDef, returnsAtEndOfControlFlow); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 163 | } |
| 164 | |
John Stiles | 991b09d | 2020-09-10 13:33:40 -0400 | [diff] [blame] | 165 | static bool contains_recursive_call(const FunctionDeclaration& funcDecl) { |
| 166 | class ContainsRecursiveCall : public ProgramVisitor { |
| 167 | public: |
| 168 | bool visit(const FunctionDeclaration& funcDecl) { |
| 169 | fFuncDecl = &funcDecl; |
| 170 | return funcDecl.fDefinition ? this->visitProgramElement(*funcDecl.fDefinition) |
| 171 | : false; |
| 172 | } |
| 173 | |
| 174 | bool visitExpression(const Expression& expr) override { |
| 175 | if (expr.is<FunctionCall>() && expr.as<FunctionCall>().fFunction.matches(*fFuncDecl)) { |
| 176 | return true; |
| 177 | } |
| 178 | return INHERITED::visitExpression(expr); |
| 179 | } |
| 180 | |
| 181 | bool visitStatement(const Statement& stmt) override { |
| 182 | if (stmt.is<InlineMarker>() && stmt.as<InlineMarker>().fFuncDecl->matches(*fFuncDecl)) { |
| 183 | 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 Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 195 | static const Type* copy_if_needed(const Type* src, SymbolTable& symbolTable) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 196 | if (src->typeKind() == Type::TypeKind::kArray) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 197 | return symbolTable.takeOwnershipOfSymbol(std::make_unique<Type>(*src)); |
| 198 | } |
| 199 | return src; |
| 200 | } |
| 201 | |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 202 | static Statement* find_parent_statement(const std::vector<std::unique_ptr<Statement>*>& stmtStack) { |
| 203 | SkASSERT(!stmtStack.empty()); |
| 204 | |
| 205 | // Walk the statement stack from back to front, ignoring the last element (which is the |
| 206 | // enclosing statement). |
| 207 | auto iter = stmtStack.rbegin(); |
| 208 | ++iter; |
| 209 | |
| 210 | // Anything counts as a parent statement other than a scopeless Block. |
| 211 | for (; iter != stmtStack.rend(); ++iter) { |
| 212 | Statement* stmt = (*iter)->get(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 213 | if (!stmt->is<Block>() || stmt->as<Block>().isScope()) { |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 214 | return stmt; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // There wasn't any parent statement to be found. |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 222 | std::unique_ptr<Expression> clone_with_ref_kind(const Expression& expr, |
| 223 | VariableReference::RefKind refKind) { |
| 224 | std::unique_ptr<Expression> clone = expr.clone(); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame^] | 225 | class SetRefKindInExpression : public ProgramWriter { |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 226 | public: |
| 227 | SetRefKindInExpression(VariableReference::RefKind refKind) : fRefKind(refKind) {} |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame^] | 228 | bool visitExpression(Expression& expr) override { |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 229 | if (expr.is<VariableReference>()) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame^] | 230 | expr.as<VariableReference>().setRefKind(fRefKind); |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 231 | } |
| 232 | return INHERITED::visitExpression(expr); |
| 233 | } |
| 234 | |
| 235 | private: |
| 236 | VariableReference::RefKind fRefKind; |
| 237 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame^] | 238 | using INHERITED = ProgramWriter; |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | SetRefKindInExpression{refKind}.visitExpression(*clone); |
| 242 | return clone; |
| 243 | } |
| 244 | |
John Stiles | 44733aa | 2020-09-29 17:42:23 -0400 | [diff] [blame] | 245 | bool is_trivial_argument(const Expression& argument) { |
| 246 | return argument.is<VariableReference>() || |
| 247 | (argument.is<Swizzle>() && is_trivial_argument(*argument.as<Swizzle>().fBase)) || |
| 248 | (argument.is<FieldAccess>() && is_trivial_argument(*argument.as<FieldAccess>().fBase)) || |
| 249 | (argument.is<IndexExpression>() && |
| 250 | argument.as<IndexExpression>().fIndex->is<IntLiteral>() && |
| 251 | is_trivial_argument(*argument.as<IndexExpression>().fBase)); |
| 252 | } |
| 253 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 254 | } // namespace |
| 255 | |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 256 | void Inliner::ensureScopedBlocks(Statement* inlinedBody, Statement* parentStmt) { |
| 257 | // No changes necessary if this statement isn't actually a block. |
| 258 | if (!inlinedBody || !inlinedBody->is<Block>()) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | // No changes necessary if the parent statement doesn't require a scope. |
| 263 | if (!parentStmt || !(parentStmt->is<IfStatement>() || parentStmt->is<ForStatement>() || |
| 264 | parentStmt->is<DoStatement>() || parentStmt->is<WhileStatement>())) { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | Block& block = inlinedBody->as<Block>(); |
| 269 | |
| 270 | // The inliner will create inlined function bodies as a Block containing multiple statements, |
| 271 | // but no scope. Normally, this is fine, but if this block is used as the statement for a |
| 272 | // do/for/if/while, this isn't actually possible to represent textually; a scope must be added |
| 273 | // for the generated code to match the intent. In the case of Blocks nested inside other Blocks, |
| 274 | // we add the scope to the outermost block if needed. Zero-statement blocks have similar |
| 275 | // issues--if we don't represent the Block textually somehow, we run the risk of accidentally |
| 276 | // absorbing the following statement into our loop--so we also add a scope to these. |
| 277 | for (Block* nestedBlock = █; ) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 278 | if (nestedBlock->isScope()) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 279 | // We found an explicit scope; all is well. |
| 280 | return; |
| 281 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 282 | if (nestedBlock->children().size() != 1) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 283 | // We found a block with multiple (or zero) statements, but no scope? Let's add a scope |
| 284 | // to the outermost block. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 285 | block.setIsScope(true); |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 286 | return; |
| 287 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 288 | if (!nestedBlock->children()[0]->is<Block>()) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 289 | // This block has exactly one thing inside, and it's not another block. No need to scope |
| 290 | // it. |
| 291 | return; |
| 292 | } |
| 293 | // We have to go deeper. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 294 | nestedBlock = &nestedBlock->children()[0]->as<Block>(); |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 298 | void Inliner::reset(const Context& context, const Program::Settings& settings) { |
| 299 | fContext = &context; |
| 300 | fSettings = &settings; |
| 301 | fInlineVarCounter = 0; |
| 302 | } |
| 303 | |
John Stiles | c75abb8 | 2020-09-14 18:24:12 -0400 | [diff] [blame] | 304 | String Inliner::uniqueNameForInlineVar(const String& baseName, SymbolTable* symbolTable) { |
| 305 | // If the base name starts with an underscore, like "_coords", we can't append another |
| 306 | // underscore, because OpenGL disallows two consecutive underscores anywhere in the string. But |
| 307 | // in the general case, using the underscore as a splitter reads nicely enough that it's worth |
| 308 | // putting in this special case. |
| 309 | const char* splitter = baseName.startsWith("_") ? "" : "_"; |
| 310 | |
| 311 | // Append a unique numeric prefix to avoid name overlap. Check the symbol table to make sure |
| 312 | // we're not reusing an existing name. (Note that within a single compilation pass, this check |
| 313 | // isn't fully comprehensive, as code isn't always generated in top-to-bottom order.) |
| 314 | String uniqueName; |
| 315 | for (;;) { |
| 316 | uniqueName = String::printf("_%d%s%s", fInlineVarCounter++, splitter, baseName.c_str()); |
| 317 | StringFragment frag{uniqueName.data(), uniqueName.length()}; |
| 318 | if ((*symbolTable)[frag] == nullptr) { |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return uniqueName; |
| 324 | } |
| 325 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 326 | std::unique_ptr<Expression> Inliner::inlineExpression(int offset, |
| 327 | VariableRewriteMap* varMap, |
| 328 | const Expression& expression) { |
| 329 | auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> { |
| 330 | if (e) { |
| 331 | return this->inlineExpression(offset, varMap, *e); |
| 332 | } |
| 333 | return nullptr; |
| 334 | }; |
| 335 | auto argList = [&](const std::vector<std::unique_ptr<Expression>>& originalArgs) |
| 336 | -> std::vector<std::unique_ptr<Expression>> { |
| 337 | std::vector<std::unique_ptr<Expression>> args; |
| 338 | args.reserve(originalArgs.size()); |
| 339 | for (const std::unique_ptr<Expression>& arg : originalArgs) { |
| 340 | args.push_back(expr(arg)); |
| 341 | } |
| 342 | return args; |
| 343 | }; |
| 344 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 345 | switch (expression.kind()) { |
| 346 | case Expression::Kind::kBinary: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 347 | const BinaryExpression& b = expression.as<BinaryExpression>(); |
| 348 | return std::make_unique<BinaryExpression>(offset, |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 349 | expr(b.leftPointer()), |
| 350 | b.getOperator(), |
| 351 | expr(b.rightPointer()), |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 352 | &b.type()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 353 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 354 | case Expression::Kind::kBoolLiteral: |
| 355 | case Expression::Kind::kIntLiteral: |
| 356 | case Expression::Kind::kFloatLiteral: |
| 357 | case Expression::Kind::kNullLiteral: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 358 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 359 | case Expression::Kind::kConstructor: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 360 | const Constructor& constructor = expression.as<Constructor>(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 361 | return std::make_unique<Constructor>(offset, &constructor.type(), |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 362 | argList(constructor.arguments())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 363 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 364 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 365 | const ExternalFunctionCall& externalCall = expression.as<ExternalFunctionCall>(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 366 | return std::make_unique<ExternalFunctionCall>(offset, &externalCall.type(), |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 367 | externalCall.fFunction, |
| 368 | argList(externalCall.fArguments)); |
| 369 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 370 | case Expression::Kind::kExternalValue: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 371 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 372 | case Expression::Kind::kFieldAccess: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 373 | const FieldAccess& f = expression.as<FieldAccess>(); |
| 374 | return std::make_unique<FieldAccess>(expr(f.fBase), f.fFieldIndex, f.fOwnerKind); |
| 375 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 376 | case Expression::Kind::kFunctionCall: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 377 | const FunctionCall& funcCall = expression.as<FunctionCall>(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 378 | return std::make_unique<FunctionCall>(offset, &funcCall.type(), funcCall.fFunction, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 379 | argList(funcCall.fArguments)); |
| 380 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 381 | case Expression::Kind::kFunctionReference: |
Brian Osman | 2b3b35f | 2020-09-08 09:17:36 -0400 | [diff] [blame] | 382 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 383 | case Expression::Kind::kIndex: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 384 | const IndexExpression& idx = expression.as<IndexExpression>(); |
| 385 | return std::make_unique<IndexExpression>(*fContext, expr(idx.fBase), expr(idx.fIndex)); |
| 386 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 387 | case Expression::Kind::kPrefix: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 388 | const PrefixExpression& p = expression.as<PrefixExpression>(); |
| 389 | return std::make_unique<PrefixExpression>(p.fOperator, expr(p.fOperand)); |
| 390 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 391 | case Expression::Kind::kPostfix: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 392 | const PostfixExpression& p = expression.as<PostfixExpression>(); |
| 393 | return std::make_unique<PostfixExpression>(expr(p.fOperand), p.fOperator); |
| 394 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 395 | case Expression::Kind::kSetting: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 396 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 397 | case Expression::Kind::kSwizzle: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 398 | const Swizzle& s = expression.as<Swizzle>(); |
| 399 | return std::make_unique<Swizzle>(*fContext, expr(s.fBase), s.fComponents); |
| 400 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 401 | case Expression::Kind::kTernary: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 402 | const TernaryExpression& t = expression.as<TernaryExpression>(); |
| 403 | return std::make_unique<TernaryExpression>(offset, expr(t.fTest), |
| 404 | expr(t.fIfTrue), expr(t.fIfFalse)); |
| 405 | } |
Brian Osman | 83ba930 | 2020-09-11 13:33:46 -0400 | [diff] [blame] | 406 | case Expression::Kind::kTypeReference: |
| 407 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 408 | case Expression::Kind::kVariableReference: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 409 | const VariableReference& v = expression.as<VariableReference>(); |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 410 | auto varMapIter = varMap->find(v.fVariable); |
| 411 | if (varMapIter != varMap->end()) { |
| 412 | return clone_with_ref_kind(*varMapIter->second, v.fRefKind); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 413 | } |
| 414 | return v.clone(); |
| 415 | } |
| 416 | default: |
| 417 | SkASSERT(false); |
| 418 | return nullptr; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | std::unique_ptr<Statement> Inliner::inlineStatement(int offset, |
| 423 | VariableRewriteMap* varMap, |
| 424 | SymbolTable* symbolTableForStatement, |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 425 | const Expression* resultExpr, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 426 | bool haveEarlyReturns, |
| 427 | const Statement& statement) { |
| 428 | auto stmt = [&](const std::unique_ptr<Statement>& s) -> std::unique_ptr<Statement> { |
| 429 | if (s) { |
John Stiles | a5f3c31 | 2020-09-22 12:05:16 -0400 | [diff] [blame] | 430 | return this->inlineStatement(offset, varMap, symbolTableForStatement, resultExpr, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 431 | haveEarlyReturns, *s); |
| 432 | } |
| 433 | return nullptr; |
| 434 | }; |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 435 | auto blockStmts = [&](const Block& block) { |
| 436 | std::vector<std::unique_ptr<Statement>> result; |
| 437 | for (const std::unique_ptr<Statement>& child : block.children()) { |
| 438 | result.push_back(stmt(child)); |
| 439 | } |
| 440 | return result; |
| 441 | }; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 442 | auto stmts = [&](const std::vector<std::unique_ptr<Statement>>& ss) { |
| 443 | std::vector<std::unique_ptr<Statement>> result; |
| 444 | for (const auto& s : ss) { |
| 445 | result.push_back(stmt(s)); |
| 446 | } |
| 447 | return result; |
| 448 | }; |
| 449 | auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> { |
| 450 | if (e) { |
| 451 | return this->inlineExpression(offset, varMap, *e); |
| 452 | } |
| 453 | return nullptr; |
| 454 | }; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 455 | switch (statement.kind()) { |
| 456 | case Statement::Kind::kBlock: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 457 | const Block& b = statement.as<Block>(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 458 | return std::make_unique<Block>(offset, blockStmts(b), b.symbolTable(), b.isScope()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 461 | case Statement::Kind::kBreak: |
| 462 | case Statement::Kind::kContinue: |
| 463 | case Statement::Kind::kDiscard: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 464 | return statement.clone(); |
| 465 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 466 | case Statement::Kind::kDo: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 467 | const DoStatement& d = statement.as<DoStatement>(); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 468 | return std::make_unique<DoStatement>(offset, stmt(d.statement()), expr(d.test())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 469 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 470 | case Statement::Kind::kExpression: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 471 | const ExpressionStatement& e = statement.as<ExpressionStatement>(); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 472 | return std::make_unique<ExpressionStatement>(expr(e.expression())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 473 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 474 | case Statement::Kind::kFor: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 475 | const ForStatement& f = statement.as<ForStatement>(); |
| 476 | // need to ensure initializer is evaluated first so that we've already remapped its |
| 477 | // declarations by the time we evaluate test & next |
| 478 | std::unique_ptr<Statement> initializer = stmt(f.fInitializer); |
| 479 | return std::make_unique<ForStatement>(offset, std::move(initializer), expr(f.fTest), |
| 480 | expr(f.fNext), stmt(f.fStatement), f.fSymbols); |
| 481 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 482 | case Statement::Kind::kIf: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 483 | const IfStatement& i = statement.as<IfStatement>(); |
| 484 | return std::make_unique<IfStatement>(offset, i.fIsStatic, expr(i.fTest), |
| 485 | stmt(i.fIfTrue), stmt(i.fIfFalse)); |
| 486 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 487 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 488 | case Statement::Kind::kNop: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 489 | return statement.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 490 | case Statement::Kind::kReturn: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 491 | const ReturnStatement& r = statement.as<ReturnStatement>(); |
| 492 | if (r.fExpression) { |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 493 | SkASSERT(resultExpr); |
John Stiles | a5f3c31 | 2020-09-22 12:05:16 -0400 | [diff] [blame] | 494 | auto assignment = |
| 495 | std::make_unique<ExpressionStatement>(std::make_unique<BinaryExpression>( |
| 496 | offset, |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 497 | clone_with_ref_kind(*resultExpr, VariableReference::kWrite_RefKind), |
John Stiles | a5f3c31 | 2020-09-22 12:05:16 -0400 | [diff] [blame] | 498 | Token::Kind::TK_EQ, |
| 499 | expr(r.fExpression), |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 500 | &resultExpr->type())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 501 | if (haveEarlyReturns) { |
| 502 | std::vector<std::unique_ptr<Statement>> block; |
| 503 | block.push_back(std::move(assignment)); |
| 504 | block.emplace_back(new BreakStatement(offset)); |
| 505 | return std::make_unique<Block>(offset, std::move(block), /*symbols=*/nullptr, |
| 506 | /*isScope=*/true); |
| 507 | } else { |
| 508 | return std::move(assignment); |
| 509 | } |
| 510 | } else { |
| 511 | if (haveEarlyReturns) { |
| 512 | return std::make_unique<BreakStatement>(offset); |
| 513 | } else { |
| 514 | return std::make_unique<Nop>(); |
| 515 | } |
| 516 | } |
| 517 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 518 | case Statement::Kind::kSwitch: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 519 | const SwitchStatement& ss = statement.as<SwitchStatement>(); |
| 520 | std::vector<std::unique_ptr<SwitchCase>> cases; |
| 521 | for (const auto& sc : ss.fCases) { |
| 522 | cases.emplace_back(new SwitchCase(offset, expr(sc->fValue), |
| 523 | stmts(sc->fStatements))); |
| 524 | } |
| 525 | return std::make_unique<SwitchStatement>(offset, ss.fIsStatic, expr(ss.fValue), |
| 526 | std::move(cases), ss.fSymbols); |
| 527 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 528 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 529 | const VarDeclaration& decl = statement.as<VarDeclaration>(); |
| 530 | std::vector<std::unique_ptr<Expression>> sizes; |
| 531 | for (const auto& size : decl.fSizes) { |
| 532 | sizes.push_back(expr(size)); |
| 533 | } |
| 534 | std::unique_ptr<Expression> initialValue = expr(decl.fValue); |
| 535 | const Variable* old = decl.fVar; |
John Stiles | c75abb8 | 2020-09-14 18:24:12 -0400 | [diff] [blame] | 536 | // We assign unique names to inlined variables--scopes hide most of the problems in this |
| 537 | // regard, but see `InlinerAvoidsVariableNameOverlap` for a counterexample where unique |
| 538 | // names are important. |
| 539 | auto name = std::make_unique<String>( |
| 540 | this->uniqueNameForInlineVar(String(old->fName), symbolTableForStatement)); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 541 | const String* namePtr = symbolTableForStatement->takeOwnershipOfString(std::move(name)); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 542 | const Type* typePtr = copy_if_needed(&old->type(), *symbolTableForStatement); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 543 | const Variable* clone = symbolTableForStatement->takeOwnershipOfSymbol( |
| 544 | std::make_unique<Variable>(offset, |
| 545 | old->fModifiers, |
| 546 | namePtr->c_str(), |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 547 | typePtr, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 548 | old->fStorage, |
| 549 | initialValue.get())); |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 550 | (*varMap)[old] = std::make_unique<VariableReference>(offset, clone); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 551 | return std::make_unique<VarDeclaration>(clone, std::move(sizes), |
| 552 | std::move(initialValue)); |
| 553 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 554 | case Statement::Kind::kVarDeclarations: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 555 | const VarDeclarations& decls = *statement.as<VarDeclarationsStatement>().fDeclaration; |
| 556 | std::vector<std::unique_ptr<VarDeclaration>> vars; |
| 557 | for (const auto& var : decls.fVars) { |
| 558 | vars.emplace_back(&stmt(var).release()->as<VarDeclaration>()); |
| 559 | } |
| 560 | const Type* typePtr = copy_if_needed(&decls.fBaseType, *symbolTableForStatement); |
| 561 | return std::unique_ptr<Statement>(new VarDeclarationsStatement( |
| 562 | std::make_unique<VarDeclarations>(offset, typePtr, std::move(vars)))); |
| 563 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 564 | case Statement::Kind::kWhile: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 565 | const WhileStatement& w = statement.as<WhileStatement>(); |
| 566 | return std::make_unique<WhileStatement>(offset, expr(w.fTest), stmt(w.fStatement)); |
| 567 | } |
| 568 | default: |
| 569 | SkASSERT(false); |
| 570 | return nullptr; |
| 571 | } |
| 572 | } |
| 573 | |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 574 | Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 575 | SymbolTable* symbolTableForCall) { |
| 576 | // Inlining is more complicated here than in a typical compiler, because we have to have a |
| 577 | // high-level IR and can't just drop statements into the middle of an expression or even use |
| 578 | // gotos. |
| 579 | // |
| 580 | // Since we can't insert statements into an expression, we run the inline function as extra |
| 581 | // statements before the statement we're currently processing, relying on a lack of execution |
| 582 | // order guarantees. Since we can't use gotos (which are normally used to replace return |
| 583 | // statements), we wrap the whole function in a loop and use break statements to jump to the |
| 584 | // end. |
| 585 | SkASSERT(fSettings); |
| 586 | SkASSERT(fContext); |
| 587 | SkASSERT(call); |
| 588 | SkASSERT(this->isSafeToInline(*call, /*inlineThreshold=*/INT_MAX)); |
| 589 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 590 | std::vector<std::unique_ptr<Expression>>& arguments = call->fArguments; |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 591 | const int offset = call->fOffset; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 592 | const FunctionDefinition& function = *call->fFunction.fDefinition; |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 593 | const bool hasEarlyReturn = has_early_return(function); |
| 594 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 595 | InlinedCall inlinedCall; |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 596 | inlinedCall.fInlinedBody = std::make_unique<Block>(offset, |
| 597 | std::vector<std::unique_ptr<Statement>>{}, |
| 598 | /*symbols=*/nullptr, |
| 599 | /*isScope=*/false); |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 600 | |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 601 | Block& inlinedBody = *inlinedCall.fInlinedBody; |
| 602 | inlinedBody.children().reserve(1 + // Inline marker |
| 603 | 1 + // Result variable |
| 604 | arguments.size() + // Function arguments (passing in) |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 605 | arguments.size() + // Function arguments (copy out-params back) |
| 606 | 1); // Inlined code (Block or do-while loop) |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 607 | |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 608 | inlinedBody.children().push_back(std::make_unique<InlineMarker>(call->fFunction)); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 609 | |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 610 | auto makeInlineVar = |
| 611 | [&](const String& baseName, const Type* type, Modifiers modifiers, |
| 612 | std::unique_ptr<Expression>* initialValue) -> std::unique_ptr<Expression> { |
John Stiles | a003e81 | 2020-09-11 09:43:49 -0400 | [diff] [blame] | 613 | // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so |
| 614 | // replace them if they ever appear here. If this happens, we likely forgot to coerce a type |
| 615 | // somewhere during compilation. |
| 616 | if (type == fContext->fFloatLiteral_Type.get()) { |
John Stiles | d2be5c5 | 2020-09-11 14:58:06 -0400 | [diff] [blame] | 617 | SkDEBUGFAIL("found a $floatLiteral type while inlining"); |
John Stiles | a003e81 | 2020-09-11 09:43:49 -0400 | [diff] [blame] | 618 | type = fContext->fFloat_Type.get(); |
| 619 | } else if (type == fContext->fIntLiteral_Type.get()) { |
John Stiles | d2be5c5 | 2020-09-11 14:58:06 -0400 | [diff] [blame] | 620 | SkDEBUGFAIL("found an $intLiteral type while inlining"); |
John Stiles | a003e81 | 2020-09-11 09:43:49 -0400 | [diff] [blame] | 621 | type = fContext->fInt_Type.get(); |
| 622 | } |
| 623 | |
John Stiles | c75abb8 | 2020-09-14 18:24:12 -0400 | [diff] [blame] | 624 | // Provide our new variable with a unique name, and add it to our symbol table. |
| 625 | String uniqueName = this->uniqueNameForInlineVar(baseName, symbolTableForCall); |
John Stiles | cf936f9 | 2020-08-31 17:18:45 -0400 | [diff] [blame] | 626 | const String* namePtr = symbolTableForCall->takeOwnershipOfString( |
| 627 | std::make_unique<String>(std::move(uniqueName))); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 628 | StringFragment nameFrag{namePtr->c_str(), namePtr->length()}; |
| 629 | |
| 630 | // Add our new variable to the symbol table. |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 631 | auto newVar = std::make_unique<Variable>(/*offset=*/-1, Modifiers(), nameFrag, type, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 632 | Variable::kLocal_Storage, initialValue->get()); |
| 633 | const Variable* variableSymbol = symbolTableForCall->add(nameFrag, std::move(newVar)); |
| 634 | |
| 635 | // Prepare the variable declaration (taking extra care with `out` params to not clobber any |
| 636 | // initial value). |
| 637 | std::vector<std::unique_ptr<VarDeclaration>> variables; |
| 638 | if (initialValue && (modifiers.fFlags & Modifiers::kOut_Flag)) { |
| 639 | variables.push_back(std::make_unique<VarDeclaration>( |
| 640 | variableSymbol, /*sizes=*/std::vector<std::unique_ptr<Expression>>{}, |
| 641 | (*initialValue)->clone())); |
| 642 | } else { |
| 643 | variables.push_back(std::make_unique<VarDeclaration>( |
| 644 | variableSymbol, /*sizes=*/std::vector<std::unique_ptr<Expression>>{}, |
| 645 | std::move(*initialValue))); |
| 646 | } |
| 647 | |
| 648 | // Add the new variable-declaration statement to our block of extra statements. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 649 | inlinedBody.children().push_back(std::make_unique<VarDeclarationsStatement>( |
John Stiles | a003e81 | 2020-09-11 09:43:49 -0400 | [diff] [blame] | 650 | std::make_unique<VarDeclarations>(offset, type, std::move(variables)))); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 651 | |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 652 | return std::make_unique<VariableReference>(offset, variableSymbol); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 653 | }; |
| 654 | |
| 655 | // Create a variable to hold the result in the extra statements (excepting void). |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 656 | std::unique_ptr<Expression> resultExpr; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 657 | if (function.fDeclaration.fReturnType != *fContext->fVoid_Type) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 658 | std::unique_ptr<Expression> noInitialValue; |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 659 | resultExpr = makeInlineVar(String(function.fDeclaration.fName), |
| 660 | &function.fDeclaration.fReturnType, |
| 661 | Modifiers{}, &noInitialValue); |
| 662 | } |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 663 | |
| 664 | // Create variables in the extra statements to hold the arguments, and assign the arguments to |
| 665 | // them. |
| 666 | VariableRewriteMap varMap; |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 667 | std::vector<int> argsToCopyBack; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 668 | for (int i = 0; i < (int) arguments.size(); ++i) { |
| 669 | const Variable* param = function.fDeclaration.fParameters[i]; |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 670 | bool isOutParam = param->fModifiers.fFlags & Modifiers::kOut_Flag; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 671 | |
John Stiles | 44733aa | 2020-09-29 17:42:23 -0400 | [diff] [blame] | 672 | // If this argument can be inlined trivially (e.g. a swizzle, or a constant array index)... |
| 673 | if (is_trivial_argument(*arguments[i])) { |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 674 | // ... and it's an `out` param, or it isn't written to within the inline function... |
| 675 | if (isOutParam || !Analysis::StatementWritesToVariable(*function.fBody, *param)) { |
John Stiles | f201af8 | 2020-09-29 16:57:55 -0400 | [diff] [blame] | 676 | // ... we don't need to copy it at all! We can just use the existing expression. |
| 677 | varMap[param] = arguments[i]->clone(); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 678 | continue; |
| 679 | } |
| 680 | } |
| 681 | |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 682 | if (isOutParam) { |
| 683 | argsToCopyBack.push_back(i); |
| 684 | } |
| 685 | |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 686 | varMap[param] = makeInlineVar(String(param->fName), &arguments[i]->type(), |
| 687 | param->fModifiers, &arguments[i]); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | const Block& body = function.fBody->as<Block>(); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 691 | auto inlineBlock = std::make_unique<Block>(offset, std::vector<std::unique_ptr<Statement>>{}); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 692 | inlineBlock->children().reserve(body.children().size()); |
| 693 | for (const std::unique_ptr<Statement>& stmt : body.children()) { |
| 694 | inlineBlock->children().push_back(this->inlineStatement( |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 695 | offset, &varMap, symbolTableForCall, resultExpr.get(), hasEarlyReturn, *stmt)); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 696 | } |
| 697 | if (hasEarlyReturn) { |
| 698 | // Since we output to backends that don't have a goto statement (which would normally be |
| 699 | // used to perform an early return), we fake it by wrapping the function in a |
| 700 | // do { } while (false); and then use break statements to jump to the end in order to |
| 701 | // emulate a goto. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 702 | inlinedBody.children().push_back(std::make_unique<DoStatement>( |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 703 | /*offset=*/-1, |
| 704 | std::move(inlineBlock), |
| 705 | std::make_unique<BoolLiteral>(*fContext, offset, /*value=*/false))); |
| 706 | } else { |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 707 | // No early returns, so we can just dump the code in. We still need to keep the block so we |
| 708 | // don't get name conflicts with locals. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 709 | inlinedBody.children().push_back(std::move(inlineBlock)); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 710 | } |
| 711 | |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 712 | // Copy back the values of `out` parameters into their real destinations. |
| 713 | for (int i : argsToCopyBack) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 714 | const Variable* p = function.fDeclaration.fParameters[i]; |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 715 | SkASSERT(varMap.find(p) != varMap.end()); |
| 716 | inlinedBody.children().push_back( |
| 717 | std::make_unique<ExpressionStatement>(std::make_unique<BinaryExpression>( |
| 718 | offset, |
| 719 | clone_with_ref_kind(*arguments[i], VariableReference::kWrite_RefKind), |
| 720 | Token::Kind::TK_EQ, |
| 721 | std::move(varMap[p]), |
| 722 | &arguments[i]->type()))); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 723 | } |
| 724 | |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 725 | if (resultExpr != nullptr) { |
| 726 | // Return our result variable as our replacement expression. |
| 727 | SkASSERT(resultExpr->as<VariableReference>().fRefKind == VariableReference::kRead_RefKind); |
| 728 | inlinedCall.fReplacementExpr = std::move(resultExpr); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 729 | } else { |
| 730 | // It's a void function, so it doesn't actually result in anything, but we have to return |
| 731 | // something non-null as a standin. |
| 732 | inlinedCall.fReplacementExpr = std::make_unique<BoolLiteral>(*fContext, offset, |
| 733 | /*value=*/false); |
| 734 | } |
| 735 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 736 | return inlinedCall; |
| 737 | } |
| 738 | |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 739 | bool Inliner::isSafeToInline(const FunctionCall& functionCall, int inlineThreshold) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 740 | SkASSERT(fSettings); |
| 741 | |
| 742 | if (functionCall.fFunction.fDefinition == nullptr) { |
| 743 | // Can't inline something if we don't actually have its definition. |
| 744 | return false; |
| 745 | } |
| 746 | const FunctionDefinition& functionDef = *functionCall.fFunction.fDefinition; |
| 747 | if (inlineThreshold < INT_MAX) { |
| 748 | if (!(functionDef.fDeclaration.fModifiers.fFlags & Modifiers::kInline_Flag) && |
| 749 | Analysis::NodeCount(functionDef) >= inlineThreshold) { |
| 750 | // The function exceeds our maximum inline size and is not flagged 'inline'. |
| 751 | return false; |
| 752 | } |
| 753 | } |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 754 | 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. |
| 757 | bool hasEarlyReturn = has_early_return(functionDef); |
| 758 | |
| 759 | // If we didn't detect an early return, there shouldn't be any returns in breakable |
| 760 | // constructs either. |
| 761 | SkASSERT(hasEarlyReturn || count_returns_in_breakable_constructs(functionDef) == 0); |
| 762 | 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. |
| 766 | bool hasReturnInBreakableConstruct = (count_returns_in_breakable_constructs(functionDef) > 0); |
| 767 | |
| 768 | // If we detected returns in breakable constructs, we should also detect an early return. |
| 769 | SkASSERT(!hasReturnInBreakableConstruct || has_early_return(functionDef)); |
| 770 | return !hasReturnInBreakableConstruct; |
| 771 | } |
| 772 | |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 773 | bool Inliner::analyze(Program& program) { |
| 774 | // A candidate function for inlining, containing everything that `inlineCall` needs. |
| 775 | struct InlineCandidate { |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 776 | SymbolTable* fSymbols; // the SymbolTable of the candidate |
| 777 | 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 |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 780 | }; |
| 781 | |
| 782 | // This is structured much like a ProgramVisitor, but does not actually use ProgramVisitor. |
| 783 | // The analyzer needs to keep track of the `unique_ptr<T>*` of statements and expressions so |
| 784 | // that they can later be replaced, and ProgramVisitor does not provide this; it only provides a |
| 785 | // `const T&`. |
| 786 | class InlineCandidateAnalyzer { |
| 787 | public: |
| 788 | // A list of all the inlining candidates we found during analysis. |
| 789 | std::vector<InlineCandidate> fInlineCandidates; |
| 790 | // A stack of the symbol tables; since most nodes don't have one, expected to be shallower |
| 791 | // than the enclosing-statement stack. |
| 792 | std::vector<SymbolTable*> fSymbolTableStack; |
| 793 | // A stack of "enclosing" statements--these would be suitable for the inliner to use for |
| 794 | // adding new instructions. Not all statements are suitable (e.g. a for-loop's initializer). |
| 795 | // The inliner might replace a statement with a block containing the statement. |
| 796 | std::vector<std::unique_ptr<Statement>*> fEnclosingStmtStack; |
| 797 | |
| 798 | void visit(Program& program) { |
| 799 | fSymbolTableStack.push_back(program.fSymbols.get()); |
| 800 | |
| 801 | for (ProgramElement& pe : program) { |
| 802 | this->visitProgramElement(&pe); |
| 803 | } |
| 804 | |
| 805 | fSymbolTableStack.pop_back(); |
| 806 | } |
| 807 | |
| 808 | void visitProgramElement(ProgramElement* pe) { |
| 809 | switch (pe->kind()) { |
| 810 | case ProgramElement::Kind::kFunction: { |
| 811 | FunctionDefinition& funcDef = pe->as<FunctionDefinition>(); |
| 812 | this->visitStatement(&funcDef.fBody); |
| 813 | break; |
| 814 | } |
| 815 | default: |
| 816 | // The inliner can't operate outside of a function's scope. |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | void visitStatement(std::unique_ptr<Statement>* stmt, |
| 822 | bool isViableAsEnclosingStatement = true) { |
| 823 | if (!*stmt) { |
| 824 | return; |
| 825 | } |
| 826 | |
| 827 | size_t oldEnclosingStmtStackSize = fEnclosingStmtStack.size(); |
| 828 | size_t oldSymbolStackSize = fSymbolTableStack.size(); |
| 829 | |
| 830 | if (isViableAsEnclosingStatement) { |
| 831 | fEnclosingStmtStack.push_back(stmt); |
| 832 | } |
| 833 | |
| 834 | switch ((*stmt)->kind()) { |
| 835 | case Statement::Kind::kBreak: |
| 836 | case Statement::Kind::kContinue: |
| 837 | case Statement::Kind::kDiscard: |
| 838 | case Statement::Kind::kInlineMarker: |
| 839 | case Statement::Kind::kNop: |
| 840 | break; |
| 841 | |
| 842 | case Statement::Kind::kBlock: { |
| 843 | Block& block = (*stmt)->as<Block>(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 844 | if (block.symbolTable()) { |
| 845 | fSymbolTableStack.push_back(block.symbolTable().get()); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 846 | } |
| 847 | |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 848 | for (std::unique_ptr<Statement>& stmt : block.children()) { |
| 849 | this->visitStatement(&stmt); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 850 | } |
| 851 | break; |
| 852 | } |
| 853 | case Statement::Kind::kDo: { |
| 854 | DoStatement& doStmt = (*stmt)->as<DoStatement>(); |
| 855 | // The loop body is a candidate for inlining. |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 856 | this->visitStatement(&doStmt.statement()); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 857 | // The inliner isn't smart enough to inline the test-expression for a do-while |
| 858 | // loop at this time. There are two limitations: |
| 859 | // - We would need to insert the inlined-body block at the very end of the do- |
| 860 | // statement's inner fStatement. We don't support that today, but it's doable. |
| 861 | // - We cannot inline the test expression if the loop uses `continue` anywhere; |
| 862 | // that would skip over the inlined block that evaluates the test expression. |
| 863 | // There isn't a good fix for this--any workaround would be more complex than |
| 864 | // the cost of a function call. However, loops that don't use `continue` would |
| 865 | // still be viable candidates for inlining. |
| 866 | break; |
| 867 | } |
| 868 | case Statement::Kind::kExpression: { |
| 869 | ExpressionStatement& expr = (*stmt)->as<ExpressionStatement>(); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 870 | this->visitExpression(&expr.expression()); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | case Statement::Kind::kFor: { |
| 874 | ForStatement& forStmt = (*stmt)->as<ForStatement>(); |
| 875 | if (forStmt.fSymbols) { |
| 876 | fSymbolTableStack.push_back(forStmt.fSymbols.get()); |
| 877 | } |
| 878 | |
| 879 | // The initializer and loop body are candidates for inlining. |
| 880 | this->visitStatement(&forStmt.fInitializer, |
| 881 | /*isViableAsEnclosingStatement=*/false); |
| 882 | this->visitStatement(&forStmt.fStatement); |
| 883 | |
| 884 | // The inliner isn't smart enough to inline the test- or increment-expressions |
| 885 | // of a for loop loop at this time. There are a handful of limitations: |
| 886 | // - We would need to insert the test-expression block at the very beginning of |
| 887 | // the for-loop's inner fStatement, and the increment-expression block at the |
| 888 | // very end. We don't support that today, but it's doable. |
| 889 | // - The for-loop's built-in test-expression would need to be dropped entirely, |
| 890 | // and the loop would be halted via a break statement at the end of the |
| 891 | // inlined test-expression. This is again something we don't support today, |
| 892 | // but it could be implemented. |
| 893 | // - We cannot inline the increment-expression if the loop uses `continue` |
| 894 | // anywhere; that would skip over the inlined block that evaluates the |
| 895 | // increment expression. There isn't a good fix for this--any workaround would |
| 896 | // be more complex than the cost of a function call. However, loops that don't |
| 897 | // use `continue` would still be viable candidates for increment-expression |
| 898 | // inlining. |
| 899 | break; |
| 900 | } |
| 901 | case Statement::Kind::kIf: { |
| 902 | IfStatement& ifStmt = (*stmt)->as<IfStatement>(); |
| 903 | this->visitExpression(&ifStmt.fTest); |
| 904 | this->visitStatement(&ifStmt.fIfTrue); |
| 905 | this->visitStatement(&ifStmt.fIfFalse); |
| 906 | break; |
| 907 | } |
| 908 | case Statement::Kind::kReturn: { |
| 909 | ReturnStatement& returnStmt = (*stmt)->as<ReturnStatement>(); |
| 910 | this->visitExpression(&returnStmt.fExpression); |
| 911 | break; |
| 912 | } |
| 913 | case Statement::Kind::kSwitch: { |
| 914 | SwitchStatement& switchStmt = (*stmt)->as<SwitchStatement>(); |
| 915 | if (switchStmt.fSymbols) { |
| 916 | fSymbolTableStack.push_back(switchStmt.fSymbols.get()); |
| 917 | } |
| 918 | |
| 919 | this->visitExpression(&switchStmt.fValue); |
| 920 | for (std::unique_ptr<SwitchCase>& switchCase : switchStmt.fCases) { |
| 921 | // The switch-case's fValue cannot be a FunctionCall; skip it. |
| 922 | for (std::unique_ptr<Statement>& caseBlock : switchCase->fStatements) { |
| 923 | this->visitStatement(&caseBlock); |
| 924 | } |
| 925 | } |
| 926 | break; |
| 927 | } |
| 928 | case Statement::Kind::kVarDeclaration: { |
| 929 | VarDeclaration& varDeclStmt = (*stmt)->as<VarDeclaration>(); |
| 930 | // Don't need to scan the declaration's sizes; those are always IntLiterals. |
| 931 | this->visitExpression(&varDeclStmt.fValue); |
| 932 | break; |
| 933 | } |
| 934 | case Statement::Kind::kVarDeclarations: { |
| 935 | VarDeclarationsStatement& varDecls = (*stmt)->as<VarDeclarationsStatement>(); |
| 936 | for (std::unique_ptr<Statement>& varDecl : varDecls.fDeclaration->fVars) { |
| 937 | this->visitStatement(&varDecl, /*isViableAsEnclosingStatement=*/false); |
| 938 | } |
| 939 | break; |
| 940 | } |
| 941 | case Statement::Kind::kWhile: { |
| 942 | WhileStatement& whileStmt = (*stmt)->as<WhileStatement>(); |
| 943 | // The loop body is a candidate for inlining. |
| 944 | this->visitStatement(&whileStmt.fStatement); |
| 945 | // The inliner isn't smart enough to inline the test-expression for a while |
| 946 | // loop at 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 |
| 949 | // doable. |
| 950 | // - The while-loop's built-in test-expression would need to be replaced with a |
| 951 | // `true` BoolLiteral, and the loop would be halted via a break statement at |
| 952 | // the end of the inlined test-expression. This is again something we don't |
| 953 | // support today, but it could be implemented. |
| 954 | break; |
| 955 | } |
| 956 | default: |
| 957 | SkUNREACHABLE; |
| 958 | } |
| 959 | |
| 960 | // Pop our symbol and enclosing-statement stacks. |
| 961 | fSymbolTableStack.resize(oldSymbolStackSize); |
| 962 | fEnclosingStmtStack.resize(oldEnclosingStmtStackSize); |
| 963 | } |
| 964 | |
| 965 | void visitExpression(std::unique_ptr<Expression>* expr) { |
| 966 | if (!*expr) { |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | switch ((*expr)->kind()) { |
| 971 | case Expression::Kind::kBoolLiteral: |
| 972 | case Expression::Kind::kDefined: |
| 973 | case Expression::Kind::kExternalValue: |
| 974 | case Expression::Kind::kFieldAccess: |
| 975 | case Expression::Kind::kFloatLiteral: |
| 976 | case Expression::Kind::kFunctionReference: |
| 977 | case Expression::Kind::kIntLiteral: |
| 978 | case Expression::Kind::kNullLiteral: |
| 979 | case Expression::Kind::kSetting: |
| 980 | case Expression::Kind::kTypeReference: |
| 981 | case Expression::Kind::kVariableReference: |
| 982 | // Nothing to scan here. |
| 983 | break; |
| 984 | |
| 985 | case Expression::Kind::kBinary: { |
| 986 | BinaryExpression& binaryExpr = (*expr)->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 987 | this->visitExpression(&binaryExpr.leftPointer()); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 988 | |
| 989 | // Logical-and and logical-or binary expressions do not inline the right side, |
| 990 | // because that would invalidate short-circuiting. That is, when evaluating |
| 991 | // expressions like these: |
| 992 | // (false && x()) // always false |
| 993 | // (true || y()) // always true |
| 994 | // It is illegal for side-effects from x() or y() to occur. The simplest way to |
| 995 | // enforce that rule is to avoid inlining the right side entirely. However, it |
| 996 | // is safe for other types of binary expression to inline both sides. |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 997 | Token::Kind op = binaryExpr.getOperator(); |
| 998 | bool shortCircuitable = (op == Token::Kind::TK_LOGICALAND || |
| 999 | op == Token::Kind::TK_LOGICALOR); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1000 | if (!shortCircuitable) { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1001 | this->visitExpression(&binaryExpr.rightPointer()); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1002 | } |
| 1003 | break; |
| 1004 | } |
| 1005 | case Expression::Kind::kConstructor: { |
| 1006 | Constructor& constructorExpr = (*expr)->as<Constructor>(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1007 | for (std::unique_ptr<Expression>& arg : constructorExpr.arguments()) { |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1008 | this->visitExpression(&arg); |
| 1009 | } |
| 1010 | break; |
| 1011 | } |
| 1012 | case Expression::Kind::kExternalFunctionCall: { |
| 1013 | ExternalFunctionCall& funcCallExpr = (*expr)->as<ExternalFunctionCall>(); |
| 1014 | for (std::unique_ptr<Expression>& arg : funcCallExpr.fArguments) { |
| 1015 | this->visitExpression(&arg); |
| 1016 | } |
| 1017 | break; |
| 1018 | } |
| 1019 | case Expression::Kind::kFunctionCall: { |
| 1020 | FunctionCall& funcCallExpr = (*expr)->as<FunctionCall>(); |
| 1021 | for (std::unique_ptr<Expression>& arg : funcCallExpr.fArguments) { |
| 1022 | this->visitExpression(&arg); |
| 1023 | } |
| 1024 | this->addInlineCandidate(expr); |
| 1025 | break; |
| 1026 | } |
| 1027 | case Expression::Kind::kIndex:{ |
| 1028 | IndexExpression& indexExpr = (*expr)->as<IndexExpression>(); |
| 1029 | this->visitExpression(&indexExpr.fBase); |
| 1030 | this->visitExpression(&indexExpr.fIndex); |
| 1031 | break; |
| 1032 | } |
| 1033 | case Expression::Kind::kPostfix: { |
| 1034 | PostfixExpression& postfixExpr = (*expr)->as<PostfixExpression>(); |
| 1035 | this->visitExpression(&postfixExpr.fOperand); |
| 1036 | break; |
| 1037 | } |
| 1038 | case Expression::Kind::kPrefix: { |
| 1039 | PrefixExpression& prefixExpr = (*expr)->as<PrefixExpression>(); |
| 1040 | this->visitExpression(&prefixExpr.fOperand); |
| 1041 | break; |
| 1042 | } |
| 1043 | case Expression::Kind::kSwizzle: { |
| 1044 | Swizzle& swizzleExpr = (*expr)->as<Swizzle>(); |
| 1045 | this->visitExpression(&swizzleExpr.fBase); |
| 1046 | break; |
| 1047 | } |
| 1048 | case Expression::Kind::kTernary: { |
| 1049 | TernaryExpression& ternaryExpr = (*expr)->as<TernaryExpression>(); |
| 1050 | // The test expression is a candidate for inlining. |
| 1051 | this->visitExpression(&ternaryExpr.fTest); |
| 1052 | // The true- and false-expressions cannot be inlined, because we are only |
| 1053 | // allowed to evaluate one side. |
| 1054 | break; |
| 1055 | } |
| 1056 | default: |
| 1057 | SkUNREACHABLE; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | void addInlineCandidate(std::unique_ptr<Expression>* candidate) { |
| 1062 | fInlineCandidates.push_back(InlineCandidate{fSymbolTableStack.back(), |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1063 | find_parent_statement(fEnclosingStmtStack), |
| 1064 | fEnclosingStmtStack.back(), |
| 1065 | candidate}); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1066 | } |
| 1067 | }; |
| 1068 | |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1069 | InlineCandidateAnalyzer analyzer; |
| 1070 | analyzer.visit(program); |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1071 | |
| 1072 | // For each of our candidate function-call sites, check if it is actually safe to inline. |
| 1073 | // Memoize our results so we don't check a function more than once. |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1074 | std::unordered_map<const FunctionDeclaration*, bool> inlinableMap; // <function, safe-to-inline> |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1075 | for (const InlineCandidate& candidate : analyzer.fInlineCandidates) { |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1076 | const FunctionCall& funcCall = (*candidate.fCandidateExpr)->as<FunctionCall>(); |
| 1077 | const FunctionDeclaration* funcDecl = &funcCall.fFunction; |
| 1078 | if (inlinableMap.find(funcDecl) == inlinableMap.end()) { |
| 1079 | // We do not perform inlining on recursive calls to avoid an infinite death spiral of |
| 1080 | // inlining. |
| 1081 | int inlineThreshold = (funcDecl->fCallCount.load() > 1) ? fSettings->fInlineThreshold |
| 1082 | : INT_MAX; |
| 1083 | inlinableMap[funcDecl] = this->isSafeToInline(funcCall, inlineThreshold) && |
| 1084 | !contains_recursive_call(*funcDecl); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1088 | // Inline the candidates where we've determined that it's safe to do so. |
| 1089 | std::unordered_set<const std::unique_ptr<Statement>*> enclosingStmtSet; |
| 1090 | bool madeChanges = false; |
| 1091 | for (const InlineCandidate& candidate : analyzer.fInlineCandidates) { |
| 1092 | FunctionCall& funcCall = (*candidate.fCandidateExpr)->as<FunctionCall>(); |
| 1093 | const FunctionDeclaration* funcDecl = &funcCall.fFunction; |
| 1094 | |
| 1095 | // If we determined that this candidate was not actually inlinable, skip it. |
| 1096 | if (!inlinableMap[funcDecl]) { |
| 1097 | continue; |
| 1098 | } |
| 1099 | |
| 1100 | // Inlining two expressions using the same enclosing statement in the same inlining pass |
| 1101 | // does not work properly. If this happens, skip it; we'll get it in the next pass. |
| 1102 | auto [unusedIter, inserted] = enclosingStmtSet.insert(candidate.fEnclosingStmt); |
| 1103 | if (!inserted) { |
| 1104 | continue; |
| 1105 | } |
| 1106 | |
| 1107 | // Convert the function call to its inlined equivalent. |
| 1108 | InlinedCall inlinedCall = this->inlineCall(&funcCall, candidate.fSymbols); |
| 1109 | if (inlinedCall.fInlinedBody) { |
| 1110 | // Ensure that the inlined body has a scope if it needs one. |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 1111 | this->ensureScopedBlocks(inlinedCall.fInlinedBody.get(), candidate.fParentStmt); |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1112 | |
| 1113 | // Move the enclosing statement to the end of the unscoped Block containing the inlined |
| 1114 | // function, then replace the enclosing statement with that Block. |
| 1115 | // Before: |
| 1116 | // fInlinedBody = Block{ stmt1, stmt2, stmt3 } |
| 1117 | // fEnclosingStmt = stmt4 |
| 1118 | // After: |
| 1119 | // fInlinedBody = null |
| 1120 | // fEnclosingStmt = Block{ stmt1, stmt2, stmt3, stmt4 } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1121 | inlinedCall.fInlinedBody->children().push_back(std::move(*candidate.fEnclosingStmt)); |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1122 | *candidate.fEnclosingStmt = std::move(inlinedCall.fInlinedBody); |
| 1123 | } |
| 1124 | |
| 1125 | // Replace the candidate function call with our replacement expression. |
| 1126 | *candidate.fCandidateExpr = std::move(inlinedCall.fReplacementExpr); |
| 1127 | madeChanges = true; |
| 1128 | |
| 1129 | // Note that nothing was destroyed except for the FunctionCall. All other nodes should |
| 1130 | // remain valid. |
| 1131 | } |
| 1132 | |
| 1133 | return madeChanges; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1134 | } |
| 1135 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 1136 | } // namespace SkSL |