blob: 4e1857df5b572263fd8a910afee7c57f4148c3cd [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 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010027class LoopOptimizationTest : public OptimizingUnitTest {
Aart Bik281c6812016-08-26 11:31:48 -070028 public:
29 LoopOptimizationTest()
Vladimir Markoca6fff82017-10-03 14:49:14 +010030 : graph_(CreateGraph()),
31 iva_(new (GetAllocator()) HInductionVarAnalysis(graph_)),
32 loop_opt_(new (GetAllocator()) HLoopOptimization(graph_, nullptr, iva_, nullptr)) {
Aart Bik281c6812016-08-26 11:31:48 -070033 BuildGraph();
34 }
35
36 ~LoopOptimizationTest() { }
37
38 /** Constructs bare minimum graph. */
39 void BuildGraph() {
40 graph_->SetNumberOfVRegs(1);
Vladimir Markoca6fff82017-10-03 14:49:14 +010041 entry_block_ = new (GetAllocator()) HBasicBlock(graph_);
42 return_block_ = new (GetAllocator()) HBasicBlock(graph_);
43 exit_block_ = new (GetAllocator()) HBasicBlock(graph_);
Aart Bik281c6812016-08-26 11:31:48 -070044 graph_->AddBlock(entry_block_);
45 graph_->AddBlock(return_block_);
46 graph_->AddBlock(exit_block_);
47 graph_->SetEntryBlock(entry_block_);
48 graph_->SetExitBlock(exit_block_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010049 parameter_ = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
50 dex::TypeIndex(0),
51 0,
52 DataType::Type::kInt32);
Aart Bik281c6812016-08-26 11:31:48 -070053 entry_block_->AddInstruction(parameter_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010054 return_block_->AddInstruction(new (GetAllocator()) HReturnVoid());
55 exit_block_->AddInstruction(new (GetAllocator()) HExit());
Aart Bik281c6812016-08-26 11:31:48 -070056 entry_block_->AddSuccessor(return_block_);
57 return_block_->AddSuccessor(exit_block_);
58 }
59
60 /** Adds a loop nest at given position before successor. */
61 HBasicBlock* AddLoop(HBasicBlock* position, HBasicBlock* successor) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010062 HBasicBlock* header = new (GetAllocator()) HBasicBlock(graph_);
63 HBasicBlock* body = new (GetAllocator()) HBasicBlock(graph_);
Aart Bik281c6812016-08-26 11:31:48 -070064 graph_->AddBlock(header);
65 graph_->AddBlock(body);
66 // Control flow.
67 position->ReplaceSuccessor(successor, header);
68 header->AddSuccessor(body);
69 header->AddSuccessor(successor);
Vladimir Markoca6fff82017-10-03 14:49:14 +010070 header->AddInstruction(new (GetAllocator()) HIf(parameter_));
Aart Bik281c6812016-08-26 11:31:48 -070071 body->AddSuccessor(header);
Vladimir Markoca6fff82017-10-03 14:49:14 +010072 body->AddInstruction(new (GetAllocator()) HGoto());
Aart Bik281c6812016-08-26 11:31:48 -070073 return header;
74 }
75
76 /** Performs analysis. */
77 void PerformAnalysis() {
78 graph_->BuildDominatorTree();
79 iva_->Run();
Aart Bik96202302016-10-04 17:33:56 -070080 // Do not release the loop hierarchy.
Vladimir Markoca6fff82017-10-03 14:49:14 +010081 ScopedArenaAllocator loop_allocator(GetArenaStack());
82 loop_opt_->loop_allocator_ = &loop_allocator;
Aart Bik96202302016-10-04 17:33:56 -070083 loop_opt_->LocalRun();
Aart Bik281c6812016-08-26 11:31:48 -070084 }
85
86 /** Constructs string representation of computed loop hierarchy. */
87 std::string LoopStructure() {
88 return LoopStructureRecurse(loop_opt_->top_loop_);
89 }
90
91 // Helper method
92 std::string LoopStructureRecurse(HLoopOptimization::LoopNode* node) {
93 std::string s;
94 for ( ; node != nullptr; node = node->next) {
95 s.append("[");
96 s.append(LoopStructureRecurse(node->inner));
97 s.append("]");
98 }
99 return s;
100 }
101
102 // General building fields.
Aart Bik281c6812016-08-26 11:31:48 -0700103 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
Artem Serovc73ee372017-07-31 15:08:40 +0100195// Check that SimplifyLoop() doesn't invalidate data flow when ordering loop headers'
196// predecessors.
197TEST_F(LoopOptimizationTest, SimplifyLoop) {
198 // Can't use AddLoop as we want special order for blocks predecessors.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100199 HBasicBlock* header = new (GetAllocator()) HBasicBlock(graph_);
200 HBasicBlock* body = new (GetAllocator()) HBasicBlock(graph_);
Artem Serovc73ee372017-07-31 15:08:40 +0100201 graph_->AddBlock(header);
202 graph_->AddBlock(body);
203
204 // Control flow: make a loop back edge first in the list of predecessors.
205 entry_block_->RemoveSuccessor(return_block_);
206 body->AddSuccessor(header);
207 entry_block_->AddSuccessor(header);
208 header->AddSuccessor(body);
209 header->AddSuccessor(return_block_);
210 DCHECK(header->GetSuccessors()[1] == return_block_);
211
212 // Data flow.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100213 header->AddInstruction(new (GetAllocator()) HIf(parameter_));
214 body->AddInstruction(new (GetAllocator()) HGoto());
Artem Serovc73ee372017-07-31 15:08:40 +0100215
Vladimir Markoca6fff82017-10-03 14:49:14 +0100216 HPhi* phi = new (GetAllocator()) HPhi(GetAllocator(), 0, 0, DataType::Type::kInt32);
217 HInstruction* add = new (GetAllocator()) HAdd(DataType::Type::kInt32, phi, parameter_);
Artem Serovc73ee372017-07-31 15:08:40 +0100218 header->AddPhi(phi);
219 body->AddInstruction(add);
220
221 phi->AddInput(add);
222 phi->AddInput(parameter_);
223
224 graph_->ClearLoopInformation();
225 graph_->ClearDominanceInformation();
226 graph_->BuildDominatorTree();
227
228 // Check that after optimizations in BuildDominatorTree()/SimplifyCFG() phi inputs
229 // are still mapped correctly to the block predecessors.
230 for (size_t i = 0, e = phi->InputCount(); i < e; i++) {
231 HInstruction* input = phi->InputAt(i);
232 ASSERT_TRUE(input->GetBlock()->Dominates(header->GetPredecessors()[i]));
233 }
234}
Aart Bik281c6812016-08-26 11:31:48 -0700235} // namespace art