/src/spirv-tools/source/opt/local_redundancy_elimination.cpp
Line | Count | Source |
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/local_redundancy_elimination.h" |
16 | | |
17 | | #include "source/opt/value_number_table.h" |
18 | | |
19 | | namespace spvtools { |
20 | | namespace opt { |
21 | | |
22 | 0 | Pass::Status LocalRedundancyEliminationPass::Process() { |
23 | 0 | bool modified = false; |
24 | 0 | ValueNumberTable vnTable(context()); |
25 | |
|
26 | 0 | for (auto& func : *get_module()) { |
27 | 0 | for (auto& bb : func) { |
28 | | // Keeps track of all ids that contain a given value number. We keep |
29 | | // track of multiple values because they could have the same value, but |
30 | | // different decorations. |
31 | 0 | std::map<uint32_t, uint32_t> value_to_ids; |
32 | 0 | if (EliminateRedundanciesInBB(&bb, vnTable, &value_to_ids)) |
33 | 0 | modified = true; |
34 | 0 | } |
35 | 0 | } |
36 | 0 | return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange); |
37 | 0 | } |
38 | | |
39 | | bool LocalRedundancyEliminationPass::EliminateRedundanciesInBB( |
40 | | BasicBlock* block, const ValueNumberTable& vnTable, |
41 | 431k | std::map<uint32_t, uint32_t>* value_to_ids) { |
42 | 431k | bool modified = false; |
43 | | |
44 | 2.52M | auto func = [this, &vnTable, &modified, value_to_ids](Instruction* inst) { |
45 | 2.52M | if (inst->result_id() == 0) { |
46 | 775k | return; |
47 | 775k | } |
48 | | |
49 | 1.74M | uint32_t value = vnTable.GetValueNumber(inst); |
50 | | |
51 | 1.74M | if (value == 0) { |
52 | 416k | return; |
53 | 416k | } |
54 | | |
55 | 1.32M | auto candidate = value_to_ids->insert({value, inst->result_id()}); |
56 | 1.32M | if (!candidate.second) { |
57 | 164k | context()->KillNamesAndDecorates(inst); |
58 | 164k | context()->ReplaceAllUsesWith(inst->result_id(), candidate.first->second); |
59 | 164k | context()->KillInst(inst); |
60 | 164k | modified = true; |
61 | 164k | } |
62 | 1.32M | }; |
63 | 431k | block->ForEachInst(func); |
64 | 431k | return modified; |
65 | 431k | } |
66 | | } // namespace opt |
67 | | } // namespace spvtools |