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 #include "include/private/SkSLProgramElement.h" 9 #include "src/sksl/SkSLProgramSettings.h" 10 #include "src/sksl/ir/SkSLProgram.h" 11 #include "src/sksl/ir/SkSLVarDeclarations.h" 12 #include "src/sksl/transform/SkSLTransform.h" 13 14 #include <algorithm> 15 16 namespace SkSL { 17 EliminateDeadGlobalVariables(Program & program,ProgramUsage * usage)18bool Transform::EliminateDeadGlobalVariables(Program& program, ProgramUsage* usage) { 19 bool madeChanges = false; 20 21 if (program.fConfig->fSettings.fRemoveDeadVariables) { 22 auto isDeadVariable = [&](const ProgramElement* element) { 23 if (!element->is<GlobalVarDeclaration>()) { 24 return false; 25 } 26 const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); 27 const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>(); 28 if (!usage->isDead(varDecl.var())) { 29 return false; 30 } 31 madeChanges = true; 32 return true; 33 }; 34 35 program.fOwnedElements.erase(std::remove_if(program.fOwnedElements.begin(), 36 program.fOwnedElements.end(), 37 [&](const std::unique_ptr<ProgramElement>& pe) { 38 return isDeadVariable(pe.get()); 39 }), 40 program.fOwnedElements.end()); 41 program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(), 42 program.fSharedElements.end(), 43 isDeadVariable), 44 program.fSharedElements.end()); 45 } 46 return madeChanges; 47 } 48 49 } // namespace SkSL 50