dead_variable_elimination.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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() != SpvOp::SpvOpVariable) {
  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, SpvDecorationLinkageAttributes,
  39. [&count](const Instruction& linkage_instruction) {
  40. uint32_t last_operand = linkage_instruction.NumOperands() - 1;
  41. if (linkage_instruction.GetSingleWordOperand(last_operand) ==
  42. SpvLinkageTypeExport) {
  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()) && user->opcode() != SpvOpName) {
  52. ++count;
  53. }
  54. });
  55. }
  56. reference_count_[result_id] = count;
  57. if (count == 0) {
  58. ids_to_remove.push_back(result_id);
  59. }
  60. }
  61. // Remove all of the variables that have a reference count of 0.
  62. bool modified = false;
  63. if (!ids_to_remove.empty()) {
  64. modified = true;
  65. for (auto result_id : ids_to_remove) {
  66. DeleteVariable(result_id);
  67. }
  68. }
  69. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  70. }
  71. void DeadVariableElimination::DeleteVariable(uint32_t result_id) {
  72. Instruction* inst = get_def_use_mgr()->GetDef(result_id);
  73. assert(inst->opcode() == SpvOpVariable &&
  74. "Should not be trying to delete anything other than an OpVariable.");
  75. // Look for an initializer that references another variable. We need to know
  76. // if that variable can be deleted after the reference is removed.
  77. if (inst->NumOperands() == 4) {
  78. Instruction* initializer =
  79. get_def_use_mgr()->GetDef(inst->GetSingleWordOperand(3));
  80. // TODO: Handle OpSpecConstantOP which might be defined in terms of other
  81. // variables. Will probably require a unified dead code pass that does all
  82. // instruction types. (Issue 906)
  83. if (initializer->opcode() == SpvOpVariable) {
  84. uint32_t initializer_id = initializer->result_id();
  85. size_t& count = reference_count_[initializer_id];
  86. if (count != kMustKeep) {
  87. --count;
  88. }
  89. if (count == 0) {
  90. DeleteVariable(initializer_id);
  91. }
  92. }
  93. }
  94. context()->KillDef(result_id);
  95. }
  96. } // namespace opt
  97. } // namespace spvtools