blob: 9ddab4150cec79baaa07235459bb54c9c7b4ed9e [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) {}
Aart Bik482095d2016-10-10 15:39:10 -070049 HLoopInformation* const loop_info;
Aart Bik281c6812016-08-26 11:31:48 -070050 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
Aart Bik6b69e0a2017-01-11 10:20:43 -080063 // Simplification.
Aart Bik281c6812016-08-26 11:31:48 -070064 void SimplifyInduction(LoopNode* node);
Aart Bik482095d2016-10-10 15:39:10 -070065 void SimplifyBlocks(LoopNode* node);
Aart Bik6b69e0a2017-01-11 10:20:43 -080066 bool SimplifyInnerLoop(LoopNode* node);
Aart Bik281c6812016-08-26 11:31:48 -070067
Aart Bik6b69e0a2017-01-11 10:20:43 -080068 // Helpers.
Aart Bikcc42be02016-10-20 16:14:16 -070069 bool IsPhiInduction(HPhi* phi);
70 bool IsEmptyHeader(HBasicBlock* block);
71 bool IsEmptyBody(HBasicBlock* block);
Aart Bik482095d2016-10-10 15:39:10 -070072 bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -070073 HInstruction* instruction,
Aart Bik6b69e0a2017-01-11 10:20:43 -080074 bool collect_loop_uses,
Aart Bik8c4a8542016-10-06 11:36:57 -070075 /*out*/ int32_t* use_count);
Aart Bik807868e2016-11-03 17:51:43 -070076 bool TryReplaceWithLastValue(HInstruction* instruction, HBasicBlock* block);
Aart Bik6b69e0a2017-01-11 10:20:43 -080077 void RemoveDeadInstructions(const HInstructionList& list);
Aart Bik281c6812016-08-26 11:31:48 -070078
Aart Bik96202302016-10-04 17:33:56 -070079 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -070080 InductionVarRange induction_range_;
81
82 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -070083 // through this allocator is immediately released when the loop optimizer is done.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +010084 ArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -070085
Aart Bik96202302016-10-04 17:33:56 -070086 // Entries into the loop hierarchy representation. The hierarchy resides
87 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -070088 LoopNode* top_loop_;
89 LoopNode* last_loop_;
90
Aart Bik8c4a8542016-10-06 11:36:57 -070091 // Temporary bookkeeping of a set of instructions.
92 // Contents reside in phase-local heap memory.
93 ArenaSet<HInstruction*>* iset_;
94
Aart Bik482095d2016-10-10 15:39:10 -070095 // Counter that tracks how many induction cycles have been simplified. Useful
96 // to trigger incremental updates of induction variable analysis of outer loops
97 // when the induction of inner loops has changed.
98 int32_t induction_simplication_count_;
99
Aart Bikdf7822e2016-12-06 10:05:30 -0800100 // Flag that tracks if any simplifications have occurred.
101 bool simplified_;
102
Aart Bik281c6812016-08-26 11:31:48 -0700103 friend class LoopOptimizationTest;
104
105 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
106};
107
108} // namespace art
109
110#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_