dead_code_elimination_pass.cpp 895 B

1234567891011121314151617181920212223242526
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "shader_recompiler/frontend/ir/basic_block.h"
  5. #include "shader_recompiler/frontend/ir/value.h"
  6. #include "shader_recompiler/ir_opt/passes.h"
  7. namespace Shader::Optimization {
  8. void DeadCodeEliminationPass(IR::Program& program) {
  9. // We iterate over the instructions in reverse order.
  10. // This is because removing an instruction reduces the number of uses for earlier instructions.
  11. for (IR::Block* const block : program.post_order_blocks) {
  12. auto it{block->end()};
  13. while (it != block->begin()) {
  14. --it;
  15. if (!it->HasUses() && !it->MayHaveSideEffects()) {
  16. it->Invalidate();
  17. it = block->Instructions().erase(it);
  18. }
  19. }
  20. }
  21. }
  22. } // namespace Shader::Optimization