blob: 6092955221c865a849ed959acc440264f75c8107 [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
Aart Bik281c6812016-08-26 11:31:48 -070020#include "induction_var_range.h"
21#include "nodes.h"
22#include "optimization.h"
23
24namespace art {
25
26/**
27 * Loop optimizations. Builds a loop hierarchy and applies optimizations to
28 * the detected nested loops, such as removal of dead induction and empty loops.
29 */
30class HLoopOptimization : public HOptimization {
31 public:
32 HLoopOptimization(HGraph* graph, HInductionVarAnalysis* induction_analysis);
33
34 void Run() OVERRIDE;
35
36 static constexpr const char* kLoopOptimizationPassName = "loop_optimization";
37
38 private:
39 /**
40 * A single loop inside the loop hierarchy representation.
41 */
Aart Bik96202302016-10-04 17:33:56 -070042 struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> {
Aart Bik281c6812016-08-26 11:31:48 -070043 explicit LoopNode(HLoopInformation* lp_info)
44 : loop_info(lp_info),
45 outer(nullptr),
46 inner(nullptr),
47 previous(nullptr),
48 next(nullptr) {}
49 const HLoopInformation* const loop_info;
50 LoopNode* outer;
51 LoopNode* inner;
52 LoopNode* previous;
53 LoopNode* next;
54 };
55
Aart Bik96202302016-10-04 17:33:56 -070056 void LocalRun();
57
Aart Bik281c6812016-08-26 11:31:48 -070058 void AddLoop(HLoopInformation* loop_info);
59 void RemoveLoop(LoopNode* node);
60
61 void TraverseLoopsInnerToOuter(LoopNode* node);
62
63 void SimplifyInduction(LoopNode* node);
64 void RemoveIfEmptyLoop(LoopNode* node);
65
66 void ReplaceAllUses(HInstruction* instruction,
67 HInstruction* replacement,
68 HInstruction* exclusion);
69
Aart Bik96202302016-10-04 17:33:56 -070070 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -070071 InductionVarRange induction_range_;
72
73 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -070074 // through this allocator is immediately released when the loop optimizer is done.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +010075 ArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -070076
Aart Bik96202302016-10-04 17:33:56 -070077 // Entries into the loop hierarchy representation. The hierarchy resides
78 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -070079 LoopNode* top_loop_;
80 LoopNode* last_loop_;
81
82 friend class LoopOptimizationTest;
83
84 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
85};
86
87} // namespace art
88
89#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_