13 m_logger(
"IROptimizer", debug)
200 void IROptimizer::process(
const std::vector<IR::Block>& pages,
const std::vector<std::string>& symbols,
const std::vector<ValTableElem>& values)
206 for (
const auto& block : pages)
212 const std::size_t end = block.size();
219 block.begin() +
static_cast<IR::Block::difference_type
>(i),
222 if (maybe_compacted.has_value())
224 auto [entity, offset] = maybe_compacted.value();
225 current_block.emplace_back(entity);
230 current_block.emplace_back(block[i]);
244 bool IROptimizer::match(
const std::vector<Instruction>& expected_insts,
const std::span<const IR::Entity> entities)
const
246 if (expected_insts.size() > entities.size())
249 for (std::size_t i = 0; i < expected_insts.size(); ++i)
251 if (expected_insts[i] != entities[i].inst())
261 return std::ranges::none_of(
262 entities | std::ranges::views::take(window_size),
270 for (
const auto& [expected, condition, createReplacement] : rules)
272 if (
match(expected, entities) && condition(entities))
274 const std::size_t window_size = expected.size();
278 auto output = createReplacement(entities);
280 if (
const auto it = std::ranges::find_if(entities, [](
const auto& entity) {
281 return entity.hasValidSourceLocation();
283 it != entities.end())
284 output.setSourceLocation(it->filename(), it->sourceLine());
297 const double val = std::get<double>(
m_values[
id].value);
300 static_cast<double>(
static_cast<long>(val)) == val;
307 return static_cast<uint16_t
>(std::get<double>(
m_values[
id].value));
Host the declaration of all the ArkScript builtins.
Optimize IR based on IR entity grouped by 2 (or more)
std::vector< ValTableElem > m_values
bool isPositiveNumberInlinable(uint16_t id) const
std::vector< IR::Block > m_ir
IROptimizer(unsigned debug)
Create a new IROptimizer.
uint16_t numberAsArg(uint16_t id) const
bool canBeOptimizedSafely(std::span< const IR::Entity > entities, std::size_t window_size) const
std::optional< EntityWithOffset > replaceWithRules(const std::vector< Rule > &rules, std::span< const IR::Entity > entities)
std::span< const IR::Entity > Entities
const std::vector< IR::Block > & intermediateRepresentation() const noexcept
Return the IR blocks (one per scope)
std::vector< Rule > m_ruleset
void process(const std::vector< IR::Block > &pages, const std::vector< std::string > &symbols, const std::vector< ValTableElem > &values)
Turn a given IR into bytecode.
std::vector< std::string > m_symbols
bool match(const std::vector< Instruction > &expected_insts, std::span< const IR::Entity > entities) const
static Entity GotoWithArg(const Entity &label, Instruction inst, uint16_t primary_arg)
uint16_t primaryArg() const
void traceStart(std::string &&trace_name)
ARK_API const std::vector< std::pair< std::string, Value > > builtins
std::vector< Entity > Block
constexpr uint16_t MaxValueForDualArg
The maximum value an argument can have when an IR entity has two arguments.
@ GET_FIELD_FROM_SYMBOL_INDEX
@ APPEND_IN_PLACE_SYM_INDEX
@ EQ_SYM_INDEX_JUMP_IF_TRUE
@ CALL_BUILTIN_WITHOUT_RETURN_ADDRESS