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
16 namespace v8 {
17 namespace internal {
18 namespace compiler {
19
20 static constexpr uint32_t kMaximumUnnestedSize = 50;
21 static 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.
unrolling_count_heuristic(uint32_t size,uint32_t depth)26 V8_INLINE uint32_t unrolling_count_heuristic(uint32_t size, uint32_t depth) {
27 return std::min((depth + 1) * kMaximumUnnestedSize / size,
28 kMaximumUnrollingCount);
29 }
30
maximum_unrollable_size(uint32_t depth)31 V8_INLINE uint32_t maximum_unrollable_size(uint32_t depth) {
32 return (depth + 1) * kMaximumUnnestedSize;
33 }
34
35 void 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