value_number_table.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/value_number_table.h"
  15. #include <algorithm>
  16. #include "source/opt/cfg.h"
  17. #include "source/opt/ir_context.h"
  18. namespace spvtools {
  19. namespace opt {
  20. uint32_t ValueNumberTable::GetValueNumber(Instruction* inst) const {
  21. assert(inst->result_id() != 0 &&
  22. "inst must have a result id to get a value number.");
  23. // Check if this instruction already has a value.
  24. auto result_id_to_val = id_to_value_.find(inst->result_id());
  25. if (result_id_to_val != id_to_value_.end()) {
  26. return result_id_to_val->second;
  27. }
  28. return 0;
  29. }
  30. uint32_t ValueNumberTable::GetValueNumber(uint32_t id) const {
  31. return GetValueNumber(context()->get_def_use_mgr()->GetDef(id));
  32. }
  33. uint32_t ValueNumberTable::AssignValueNumber(Instruction* inst) {
  34. // If it already has a value return that.
  35. uint32_t value = GetValueNumber(inst);
  36. if (value != 0) {
  37. return value;
  38. }
  39. // If the instruction has other side effects, then it must
  40. // have its own value number.
  41. // OpSampledImage and OpImage must remain in the same basic block in which
  42. // they are used, because of this we will assign each one it own value number.
  43. if (!context()->IsCombinatorInstruction(inst)) {
  44. value = TakeNextValueNumber();
  45. id_to_value_[inst->result_id()] = value;
  46. return value;
  47. }
  48. switch (inst->opcode()) {
  49. case SpvOpSampledImage:
  50. case SpvOpImage:
  51. case SpvOpVariable:
  52. value = TakeNextValueNumber();
  53. id_to_value_[inst->result_id()] = value;
  54. return value;
  55. default:
  56. break;
  57. }
  58. // If it is a load from memory that can be modified, we have to assume the
  59. // memory has been modified, so we give it a new value number.
  60. //
  61. // Note that this test will also handle volatile loads because they are not
  62. // read only. However, if this is ever relaxed because we analyze stores, we
  63. // will have to add a new case for volatile loads.
  64. if (inst->IsLoad() && !inst->IsReadOnlyLoad()) {
  65. value = TakeNextValueNumber();
  66. id_to_value_[inst->result_id()] = value;
  67. return value;
  68. }
  69. // When we copy an object, the value numbers should be the same.
  70. if (inst->opcode() == SpvOpCopyObject) {
  71. value = GetValueNumber(inst->GetSingleWordInOperand(0));
  72. if (value != 0) {
  73. id_to_value_[inst->result_id()] = value;
  74. return value;
  75. }
  76. }
  77. // Phi nodes are a type of copy. If all of the inputs have the same value
  78. // number, then we can assign the result of the phi the same value number.
  79. if (inst->opcode() == SpvOpPhi) {
  80. value = GetValueNumber(inst->GetSingleWordInOperand(0));
  81. if (value != 0) {
  82. for (uint32_t op = 2; op < inst->NumInOperands(); op += 2) {
  83. if (value != GetValueNumber(inst->GetSingleWordInOperand(op))) {
  84. value = 0;
  85. break;
  86. }
  87. }
  88. if (value != 0) {
  89. id_to_value_[inst->result_id()] = value;
  90. return value;
  91. }
  92. }
  93. }
  94. // Replace all of the operands by their value number. The sign bit will be
  95. // set to distinguish between an id and a value number.
  96. Instruction value_ins(context(), inst->opcode(), inst->type_id(),
  97. inst->result_id(), {});
  98. for (uint32_t o = 0; o < inst->NumInOperands(); ++o) {
  99. const Operand& op = inst->GetInOperand(o);
  100. if (spvIsIdType(op.type)) {
  101. uint32_t id_value = op.words[0];
  102. auto use_id_to_val = id_to_value_.find(id_value);
  103. if (use_id_to_val != id_to_value_.end()) {
  104. id_value = (1 << 31) | use_id_to_val->second;
  105. }
  106. value_ins.AddOperand(Operand(op.type, {id_value}));
  107. } else {
  108. value_ins.AddOperand(Operand(op.type, op.words));
  109. }
  110. }
  111. // TODO: Implement a normal form for opcodes that commute like integer
  112. // addition. This will let us know that a+b is the same value as b+a.
  113. // Otherwise, we check if this value has been computed before.
  114. auto value_iterator = instruction_to_value_.find(value_ins);
  115. if (value_iterator != instruction_to_value_.end()) {
  116. value = id_to_value_[value_iterator->first.result_id()];
  117. id_to_value_[inst->result_id()] = value;
  118. return value;
  119. }
  120. // If not, assign it a new value number.
  121. value = TakeNextValueNumber();
  122. id_to_value_[inst->result_id()] = value;
  123. instruction_to_value_[value_ins] = value;
  124. return value;
  125. }
  126. void ValueNumberTable::BuildDominatorTreeValueNumberTable() {
  127. // First value number the headers.
  128. for (auto& inst : context()->annotations()) {
  129. if (inst.result_id() != 0) {
  130. AssignValueNumber(&inst);
  131. }
  132. }
  133. for (auto& inst : context()->capabilities()) {
  134. if (inst.result_id() != 0) {
  135. AssignValueNumber(&inst);
  136. }
  137. }
  138. for (auto& inst : context()->types_values()) {
  139. if (inst.result_id() != 0) {
  140. AssignValueNumber(&inst);
  141. }
  142. }
  143. for (auto& inst : context()->module()->ext_inst_imports()) {
  144. if (inst.result_id() != 0) {
  145. AssignValueNumber(&inst);
  146. }
  147. }
  148. for (Function& func : *context()->module()) {
  149. // For best results we want to traverse the code in reverse post order.
  150. // This happens naturally because of the forward referencing rules.
  151. for (BasicBlock& block : func) {
  152. for (Instruction& inst : block) {
  153. if (inst.result_id() != 0) {
  154. AssignValueNumber(&inst);
  155. }
  156. }
  157. }
  158. }
  159. }
  160. bool ComputeSameValue::operator()(const Instruction& lhs,
  161. const Instruction& rhs) const {
  162. if (lhs.result_id() == 0 || rhs.result_id() == 0) {
  163. return false;
  164. }
  165. if (lhs.opcode() != rhs.opcode()) {
  166. return false;
  167. }
  168. if (lhs.type_id() != rhs.type_id()) {
  169. return false;
  170. }
  171. if (lhs.NumInOperands() != rhs.NumInOperands()) {
  172. return false;
  173. }
  174. for (uint32_t i = 0; i < lhs.NumInOperands(); ++i) {
  175. if (lhs.GetInOperand(i) != rhs.GetInOperand(i)) {
  176. return false;
  177. }
  178. }
  179. return lhs.context()->get_decoration_mgr()->HaveTheSameDecorations(
  180. lhs.result_id(), rhs.result_id());
  181. }
  182. std::size_t ValueTableHash::operator()(const Instruction& inst) const {
  183. // We hash the opcode and in-operands, not the result, because we want
  184. // instructions that are the same except for the result to hash to the
  185. // same value.
  186. std::u32string h;
  187. h.push_back(inst.opcode());
  188. h.push_back(inst.type_id());
  189. for (uint32_t i = 0; i < inst.NumInOperands(); ++i) {
  190. const auto& opnd = inst.GetInOperand(i);
  191. for (uint32_t word : opnd.words) {
  192. h.push_back(word);
  193. }
  194. }
  195. return std::hash<std::u32string>()(h);
  196. }
  197. } // namespace opt
  198. } // namespace spvtools