value_number_table.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. analysis::DecorationManager* dec_mgr = context()->get_decoration_mgr();
  70. // When we copy an object, the value numbers should be the same.
  71. if (inst->opcode() == SpvOpCopyObject &&
  72. dec_mgr->HaveTheSameDecorations(inst->result_id(),
  73. inst->GetSingleWordInOperand(0))) {
  74. value = GetValueNumber(inst->GetSingleWordInOperand(0));
  75. if (value != 0) {
  76. id_to_value_[inst->result_id()] = value;
  77. return value;
  78. }
  79. }
  80. // Phi nodes are a type of copy. If all of the inputs have the same value
  81. // number, then we can assign the result of the phi the same value number.
  82. if (inst->opcode() == SpvOpPhi &&
  83. dec_mgr->HaveTheSameDecorations(inst->result_id(),
  84. inst->GetSingleWordInOperand(0))) {
  85. value = GetValueNumber(inst->GetSingleWordInOperand(0));
  86. if (value != 0) {
  87. for (uint32_t op = 2; op < inst->NumInOperands(); op += 2) {
  88. if (value != GetValueNumber(inst->GetSingleWordInOperand(op))) {
  89. value = 0;
  90. break;
  91. }
  92. }
  93. if (value != 0) {
  94. id_to_value_[inst->result_id()] = value;
  95. return value;
  96. }
  97. }
  98. }
  99. // Replace all of the operands by their value number. The sign bit will be
  100. // set to distinguish between an id and a value number.
  101. Instruction value_ins(context(), inst->opcode(), inst->type_id(),
  102. inst->result_id(), {});
  103. for (uint32_t o = 0; o < inst->NumInOperands(); ++o) {
  104. const Operand& op = inst->GetInOperand(o);
  105. if (spvIsIdType(op.type)) {
  106. uint32_t id_value = op.words[0];
  107. auto use_id_to_val = id_to_value_.find(id_value);
  108. if (use_id_to_val != id_to_value_.end()) {
  109. id_value = (1 << 31) | use_id_to_val->second;
  110. }
  111. value_ins.AddOperand(Operand(op.type, {id_value}));
  112. } else {
  113. value_ins.AddOperand(Operand(op.type, op.words));
  114. }
  115. }
  116. // TODO: Implement a normal form for opcodes that commute like integer
  117. // addition. This will let us know that a+b is the same value as b+a.
  118. // Otherwise, we check if this value has been computed before.
  119. auto value_iterator = instruction_to_value_.find(value_ins);
  120. if (value_iterator != instruction_to_value_.end()) {
  121. value = id_to_value_[value_iterator->first.result_id()];
  122. id_to_value_[inst->result_id()] = value;
  123. return value;
  124. }
  125. // If not, assign it a new value number.
  126. value = TakeNextValueNumber();
  127. id_to_value_[inst->result_id()] = value;
  128. instruction_to_value_[value_ins] = value;
  129. return value;
  130. }
  131. void ValueNumberTable::BuildDominatorTreeValueNumberTable() {
  132. // First value number the headers.
  133. for (auto& inst : context()->annotations()) {
  134. if (inst.result_id() != 0) {
  135. AssignValueNumber(&inst);
  136. }
  137. }
  138. for (auto& inst : context()->capabilities()) {
  139. if (inst.result_id() != 0) {
  140. AssignValueNumber(&inst);
  141. }
  142. }
  143. for (auto& inst : context()->types_values()) {
  144. if (inst.result_id() != 0) {
  145. AssignValueNumber(&inst);
  146. }
  147. }
  148. for (auto& inst : context()->module()->ext_inst_imports()) {
  149. if (inst.result_id() != 0) {
  150. AssignValueNumber(&inst);
  151. }
  152. }
  153. for (Function& func : *context()->module()) {
  154. // For best results we want to traverse the code in reverse post order.
  155. // This happens naturally because of the forward referencing rules.
  156. for (BasicBlock& block : func) {
  157. for (Instruction& inst : block) {
  158. if (inst.result_id() != 0) {
  159. AssignValueNumber(&inst);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. bool ComputeSameValue::operator()(const Instruction& lhs,
  166. const Instruction& rhs) const {
  167. if (lhs.result_id() == 0 || rhs.result_id() == 0) {
  168. return false;
  169. }
  170. if (lhs.opcode() != rhs.opcode()) {
  171. return false;
  172. }
  173. if (lhs.type_id() != rhs.type_id()) {
  174. return false;
  175. }
  176. if (lhs.NumInOperands() != rhs.NumInOperands()) {
  177. return false;
  178. }
  179. for (uint32_t i = 0; i < lhs.NumInOperands(); ++i) {
  180. if (lhs.GetInOperand(i) != rhs.GetInOperand(i)) {
  181. return false;
  182. }
  183. }
  184. return lhs.context()->get_decoration_mgr()->HaveTheSameDecorations(
  185. lhs.result_id(), rhs.result_id());
  186. }
  187. std::size_t ValueTableHash::operator()(const Instruction& inst) const {
  188. // We hash the opcode and in-operands, not the result, because we want
  189. // instructions that are the same except for the result to hash to the
  190. // same value.
  191. std::u32string h;
  192. h.push_back(inst.opcode());
  193. h.push_back(inst.type_id());
  194. for (uint32_t i = 0; i < inst.NumInOperands(); ++i) {
  195. const auto& opnd = inst.GetInOperand(i);
  196. for (uint32_t word : opnd.words) {
  197. h.push_back(word);
  198. }
  199. }
  200. return std::hash<std::u32string>()(h);
  201. }
  202. } // namespace opt
  203. } // namespace spvtools