blob: 591e45a7fb0f3137e7859c37dc0081d3f2be5dd6 [file] [log] [blame]
Aart Bik281c6812016-08-26 11:31:48 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_
18#define ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_
19
20#include <string>
21
22#include "induction_var_range.h"
23#include "nodes.h"
24#include "optimization.h"
25
26namespace art {
27
28/**
29 * Loop optimizations. Builds a loop hierarchy and applies optimizations to
30 * the detected nested loops, such as removal of dead induction and empty loops.
31 */
32class HLoopOptimization : public HOptimization {
33 public:
34 HLoopOptimization(HGraph* graph, HInductionVarAnalysis* induction_analysis);
Nicolas Geoffray5ed20f92016-10-05 13:49:44 +010035 HLoopOptimization(HGraph* graph,
36 HInductionVarAnalysis* induction_analysis,
37 ArenaAllocator* allocator);
Aart Bik281c6812016-08-26 11:31:48 -070038
39 void Run() OVERRIDE;
40
41 static constexpr const char* kLoopOptimizationPassName = "loop_optimization";
42
43 private:
44 /**
45 * A single loop inside the loop hierarchy representation.
46 */
47 struct LoopNode : public ArenaObject<kArenaAllocInductionVarAnalysis> {
48 explicit LoopNode(HLoopInformation* lp_info)
49 : loop_info(lp_info),
50 outer(nullptr),
51 inner(nullptr),
52 previous(nullptr),
53 next(nullptr) {}
54 const HLoopInformation* const loop_info;
55 LoopNode* outer;
56 LoopNode* inner;
57 LoopNode* previous;
58 LoopNode* next;
59 };
60
61 void AddLoop(HLoopInformation* loop_info);
62 void RemoveLoop(LoopNode* node);
63
64 void TraverseLoopsInnerToOuter(LoopNode* node);
65
66 void SimplifyInduction(LoopNode* node);
67 void RemoveIfEmptyLoop(LoopNode* node);
68
69 void ReplaceAllUses(HInstruction* instruction,
70 HInstruction* replacement,
71 HInstruction* exclusion);
72
73 // Range analysis based on induction variables.
74 InductionVarRange induction_range_;
75
76 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
77 // through this allocator is released when the loop optimizer is done.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +010078 ArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -070079
80 // Entries into the loop hierarchy representation.
81 LoopNode* top_loop_;
82 LoopNode* last_loop_;
83
84 friend class LoopOptimizationTest;
85
86 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
87};
88
89} // namespace art
90
91#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_