• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include "source/reduce/reduction_util.h"
16 
17 #include "source/opt/ir_context.h"
18 
19 namespace spvtools {
20 namespace reduce {
21 
22 using namespace opt;
23 
FindOrCreateGlobalUndef(IRContext * context,uint32_t type_id)24 uint32_t FindOrCreateGlobalUndef(IRContext* context, uint32_t type_id) {
25   for (auto& inst : context->module()->types_values()) {
26     if (inst.opcode() != SpvOpUndef) {
27       continue;
28     }
29     if (inst.type_id() == type_id) {
30       return inst.result_id();
31     }
32   }
33   // TODO(2182): this is adapted from MemPass::Type2Undef.  In due course it
34   // would be good to factor out this duplication.
35   const uint32_t undef_id = context->TakeNextId();
36   std::unique_ptr<Instruction> undef_inst(
37       new Instruction(context, SpvOpUndef, type_id, undef_id, {}));
38   assert(undef_id == undef_inst->result_id());
39   context->module()->AddGlobalValue(std::move(undef_inst));
40   return undef_id;
41 }
42 
43 }  // namespace reduce
44 }  // namespace spvtools
45