reduction_util.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2018 Google LLC
  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/reduce/reduction_util.h"
  15. #include "source/opt/ir_context.h"
  16. #include "source/util/make_unique.h"
  17. namespace spvtools {
  18. namespace reduce {
  19. const uint32_t kTrueBranchOperandIndex = 1;
  20. const uint32_t kFalseBranchOperandIndex = 2;
  21. uint32_t FindOrCreateGlobalVariable(opt::IRContext* context,
  22. uint32_t pointer_type_id) {
  23. for (auto& inst : context->module()->types_values()) {
  24. if (inst.opcode() != SpvOpVariable) {
  25. continue;
  26. }
  27. if (inst.type_id() == pointer_type_id) {
  28. return inst.result_id();
  29. }
  30. }
  31. const uint32_t variable_id = context->TakeNextId();
  32. auto variable_inst = MakeUnique<opt::Instruction>(
  33. context, SpvOpVariable, pointer_type_id, variable_id,
  34. opt::Instruction::OperandList(
  35. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  36. {static_cast<uint32_t>(context->get_type_mgr()
  37. ->GetType(pointer_type_id)
  38. ->AsPointer()
  39. ->storage_class())}}}));
  40. context->module()->AddGlobalValue(std::move(variable_inst));
  41. return variable_id;
  42. }
  43. uint32_t FindOrCreateFunctionVariable(opt::IRContext* context,
  44. opt::Function* function,
  45. uint32_t pointer_type_id) {
  46. // The pointer type of a function variable must have Function storage class.
  47. assert(context->get_type_mgr()
  48. ->GetType(pointer_type_id)
  49. ->AsPointer()
  50. ->storage_class() == SpvStorageClassFunction);
  51. // Go through the instructions in the function's first block until we find a
  52. // suitable variable, or go past all the variables.
  53. opt::BasicBlock::iterator iter = function->begin()->begin();
  54. for (;; ++iter) {
  55. // We will either find a suitable variable, or find a non-variable
  56. // instruction; we won't exhaust all instructions.
  57. assert(iter != function->begin()->end());
  58. if (iter->opcode() != SpvOpVariable) {
  59. // If we see a non-variable, we have gone through all the variables.
  60. break;
  61. }
  62. if (iter->type_id() == pointer_type_id) {
  63. return iter->result_id();
  64. }
  65. }
  66. // At this point, iter refers to the first non-function instruction of the
  67. // function's entry block.
  68. const uint32_t variable_id = context->TakeNextId();
  69. auto variable_inst = MakeUnique<opt::Instruction>(
  70. context, SpvOpVariable, pointer_type_id, variable_id,
  71. opt::Instruction::OperandList(
  72. {{SPV_OPERAND_TYPE_STORAGE_CLASS, {SpvStorageClassFunction}}}));
  73. iter->InsertBefore(std::move(variable_inst));
  74. return variable_id;
  75. }
  76. uint32_t FindOrCreateGlobalUndef(opt::IRContext* context, uint32_t type_id) {
  77. for (auto& inst : context->module()->types_values()) {
  78. if (inst.opcode() != SpvOpUndef) {
  79. continue;
  80. }
  81. if (inst.type_id() == type_id) {
  82. return inst.result_id();
  83. }
  84. }
  85. const uint32_t undef_id = context->TakeNextId();
  86. auto undef_inst = MakeUnique<opt::Instruction>(
  87. context, SpvOpUndef, type_id, undef_id, opt::Instruction::OperandList());
  88. assert(undef_id == undef_inst->result_id());
  89. context->module()->AddGlobalValue(std::move(undef_inst));
  90. return undef_id;
  91. }
  92. void AdaptPhiInstructionsForRemovedEdge(uint32_t from_id,
  93. opt::BasicBlock* to_block) {
  94. to_block->ForEachPhiInst([&from_id](opt::Instruction* phi_inst) {
  95. opt::Instruction::OperandList new_in_operands;
  96. // Go through the OpPhi's input operands in (variable, parent) pairs.
  97. for (uint32_t index = 0; index < phi_inst->NumInOperands(); index += 2) {
  98. // Keep all pairs where the parent is not the block from which the edge
  99. // is being removed.
  100. if (phi_inst->GetInOperand(index + 1).words[0] != from_id) {
  101. new_in_operands.push_back(phi_inst->GetInOperand(index));
  102. new_in_operands.push_back(phi_inst->GetInOperand(index + 1));
  103. }
  104. }
  105. phi_inst->SetInOperands(std::move(new_in_operands));
  106. });
  107. }
  108. } // namespace reduce
  109. } // namespace spvtools