redundancy_elimination.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/redundancy_elimination.h"
  15. #include "source/opt/value_number_table.h"
  16. namespace spvtools {
  17. namespace opt {
  18. Pass::Status RedundancyEliminationPass::Process() {
  19. bool modified = false;
  20. ValueNumberTable vnTable(context());
  21. for (auto& func : *get_module()) {
  22. if (func.IsDeclaration()) {
  23. continue;
  24. }
  25. // Build the dominator tree for this function. It is how the code is
  26. // traversed.
  27. DominatorTree& dom_tree =
  28. context()->GetDominatorAnalysis(&func)->GetDomTree();
  29. // Keeps track of all ids that contain a given value number. We keep
  30. // track of multiple values because they could have the same value, but
  31. // different decorations.
  32. std::map<uint32_t, uint32_t> value_to_ids;
  33. if (EliminateRedundanciesFrom(dom_tree.GetRoot(), vnTable, value_to_ids)) {
  34. modified = true;
  35. }
  36. }
  37. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  38. }
  39. bool RedundancyEliminationPass::EliminateRedundanciesFrom(
  40. DominatorTreeNode* bb, const ValueNumberTable& vnTable,
  41. std::map<uint32_t, uint32_t> value_to_ids) {
  42. bool modified = EliminateRedundanciesInBB(bb->bb_, vnTable, &value_to_ids);
  43. for (auto dominated_bb : bb->children_) {
  44. modified |= EliminateRedundanciesFrom(dominated_bb, vnTable, value_to_ids);
  45. }
  46. return modified;
  47. }
  48. } // namespace opt
  49. } // namespace spvtools