blob: 13e14c53b57e275811541825b74b2de16e72fe16 [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
Mark Mendellfb8d2792015-03-31 22:16:59 -040019#include "arch/x86/instruction_set_features_x86.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_allocator.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010021#include "base/stringprintf.h"
22#include "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010023#include "code_generator.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010024#include "code_generator_x86.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010025#include "dex_file.h"
26#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000027#include "driver/compiler_options.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010028#include "graph_visualizer.h"
29#include "nodes.h"
30#include "optimizing_unit_test.h"
31#include "pretty_printer.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010032#include "ssa_liveness_analysis.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010033
Alex Light68289a52015-12-15 17:30:30 -080034namespace art {
David Brazdild9510df2015-11-04 23:30:22 +000035
David Brazdil4833f5a2015-12-16 10:37:39 +000036class LinearizeTest : public CommonCompilerTest {};
37
Vladimir Markofa6b93c2015-09-15 10:15:55 +010038template <size_t number_of_blocks>
39static void TestCode(const uint16_t* data, const uint32_t (&expected_order)[number_of_blocks]) {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010040 ArenaPool pool;
41 ArenaAllocator allocator(&pool);
David Brazdilbadd8262016-02-02 16:28:56 +000042 HGraph* graph = CreateCFG(&allocator, data);
Mark Mendellfb8d2792015-03-31 22:16:59 -040043 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
44 X86InstructionSetFeatures::FromCppDefines());
45 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010046 SsaLivenessAnalysis liveness(graph, &codegen);
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
69 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
70 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
94 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
95 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 ++
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100120 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
121 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 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100150 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
151 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 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100180 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
181 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
206 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
207 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 //
234 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
235 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