eliminate_dead_functions_pass.cpp 1.6 KB

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