• 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 opt::IRContext;
23 using opt::Instruction;
24 
25 const uint32_t kTrueBranchOperandIndex = 1;
26 const uint32_t kFalseBranchOperandIndex = 2;
27 
FindOrCreateGlobalUndef(IRContext * context,uint32_t type_id)28 uint32_t FindOrCreateGlobalUndef(IRContext* context, uint32_t type_id) {
29   for (auto& inst : context->module()->types_values()) {
30     if (inst.opcode() != SpvOpUndef) {
31       continue;
32     }
33     if (inst.type_id() == type_id) {
34       return inst.result_id();
35     }
36   }
37   // TODO(2182): this is adapted from MemPass::Type2Undef.  In due course it
38   // would be good to factor out this duplication.
39   const uint32_t undef_id = context->TakeNextId();
40   std::unique_ptr<Instruction> undef_inst(
41       new Instruction(context, SpvOpUndef, type_id, undef_id, {}));
42   assert(undef_id == undef_inst->result_id());
43   context->module()->AddGlobalValue(std::move(undef_inst));
44   return undef_id;
45 }
46 
AdaptPhiInstructionsForRemovedEdge(uint32_t from_id,opt::BasicBlock * to_block)47 void AdaptPhiInstructionsForRemovedEdge(uint32_t from_id,
48                                         opt::BasicBlock* to_block) {
49   to_block->ForEachPhiInst([&from_id](Instruction* phi_inst) {
50     Instruction::OperandList new_in_operands;
51     // Go through the OpPhi's input operands in (variable, parent) pairs.
52     for (uint32_t index = 0; index < phi_inst->NumInOperands(); index += 2) {
53       // Keep all pairs where the parent is not the block from which the edge
54       // is being removed.
55       if (phi_inst->GetInOperand(index + 1).words[0] != from_id) {
56         new_in_operands.push_back(phi_inst->GetInOperand(index));
57         new_in_operands.push_back(phi_inst->GetInOperand(index + 1));
58       }
59     }
60     phi_inst->SetInOperands(std::move(new_in_operands));
61   });
62 }
63 
64 }  // namespace reduce
65 }  // namespace spvtools
66