blob: b2bf1c8507e631ae08441c08f3e0e4d3ac8c8bf2 [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
Aart Bik8c4a8542016-10-06 11:36:57 -070066 bool IsOnlyUsedAfterLoop(const HLoopInformation& loop_info,
67 HInstruction* instruction,
68 /*out*/ int32_t* use_count);
69 void ReplaceAllUses(HInstruction* instruction, HInstruction* replacement);
70 bool TryReplaceWithLastValue(HInstruction* instruction,
71 int32_t use_count,
72 HBasicBlock* block);
Aart Bik281c6812016-08-26 11:31:48 -070073
Aart Bik96202302016-10-04 17:33:56 -070074 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -070075 InductionVarRange induction_range_;
76
77 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -070078 // through this allocator is immediately released when the loop optimizer is done.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +010079 ArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -070080
Aart Bik96202302016-10-04 17:33:56 -070081 // Entries into the loop hierarchy representation. The hierarchy resides
82 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -070083 LoopNode* top_loop_;
84 LoopNode* last_loop_;
85
Aart Bik8c4a8542016-10-06 11:36:57 -070086 // Temporary bookkeeping of a set of instructions.
87 // Contents reside in phase-local heap memory.
88 ArenaSet<HInstruction*>* iset_;
89
Aart Bik281c6812016-08-26 11:31:48 -070090 friend class LoopOptimizationTest;
91
92 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
93};
94
95} // namespace art
96
97#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_