eliminate_dead_functions_util.cpp 2.5 KB

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