MergeBlocks.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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->terminator()->type() != Instruction::Type::Jump)
  26. continue;
  27. {
  28. InstructionStreamIterator it { entry.key->instruction_stream() };
  29. auto& first_instruction = *it;
  30. if (first_instruction.type() == Instruction::Type::Jump) {
  31. auto const* replacing_block = &static_cast<Op::Jump const&>(first_instruction).true_target()->block();
  32. if (replacing_block != entry.key) {
  33. blocks_to_replace.set(entry.key, replacing_block);
  34. }
  35. continue;
  36. }
  37. }
  38. if (auto cfg_iter = inverted_cfg.find(*entry.value.begin()); cfg_iter != inverted_cfg.end()) {
  39. auto& predecessor_entry = cfg_iter->value;
  40. if (predecessor_entry.size() != 1)
  41. continue;
  42. }
  43. // The two blocks are safe to merge.
  44. blocks_to_merge.set(entry.key);
  45. }
  46. for (auto& entry : blocks_to_replace) {
  47. auto const* replacement = entry.value;
  48. for (;;) {
  49. auto lookup = blocks_to_replace.get(replacement);
  50. if (!lookup.has_value())
  51. break;
  52. if (replacement == *lookup)
  53. break;
  54. replacement = *lookup;
  55. }
  56. entry.value = replacement;
  57. }
  58. auto replace_blocks = [&](auto& blocks, auto& replacement) {
  59. Optional<size_t> first_successor_position;
  60. for (auto& entry : blocks) {
  61. blocks_to_remove.append(entry);
  62. auto it = executable.executable.basic_blocks.find_if([entry](auto& block) { return entry == block; });
  63. VERIFY(!it.is_end());
  64. if (!first_successor_position.has_value())
  65. first_successor_position = it.index();
  66. }
  67. for (auto& block : executable.executable.basic_blocks) {
  68. InstructionStreamIterator it { block.instruction_stream() };
  69. while (!it.at_end()) {
  70. auto& instruction = *it;
  71. ++it;
  72. for (auto& entry : blocks)
  73. const_cast<Instruction&>(instruction).replace_references(*entry, replacement);
  74. }
  75. }
  76. return first_successor_position;
  77. };
  78. for (auto& entry : blocks_to_replace) {
  79. AK::Array candidates { entry.key };
  80. (void)replace_blocks(candidates, *entry.value);
  81. }
  82. while (!blocks_to_merge.is_empty()) {
  83. auto it = blocks_to_merge.begin();
  84. auto const* current_block = *it;
  85. blocks_to_merge.remove(it);
  86. Vector<BasicBlock const*> successors { current_block };
  87. for (;;) {
  88. auto const* last = successors.last();
  89. auto entry = cfg.find(last);
  90. if (entry == cfg.end())
  91. break;
  92. auto const* successor = *entry->value.begin();
  93. successors.append(successor);
  94. if (!blocks_to_merge.remove(successor))
  95. break;
  96. }
  97. auto blocks_to_merge_copy = blocks_to_merge;
  98. // We need to do the following multiple times, due to it not being
  99. // guaranteed, that the blocks are in sequential order
  100. bool did_prepend = true;
  101. while (did_prepend) {
  102. did_prepend = false;
  103. for (auto const* last : blocks_to_merge) {
  104. auto entry = cfg.find(last);
  105. if (entry == cfg.end())
  106. continue;
  107. auto const* successor = *entry->value.begin();
  108. if (successor == successors.first()) {
  109. successors.prepend(last);
  110. blocks_to_merge_copy.remove(last);
  111. did_prepend = true;
  112. }
  113. }
  114. }
  115. blocks_to_merge = move(blocks_to_merge_copy);
  116. size_t size = 0;
  117. StringBuilder builder;
  118. builder.append("merge"sv);
  119. for (auto& entry : successors) {
  120. size += entry->size();
  121. builder.append('.');
  122. builder.append(entry->name());
  123. }
  124. auto new_block = BasicBlock::create(builder.build(), size);
  125. auto& block = *new_block;
  126. auto first_successor_position = replace_blocks(successors, *new_block);
  127. VERIFY(first_successor_position.has_value());
  128. size_t last_successor_index = successors.size() - 1;
  129. for (size_t i = 0; i < successors.size(); ++i) {
  130. auto& entry = successors[i];
  131. InstructionStreamIterator it { entry->instruction_stream() };
  132. while (!it.at_end()) {
  133. auto& instruction = *it;
  134. ++it;
  135. if (instruction.is_terminator() && last_successor_index != i)
  136. break;
  137. // FIXME: Op::NewBigInt is not trivially copyable, so we cant use
  138. // a simple memcpy to transfer them.
  139. // When this is resolved we can use a single memcpy to copy
  140. // the whole block at once
  141. if (instruction.type() == Instruction::Type::NewBigInt) {
  142. new (block.next_slot()) Op::NewBigInt(static_cast<Op::NewBigInt const&>(instruction));
  143. block.grow(sizeof(Op::NewBigInt));
  144. } else {
  145. auto instruction_size = instruction.length();
  146. memcpy(block.next_slot(), &instruction, instruction_size);
  147. block.grow(instruction_size);
  148. }
  149. }
  150. }
  151. executable.executable.basic_blocks.insert(*first_successor_position, move(new_block));
  152. }
  153. executable.executable.basic_blocks.remove_all_matching([&blocks_to_remove](auto& candidate) { return blocks_to_remove.contains_slow(candidate.ptr()); });
  154. finished();
  155. }
  156. }