eliminate_dead_functions_util.cpp 2.4 KB

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