LoadElimination.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2022, Leon Albrecht <leon.a@serenityos.com>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Bitmap.h>
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Bytecode/Op.h>
  9. #include <LibJS/Bytecode/PassManager.h>
  10. namespace JS::Bytecode::Passes {
  11. static NonnullOwnPtr<BasicBlock> eliminate_loads(BasicBlock const& block, size_t number_of_registers)
  12. {
  13. auto array_ranges = Bitmap::create(number_of_registers, false).release_value_but_fixme_should_propagate_errors();
  14. for (auto it = InstructionStreamIterator(block.instruction_stream()); !it.at_end(); ++it) {
  15. if ((*it).type() == Instruction::Type::NewArray) {
  16. Op::NewArray const& array_instruction = static_cast<Op::NewArray const&>(*it);
  17. if (size_t element_count = array_instruction.element_count())
  18. array_ranges.set_range<true, false>(array_instruction.start().index(), element_count);
  19. }
  20. }
  21. auto new_block = BasicBlock::create(block.name(), block.size());
  22. HashMap<size_t, Register> identifier_table {};
  23. HashMap<u32, Register> register_rerouting_table {};
  24. for (auto it = InstructionStreamIterator(block.instruction_stream()); !it.at_end();) {
  25. using enum Instruction::Type;
  26. // Note: When creating a variable, we technically purge the cache of any
  27. // variables of the same name;
  28. // In practice, we always generate a coinciding SetVariable, which
  29. // does the same
  30. switch ((*it).type()) {
  31. case GetVariable: {
  32. auto const& get_variable = static_cast<Op::GetVariable const&>(*it);
  33. ++it;
  34. auto const& next_instruction = *it;
  35. if (auto reg = identifier_table.find(get_variable.identifier().value()); reg != identifier_table.end()) {
  36. // If we have already seen a variable, we can replace its GetVariable with a simple Load
  37. // knowing that it was already stored in a register
  38. new (new_block->next_slot()) Op::Load(reg->value);
  39. new_block->grow(sizeof(Op::Load));
  40. if (next_instruction.type() == Instruction::Type::Store) {
  41. // If the next instruction is a Store, that is not meant to
  42. // construct an array, we can simply elide that store and reroute
  43. // all further references to the stores destination to the cached
  44. // instance of variable.
  45. // FIXME: We might be able to elide the previous load in the non-array case,
  46. // because we do not yet reuse the accumulator
  47. auto const& store = static_cast<Op::Store const&>(next_instruction);
  48. if (array_ranges.get(store.dst().index())) {
  49. // re-emit the store
  50. new (new_block->next_slot()) Op::Store(store);
  51. new_block->grow(sizeof(Op::Store));
  52. } else {
  53. register_rerouting_table.set(store.dst().index(), reg->value);
  54. }
  55. ++it;
  56. }
  57. continue;
  58. }
  59. // Otherwise we need to emit the GetVariable
  60. new (new_block->next_slot()) Op::GetVariable(get_variable);
  61. new_block->grow(sizeof(Op::GetVariable));
  62. // And if the next instruction is a Store, we can cache it's destination
  63. if (next_instruction.type() == Instruction::Type::Store) {
  64. auto const& store = static_cast<Op::Store const&>(next_instruction);
  65. identifier_table.set(get_variable.identifier().value(), store.dst());
  66. new (new_block->next_slot()) Op::Store(store);
  67. new_block->grow(sizeof(Op::Store));
  68. ++it;
  69. }
  70. continue;
  71. }
  72. case SetVariable: {
  73. // When a variable is set we need to remove it from the cache, because
  74. // we don't have an accurate view on it anymore
  75. // FIXME: If the previous instruction was a `Load $reg`, we could
  76. // update the cache instead
  77. auto const& set_variable = static_cast<Op::SetVariable const&>(*it);
  78. identifier_table.remove(set_variable.identifier().value());
  79. break;
  80. }
  81. case DeleteVariable: {
  82. // When a variable is deleted we need to remove it from the cache, it does not
  83. // exist anymore, although a variable of the same name may exist in upper scopes
  84. auto const& set_variable = static_cast<Op::DeleteVariable const&>(*it);
  85. identifier_table.remove(set_variable.identifier().value());
  86. break;
  87. }
  88. case Store: {
  89. // If we store to a position that we have are rerouting from,
  90. // we need to remove it from the routeing table
  91. // FIXME: This may be redundant due to us assigning to registers only once
  92. auto const& store = static_cast<Op::Store const&>(*it);
  93. register_rerouting_table.remove(store.dst().index());
  94. break;
  95. }
  96. case DeleteById:
  97. case DeleteByValue:
  98. // These can trigger proxies, which call into user code
  99. // So these are treated like calls
  100. case GetByValue:
  101. case GetById:
  102. case PutByValue:
  103. case PutById:
  104. // Attribute accesses (`a.o` or `a[o]`) may result in calls to getters or setters
  105. // or may trigger proxies
  106. // So these are treated like calls
  107. case Call:
  108. // Calls, especially to local functions and eval, may poison visible and
  109. // cached variables, hence we need to clear the lookup cache after emitting them
  110. // FIXME: In strict mode and with better identifier metrics, we might be able
  111. // to safe some caching with a more fine-grained identifier table
  112. // FIXME: We might be able to save some lookups to objects like `this`
  113. // which should not change their pointer
  114. memcpy(new_block->next_slot(), &*it, (*it).length());
  115. for (auto route : register_rerouting_table)
  116. reinterpret_cast<Instruction*>(new_block->next_slot())->replace_references(Register { route.key }, route.value);
  117. new_block->grow((*it).length());
  118. identifier_table.clear_with_capacity();
  119. ++it;
  120. continue;
  121. case NewBigInt:
  122. // FIXME: This is the only non trivially copyable Instruction,
  123. // so we need to do some extra work here
  124. new (new_block->next_slot()) Op::NewBigInt(static_cast<Op::NewBigInt const&>(*it));
  125. new_block->grow(sizeof(Op::NewBigInt));
  126. ++it;
  127. continue;
  128. default:
  129. break;
  130. }
  131. memcpy(new_block->next_slot(), &*it, (*it).length());
  132. for (auto route : register_rerouting_table) {
  133. // rerouting from key to value
  134. reinterpret_cast<Instruction*>(new_block->next_slot())->replace_references(Register { route.key }, route.value);
  135. }
  136. // because we are replacing the current block, we need to replace references
  137. // to ourselves here
  138. reinterpret_cast<Instruction*>(new_block->next_slot())->replace_references(block, *new_block);
  139. new_block->grow((*it).length());
  140. ++it;
  141. }
  142. return new_block;
  143. }
  144. void EliminateLoads::perform(PassPipelineExecutable& executable)
  145. {
  146. started();
  147. // FIXME: If we walk the CFG instead of the block list, we might be able to
  148. // save some work between blocks
  149. for (auto it = executable.executable.basic_blocks.begin(); it != executable.executable.basic_blocks.end(); ++it) {
  150. auto const& old_block = *it;
  151. auto new_block = eliminate_loads(*old_block, executable.executable.number_of_registers);
  152. // We will replace the old block, with a new one, so we need to replace all references,
  153. // to the old one with the new one
  154. for (auto& block : executable.executable.basic_blocks) {
  155. InstructionStreamIterator it { block->instruction_stream() };
  156. while (!it.at_end()) {
  157. auto& instruction = *it;
  158. ++it;
  159. const_cast<Instruction&>(instruction).replace_references(*old_block, *new_block);
  160. }
  161. }
  162. executable.executable.basic_blocks[it.index()] = move(new_block);
  163. }
  164. finished();
  165. }
  166. }