Flutter Engine
The Flutter Engine
SkSLEliminateDeadFunctions.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
17
18#include <algorithm>
19#include <cstddef>
20#include <memory>
21#include <vector>
22
23namespace SkSL {
24
26 if (!element->is<FunctionDefinition>()) {
27 return false;
28 }
29 const FunctionDefinition& fn = element->as<FunctionDefinition>();
30 if (fn.declaration().isMain() || usage->get(fn.declaration()) > 0) {
31 return false;
32 }
33 // This function is about to be eliminated by remove_if; update ProgramUsage accordingly.
34 usage->remove(*element);
35 return true;
36}
37
39 ProgramUsage* usage = program.fUsage.get();
40
41 size_t numOwnedElements = program.fOwnedElements.size();
42 size_t numSharedElements = program.fSharedElements.size();
43
44 if (program.fConfig->fSettings.fRemoveDeadFunctions) {
45 program.fOwnedElements.erase(std::remove_if(program.fOwnedElements.begin(),
46 program.fOwnedElements.end(),
47 [&](const std::unique_ptr<ProgramElement>& pe) {
48 return dead_function_predicate(pe.get(),
49 usage);
50 }),
51 program.fOwnedElements.end());
52 program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(),
53 program.fSharedElements.end(),
54 [&](const ProgramElement* pe) {
55 return dead_function_predicate(pe, usage);
56 }),
57 program.fSharedElements.end());
58 }
59 return program.fOwnedElements.size() < numOwnedElements ||
60 program.fSharedElements.size() < numSharedElements;
61}
62
64 Module& module,
66 size_t numElements = module.fElements.size();
67
69 module.fElements.erase(std::remove_if(module.fElements.begin(),
70 module.fElements.end(),
71 [&](const std::unique_ptr<ProgramElement>& pe) {
72 return dead_function_predicate(pe.get(), usage);
73 }),
74 module.fElements.end());
75 }
76 return module.fElements.size() < numElements;
77}
78
79} // namespace SkSL
ProgramConfig * fConfig
Definition: SkSLContext.h:33
const FunctionDeclaration & declaration() const
bool is() const
Definition: SkSLIRNode.h:124
const T & as() const
Definition: SkSLIRNode.h:133
bool EliminateDeadFunctions(const Context &context, Module &module, ProgramUsage *usage)
static bool dead_function_predicate(const ProgramElement *element, ProgramUsage *usage)
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