blob: 28c5555d574db679ef505bdc70de56eabfd76110 [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"
32#include "ssa_builder.h"
33#include "ssa_liveness_analysis.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010034
35#include "gtest/gtest.h"
36
37namespace art {
38
39static void TestCode(const uint16_t* data, const int* expected_order, size_t number_of_blocks) {
40 ArenaPool pool;
41 ArenaAllocator allocator(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +000042 HGraph* graph = new (&allocator) HGraph(&allocator);
43 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
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000048 graph->TryBuildingSsa();
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 Geoffray8a16d972014-09-11 10:30:02 +010053 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010054 liveness.Analyze();
55
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000056 ASSERT_EQ(liveness.GetLinearOrder().Size(), number_of_blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010057 for (size_t i = 0; i < number_of_blocks; ++i) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000058 ASSERT_EQ(liveness.GetLinearOrder().Get(i)->GetBlockId(), expected_order[i]);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010059 }
60}
61
62TEST(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
83 const int blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
84 TestCode(data, blocks, 9);
85}
86
87TEST(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
108 const int blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
109 TestCode(data, blocks, 9);
110}
111
112TEST(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
135 const int blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
136 TestCode(data, blocks, 10);
137}
138
139TEST(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
165 const int blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
166 TestCode(data, blocks, 12);
167}
168
169TEST(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
195 const int blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
196 TestCode(data, blocks, 12);
197}
198
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000199TEST(LinearizeTest, CFG6) {
200 // 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
221 const int blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
222 TestCode(data, blocks, arraysize(blocks));
223}
224
225TEST(LinearizeTest, CFG7) {
226 // 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
249 const int blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
250 TestCode(data, blocks, arraysize(blocks));
251}
252
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100253} // namespace art