eliminate_dead_functions_util.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. (*func_iter)
  23. ->ForEachInst(
  24. [context, first_func, func_iter, &seen_func_end](Instruction* inst) {
  25. if (inst->opcode() == SpvOpFunctionEnd) {
  26. seen_func_end = true;
  27. }
  28. // Move non-semantic instructions to the previous function or
  29. // global values if this is the first function.
  30. if (seen_func_end && inst->opcode() == SpvOpExtInst) {
  31. assert(inst->IsNonSemanticInstruction());
  32. std::unique_ptr<Instruction> clone(inst->Clone(context));
  33. context->ForgetUses(inst);
  34. context->AnalyzeDefUse(clone.get());
  35. if (first_func) {
  36. context->AddGlobalValue(std::move(clone));
  37. } else {
  38. auto prev_func_iter = *func_iter;
  39. --prev_func_iter;
  40. prev_func_iter->AddNonSemanticInstruction(std::move(clone));
  41. }
  42. inst->ToNop();
  43. } else {
  44. context->KillNonSemanticInfo(inst);
  45. context->KillInst(inst);
  46. }
  47. },
  48. true, true);
  49. return func_iter->Erase();
  50. }
  51. } // namespace eliminatedeadfunctionsutil
  52. } // namespace opt
  53. } // namespace spvtools