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