1 // Copyright (c) 2019 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 "eliminate_dead_functions_util.h"
16
17 namespace spvtools {
18 namespace opt {
19
20 namespace eliminatedeadfunctionsutil {
21
EliminateFunction(IRContext * context,Module::iterator * func_iter)22 Module::iterator EliminateFunction(IRContext* context,
23 Module::iterator* func_iter) {
24 bool first_func = *func_iter == context->module()->begin();
25 bool seen_func_end = false;
26 std::unordered_set<Instruction*> to_kill;
27 (*func_iter)
28 ->ForEachInst(
29 [context, first_func, func_iter, &seen_func_end,
30 &to_kill](Instruction* inst) {
31 if (inst->opcode() == SpvOpFunctionEnd) {
32 seen_func_end = true;
33 }
34 // Move non-semantic instructions to the previous function or
35 // global values if this is the first function.
36 if (seen_func_end && inst->opcode() == SpvOpExtInst) {
37 assert(inst->IsNonSemanticInstruction());
38 if (to_kill.find(inst) != to_kill.end()) return;
39 std::unique_ptr<Instruction> clone(inst->Clone(context));
40 context->ForgetUses(inst);
41 context->AnalyzeDefUse(clone.get());
42 if (first_func) {
43 context->AddGlobalValue(std::move(clone));
44 } else {
45 auto prev_func_iter = *func_iter;
46 --prev_func_iter;
47 prev_func_iter->AddNonSemanticInstruction(std::move(clone));
48 }
49 inst->ToNop();
50 } else if (to_kill.find(inst) == to_kill.end()) {
51 context->CollectNonSemanticTree(inst, &to_kill);
52 context->KillInst(inst);
53 }
54 },
55 true, true);
56
57 for (auto* dead : to_kill) {
58 context->KillInst(dead);
59 }
60
61 return func_iter->Erase();
62 }
63
64 } // namespace eliminatedeadfunctionsutil
65 } // namespace opt
66 } // namespace spvtools
67