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#include "source/opt/eliminate_dead_functions_util.h"
17
18#include <unordered_set>
19
20#include "source/opt/ir_context.h"
21
22namespace spvtools {
23namespace opt {
24
25Pass::Status EliminateDeadFunctionsPass::Process() {
26 // Identify live functions first. Those that are not live
27 // are dead.
28 std::unordered_set<const Function*> live_function_set;
29 ProcessFunction mark_live = [&live_function_set](Function* fp) {
30 live_function_set.insert(fp);
31 return false;
32 };
33 context()->ProcessReachableCallTree(mark_live);
34
35 bool modified = false;
36 for (auto funcIter = get_module()->begin();
37 funcIter != get_module()->end();) {
38 if (live_function_set.count(&*funcIter) == 0) {
39 modified = true;
40 funcIter =
41 eliminatedeadfunctionsutil::EliminateFunction(context(), &funcIter);
42 } else {
43 ++funcIter;
44 }
45 }
46
47 return modified ? Pass::Status::SuccessWithChange
48 : Pass::Status::SuccessWithoutChange;
49}
50
51} // namespace opt
52} // namespace spvtools
53