• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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() == spv::Op::OpFunctionEnd) {
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() == spv::Op::OpExtInst) {
37               assert(inst->IsNonSemanticInstruction());
38               if (to_kill.find(inst) != to_kill.end()) return;
39               std::unique_ptr<Instruction> clone(inst->Clone(context));
40               // Clear uses of "inst" to in case this moves a dependent chain of
41               // instructions.
42               context->get_def_use_mgr()->ClearInst(inst);
43               context->AnalyzeDefUse(clone.get());
44               if (first_func) {
45                 context->AddGlobalValue(std::move(clone));
46               } else {
47                 auto prev_func_iter = *func_iter;
48                 --prev_func_iter;
49                 prev_func_iter->AddNonSemanticInstruction(std::move(clone));
50               }
51               inst->ToNop();
52             } else if (to_kill.find(inst) == to_kill.end()) {
53               context->CollectNonSemanticTree(inst, &to_kill);
54               context->KillInst(inst);
55             }
56           },
57           true, true);
58 
59   for (auto* dead : to_kill) {
60     context->KillInst(dead);
61   }
62 
63   return func_iter->Erase();
64 }
65 
66 }  // namespace eliminatedeadfunctionsutil
67 }  // namespace opt
68 }  // namespace spvtools
69