MergeBlocks.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Bytecode/PassManager.h>
  7. namespace JS::Bytecode::Passes {
  8. void MergeBlocks::perform(PassPipelineExecutable& executable)
  9. {
  10. started();
  11. VERIFY(executable.cfg.has_value());
  12. VERIFY(executable.inverted_cfg.has_value());
  13. auto cfg = executable.cfg.release_value();
  14. auto inverted_cfg = executable.inverted_cfg.release_value();
  15. // Figure out which blocks can be merged
  16. HashTable<BasicBlock const*> blocks_to_merge;
  17. HashMap<BasicBlock const*, BasicBlock const*> blocks_to_replace;
  18. Vector<BasicBlock const*> blocks_to_remove;
  19. Vector<size_t> boundaries;
  20. for (auto& entry : cfg) {
  21. if (entry.value.size() != 1)
  22. continue;
  23. if (executable.exported_blocks->contains(*entry.value.begin()))
  24. continue;
  25. if (!entry.key->is_terminated())
  26. continue;
  27. if (entry.key->terminator()->type() != Instruction::Type::Jump)
  28. continue;
  29. // NOTE: We can't replace the first block in a function, as it's the entry block.
  30. if (entry.key != executable.executable.basic_blocks.first()) {
  31. InstructionStreamIterator it { entry.key->instruction_stream() };
  32. auto& first_instruction = *it;
  33. if (first_instruction.type() == Instruction::Type::Jump) {
  34. auto const* replacing_block = &static_cast<Op::Jump const&>(first_instruction).true_target()->block();
  35. if (replacing_block != entry.key) {
  36. blocks_to_replace.set(entry.key, replacing_block);
  37. }
  38. continue;
  39. }
  40. }
  41. if (auto cfg_iter = inverted_cfg.find(*entry.value.begin()); cfg_iter != inverted_cfg.end()) {
  42. auto& predecessor_entry = cfg_iter->value;
  43. if (predecessor_entry.size() != 1)
  44. continue;
  45. }
  46. // The two blocks are safe to merge.
  47. blocks_to_merge.set(entry.key);
  48. }
  49. for (auto& entry : blocks_to_replace) {
  50. auto const* replacement = entry.value;
  51. for (;;) {
  52. auto lookup = blocks_to_replace.get(replacement);
  53. if (!lookup.has_value())
  54. break;
  55. if (replacement == *lookup)
  56. break;
  57. replacement = *lookup;
  58. }
  59. entry.value = replacement;
  60. }
  61. auto replace_blocks = [&](auto& blocks, auto& replacement) {
  62. Optional<size_t> first_successor_position;
  63. for (auto& entry : blocks) {
  64. blocks_to_remove.append(entry);
  65. auto it = executable.executable.basic_blocks.find_if([entry](auto& block) { return entry == block; });
  66. VERIFY(!it.is_end());
  67. if (!first_successor_position.has_value())
  68. first_successor_position = it.index();
  69. }
  70. for (auto& block : executable.executable.basic_blocks) {
  71. InstructionStreamIterator it { block->instruction_stream() };
  72. while (!it.at_end()) {
  73. auto& instruction = *it;
  74. ++it;
  75. for (auto& entry : blocks)
  76. const_cast<Instruction&>(instruction).replace_references(*entry, replacement);
  77. }
  78. }
  79. return first_successor_position;
  80. };
  81. for (auto& entry : blocks_to_replace) {
  82. AK::Array candidates { entry.key };
  83. (void)replace_blocks(candidates, *entry.value);
  84. }
  85. while (!blocks_to_merge.is_empty()) {
  86. auto it = blocks_to_merge.begin();
  87. auto const* current_block = *it;
  88. blocks_to_merge.remove(it);
  89. Vector<BasicBlock const*> successors { current_block };
  90. for (;;) {
  91. auto const* last = successors.last();
  92. auto entry = cfg.find(last);
  93. if (entry == cfg.end())
  94. break;
  95. auto const* successor = *entry->value.begin();
  96. successors.append(successor);
  97. if (!blocks_to_merge.remove(successor))
  98. break;
  99. }
  100. auto blocks_to_merge_copy = blocks_to_merge;
  101. // We need to do the following multiple times, due to it not being
  102. // guaranteed, that the blocks are in sequential order
  103. bool did_prepend = true;
  104. while (did_prepend) {
  105. did_prepend = false;
  106. for (auto const* last : blocks_to_merge) {
  107. auto entry = cfg.find(last);
  108. if (entry == cfg.end())
  109. continue;
  110. auto const* successor = *entry->value.begin();
  111. if (successor == successors.first()) {
  112. successors.prepend(last);
  113. blocks_to_merge_copy.remove(last);
  114. did_prepend = true;
  115. }
  116. }
  117. }
  118. blocks_to_merge = move(blocks_to_merge_copy);
  119. StringBuilder builder;
  120. builder.append("merge"sv);
  121. for (auto& entry : successors) {
  122. builder.append('.');
  123. builder.append(entry->name());
  124. }
  125. auto new_block = BasicBlock::create(MUST(builder.to_string()));
  126. auto& block = *new_block;
  127. auto first_successor_position = replace_blocks(successors, *new_block);
  128. VERIFY(first_successor_position.has_value());
  129. size_t last_successor_index = successors.size() - 1;
  130. for (size_t i = 0; i < successors.size(); ++i) {
  131. auto& entry = successors[i];
  132. InstructionStreamIterator it { entry->instruction_stream() };
  133. while (!it.at_end()) {
  134. auto& instruction = *it;
  135. ++it;
  136. if (instruction.is_terminator() && last_successor_index != i)
  137. break;
  138. // FIXME: Use a single memcpy to copy the whole block at once.
  139. auto instruction_size = instruction.length();
  140. size_t slot_offset = block.size();
  141. block.grow(instruction_size);
  142. auto* next_slot = block.data() + slot_offset;
  143. memcpy(next_slot, &instruction, instruction_size);
  144. }
  145. }
  146. executable.executable.basic_blocks.insert(*first_successor_position, move(new_block));
  147. }
  148. executable.executable.basic_blocks.remove_all_matching([&blocks_to_remove](auto& candidate) { return blocks_to_remove.contains_slow(candidate.ptr()); });
  149. finished();
  150. }
  151. }