blob: a059766e00430a0514f7c7a7788295b750180ea9 [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"
Alex Light68289a52015-12-15 17:30:30 -080032#include "ssa_builder.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010033#include "ssa_liveness_analysis.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010034
Alex Light68289a52015-12-15 17:30:30 -080035#include "gtest/gtest.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010036
Alex Light68289a52015-12-15 17:30:30 -080037namespace art {
David Brazdild9510df2015-11-04 23:30:22 +000038
Vladimir Markofa6b93c2015-09-15 10:15:55 +010039template <size_t number_of_blocks>
40static void TestCode(const uint16_t* data, const uint32_t (&expected_order)[number_of_blocks]) {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010041 ArenaPool pool;
42 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010043 HGraph* graph = CreateGraph(&allocator);
David Brazdil5e8b1372015-01-23 14:39:08 +000044 HGraphBuilder builder(graph);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010045 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +000046 bool graph_built = builder.BuildGraph(*item);
47 ASSERT_TRUE(graph_built);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010048
Alex Light68289a52015-12-15 17:30:30 -080049 graph->TryBuildingSsa();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010050
Mark Mendellfb8d2792015-03-31 22:16:59 -040051 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
52 X86InstructionSetFeatures::FromCppDefines());
53 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010054 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010055 liveness.Analyze();
56
Vladimir Markofa6b93c2015-09-15 10:15:55 +010057 ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010058 for (size_t i = 0; i < number_of_blocks; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010059 ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010060 }
61}
62
Alex Light68289a52015-12-15 17:30:30 -080063TEST(LinearizeTest, CFG1) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010064 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010065 // Block0
66 // |
67 // Block1
68 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010069 // Block2 ++++++
70 // / \ +
71 // Block5 Block7 +
72 // | | +
73 // Block6 Block3 +
74 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010075 // Block4 Block8
76
77 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
78 Instruction::CONST_4 | 0 | 0,
79 Instruction::IF_EQ, 5,
80 Instruction::IF_EQ, 0xFFFE,
81 Instruction::GOTO | 0xFE00,
82 Instruction::RETURN_VOID);
83
Vladimir Markofa6b93c2015-09-15 10:15:55 +010084 const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
85 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010086}
87
Alex Light68289a52015-12-15 17:30:30 -080088TEST(LinearizeTest, CFG2) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010089 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010090 // Block0
91 // |
92 // Block1
93 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010094 // Block2 ++++++
95 // / \ +
96 // Block3 Block7 +
97 // | | +
98 // Block6 Block4 +
99 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100100 // Block5 Block8
101
102 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
103 Instruction::CONST_4 | 0 | 0,
104 Instruction::IF_EQ, 3,
105 Instruction::RETURN_VOID,
106 Instruction::IF_EQ, 0xFFFD,
107 Instruction::GOTO | 0xFE00);
108
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100109 const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
110 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100111}
112
Alex Light68289a52015-12-15 17:30:30 -0800113TEST(LinearizeTest, CFG3) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100114 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100115 // Block0
116 // |
117 // Block1
118 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100119 // Block2 ++++++
120 // / \ +
121 // Block3 Block8 +
122 // | | +
123 // Block7 Block5 +
124 // / + \ +
125 // Block6 + Block9
126 // | +
127 // Block4 ++
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100128 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
129 Instruction::CONST_4 | 0 | 0,
130 Instruction::IF_EQ, 4,
131 Instruction::RETURN_VOID,
132 Instruction::GOTO | 0x0100,
133 Instruction::IF_EQ, 0xFFFC,
134 Instruction::GOTO | 0xFD00);
135
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100136 const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
137 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100138}
139
Alex Light68289a52015-12-15 17:30:30 -0800140TEST(LinearizeTest, CFG4) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100141 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100142 // Block0
143 // |
144 // Block1
145 // |
146 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100147 // / + \
148 // Block6 + Block8
149 // | + |
150 // Block7 + Block3 +++++++
151 // + / \ +
152 // Block9 Block10 +
153 // | +
154 // Block4 +
155 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100156 // Block5 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100157 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100158 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
159 Instruction::CONST_4 | 0 | 0,
160 Instruction::IF_EQ, 7,
161 Instruction::IF_EQ, 0xFFFE,
162 Instruction::IF_EQ, 0xFFFE,
163 Instruction::GOTO | 0xFE00,
164 Instruction::RETURN_VOID);
165
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100166 const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
167 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100168}
169
Alex Light68289a52015-12-15 17:30:30 -0800170TEST(LinearizeTest, CFG5) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100171 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100172 // Block0
173 // |
174 // Block1
175 // |
176 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100177 // / + \
178 // Block3 + Block8
179 // | + |
180 // Block7 + Block4 +++++++
181 // + / \ +
182 // Block9 Block10 +
183 // | +
184 // Block5 +
185 // +/ \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100186 // Block6 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100187 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100188 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
189 Instruction::CONST_4 | 0 | 0,
190 Instruction::IF_EQ, 3,
191 Instruction::RETURN_VOID,
192 Instruction::IF_EQ, 0xFFFD,
193 Instruction::IF_EQ, 0xFFFE,
194 Instruction::GOTO | 0xFE00);
195
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100196 const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
197 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100198}
199
Alex Light68289a52015-12-15 17:30:30 -0800200TEST(LinearizeTest, CFG6) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000201 // Block0
202 // |
203 // Block1
204 // |
205 // Block2 ++++++++++++++
206 // | +
207 // Block3 +
208 // / \ +
209 // Block8 Block4 +
210 // | / \ +
211 // Block5 <- Block9 Block6 +
212 // |
213 // Block7
214 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
215 Instruction::CONST_4 | 0 | 0,
216 Instruction::GOTO | 0x0100,
217 Instruction::IF_EQ, 0x0004,
218 Instruction::IF_EQ, 0x0003,
219 Instruction::RETURN_VOID,
220 Instruction::GOTO | 0xFA00);
221
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100222 const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
223 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000224}
225
Alex Light68289a52015-12-15 17:30:30 -0800226TEST(LinearizeTest, CFG7) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000227 // Structure of this graph (+ are back edges)
228 // Block0
229 // |
230 // Block1
231 // |
232 // Block2 ++++++++
233 // | +
234 // Block3 +
235 // / \ +
236 // Block4 Block8 +
237 // / \ | +
238 // Block5 Block9 - Block6 +
239 // |
240 // Block7
241 //
242 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
243 Instruction::CONST_4 | 0 | 0,
244 Instruction::GOTO | 0x0100,
245 Instruction::IF_EQ, 0x0005,
246 Instruction::IF_EQ, 0x0003,
247 Instruction::RETURN_VOID,
248 Instruction::GOTO | 0xFA00);
249
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100250 const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
251 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000252}
253
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100254} // namespace art