blob: 0b798fc7a9af0352098123f6fd29af2cef3616d1 [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
Aart Bik92685a82017-03-06 11:13:43 -080026class CompilerDriver;
27
Aart Bik281c6812016-08-26 11:31:48 -070028/**
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:
Aart Bik92685a82017-03-06 11:13:43 -080034 HLoopOptimization(HGraph* graph,
35 CompilerDriver* compiler_driver,
36 HInductionVarAnalysis* induction_analysis);
Aart Bik281c6812016-08-26 11:31:48 -070037
38 void Run() OVERRIDE;
39
40 static constexpr const char* kLoopOptimizationPassName = "loop_optimization";
41
42 private:
43 /**
44 * A single loop inside the loop hierarchy representation.
45 */
Aart Bik96202302016-10-04 17:33:56 -070046 struct LoopNode : public ArenaObject<kArenaAllocLoopOptimization> {
Aart Bik281c6812016-08-26 11:31:48 -070047 explicit LoopNode(HLoopInformation* lp_info)
48 : loop_info(lp_info),
49 outer(nullptr),
50 inner(nullptr),
51 previous(nullptr),
52 next(nullptr) {}
Aart Bik482095d2016-10-10 15:39:10 -070053 HLoopInformation* const loop_info;
Aart Bik281c6812016-08-26 11:31:48 -070054 LoopNode* outer;
55 LoopNode* inner;
56 LoopNode* previous;
57 LoopNode* next;
58 };
59
Aart Bik96202302016-10-04 17:33:56 -070060 void LocalRun();
61
Aart Bik281c6812016-08-26 11:31:48 -070062 void AddLoop(HLoopInformation* loop_info);
63 void RemoveLoop(LoopNode* node);
64
65 void TraverseLoopsInnerToOuter(LoopNode* node);
66
Aart Bik6b69e0a2017-01-11 10:20:43 -080067 // Simplification.
Aart Bik281c6812016-08-26 11:31:48 -070068 void SimplifyInduction(LoopNode* node);
Aart Bik482095d2016-10-10 15:39:10 -070069 void SimplifyBlocks(LoopNode* node);
Aart Bik6b69e0a2017-01-11 10:20:43 -080070 bool SimplifyInnerLoop(LoopNode* node);
Aart Bik281c6812016-08-26 11:31:48 -070071
Aart Bik6b69e0a2017-01-11 10:20:43 -080072 // Helpers.
Aart Bikcc42be02016-10-20 16:14:16 -070073 bool IsPhiInduction(HPhi* phi);
74 bool IsEmptyHeader(HBasicBlock* block);
75 bool IsEmptyBody(HBasicBlock* block);
Aart Bik482095d2016-10-10 15:39:10 -070076 bool IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -070077 HInstruction* instruction,
Aart Bik6b69e0a2017-01-11 10:20:43 -080078 bool collect_loop_uses,
Aart Bik8c4a8542016-10-06 11:36:57 -070079 /*out*/ int32_t* use_count);
Aart Bik807868e2016-11-03 17:51:43 -070080 bool TryReplaceWithLastValue(HInstruction* instruction, HBasicBlock* block);
Aart Bik6b69e0a2017-01-11 10:20:43 -080081 void RemoveDeadInstructions(const HInstructionList& list);
Aart Bik281c6812016-08-26 11:31:48 -070082
Aart Bik92685a82017-03-06 11:13:43 -080083 // Compiler driver (to query ISA features).
84 const CompilerDriver* compiler_driver_;
85
Aart Bik96202302016-10-04 17:33:56 -070086 // Range information based on prior induction variable analysis.
Aart Bik281c6812016-08-26 11:31:48 -070087 InductionVarRange induction_range_;
88
89 // Phase-local heap memory allocator for the loop optimizer. Storage obtained
Aart Bik96202302016-10-04 17:33:56 -070090 // through this allocator is immediately released when the loop optimizer is done.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +010091 ArenaAllocator* loop_allocator_;
Aart Bik281c6812016-08-26 11:31:48 -070092
Aart Bik96202302016-10-04 17:33:56 -070093 // Entries into the loop hierarchy representation. The hierarchy resides
94 // in phase-local heap memory.
Aart Bik281c6812016-08-26 11:31:48 -070095 LoopNode* top_loop_;
96 LoopNode* last_loop_;
97
Aart Bik8c4a8542016-10-06 11:36:57 -070098 // Temporary bookkeeping of a set of instructions.
99 // Contents reside in phase-local heap memory.
100 ArenaSet<HInstruction*>* iset_;
101
Aart Bik482095d2016-10-10 15:39:10 -0700102 // Counter that tracks how many induction cycles have been simplified. Useful
103 // to trigger incremental updates of induction variable analysis of outer loops
104 // when the induction of inner loops has changed.
105 int32_t induction_simplication_count_;
106
Aart Bikdf7822e2016-12-06 10:05:30 -0800107 // Flag that tracks if any simplifications have occurred.
108 bool simplified_;
109
Aart Bik281c6812016-08-26 11:31:48 -0700110 friend class LoopOptimizationTest;
111
112 DISALLOW_COPY_AND_ASSIGN(HLoopOptimization);
113};
114
115} // namespace art
116
117#endif // ART_COMPILER_OPTIMIZING_LOOP_OPTIMIZATION_H_