1 // Copyright (c) 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "source/opt/eliminate_dead_functions_pass.h" 16 17 #include <unordered_set> 18 19 #include "source/opt/ir_context.h" 20 21 namespace spvtools { 22 namespace opt { 23 Process()24Pass::Status EliminateDeadFunctionsPass::Process() { 25 // Identify live functions first. Those that are not live 26 // are dead. 27 std::unordered_set<const Function*> live_function_set; 28 ProcessFunction mark_live = [&live_function_set](Function* fp) { 29 live_function_set.insert(fp); 30 return false; 31 }; 32 context()->ProcessReachableCallTree(mark_live); 33 34 bool modified = false; 35 for (auto funcIter = get_module()->begin(); 36 funcIter != get_module()->end();) { 37 if (live_function_set.count(&*funcIter) == 0) { 38 modified = true; 39 EliminateFunction(&*funcIter); 40 funcIter = funcIter.Erase(); 41 } else { 42 ++funcIter; 43 } 44 } 45 46 return modified ? Pass::Status::SuccessWithChange 47 : Pass::Status::SuccessWithoutChange; 48 } 49 EliminateFunction(Function * func)50void EliminateDeadFunctionsPass::EliminateFunction(Function* func) { 51 // Remove all of the instruction in the function body 52 func->ForEachInst([this](Instruction* inst) { context()->KillInst(inst); }, 53 true); 54 } 55 } // namespace opt 56 } // namespace spvtools 57