blob: d5aecacd2a0301f620d60466b40abee5cf44798b [file] [log] [blame]
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +01001/*
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
17#include <fstream>
18
Mathieu Chartierb666f482015-02-18 14:33:14 -080019#include "base/arena_allocator.h"
Vladimir Markoe2727152019-10-10 10:46:42 +010020#include "base/macros.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010021#include "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010022#include "code_generator.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file.h"
24#include "dex/dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000025#include "driver/compiler_options.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010026#include "graph_visualizer.h"
27#include "nodes.h"
28#include "optimizing_unit_test.h"
29#include "pretty_printer.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010030#include "ssa_liveness_analysis.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010031
Vladimir Markoe2727152019-10-10 10:46:42 +010032namespace art HIDDEN {
David Brazdild9510df2015-11-04 23:30:22 +000033
Vladimir Markoca6fff82017-10-03 14:49:14 +010034class LinearizeTest : public OptimizingUnitTest {
35 protected:
36 template <size_t number_of_blocks>
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080037 void TestCode(const std::vector<uint16_t>& data,
38 const uint32_t (&expected_order)[number_of_blocks]);
Vladimir Markoca6fff82017-10-03 14:49:14 +010039};
David Brazdil4833f5a2015-12-16 10:37:39 +000040
Vladimir Markofa6b93c2015-09-15 10:15:55 +010041template <size_t number_of_blocks>
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080042void LinearizeTest::TestCode(const std::vector<uint16_t>& data,
Vladimir Markoca6fff82017-10-03 14:49:14 +010043 const uint32_t (&expected_order)[number_of_blocks]) {
44 HGraph* graph = CreateCFG(data);
Vladimir Markoa0431112018-06-25 09:32:54 +010045 std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_);
46 SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010047 liveness.Analyze();
48
Vladimir Markofa6b93c2015-09-15 10:15:55 +010049 ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010050 for (size_t i = 0; i < number_of_blocks; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010051 ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010052 }
53}
54
David Brazdil4833f5a2015-12-16 10:37:39 +000055TEST_F(LinearizeTest, CFG1) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010056 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010057 // Block0
58 // |
59 // Block1
60 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010061 // Block2 ++++++
62 // / \ +
63 // Block5 Block7 +
64 // | | +
65 // Block6 Block3 +
66 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010067 // Block4 Block8
68
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080069 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010070 Instruction::CONST_4 | 0 | 0,
71 Instruction::IF_EQ, 5,
72 Instruction::IF_EQ, 0xFFFE,
73 Instruction::GOTO | 0xFE00,
74 Instruction::RETURN_VOID);
75
Vladimir Markofa6b93c2015-09-15 10:15:55 +010076 const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
77 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010078}
79
David Brazdil4833f5a2015-12-16 10:37:39 +000080TEST_F(LinearizeTest, CFG2) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010081 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010082 // Block0
83 // |
84 // Block1
85 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010086 // Block2 ++++++
87 // / \ +
88 // Block3 Block7 +
89 // | | +
90 // Block6 Block4 +
91 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010092 // Block5 Block8
93
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080094 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010095 Instruction::CONST_4 | 0 | 0,
96 Instruction::IF_EQ, 3,
97 Instruction::RETURN_VOID,
98 Instruction::IF_EQ, 0xFFFD,
99 Instruction::GOTO | 0xFE00);
100
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100101 const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
102 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100103}
104
David Brazdil4833f5a2015-12-16 10:37:39 +0000105TEST_F(LinearizeTest, CFG3) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100106 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100107 // Block0
108 // |
109 // Block1
110 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100111 // Block2 ++++++
112 // / \ +
113 // Block3 Block8 +
114 // | | +
115 // Block7 Block5 +
116 // / + \ +
117 // Block6 + Block9
118 // | +
119 // Block4 ++
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800120 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100121 Instruction::CONST_4 | 0 | 0,
122 Instruction::IF_EQ, 4,
123 Instruction::RETURN_VOID,
124 Instruction::GOTO | 0x0100,
125 Instruction::IF_EQ, 0xFFFC,
126 Instruction::GOTO | 0xFD00);
127
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100128 const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
129 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100130}
131
David Brazdil4833f5a2015-12-16 10:37:39 +0000132TEST_F(LinearizeTest, CFG4) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100133 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100134 // Block0
135 // |
136 // Block1
137 // |
138 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100139 // / + \
140 // Block6 + Block8
141 // | + |
142 // Block7 + Block3 +++++++
143 // + / \ +
144 // Block9 Block10 +
145 // | +
146 // Block4 +
147 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100148 // Block5 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100149 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800150 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100151 Instruction::CONST_4 | 0 | 0,
152 Instruction::IF_EQ, 7,
153 Instruction::IF_EQ, 0xFFFE,
154 Instruction::IF_EQ, 0xFFFE,
155 Instruction::GOTO | 0xFE00,
156 Instruction::RETURN_VOID);
157
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100158 const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
159 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100160}
161
David Brazdil4833f5a2015-12-16 10:37:39 +0000162TEST_F(LinearizeTest, CFG5) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100163 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100164 // Block0
165 // |
166 // Block1
167 // |
168 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100169 // / + \
170 // Block3 + Block8
171 // | + |
172 // Block7 + Block4 +++++++
173 // + / \ +
174 // Block9 Block10 +
175 // | +
176 // Block5 +
177 // +/ \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100178 // Block6 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100179 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800180 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100181 Instruction::CONST_4 | 0 | 0,
182 Instruction::IF_EQ, 3,
183 Instruction::RETURN_VOID,
184 Instruction::IF_EQ, 0xFFFD,
185 Instruction::IF_EQ, 0xFFFE,
186 Instruction::GOTO | 0xFE00);
187
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100188 const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
189 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100190}
191
David Brazdil4833f5a2015-12-16 10:37:39 +0000192TEST_F(LinearizeTest, CFG6) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000193 // Block0
194 // |
195 // Block1
196 // |
197 // Block2 ++++++++++++++
198 // | +
199 // Block3 +
200 // / \ +
201 // Block8 Block4 +
202 // | / \ +
203 // Block5 <- Block9 Block6 +
204 // |
205 // Block7
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800206 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000207 Instruction::CONST_4 | 0 | 0,
208 Instruction::GOTO | 0x0100,
209 Instruction::IF_EQ, 0x0004,
210 Instruction::IF_EQ, 0x0003,
211 Instruction::RETURN_VOID,
212 Instruction::GOTO | 0xFA00);
213
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100214 const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
215 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000216}
217
David Brazdil4833f5a2015-12-16 10:37:39 +0000218TEST_F(LinearizeTest, CFG7) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000219 // Structure of this graph (+ are back edges)
220 // Block0
221 // |
222 // Block1
223 // |
224 // Block2 ++++++++
225 // | +
226 // Block3 +
227 // / \ +
228 // Block4 Block8 +
229 // / \ | +
230 // Block5 Block9 - Block6 +
231 // |
232 // Block7
233 //
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800234 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000235 Instruction::CONST_4 | 0 | 0,
236 Instruction::GOTO | 0x0100,
237 Instruction::IF_EQ, 0x0005,
238 Instruction::IF_EQ, 0x0003,
239 Instruction::RETURN_VOID,
240 Instruction::GOTO | 0xFA00);
241
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100242 const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
243 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000244}
245
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100246} // namespace art