blob: 2ab9b571fffadd843d6fd6c5f5e1625ad714f452 [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
19#include "base/stringprintf.h"
20#include "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010021#include "code_generator.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010022#include "code_generator_x86.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010023#include "dex_file.h"
24#include "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"
30#include "ssa_builder.h"
31#include "ssa_liveness_analysis.h"
32#include "utils/arena_allocator.h"
33
34#include "gtest/gtest.h"
35
36namespace art {
37
38static void TestCode(const uint16_t* data, const int* expected_order, size_t number_of_blocks) {
39 ArenaPool pool;
40 ArenaAllocator allocator(&pool);
41 HGraphBuilder builder(&allocator);
42 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
43 HGraph* graph = builder.BuildGraph(*item);
44 ASSERT_NE(graph, nullptr);
45
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000046 graph->TryBuildingSsa();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010047
Calin Juravlecd6dffe2015-01-08 17:35:35 +000048 x86::CodeGeneratorX86 codegen(graph, CompilerOptions());
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010049 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010050 liveness.Analyze();
51
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000052 ASSERT_EQ(liveness.GetLinearOrder().Size(), number_of_blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010053 for (size_t i = 0; i < number_of_blocks; ++i) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000054 ASSERT_EQ(liveness.GetLinearOrder().Get(i)->GetBlockId(), expected_order[i]);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010055 }
56}
57
58TEST(LinearizeTest, CFG1) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010059 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010060 // Block0
61 // |
62 // Block1
63 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010064 // Block2 ++++++
65 // / \ +
66 // Block5 Block7 +
67 // | | +
68 // Block6 Block3 +
69 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010070 // Block4 Block8
71
72 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
73 Instruction::CONST_4 | 0 | 0,
74 Instruction::IF_EQ, 5,
75 Instruction::IF_EQ, 0xFFFE,
76 Instruction::GOTO | 0xFE00,
77 Instruction::RETURN_VOID);
78
79 const int blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
80 TestCode(data, blocks, 9);
81}
82
83TEST(LinearizeTest, CFG2) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010084 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010085 // Block0
86 // |
87 // Block1
88 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010089 // Block2 ++++++
90 // / \ +
91 // Block3 Block7 +
92 // | | +
93 // Block6 Block4 +
94 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010095 // Block5 Block8
96
97 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
98 Instruction::CONST_4 | 0 | 0,
99 Instruction::IF_EQ, 3,
100 Instruction::RETURN_VOID,
101 Instruction::IF_EQ, 0xFFFD,
102 Instruction::GOTO | 0xFE00);
103
104 const int blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
105 TestCode(data, blocks, 9);
106}
107
108TEST(LinearizeTest, CFG3) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100109 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100110 // Block0
111 // |
112 // Block1
113 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100114 // Block2 ++++++
115 // / \ +
116 // Block3 Block8 +
117 // | | +
118 // Block7 Block5 +
119 // / + \ +
120 // Block6 + Block9
121 // | +
122 // Block4 ++
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100123 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
124 Instruction::CONST_4 | 0 | 0,
125 Instruction::IF_EQ, 4,
126 Instruction::RETURN_VOID,
127 Instruction::GOTO | 0x0100,
128 Instruction::IF_EQ, 0xFFFC,
129 Instruction::GOTO | 0xFD00);
130
131 const int blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
132 TestCode(data, blocks, 10);
133}
134
135TEST(LinearizeTest, CFG4) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100136 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100137 // Block0
138 // |
139 // Block1
140 // |
141 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100142 // / + \
143 // Block6 + Block8
144 // | + |
145 // Block7 + Block3 +++++++
146 // + / \ +
147 // Block9 Block10 +
148 // | +
149 // Block4 +
150 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100151 // Block5 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100152 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100153 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
154 Instruction::CONST_4 | 0 | 0,
155 Instruction::IF_EQ, 7,
156 Instruction::IF_EQ, 0xFFFE,
157 Instruction::IF_EQ, 0xFFFE,
158 Instruction::GOTO | 0xFE00,
159 Instruction::RETURN_VOID);
160
161 const int blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
162 TestCode(data, blocks, 12);
163}
164
165TEST(LinearizeTest, CFG5) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100166 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100167 // Block0
168 // |
169 // Block1
170 // |
171 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100172 // / + \
173 // Block3 + Block8
174 // | + |
175 // Block7 + Block4 +++++++
176 // + / \ +
177 // Block9 Block10 +
178 // | +
179 // Block5 +
180 // +/ \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100181 // Block6 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100182 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100183 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
184 Instruction::CONST_4 | 0 | 0,
185 Instruction::IF_EQ, 3,
186 Instruction::RETURN_VOID,
187 Instruction::IF_EQ, 0xFFFD,
188 Instruction::IF_EQ, 0xFFFE,
189 Instruction::GOTO | 0xFE00);
190
191 const int blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
192 TestCode(data, blocks, 12);
193}
194
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000195TEST(LinearizeTest, CFG6) {
196 // Block0
197 // |
198 // Block1
199 // |
200 // Block2 ++++++++++++++
201 // | +
202 // Block3 +
203 // / \ +
204 // Block8 Block4 +
205 // | / \ +
206 // Block5 <- Block9 Block6 +
207 // |
208 // Block7
209 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
210 Instruction::CONST_4 | 0 | 0,
211 Instruction::GOTO | 0x0100,
212 Instruction::IF_EQ, 0x0004,
213 Instruction::IF_EQ, 0x0003,
214 Instruction::RETURN_VOID,
215 Instruction::GOTO | 0xFA00);
216
217 const int blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
218 TestCode(data, blocks, arraysize(blocks));
219}
220
221TEST(LinearizeTest, CFG7) {
222 // Structure of this graph (+ are back edges)
223 // Block0
224 // |
225 // Block1
226 // |
227 // Block2 ++++++++
228 // | +
229 // Block3 +
230 // / \ +
231 // Block4 Block8 +
232 // / \ | +
233 // Block5 Block9 - Block6 +
234 // |
235 // Block7
236 //
237 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
238 Instruction::CONST_4 | 0 | 0,
239 Instruction::GOTO | 0x0100,
240 Instruction::IF_EQ, 0x0005,
241 Instruction::IF_EQ, 0x0003,
242 Instruction::RETURN_VOID,
243 Instruction::GOTO | 0xFA00);
244
245 const int blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
246 TestCode(data, blocks, arraysize(blocks));
247}
248
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100249} // namespace art