1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_COMPILER_LOOP_UNROLLING_H_
6#define V8_COMPILER_LOOP_UNROLLING_H_
7
8// Loop unrolling is an optimization that copies the body of a loop and creates
9// a fresh loop, whose iteration corresponds to 2 or more iterations of the
10// initial loop. For a high-level description of the algorithm see
11// https://bit.ly/3G0VdWW.
12
13#include "src/compiler/common-operator.h"
14#include "src/compiler/loop-analysis.h"
15
16namespace v8 {
17namespace internal {
18namespace compiler {
19
20static constexpr uint32_t kMaximumUnnestedSize = 50;
21static constexpr uint32_t kMaximumUnrollingCount = 5;
22
23// A simple heuristic to decide how many times to unroll a loop. Favors small
24// and deeply nested loops.
25// TODO(manoskouk): Investigate how this can be improved.
26V8_INLINE uint32_t unrolling_count_heuristic(uint32_t size, uint32_t depth) {
27  return std::min((depth + 1) * kMaximumUnnestedSize / size,
28                  kMaximumUnrollingCount);
29}
30
31V8_INLINE uint32_t maximum_unrollable_size(uint32_t depth) {
32  return (depth + 1) * kMaximumUnnestedSize;
33}
34
35void UnrollLoop(Node* loop_node, ZoneUnorderedSet<Node*>* loop, uint32_t depth,
36                Graph* graph, CommonOperatorBuilder* common, Zone* tmp_zone,
37                SourcePositionTable* source_positions,
38                NodeOriginTable* node_origins);
39
40}  // namespace compiler
41}  // namespace internal
42}  // namespace v8
43
44#endif  // V8_COMPILER_LOOP_UNROLLING_H_
45