dead_variable_elimination.cpp 3.9 KB

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