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/redundancy_elimination.h"
16
17 #include "source/opt/value_number_table.h"
18
19 namespace spvtools {
20 namespace opt {
21
Process()22 Pass::Status RedundancyEliminationPass::Process() {
23 bool modified = false;
24 ValueNumberTable vnTable(context());
25
26 for (auto& func : *get_module()) {
27 if (func.IsDeclaration()) {
28 continue;
29 }
30
31 // Build the dominator tree for this function. It is how the code is
32 // traversed.
33 DominatorTree& dom_tree =
34 context()->GetDominatorAnalysis(&func)->GetDomTree();
35
36
37 if (EliminateRedundanciesFrom(dom_tree.GetRoot(), vnTable)) {
38 modified = true;
39 }
40 }
41 return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
42 }
43
EliminateRedundanciesFrom(DominatorTreeNode * bb,const ValueNumberTable & vnTable)44 bool RedundancyEliminationPass::EliminateRedundanciesFrom(
45 DominatorTreeNode* bb, const ValueNumberTable& vnTable) {
46 struct State {
47 DominatorTreeNode* node;
48 std::map<uint32_t, uint32_t> value_to_id_map;
49 };
50 std::vector<State> todo;
51 todo.push_back({bb, std::map<uint32_t, uint32_t>()});
52 bool modified = false;
53 for (size_t next_node = 0; next_node < todo.size(); next_node++) {
54 modified |= EliminateRedundanciesInBB(todo[next_node].node->bb_, vnTable,
55 &todo[next_node].value_to_id_map);
56 for (DominatorTreeNode* child : todo[next_node].node->children_) {
57 todo.push_back({child, todo[next_node].value_to_id_map});
58 }
59 }
60 return modified;
61 }
62 } // namespace opt
63 } // namespace spvtools
64