1 /* 2 * Copyright 2021 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 #ifndef SKSL_TRANSFORM 9 #define SKSL_TRANSFORM 10 11 #include "include/core/SkSpan.h" 12 #include <memory> 13 #include <vector> 14 15 namespace SkSL { 16 17 class Context; 18 class Expression; 19 struct Modifiers; 20 struct Module; 21 struct Program; 22 class ProgramElement; 23 class ProgramUsage; 24 class Statement; 25 class Variable; 26 enum class ProgramKind : int8_t; 27 28 namespace Transform { 29 30 /** 31 * Checks to see if it would be safe to add `const` to the modifiers of a variable. If so, returns 32 * the modifiers with `const` applied; if not, returns the existing modifiers as-is. Adding `const` 33 * allows the inliner to fold away more values and generate tighter code. 34 */ 35 const Modifiers* AddConstToVarModifiers(const Context& context, 36 const Variable& var, 37 const Expression* initialValue, 38 const ProgramUsage* usage); 39 40 /** 41 * Copies built-in functions from modules into the program. Relies on ProgramUsage to determine 42 * which functions are necessary. 43 */ 44 void FindAndDeclareBuiltinFunctions(Program& program); 45 46 /** 47 * Scans the finished program for built-in variables like `sk_FragColor` and adds them to the 48 * program's shared elements. 49 */ 50 void FindAndDeclareBuiltinVariables(Program& program); 51 52 /** 53 * Eliminates statements in a block which cannot be reached; for example, a statement 54 * immediately after a `return` or `continue` can safely be eliminated. 55 */ 56 void EliminateUnreachableCode(Module& module, ProgramUsage* usage); 57 void EliminateUnreachableCode(Program& program); 58 59 /** 60 * Eliminates empty statements in a module (Nops, or blocks holding only Nops). Not implemented for 61 * Programs because Nops are harmless, but they waste space in long-lived module IR. 62 */ 63 void EliminateEmptyStatements(Module& module); 64 65 /** 66 * Eliminates functions in a program which are never called. Returns true if any changes were made. 67 */ 68 bool EliminateDeadFunctions(const Context& context, Module& module, ProgramUsage* usage); 69 bool EliminateDeadFunctions(Program& program); 70 71 /** 72 * Eliminates variables in a program which are never read or written (past their initializer). 73 * Preserves side effects from initializers, if any. Returns true if any changes were made. 74 */ 75 bool EliminateDeadLocalVariables(const Context& context, 76 Module& module, 77 ProgramUsage* usage); 78 bool EliminateDeadLocalVariables(Program& program); 79 bool EliminateDeadGlobalVariables(const Context& context, 80 Module& module, 81 ProgramUsage* usage, 82 bool onlyPrivateGlobals); 83 bool EliminateDeadGlobalVariables(Program& program); 84 85 /** Renames private functions and function-local variables to minimize code size. */ 86 void RenamePrivateSymbols(Context& context, Module& module, ProgramUsage* usage, ProgramKind kind); 87 88 /** Replaces constant variables in a program with their equivalent values. */ 89 void ReplaceConstVarsWithLiterals(Module& module, ProgramUsage* usage); 90 91 } // namespace Transform 92 } // namespace SkSL 93 94 #endif 95