Flutter Engine
The Flutter Engine
SkSLEliminateDeadGlobalVariables.cpp
Go to the documentation of this file.
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
18
19#include <algorithm>
20#include <cstddef>
21#include <memory>
22#include <vector>
23
24namespace SkSL {
25
26static bool is_dead_variable(const ProgramElement& element,
28 bool onlyPrivateGlobals) {
29 if (!element.is<GlobalVarDeclaration>()) {
30 return false;
31 }
32 const GlobalVarDeclaration& global = element.as<GlobalVarDeclaration>();
33 const VarDeclaration& varDecl = global.varDeclaration();
34 if (onlyPrivateGlobals && !skstd::starts_with(varDecl.var()->name(), '$')) {
35 return false;
36 }
37 if (!usage->isDead(*varDecl.var())) {
38 return false;
39 }
40 // This declaration is about to be eliminated by remove_if; update ProgramUsage accordingly.
41 usage->remove(&varDecl);
42 return true;
43}
44
46 Module& module,
48 bool onlyPrivateGlobals) {
49 auto isDeadVariable = [&](const ProgramElement& element) {
50 return is_dead_variable(element, usage, onlyPrivateGlobals);
51 };
52
53 size_t numElements = module.fElements.size();
55 module.fElements.erase(std::remove_if(module.fElements.begin(),
56 module.fElements.end(),
57 [&](const std::unique_ptr<ProgramElement>& pe) {
58 return isDeadVariable(*pe);
59 }),
60 module.fElements.end());
61 }
62 return module.fElements.size() < numElements;
63}
64
66 auto isDeadVariable = [&](const ProgramElement& element) {
67 return is_dead_variable(element, program.fUsage.get(), /*onlyPrivateGlobals=*/false);
68 };
69
70 size_t numOwnedElements = program.fOwnedElements.size();
71 size_t numSharedElements = program.fSharedElements.size();
72 if (program.fConfig->fSettings.fRemoveDeadVariables) {
73 program.fOwnedElements.erase(std::remove_if(program.fOwnedElements.begin(),
74 program.fOwnedElements.end(),
75 [&](const std::unique_ptr<ProgramElement>& pe) {
76 return isDeadVariable(*pe);
77 }),
78 program.fOwnedElements.end());
79 program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(),
80 program.fSharedElements.end(),
81 [&](const ProgramElement* pe) {
82 return isDeadVariable(*pe);
83 }),
84 program.fSharedElements.end());
85 }
86 return program.fOwnedElements.size() < numOwnedElements ||
87 program.fSharedElements.size() < numSharedElements;
88}
89
90} // namespace SkSL
ProgramConfig * fConfig
Definition: SkSLContext.h:33
VarDeclaration & varDeclaration()
bool is() const
Definition: SkSLIRNode.h:124
const T & as() const
Definition: SkSLIRNode.h:133
bool EliminateDeadGlobalVariables(const Context &context, Module &module, ProgramUsage *usage, bool onlyPrivateGlobals)
static bool is_dead_variable(const ProgramElement &element, ProgramUsage *usage, bool onlyPrivateGlobals)
constexpr bool starts_with(std::string_view str, std::string_view prefix)
Definition: SkStringView.h:17
static void usage(char *argv0)
std::vector< std::unique_ptr< ProgramElement > > fElements
Definition: SkSLCompiler.h:59
ProgramSettings fSettings
std::vector< std::unique_ptr< ProgramElement > > fOwnedElements
Definition: SkSLProgram.h:161
std::unique_ptr< ProgramUsage > fUsage
Definition: SkSLProgram.h:155
std::vector< const ProgramElement * > fSharedElements
Definition: SkSLProgram.h:164
std::unique_ptr< ProgramConfig > fConfig
Definition: SkSLProgram.h:153