blob: 3831aa6c91d0d5029afb31970a83891bbab23067 [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 "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010022#include "code_generator.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010023#include "code_generator_x86.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010024#include "dex_file.h"
25#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000026#include "driver/compiler_options.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010027#include "graph_visualizer.h"
28#include "nodes.h"
29#include "optimizing_unit_test.h"
30#include "pretty_printer.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010031#include "ssa_liveness_analysis.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010032
Alex Light68289a52015-12-15 17:30:30 -080033namespace art {
David Brazdild9510df2015-11-04 23:30:22 +000034
David Brazdil4833f5a2015-12-16 10:37:39 +000035class LinearizeTest : public CommonCompilerTest {};
36
Vladimir Markofa6b93c2015-09-15 10:15:55 +010037template <size_t number_of_blocks>
38static void TestCode(const uint16_t* data, const uint32_t (&expected_order)[number_of_blocks]) {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010039 ArenaPool pool;
40 ArenaAllocator allocator(&pool);
David Brazdilbadd8262016-02-02 16:28:56 +000041 HGraph* graph = CreateCFG(&allocator, data);
Mark Mendellfb8d2792015-03-31 22:16:59 -040042 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
43 X86InstructionSetFeatures::FromCppDefines());
44 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010045 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010046 liveness.Analyze();
47
Vladimir Markofa6b93c2015-09-15 10:15:55 +010048 ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010049 for (size_t i = 0; i < number_of_blocks; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010050 ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010051 }
52}
53
David Brazdil4833f5a2015-12-16 10:37:39 +000054TEST_F(LinearizeTest, CFG1) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010055 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010056 // Block0
57 // |
58 // Block1
59 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010060 // Block2 ++++++
61 // / \ +
62 // Block5 Block7 +
63 // | | +
64 // Block6 Block3 +
65 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010066 // Block4 Block8
67
68 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
69 Instruction::CONST_4 | 0 | 0,
70 Instruction::IF_EQ, 5,
71 Instruction::IF_EQ, 0xFFFE,
72 Instruction::GOTO | 0xFE00,
73 Instruction::RETURN_VOID);
74
Vladimir Markofa6b93c2015-09-15 10:15:55 +010075 const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
76 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010077}
78
David Brazdil4833f5a2015-12-16 10:37:39 +000079TEST_F(LinearizeTest, CFG2) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010080 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010081 // Block0
82 // |
83 // Block1
84 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010085 // Block2 ++++++
86 // / \ +
87 // Block3 Block7 +
88 // | | +
89 // Block6 Block4 +
90 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010091 // Block5 Block8
92
93 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
94 Instruction::CONST_4 | 0 | 0,
95 Instruction::IF_EQ, 3,
96 Instruction::RETURN_VOID,
97 Instruction::IF_EQ, 0xFFFD,
98 Instruction::GOTO | 0xFE00);
99
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100100 const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
101 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100102}
103
David Brazdil4833f5a2015-12-16 10:37:39 +0000104TEST_F(LinearizeTest, CFG3) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100105 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100106 // Block0
107 // |
108 // Block1
109 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100110 // Block2 ++++++
111 // / \ +
112 // Block3 Block8 +
113 // | | +
114 // Block7 Block5 +
115 // / + \ +
116 // Block6 + Block9
117 // | +
118 // Block4 ++
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100119 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
120 Instruction::CONST_4 | 0 | 0,
121 Instruction::IF_EQ, 4,
122 Instruction::RETURN_VOID,
123 Instruction::GOTO | 0x0100,
124 Instruction::IF_EQ, 0xFFFC,
125 Instruction::GOTO | 0xFD00);
126
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100127 const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
128 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100129}
130
David Brazdil4833f5a2015-12-16 10:37:39 +0000131TEST_F(LinearizeTest, CFG4) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100132 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100133 // Block0
134 // |
135 // Block1
136 // |
137 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100138 // / + \
139 // Block6 + Block8
140 // | + |
141 // Block7 + Block3 +++++++
142 // + / \ +
143 // Block9 Block10 +
144 // | +
145 // Block4 +
146 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100147 // Block5 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100148 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100149 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
150 Instruction::CONST_4 | 0 | 0,
151 Instruction::IF_EQ, 7,
152 Instruction::IF_EQ, 0xFFFE,
153 Instruction::IF_EQ, 0xFFFE,
154 Instruction::GOTO | 0xFE00,
155 Instruction::RETURN_VOID);
156
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
158 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100159}
160
David Brazdil4833f5a2015-12-16 10:37:39 +0000161TEST_F(LinearizeTest, CFG5) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100162 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100163 // Block0
164 // |
165 // Block1
166 // |
167 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100168 // / + \
169 // Block3 + Block8
170 // | + |
171 // Block7 + Block4 +++++++
172 // + / \ +
173 // Block9 Block10 +
174 // | +
175 // Block5 +
176 // +/ \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100177 // Block6 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100178 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100179 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
180 Instruction::CONST_4 | 0 | 0,
181 Instruction::IF_EQ, 3,
182 Instruction::RETURN_VOID,
183 Instruction::IF_EQ, 0xFFFD,
184 Instruction::IF_EQ, 0xFFFE,
185 Instruction::GOTO | 0xFE00);
186
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100187 const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
188 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100189}
190
David Brazdil4833f5a2015-12-16 10:37:39 +0000191TEST_F(LinearizeTest, CFG6) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000192 // Block0
193 // |
194 // Block1
195 // |
196 // Block2 ++++++++++++++
197 // | +
198 // Block3 +
199 // / \ +
200 // Block8 Block4 +
201 // | / \ +
202 // Block5 <- Block9 Block6 +
203 // |
204 // Block7
205 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
206 Instruction::CONST_4 | 0 | 0,
207 Instruction::GOTO | 0x0100,
208 Instruction::IF_EQ, 0x0004,
209 Instruction::IF_EQ, 0x0003,
210 Instruction::RETURN_VOID,
211 Instruction::GOTO | 0xFA00);
212
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100213 const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
214 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000215}
216
David Brazdil4833f5a2015-12-16 10:37:39 +0000217TEST_F(LinearizeTest, CFG7) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000218 // Structure of this graph (+ are back edges)
219 // Block0
220 // |
221 // Block1
222 // |
223 // Block2 ++++++++
224 // | +
225 // Block3 +
226 // / \ +
227 // Block4 Block8 +
228 // / \ | +
229 // Block5 Block9 - Block6 +
230 // |
231 // Block7
232 //
233 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
234 Instruction::CONST_4 | 0 | 0,
235 Instruction::GOTO | 0x0100,
236 Instruction::IF_EQ, 0x0005,
237 Instruction::IF_EQ, 0x0003,
238 Instruction::RETURN_VOID,
239 Instruction::GOTO | 0xFA00);
240
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100241 const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
242 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000243}
244
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100245} // namespace art