11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_COMPILER_LOOP_UNROLLING_H_
61cb0ef41Sopenharmony_ci#define V8_COMPILER_LOOP_UNROLLING_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Loop unrolling is an optimization that copies the body of a loop and creates
91cb0ef41Sopenharmony_ci// a fresh loop, whose iteration corresponds to 2 or more iterations of the
101cb0ef41Sopenharmony_ci// initial loop. For a high-level description of the algorithm see
111cb0ef41Sopenharmony_ci// https://bit.ly/3G0VdWW.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#include "src/compiler/common-operator.h"
141cb0ef41Sopenharmony_ci#include "src/compiler/loop-analysis.h"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cinamespace v8 {
171cb0ef41Sopenharmony_cinamespace internal {
181cb0ef41Sopenharmony_cinamespace compiler {
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cistatic constexpr uint32_t kMaximumUnnestedSize = 50;
211cb0ef41Sopenharmony_cistatic constexpr uint32_t kMaximumUnrollingCount = 5;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// A simple heuristic to decide how many times to unroll a loop. Favors small
241cb0ef41Sopenharmony_ci// and deeply nested loops.
251cb0ef41Sopenharmony_ci// TODO(manoskouk): Investigate how this can be improved.
261cb0ef41Sopenharmony_ciV8_INLINE uint32_t unrolling_count_heuristic(uint32_t size, uint32_t depth) {
271cb0ef41Sopenharmony_ci  return std::min((depth + 1) * kMaximumUnnestedSize / size,
281cb0ef41Sopenharmony_ci                  kMaximumUnrollingCount);
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciV8_INLINE uint32_t maximum_unrollable_size(uint32_t depth) {
321cb0ef41Sopenharmony_ci  return (depth + 1) * kMaximumUnnestedSize;
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_civoid UnrollLoop(Node* loop_node, ZoneUnorderedSet<Node*>* loop, uint32_t depth,
361cb0ef41Sopenharmony_ci                Graph* graph, CommonOperatorBuilder* common, Zone* tmp_zone,
371cb0ef41Sopenharmony_ci                SourcePositionTable* source_positions,
381cb0ef41Sopenharmony_ci                NodeOriginTable* node_origins);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci}  // namespace compiler
411cb0ef41Sopenharmony_ci}  // namespace internal
421cb0ef41Sopenharmony_ci}  // namespace v8
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci#endif  // V8_COMPILER_LOOP_UNROLLING_H_
45