blob: 4e007d4e9a3478c8c5af756c384b398c64a68a8b [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_)),
34 loop_opt_(new (&allocator_) HLoopOptimization(graph_, iva_)) {
35 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();
79 loop_opt_->Run();
80 }
81
82 /** Constructs string representation of computed loop hierarchy. */
83 std::string LoopStructure() {
84 return LoopStructureRecurse(loop_opt_->top_loop_);
85 }
86
87 // Helper method
88 std::string LoopStructureRecurse(HLoopOptimization::LoopNode* node) {
89 std::string s;
90 for ( ; node != nullptr; node = node->next) {
91 s.append("[");
92 s.append(LoopStructureRecurse(node->inner));
93 s.append("]");
94 }
95 return s;
96 }
97
98 // General building fields.
99 ArenaPool pool_;
100 ArenaAllocator allocator_;
101 HGraph* graph_;
102 HInductionVarAnalysis* iva_;
103 HLoopOptimization* loop_opt_;
104
105 HBasicBlock* entry_block_;
106 HBasicBlock* return_block_;
107 HBasicBlock* exit_block_;
108
109 HInstruction* parameter_;
110};
111
112//
113// The actual tests.
114//
115
116TEST_F(LoopOptimizationTest, NoLoops) {
117 PerformAnalysis();
118 EXPECT_EQ("", LoopStructure());
119}
120
121TEST_F(LoopOptimizationTest, SingleLoop) {
122 AddLoop(entry_block_, return_block_);
123 PerformAnalysis();
124 EXPECT_EQ("[]", LoopStructure());
125}
126
127TEST_F(LoopOptimizationTest, LoopNest10) {
128 HBasicBlock* b = entry_block_;
129 HBasicBlock* s = return_block_;
130 for (int i = 0; i < 10; i++) {
131 s = AddLoop(b, s);
132 b = s->GetSuccessors()[0];
133 }
134 PerformAnalysis();
135 EXPECT_EQ("[[[[[[[[[[]]]]]]]]]]", LoopStructure());
136}
137
138TEST_F(LoopOptimizationTest, LoopSequence10) {
139 HBasicBlock* b = entry_block_;
140 HBasicBlock* s = return_block_;
141 for (int i = 0; i < 10; i++) {
142 b = AddLoop(b, s);
143 s = b->GetSuccessors()[1];
144 }
145 PerformAnalysis();
146 EXPECT_EQ("[][][][][][][][][][]", LoopStructure());
147}
148
149TEST_F(LoopOptimizationTest, LoopSequenceOfNests) {
150 HBasicBlock* b = entry_block_;
151 HBasicBlock* s = return_block_;
152 for (int i = 0; i < 10; i++) {
153 b = AddLoop(b, s);
154 s = b->GetSuccessors()[1];
155 HBasicBlock* bi = b->GetSuccessors()[0];
156 HBasicBlock* si = b;
157 for (int j = 0; j < i; j++) {
158 si = AddLoop(bi, si);
159 bi = si->GetSuccessors()[0];
160 }
161 }
162 PerformAnalysis();
163 EXPECT_EQ("[]"
164 "[[]]"
165 "[[[]]]"
166 "[[[[]]]]"
167 "[[[[[]]]]]"
168 "[[[[[[]]]]]]"
169 "[[[[[[[]]]]]]]"
170 "[[[[[[[[]]]]]]]]"
171 "[[[[[[[[[]]]]]]]]]"
172 "[[[[[[[[[[]]]]]]]]]]",
173 LoopStructure());
174}
175
176TEST_F(LoopOptimizationTest, LoopNestWithSequence) {
177 HBasicBlock* b = entry_block_;
178 HBasicBlock* s = return_block_;
179 for (int i = 0; i < 10; i++) {
180 s = AddLoop(b, s);
181 b = s->GetSuccessors()[0];
182 }
183 b = s;
184 s = b->GetSuccessors()[1];
185 for (int i = 0; i < 9; i++) {
186 b = AddLoop(b, s);
187 s = b->GetSuccessors()[1];
188 }
189 PerformAnalysis();
190 EXPECT_EQ("[[[[[[[[[[][][][][][][][][][]]]]]]]]]]", LoopStructure());
191}
192
193} // namespace art