blob: 7805a69a06c588464f070a886facbef2b25edd43 [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#include "loop_optimization.h"
18#include "optimizing_unit_test.h"
19
20namespace art {
21
22/**
23 * Fixture class for the loop optimization tests. These unit tests focus
24 * constructing the loop hierarchy. Actual optimizations are tested
25 * through the checker tests.
26 */
27class LoopOptimizationTest : public CommonCompilerTest {
28 public:
29 LoopOptimizationTest()
30 : pool_(),
31 allocator_(&pool_),
32 graph_(CreateGraph(&allocator_)),
33 iva_(new (&allocator_) HInductionVarAnalysis(graph_)),
Aart Bik96202302016-10-04 17:33:56 -070034 loop_opt_(new (&allocator_) HLoopOptimization(graph_, iva_)) {
Aart Bik281c6812016-08-26 11:31:48 -070035 BuildGraph();
36 }
37
38 ~LoopOptimizationTest() { }
39
40 /** Constructs bare minimum graph. */
41 void BuildGraph() {
42 graph_->SetNumberOfVRegs(1);
43 entry_block_ = new (&allocator_) HBasicBlock(graph_);
44 return_block_ = new (&allocator_) HBasicBlock(graph_);
45 exit_block_ = new (&allocator_) HBasicBlock(graph_);
46 graph_->AddBlock(entry_block_);
47 graph_->AddBlock(return_block_);
48 graph_->AddBlock(exit_block_);
49 graph_->SetEntryBlock(entry_block_);
50 graph_->SetExitBlock(exit_block_);
51 parameter_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), 0, 0, Primitive::kPrimInt);
52 entry_block_->AddInstruction(parameter_);
53 return_block_->AddInstruction(new (&allocator_) HReturnVoid());
54 exit_block_->AddInstruction(new (&allocator_) HExit());
55 entry_block_->AddSuccessor(return_block_);
56 return_block_->AddSuccessor(exit_block_);
57 }
58
59 /** Adds a loop nest at given position before successor. */
60 HBasicBlock* AddLoop(HBasicBlock* position, HBasicBlock* successor) {
61 HBasicBlock* header = new (&allocator_) HBasicBlock(graph_);
62 HBasicBlock* body = new (&allocator_) HBasicBlock(graph_);
63 graph_->AddBlock(header);
64 graph_->AddBlock(body);
65 // Control flow.
66 position->ReplaceSuccessor(successor, header);
67 header->AddSuccessor(body);
68 header->AddSuccessor(successor);
69 header->AddInstruction(new (&allocator_) HIf(parameter_));
70 body->AddSuccessor(header);
71 body->AddInstruction(new (&allocator_) HGoto());
72 return header;
73 }
74
75 /** Performs analysis. */
76 void PerformAnalysis() {
77 graph_->BuildDominatorTree();
78 iva_->Run();
Aart Bik96202302016-10-04 17:33:56 -070079 // Do not release the loop hierarchy.
80 loop_opt_->loop_allocator_ = &allocator_;
81 loop_opt_->LocalRun();
Aart Bik281c6812016-08-26 11:31:48 -070082 }
83
84 /** Constructs string representation of computed loop hierarchy. */
85 std::string LoopStructure() {
86 return LoopStructureRecurse(loop_opt_->top_loop_);
87 }
88
89 // Helper method
90 std::string LoopStructureRecurse(HLoopOptimization::LoopNode* node) {
91 std::string s;
92 for ( ; node != nullptr; node = node->next) {
93 s.append("[");
94 s.append(LoopStructureRecurse(node->inner));
95 s.append("]");
96 }
97 return s;
98 }
99
100 // General building fields.
101 ArenaPool pool_;
102 ArenaAllocator allocator_;
103 HGraph* graph_;
104 HInductionVarAnalysis* iva_;
105 HLoopOptimization* loop_opt_;
106
107 HBasicBlock* entry_block_;
108 HBasicBlock* return_block_;
109 HBasicBlock* exit_block_;
110
111 HInstruction* parameter_;
112};
113
114//
115// The actual tests.
116//
117
118TEST_F(LoopOptimizationTest, NoLoops) {
119 PerformAnalysis();
120 EXPECT_EQ("", LoopStructure());
121}
122
123TEST_F(LoopOptimizationTest, SingleLoop) {
124 AddLoop(entry_block_, return_block_);
125 PerformAnalysis();
126 EXPECT_EQ("[]", LoopStructure());
127}
128
129TEST_F(LoopOptimizationTest, LoopNest10) {
130 HBasicBlock* b = entry_block_;
131 HBasicBlock* s = return_block_;
132 for (int i = 0; i < 10; i++) {
133 s = AddLoop(b, s);
134 b = s->GetSuccessors()[0];
135 }
136 PerformAnalysis();
137 EXPECT_EQ("[[[[[[[[[[]]]]]]]]]]", LoopStructure());
138}
139
140TEST_F(LoopOptimizationTest, LoopSequence10) {
141 HBasicBlock* b = entry_block_;
142 HBasicBlock* s = return_block_;
143 for (int i = 0; i < 10; i++) {
144 b = AddLoop(b, s);
145 s = b->GetSuccessors()[1];
146 }
147 PerformAnalysis();
148 EXPECT_EQ("[][][][][][][][][][]", LoopStructure());
149}
150
151TEST_F(LoopOptimizationTest, LoopSequenceOfNests) {
152 HBasicBlock* b = entry_block_;
153 HBasicBlock* s = return_block_;
154 for (int i = 0; i < 10; i++) {
155 b = AddLoop(b, s);
156 s = b->GetSuccessors()[1];
157 HBasicBlock* bi = b->GetSuccessors()[0];
158 HBasicBlock* si = b;
159 for (int j = 0; j < i; j++) {
160 si = AddLoop(bi, si);
161 bi = si->GetSuccessors()[0];
162 }
163 }
164 PerformAnalysis();
165 EXPECT_EQ("[]"
166 "[[]]"
167 "[[[]]]"
168 "[[[[]]]]"
169 "[[[[[]]]]]"
170 "[[[[[[]]]]]]"
171 "[[[[[[[]]]]]]]"
172 "[[[[[[[[]]]]]]]]"
173 "[[[[[[[[[]]]]]]]]]"
174 "[[[[[[[[[[]]]]]]]]]]",
175 LoopStructure());
176}
177
178TEST_F(LoopOptimizationTest, LoopNestWithSequence) {
179 HBasicBlock* b = entry_block_;
180 HBasicBlock* s = return_block_;
181 for (int i = 0; i < 10; i++) {
182 s = AddLoop(b, s);
183 b = s->GetSuccessors()[0];
184 }
185 b = s;
186 s = b->GetSuccessors()[1];
187 for (int i = 0; i < 9; i++) {
188 b = AddLoop(b, s);
189 s = b->GetSuccessors()[1];
190 }
191 PerformAnalysis();
192 EXPECT_EQ("[[[[[[[[[[][][][][][][][][][]]]]]]]]]]", LoopStructure());
193}
194
195} // namespace art