reduction_util.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. namespace spvtools {
  17. namespace reduce {
  18. using opt::IRContext;
  19. using opt::Instruction;
  20. const uint32_t kTrueBranchOperandIndex = 1;
  21. const uint32_t kFalseBranchOperandIndex = 2;
  22. uint32_t FindOrCreateGlobalUndef(IRContext* context, uint32_t type_id) {
  23. for (auto& inst : context->module()->types_values()) {
  24. if (inst.opcode() != SpvOpUndef) {
  25. continue;
  26. }
  27. if (inst.type_id() == type_id) {
  28. return inst.result_id();
  29. }
  30. }
  31. // TODO(2182): this is adapted from MemPass::Type2Undef. In due course it
  32. // would be good to factor out this duplication.
  33. const uint32_t undef_id = context->TakeNextId();
  34. std::unique_ptr<Instruction> undef_inst(
  35. new Instruction(context, SpvOpUndef, type_id, undef_id, {}));
  36. assert(undef_id == undef_inst->result_id());
  37. context->module()->AddGlobalValue(std::move(undef_inst));
  38. return undef_id;
  39. }
  40. } // namespace reduce
  41. } // namespace spvtools