blob: d3b8cb1cf89f15f3b286a31c1574d819e2378d37 [file] [log] [blame]
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001/*
2 * Copyright (C) 2014 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
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_allocator.h"
Vladimír Marko434d9682022-11-04 14:04:17 +000018#include "base/macros.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000019#include "builder.h"
David Sehr9e734c72018-01-04 17:56:19 -080020#include "dex/dex_instruction.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000021#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000022#include "optimizing_unit_test.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000023
24#include "gtest/gtest.h"
25
Vladimír Marko434d9682022-11-04 14:04:17 +000026namespace art HIDDEN {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000027
Vladimir Markoca6fff82017-10-03 14:49:14 +010028class OptimizerTest : public OptimizingUnitTest {
29 protected:
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080030 void TestCode(const std::vector<uint16_t>& data, const uint32_t* blocks, size_t blocks_length);
Vladimir Markoca6fff82017-10-03 14:49:14 +010031};
David Brazdilbadd8262016-02-02 16:28:56 +000032
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080033void OptimizerTest::TestCode(const std::vector<uint16_t>& data,
34 const uint32_t* blocks,
35 size_t blocks_length) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010036 HGraph* graph = CreateCFG(data);
Vladimir Markofa6b93c2015-09-15 10:15:55 +010037 ASSERT_EQ(graph->GetBlocks().size(), blocks_length);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010038 for (size_t i = 0, e = blocks_length; i < e; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010039 if (blocks[i] == kInvalidBlockId) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010040 if (graph->GetBlocks()[i] == nullptr) {
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010041 // Dead block.
42 } else {
43 // Only the entry block has no dominator.
Vladimir Markoec7802a2015-10-01 20:57:57 +010044 ASSERT_EQ(nullptr, graph->GetBlocks()[i]->GetDominator());
45 ASSERT_TRUE(graph->GetBlocks()[i]->IsEntryBlock());
Nicolas Geoffrayf776b922015-04-15 18:22:45 +010046 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000047 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +010048 ASSERT_NE(nullptr, graph->GetBlocks()[i]->GetDominator());
49 ASSERT_EQ(blocks[i], graph->GetBlocks()[i]->GetDominator()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000050 }
51 }
52}
53
David Brazdilbadd8262016-02-02 16:28:56 +000054TEST_F(OptimizerTest, ReturnVoid) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080055 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056 Instruction::RETURN_VOID); // Block number 1
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057
Vladimir Markofa6b93c2015-09-15 10:15:55 +010058 const uint32_t dominators[] = {
59 kInvalidBlockId,
60 0,
61 1
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000062 };
63
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000064 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000065}
66
David Brazdilbadd8262016-02-02 16:28:56 +000067TEST_F(OptimizerTest, CFG1) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080068 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000069 Instruction::GOTO | 0x100, // Block number 1
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000070 Instruction::RETURN_VOID); // Block number 2
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000071
Vladimir Markofa6b93c2015-09-15 10:15:55 +010072 const uint32_t dominators[] = {
73 kInvalidBlockId,
74 0,
75 1,
76 2
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077 };
78
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000079 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080}
81
David Brazdilbadd8262016-02-02 16:28:56 +000082TEST_F(OptimizerTest, CFG2) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080083 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000084 Instruction::GOTO | 0x100, // Block number 1
85 Instruction::GOTO | 0x100, // Block number 2
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000086 Instruction::RETURN_VOID); // Block number 3
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000087
Vladimir Markofa6b93c2015-09-15 10:15:55 +010088 const uint32_t dominators[] = {
89 kInvalidBlockId,
90 0,
91 1,
92 2,
93 3
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094 };
95
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000096 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000097}
98
David Brazdilbadd8262016-02-02 16:28:56 +000099TEST_F(OptimizerTest, CFG3) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800100 const std::vector<uint16_t> data1 = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000101 Instruction::GOTO | 0x200, // Block number 1
102 Instruction::RETURN_VOID, // Block number 2
103 Instruction::GOTO | 0xFF00); // Block number 3
104
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100105 const uint32_t dominators[] = {
106 kInvalidBlockId,
107 0,
108 3,
109 1,
110 2
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000111 };
112
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000113 TestCode(data1, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000114
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800115 const std::vector<uint16_t> data2 = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000116 Instruction::GOTO_16, 3,
117 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 TestCode(data2, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800122 const std::vector<uint16_t> data3 = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 Instruction::GOTO_32, 4, 0,
124 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000125 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000126
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 TestCode(data3, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000128}
129
David Brazdilbadd8262016-02-02 16:28:56 +0000130TEST_F(OptimizerTest, CFG4) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800131 const std::vector<uint16_t> data1 = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000132 Instruction::NOP,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000133 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100135 const uint32_t dominators[] = {
136 kInvalidBlockId,
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000137 3,
138 kInvalidBlockId,
139 0
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000140 };
141
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000142 TestCode(data1, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000143
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800144 const std::vector<uint16_t> data2 = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000145 Instruction::GOTO_32, 0, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000146
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 TestCode(data2, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000148}
149
David Brazdilbadd8262016-02-02 16:28:56 +0000150TEST_F(OptimizerTest, CFG5) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800151 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000152 Instruction::RETURN_VOID, // Block number 1
153 Instruction::GOTO | 0x100, // Dead block
154 Instruction::GOTO | 0xFE00); // Block number 2
155
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000156
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 const uint32_t dominators[] = {
158 kInvalidBlockId,
159 0,
160 kInvalidBlockId,
161 1
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162 };
163
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000164 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165}
166
David Brazdilbadd8262016-02-02 16:28:56 +0000167TEST_F(OptimizerTest, CFG6) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800168 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray788aaad2014-03-10 12:00:43 +0000169 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170 Instruction::IF_EQ, 3,
171 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000172 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100174 const uint32_t dominators[] = {
175 kInvalidBlockId,
176 0,
177 1,
178 1,
179 3,
180 1, // Synthesized block to avoid critical edge.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000181 };
182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000183 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000184}
185
David Brazdilbadd8262016-02-02 16:28:56 +0000186TEST_F(OptimizerTest, CFG7) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800187 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray788aaad2014-03-10 12:00:43 +0000188 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000189 Instruction::IF_EQ, 3, // Block number 1
190 Instruction::GOTO | 0x100, // Block number 2
191 Instruction::GOTO | 0xFF00); // Block number 3
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000192
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100193 const uint32_t dominators[] = {
194 kInvalidBlockId,
195 0,
196 1,
197 1,
198 kInvalidBlockId, // exit block is not dominated by any block due to the spin loop.
199 1, // block to avoid critical edge.
200 1 // block to avoid critical edge.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201 };
202
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000203 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204}
205
David Brazdilbadd8262016-02-02 16:28:56 +0000206TEST_F(OptimizerTest, CFG8) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800207 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray788aaad2014-03-10 12:00:43 +0000208 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000209 Instruction::IF_EQ, 3, // Block number 1
210 Instruction::GOTO | 0x200, // Block number 2
211 Instruction::GOTO | 0x100, // Block number 3
212 Instruction::GOTO | 0xFF00); // Block number 4
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000213
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100214 const uint32_t dominators[] = {
215 kInvalidBlockId,
216 0,
217 1,
218 1,
219 1,
220 kInvalidBlockId, // exit block is not dominated by any block due to the spin loop.
221 1 // block to avoid critical edge.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 };
223
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000224 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000225}
226
David Brazdilbadd8262016-02-02 16:28:56 +0000227TEST_F(OptimizerTest, CFG9) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800228 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray788aaad2014-03-10 12:00:43 +0000229 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000230 Instruction::IF_EQ, 3, // Block number 1
231 Instruction::GOTO | 0x200, // Block number 2
232 Instruction::GOTO | 0x100, // Block number 3
233 Instruction::GOTO | 0xFE00); // Block number 4
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100235 const uint32_t dominators[] = {
236 kInvalidBlockId,
237 0,
238 1,
239 1,
240 1,
241 kInvalidBlockId, // exit block is not dominated by any block due to the spin loop.
242 1 // block to avoid critical edge.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000243 };
244
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000245 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000246}
247
David Brazdilbadd8262016-02-02 16:28:56 +0000248TEST_F(OptimizerTest, CFG10) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800249 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray788aaad2014-03-10 12:00:43 +0000250 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000251 Instruction::IF_EQ, 6, // Block number 1
252 Instruction::IF_EQ, 3, // Block number 2
253 Instruction::GOTO | 0x100, // Block number 3
254 Instruction::GOTO | 0x100, // Block number 4
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000255 Instruction::RETURN_VOID); // Block number 5
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000256
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100257 const uint32_t dominators[] = {
258 kInvalidBlockId,
259 0,
260 1,
261 2,
262 2,
263 1,
264 5, // Block number 5 dominates exit block
265 1, // block to avoid critical edge.
266 2 // block to avoid critical edge.
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267 };
268
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000269 TestCode(data, dominators, sizeof(dominators) / sizeof(int));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270}
271
272} // namespace art