blob: ed275b1544a8aa0bcc86ec33f761ee588c4606bc [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
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010034namespace art {
35
David Brazdild9510df2015-11-04 23:30:22 +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);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010042 HGraph* graph = CreateGraph(&allocator);
David Brazdil5e8b1372015-01-23 14:39:08 +000043 HGraphBuilder builder(graph);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010044 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +000045 bool graph_built = builder.BuildGraph(*item);
46 ASSERT_TRUE(graph_built);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010047
David Brazdild9510df2015-11-04 23:30:22 +000048 TransformToSsa(graph);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010049
Mark Mendellfb8d2792015-03-31 22:16:59 -040050 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
51 X86InstructionSetFeatures::FromCppDefines());
52 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010053 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010054 liveness.Analyze();
55
Vladimir Markofa6b93c2015-09-15 10:15:55 +010056 ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010057 for (size_t i = 0; i < number_of_blocks; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010058 ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010059 }
60}
61
David Brazdild9510df2015-11-04 23:30:22 +000062TEST_F(LinearizeTest, CFG1) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010063 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010064 // Block0
65 // |
66 // Block1
67 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010068 // Block2 ++++++
69 // / \ +
70 // Block5 Block7 +
71 // | | +
72 // Block6 Block3 +
73 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010074 // Block4 Block8
75
76 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
77 Instruction::CONST_4 | 0 | 0,
78 Instruction::IF_EQ, 5,
79 Instruction::IF_EQ, 0xFFFE,
80 Instruction::GOTO | 0xFE00,
81 Instruction::RETURN_VOID);
82
Vladimir Markofa6b93c2015-09-15 10:15:55 +010083 const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
84 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010085}
86
David Brazdild9510df2015-11-04 23:30:22 +000087TEST_F(LinearizeTest, CFG2) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010088 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010089 // Block0
90 // |
91 // Block1
92 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010093 // Block2 ++++++
94 // / \ +
95 // Block3 Block7 +
96 // | | +
97 // Block6 Block4 +
98 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010099 // Block5 Block8
100
101 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
102 Instruction::CONST_4 | 0 | 0,
103 Instruction::IF_EQ, 3,
104 Instruction::RETURN_VOID,
105 Instruction::IF_EQ, 0xFFFD,
106 Instruction::GOTO | 0xFE00);
107
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100108 const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
109 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100110}
111
David Brazdild9510df2015-11-04 23:30:22 +0000112TEST_F(LinearizeTest, CFG3) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100113 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100114 // Block0
115 // |
116 // Block1
117 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100118 // Block2 ++++++
119 // / \ +
120 // Block3 Block8 +
121 // | | +
122 // Block7 Block5 +
123 // / + \ +
124 // Block6 + Block9
125 // | +
126 // Block4 ++
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100127 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
128 Instruction::CONST_4 | 0 | 0,
129 Instruction::IF_EQ, 4,
130 Instruction::RETURN_VOID,
131 Instruction::GOTO | 0x0100,
132 Instruction::IF_EQ, 0xFFFC,
133 Instruction::GOTO | 0xFD00);
134
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100135 const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
136 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100137}
138
David Brazdild9510df2015-11-04 23:30:22 +0000139TEST_F(LinearizeTest, CFG4) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100140 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100141 // Block0
142 // |
143 // Block1
144 // |
145 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100146 // / + \
147 // Block6 + Block8
148 // | + |
149 // Block7 + Block3 +++++++
150 // + / \ +
151 // Block9 Block10 +
152 // | +
153 // Block4 +
154 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100155 // Block5 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100156 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100157 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
158 Instruction::CONST_4 | 0 | 0,
159 Instruction::IF_EQ, 7,
160 Instruction::IF_EQ, 0xFFFE,
161 Instruction::IF_EQ, 0xFFFE,
162 Instruction::GOTO | 0xFE00,
163 Instruction::RETURN_VOID);
164
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100165 const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
166 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100167}
168
David Brazdild9510df2015-11-04 23:30:22 +0000169TEST_F(LinearizeTest, CFG5) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100170 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100171 // Block0
172 // |
173 // Block1
174 // |
175 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100176 // / + \
177 // Block3 + Block8
178 // | + |
179 // Block7 + Block4 +++++++
180 // + / \ +
181 // Block9 Block10 +
182 // | +
183 // Block5 +
184 // +/ \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100185 // Block6 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100186 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100187 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
188 Instruction::CONST_4 | 0 | 0,
189 Instruction::IF_EQ, 3,
190 Instruction::RETURN_VOID,
191 Instruction::IF_EQ, 0xFFFD,
192 Instruction::IF_EQ, 0xFFFE,
193 Instruction::GOTO | 0xFE00);
194
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100195 const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
196 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100197}
198
David Brazdild9510df2015-11-04 23:30:22 +0000199TEST_F(LinearizeTest, CFG6) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000200 // Block0
201 // |
202 // Block1
203 // |
204 // Block2 ++++++++++++++
205 // | +
206 // Block3 +
207 // / \ +
208 // Block8 Block4 +
209 // | / \ +
210 // Block5 <- Block9 Block6 +
211 // |
212 // Block7
213 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
214 Instruction::CONST_4 | 0 | 0,
215 Instruction::GOTO | 0x0100,
216 Instruction::IF_EQ, 0x0004,
217 Instruction::IF_EQ, 0x0003,
218 Instruction::RETURN_VOID,
219 Instruction::GOTO | 0xFA00);
220
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100221 const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
222 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000223}
224
David Brazdild9510df2015-11-04 23:30:22 +0000225TEST_F(LinearizeTest, CFG7) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000226 // Structure of this graph (+ are back edges)
227 // Block0
228 // |
229 // Block1
230 // |
231 // Block2 ++++++++
232 // | +
233 // Block3 +
234 // / \ +
235 // Block4 Block8 +
236 // / \ | +
237 // Block5 Block9 - Block6 +
238 // |
239 // Block7
240 //
241 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
242 Instruction::CONST_4 | 0 | 0,
243 Instruction::GOTO | 0x0100,
244 Instruction::IF_EQ, 0x0005,
245 Instruction::IF_EQ, 0x0003,
246 Instruction::RETURN_VOID,
247 Instruction::GOTO | 0xFA00);
248
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100249 const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
250 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000251}
252
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100253} // namespace art