reduction_util.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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() != spv::Op::OpVariable) {
  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, spv::Op::OpVariable, 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() == spv::StorageClass::Function);
  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() != spv::Op::OpVariable) {
  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, spv::Op::OpVariable, pointer_type_id, variable_id,
  71. opt::Instruction::OperandList(
  72. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  73. {uint32_t(spv::StorageClass::Function)}}}));
  74. iter->InsertBefore(std::move(variable_inst));
  75. return variable_id;
  76. }
  77. uint32_t FindOrCreateGlobalUndef(opt::IRContext* context, uint32_t type_id) {
  78. for (auto& inst : context->module()->types_values()) {
  79. if (inst.opcode() != spv::Op::OpUndef) {
  80. continue;
  81. }
  82. if (inst.type_id() == type_id) {
  83. return inst.result_id();
  84. }
  85. }
  86. const uint32_t undef_id = context->TakeNextId();
  87. auto undef_inst =
  88. MakeUnique<opt::Instruction>(context, spv::Op::OpUndef, type_id, undef_id,
  89. opt::Instruction::OperandList());
  90. assert(undef_id == undef_inst->result_id());
  91. context->module()->AddGlobalValue(std::move(undef_inst));
  92. return undef_id;
  93. }
  94. void AdaptPhiInstructionsForRemovedEdge(uint32_t from_id,
  95. opt::BasicBlock* to_block) {
  96. to_block->ForEachPhiInst([&from_id](opt::Instruction* phi_inst) {
  97. opt::Instruction::OperandList new_in_operands;
  98. // Go through the OpPhi's input operands in (variable, parent) pairs.
  99. for (uint32_t index = 0; index < phi_inst->NumInOperands(); index += 2) {
  100. // Keep all pairs where the parent is not the block from which the edge
  101. // is being removed.
  102. if (phi_inst->GetInOperand(index + 1).words[0] != from_id) {
  103. new_in_operands.push_back(phi_inst->GetInOperand(index));
  104. new_in_operands.push_back(phi_inst->GetInOperand(index + 1));
  105. }
  106. }
  107. phi_inst->SetInOperands(std::move(new_in_operands));
  108. });
  109. }
  110. } // namespace reduce
  111. } // namespace spvtools