blob: d56ae11ca9d1aabc6c19164814894e27dd7bbdfd [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"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010020#include "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010021#include "code_generator.h"
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/dex_file.h"
23#include "dex/dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000024#include "driver/compiler_options.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010025#include "graph_visualizer.h"
26#include "nodes.h"
27#include "optimizing_unit_test.h"
28#include "pretty_printer.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010029#include "ssa_liveness_analysis.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010030
Vladimir Marko0a516052019-10-14 13:00:44 +000031namespace art {
David Brazdild9510df2015-11-04 23:30:22 +000032
Vladimir Markoca6fff82017-10-03 14:49:14 +010033class LinearizeTest : public OptimizingUnitTest {
34 protected:
35 template <size_t number_of_blocks>
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080036 void TestCode(const std::vector<uint16_t>& data,
37 const uint32_t (&expected_order)[number_of_blocks]);
Vladimir Markoca6fff82017-10-03 14:49:14 +010038};
David Brazdil4833f5a2015-12-16 10:37:39 +000039
Vladimir Markofa6b93c2015-09-15 10:15:55 +010040template <size_t number_of_blocks>
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080041void LinearizeTest::TestCode(const std::vector<uint16_t>& data,
Vladimir Markoca6fff82017-10-03 14:49:14 +010042 const uint32_t (&expected_order)[number_of_blocks]) {
43 HGraph* graph = CreateCFG(data);
Vladimir Markof91fc122020-05-13 09:21:00 +010044 std::unique_ptr<CompilerOptions> compiler_options =
45 CommonCompilerTest::CreateCompilerOptions(kRuntimeISA, "default");
46 std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options);
Vladimir Markoa0431112018-06-25 09:32:54 +010047 SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010048 liveness.Analyze();
49
Vladimir Markofa6b93c2015-09-15 10:15:55 +010050 ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010051 for (size_t i = 0; i < number_of_blocks; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010052 ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010053 }
54}
55
David Brazdil4833f5a2015-12-16 10:37:39 +000056TEST_F(LinearizeTest, CFG1) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010057 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010058 // Block0
59 // |
60 // Block1
61 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010062 // Block2 ++++++
63 // / \ +
64 // Block5 Block7 +
65 // | | +
66 // Block6 Block3 +
67 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010068 // Block4 Block8
69
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080070 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010071 Instruction::CONST_4 | 0 | 0,
72 Instruction::IF_EQ, 5,
73 Instruction::IF_EQ, 0xFFFE,
74 Instruction::GOTO | 0xFE00,
75 Instruction::RETURN_VOID);
76
Vladimir Markofa6b93c2015-09-15 10:15:55 +010077 const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
78 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010079}
80
David Brazdil4833f5a2015-12-16 10:37:39 +000081TEST_F(LinearizeTest, CFG2) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010082 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010083 // Block0
84 // |
85 // Block1
86 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010087 // Block2 ++++++
88 // / \ +
89 // Block3 Block7 +
90 // | | +
91 // Block6 Block4 +
92 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010093 // Block5 Block8
94
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080095 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010096 Instruction::CONST_4 | 0 | 0,
97 Instruction::IF_EQ, 3,
98 Instruction::RETURN_VOID,
99 Instruction::IF_EQ, 0xFFFD,
100 Instruction::GOTO | 0xFE00);
101
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100102 const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
103 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100104}
105
David Brazdil4833f5a2015-12-16 10:37:39 +0000106TEST_F(LinearizeTest, CFG3) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100107 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100108 // Block0
109 // |
110 // Block1
111 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100112 // Block2 ++++++
113 // / \ +
114 // Block3 Block8 +
115 // | | +
116 // Block7 Block5 +
117 // / + \ +
118 // Block6 + Block9
119 // | +
120 // Block4 ++
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800121 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100122 Instruction::CONST_4 | 0 | 0,
123 Instruction::IF_EQ, 4,
124 Instruction::RETURN_VOID,
125 Instruction::GOTO | 0x0100,
126 Instruction::IF_EQ, 0xFFFC,
127 Instruction::GOTO | 0xFD00);
128
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100129 const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
130 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100131}
132
David Brazdil4833f5a2015-12-16 10:37:39 +0000133TEST_F(LinearizeTest, CFG4) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100134 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100135 // Block0
136 // |
137 // Block1
138 // |
139 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100140 // / + \
141 // Block6 + Block8
142 // | + |
143 // Block7 + Block3 +++++++
144 // + / \ +
145 // Block9 Block10 +
146 // | +
147 // Block4 +
148 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100149 // Block5 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100150 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800151 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100152 Instruction::CONST_4 | 0 | 0,
153 Instruction::IF_EQ, 7,
154 Instruction::IF_EQ, 0xFFFE,
155 Instruction::IF_EQ, 0xFFFE,
156 Instruction::GOTO | 0xFE00,
157 Instruction::RETURN_VOID);
158
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100159 const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
160 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100161}
162
David Brazdil4833f5a2015-12-16 10:37:39 +0000163TEST_F(LinearizeTest, CFG5) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100164 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100165 // Block0
166 // |
167 // Block1
168 // |
169 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100170 // / + \
171 // Block3 + Block8
172 // | + |
173 // Block7 + Block4 +++++++
174 // + / \ +
175 // Block9 Block10 +
176 // | +
177 // Block5 +
178 // +/ \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100179 // Block6 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100180 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800181 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100182 Instruction::CONST_4 | 0 | 0,
183 Instruction::IF_EQ, 3,
184 Instruction::RETURN_VOID,
185 Instruction::IF_EQ, 0xFFFD,
186 Instruction::IF_EQ, 0xFFFE,
187 Instruction::GOTO | 0xFE00);
188
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100189 const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
190 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100191}
192
David Brazdil4833f5a2015-12-16 10:37:39 +0000193TEST_F(LinearizeTest, CFG6) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000194 // Block0
195 // |
196 // Block1
197 // |
198 // Block2 ++++++++++++++
199 // | +
200 // Block3 +
201 // / \ +
202 // Block8 Block4 +
203 // | / \ +
204 // Block5 <- Block9 Block6 +
205 // |
206 // Block7
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800207 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000208 Instruction::CONST_4 | 0 | 0,
209 Instruction::GOTO | 0x0100,
210 Instruction::IF_EQ, 0x0004,
211 Instruction::IF_EQ, 0x0003,
212 Instruction::RETURN_VOID,
213 Instruction::GOTO | 0xFA00);
214
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100215 const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
216 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000217}
218
David Brazdil4833f5a2015-12-16 10:37:39 +0000219TEST_F(LinearizeTest, CFG7) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000220 // Structure of this graph (+ are back edges)
221 // Block0
222 // |
223 // Block1
224 // |
225 // Block2 ++++++++
226 // | +
227 // Block3 +
228 // / \ +
229 // Block4 Block8 +
230 // / \ | +
231 // Block5 Block9 - Block6 +
232 // |
233 // Block7
234 //
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800235 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000236 Instruction::CONST_4 | 0 | 0,
237 Instruction::GOTO | 0x0100,
238 Instruction::IF_EQ, 0x0005,
239 Instruction::IF_EQ, 0x0003,
240 Instruction::RETURN_VOID,
241 Instruction::GOTO | 0xFA00);
242
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100243 const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
244 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000245}
246
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100247} // namespace art