Peephole.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2024, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Bytecode/PassManager.h>
  7. namespace JS::Bytecode::Passes {
  8. void Peephole::perform(PassPipelineExecutable& executable)
  9. {
  10. started();
  11. // Fuse compare-followed-by-jump into a single compare-and-jump
  12. // This is a very common pattern in bytecode, and it's nice to have it as a single instruction
  13. // For example, LessThan + JumpIf => JumpLessThan
  14. HashMap<BasicBlock const*, BasicBlock const*> replacement_blocks;
  15. Vector<NonnullOwnPtr<BasicBlock>> replaced_blocks;
  16. for (size_t i = 0; i < executable.executable.basic_blocks.size(); ++i) {
  17. auto& block = executable.executable.basic_blocks[i];
  18. auto new_block = BasicBlock::create(block->name());
  19. if (block->handler())
  20. new_block->set_handler(*block->handler());
  21. if (block->finalizer())
  22. new_block->set_finalizer(*block->finalizer());
  23. replacement_blocks.set(block.ptr(), new_block.ptr());
  24. InstructionStreamIterator it { block->instruction_stream() };
  25. while (!it.at_end()) {
  26. auto const& instruction = *it;
  27. ++it;
  28. if (!it.at_end()) {
  29. auto const& next_instruction = *it;
  30. if (next_instruction.type() == Instruction::Type::JumpIf) {
  31. auto const& jump = static_cast<Op::JumpIf const&>(next_instruction);
  32. #define DO_FUSE_JUMP(PreOp, ...) \
  33. if (instruction.type() == Instruction::Type::PreOp) { \
  34. auto const& compare = static_cast<Op::PreOp const&>(instruction); \
  35. VERIFY(jump.condition() == compare.dst()); \
  36. new_block->append<Op::Jump##PreOp>( \
  37. compare.source_record().source_start_offset, \
  38. compare.source_record().source_end_offset, \
  39. compare.lhs(), \
  40. compare.rhs(), \
  41. *jump.true_target(), \
  42. *jump.false_target()); \
  43. ++it; \
  44. VERIFY(it.at_end()); \
  45. continue; \
  46. }
  47. JS_ENUMERATE_FUSABLE_BINARY_OPS(DO_FUSE_JUMP)
  48. }
  49. }
  50. auto slot_offset = new_block->size();
  51. new_block->grow(instruction.length());
  52. memcpy(new_block->data() + slot_offset, &instruction, instruction.length());
  53. if (instruction.is_terminator())
  54. new_block->terminate(slot_offset);
  55. }
  56. replaced_blocks.append(move(executable.executable.basic_blocks[i]));
  57. executable.executable.basic_blocks[i] = move(new_block);
  58. }
  59. auto update_block_references = [&](BasicBlock const& original, BasicBlock const& replacement) {
  60. for (auto& block : executable.executable.basic_blocks) {
  61. InstructionStreamIterator it { block->instruction_stream() };
  62. if (block->handler() == &original)
  63. block->set_handler(replacement);
  64. if (block->finalizer() == &original)
  65. block->set_finalizer(replacement);
  66. while (!it.at_end()) {
  67. auto const& instruction = *it;
  68. ++it;
  69. const_cast<Instruction&>(instruction).replace_references(original, replacement);
  70. }
  71. }
  72. };
  73. for (auto& entry : replacement_blocks)
  74. update_block_references(*entry.key, *entry.value);
  75. finished();
  76. }
  77. }