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