blob: 3391bef4e9b41a2814a996f6017249293a582731 [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
63 void SimplifyInduction(LoopNode* node);
Aart Bik482095d2016-10-10 15:39:10 -070064 void SimplifyBlocks(LoopNode* node);
Aart Bik9abf8942016-10-14 09:49:42 -070065 void RemoveIfEmptyInnerLoop(LoopNode* node);
Aart Bik281c6812016-08-26 11:31:48 -070066
Aart Bikcc42be02016-10-20 16:14:16 -070067 bool IsPhiInduction(HPhi* phi);
68 bool IsEmptyHeader(HBasicBlock* block);
69 bool IsEmptyBody(HBasicBlock* block);
70
Aart Bik482095d2016-10-10 15:39:10 -070071 bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -070072 HInstruction* instruction,
73 /*out*/ int32_t* use_count);
74 void ReplaceAllUses(HInstruction* instruction, HInstruction* replacement);
Aart Bik807868e2016-11-03 17:51:43 -070075 bool TryReplaceWithLastValue(HInstruction* instruction, HBasicBlock* block);
Aart Bik281c6812016-08-26 11:31:48 -070076
Aart Bik96202302016-10-04 17:33:56 -070077 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -070078 InductionVarRange induction_range_;
79
80 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -070081 // through this allocator is immediately released when the loop optimizer is done.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +010082 ArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -070083
Aart Bik96202302016-10-04 17:33:56 -070084 // Entries into the loop hierarchy representation. The hierarchy resides
85 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -070086 LoopNode* top_loop_;
87 LoopNode* last_loop_;
88
Aart Bik8c4a8542016-10-06 11:36:57 -070089 // Temporary bookkeeping of a set of instructions.
90 // Contents reside in phase-local heap memory.
91 ArenaSet<HInstruction*>* iset_;
92
Aart Bik482095d2016-10-10 15:39:10 -070093 // Counter that tracks how many induction cycles have been simplified. Useful
94 // to trigger incremental updates of induction variable analysis of outer loops
95 // when the induction of inner loops has changed.
96 int32_t induction_simplication_count_;
97
Aart Bik281c6812016-08-26 11:31:48 -070098 friend class LoopOptimizationTest;
99
100 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
101};
102
103} // namespace art
104
105#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_