Peephole.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if (instruction.type() == Instruction::Type::Not) {
  33. auto const& not_ = static_cast<Op::Not const&>(instruction);
  34. if (jump.condition() != not_.dst()) {
  35. auto slot_offset = new_block->size();
  36. new_block->grow(not_.length());
  37. memcpy(new_block->data() + slot_offset, &not_, not_.length());
  38. continue;
  39. }
  40. }
  41. #define DO_FUSE_JUMP(PreOp, ...) \
  42. if (instruction.type() == Instruction::Type::PreOp) { \
  43. auto const& compare = static_cast<Op::PreOp const&>(instruction); \
  44. if (jump.condition() == compare.dst()) { \
  45. new_block->append<Op::Jump##PreOp>( \
  46. compare.source_record().source_start_offset, \
  47. compare.source_record().source_end_offset, \
  48. compare.lhs(), \
  49. compare.rhs(), \
  50. *jump.true_target(), \
  51. *jump.false_target()); \
  52. ++it; \
  53. VERIFY(it.at_end()); \
  54. continue; \
  55. } \
  56. }
  57. JS_ENUMERATE_FUSABLE_BINARY_OPS(DO_FUSE_JUMP)
  58. }
  59. }
  60. auto slot_offset = new_block->size();
  61. new_block->grow(instruction.length());
  62. memcpy(new_block->data() + slot_offset, &instruction, instruction.length());
  63. if (instruction.is_terminator())
  64. new_block->terminate(slot_offset);
  65. }
  66. replaced_blocks.append(move(executable.executable.basic_blocks[i]));
  67. executable.executable.basic_blocks[i] = move(new_block);
  68. }
  69. auto update_block_references = [&](BasicBlock const& original, BasicBlock const& replacement) {
  70. for (auto& block : executable.executable.basic_blocks) {
  71. InstructionStreamIterator it { block->instruction_stream() };
  72. if (block->handler() == &original)
  73. block->set_handler(replacement);
  74. if (block->finalizer() == &original)
  75. block->set_finalizer(replacement);
  76. while (!it.at_end()) {
  77. auto const& instruction = *it;
  78. ++it;
  79. const_cast<Instruction&>(instruction).replace_references(original, replacement);
  80. }
  81. }
  82. };
  83. for (auto& entry : replacement_blocks)
  84. update_block_references(*entry.key, *entry.value);
  85. finished();
  86. }
  87. }