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/dead_variable_elimination.h"
16
17 #include <vector>
18
19 #include "source/opt/ir_context.h"
20 #include "source/opt/reflect.h"
21
22 namespace spvtools {
23 namespace opt {
24
25 // This optimization removes global variables that are not needed because they
26 // are definitely not accessed.
Process()27 Pass::Status DeadVariableElimination::Process() {
28 // The algorithm will compute the reference count for every global variable.
29 // Anything with a reference count of 0 will then be deleted. For variables
30 // that might have references that are not explicit in this context, we use
31 // the value kMustKeep as the reference count.
32 std::vector<uint32_t> ids_to_remove;
33
34 // Get the reference count for all of the global OpVariable instructions.
35 for (auto& inst : context()->types_values()) {
36 if (inst.opcode() != spv::Op::OpVariable) {
37 continue;
38 }
39
40 size_t count = 0;
41 uint32_t result_id = inst.result_id();
42
43 // Check the linkage. If it is exported, it could be reference somewhere
44 // else, so we must keep the variable around.
45 get_decoration_mgr()->ForEachDecoration(
46 result_id, uint32_t(spv::Decoration::LinkageAttributes),
47 [&count](const Instruction& linkage_instruction) {
48 uint32_t last_operand = linkage_instruction.NumOperands() - 1;
49 if (spv::LinkageType(linkage_instruction.GetSingleWordOperand(
50 last_operand)) == spv::LinkageType::Export) {
51 count = kMustKeep;
52 }
53 });
54
55 if (count != kMustKeep) {
56 // If we don't have to keep the instruction for other reasons, then look
57 // at the uses and count the number of real references.
58 count = 0;
59 get_def_use_mgr()->ForEachUser(result_id, [&count](Instruction* user) {
60 if (!IsAnnotationInst(user->opcode()) &&
61 user->opcode() != spv::Op::OpName) {
62 ++count;
63 }
64 });
65 }
66 reference_count_[result_id] = count;
67 if (count == 0) {
68 ids_to_remove.push_back(result_id);
69 }
70 }
71
72 // Remove all of the variables that have a reference count of 0.
73 bool modified = false;
74 if (!ids_to_remove.empty()) {
75 modified = true;
76 for (auto result_id : ids_to_remove) {
77 DeleteVariable(result_id);
78 }
79 }
80 return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
81 }
82
DeleteVariable(uint32_t result_id)83 void DeadVariableElimination::DeleteVariable(uint32_t result_id) {
84 Instruction* inst = get_def_use_mgr()->GetDef(result_id);
85 assert(inst->opcode() == spv::Op::OpVariable &&
86 "Should not be trying to delete anything other than an OpVariable.");
87
88 // Look for an initializer that references another variable. We need to know
89 // if that variable can be deleted after the reference is removed.
90 if (inst->NumOperands() == 4) {
91 Instruction* initializer =
92 get_def_use_mgr()->GetDef(inst->GetSingleWordOperand(3));
93
94 // TODO: Handle OpSpecConstantOP which might be defined in terms of other
95 // variables. Will probably require a unified dead code pass that does all
96 // instruction types. (Issue 906)
97 if (initializer->opcode() == spv::Op::OpVariable) {
98 uint32_t initializer_id = initializer->result_id();
99 size_t& count = reference_count_[initializer_id];
100 if (count != kMustKeep) {
101 --count;
102 }
103
104 if (count == 0) {
105 DeleteVariable(initializer_id);
106 }
107 }
108 }
109 context()->KillDef(result_id);
110 }
111 } // namespace opt
112 } // namespace spvtools
113