blob: 9fa5b74c6255a7cb015b8c16e34a8dda3cafb940 [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"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/dex_file.h"
25#include "dex/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
Vladimir Markoca6fff82017-10-03 14:49:14 +010035class LinearizeTest : public OptimizingUnitTest {
36 protected:
37 template <size_t number_of_blocks>
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080038 void TestCode(const std::vector<uint16_t>& data,
39 const uint32_t (&expected_order)[number_of_blocks]);
Vladimir Markoca6fff82017-10-03 14:49:14 +010040};
David Brazdil4833f5a2015-12-16 10:37:39 +000041
Vladimir Markofa6b93c2015-09-15 10:15:55 +010042template <size_t number_of_blocks>
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080043void LinearizeTest::TestCode(const std::vector<uint16_t>& data,
Vladimir Markoca6fff82017-10-03 14:49:14 +010044 const uint32_t (&expected_order)[number_of_blocks]) {
45 HGraph* graph = CreateCFG(data);
Mark Mendellfb8d2792015-03-31 22:16:59 -040046 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
47 X86InstructionSetFeatures::FromCppDefines());
48 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Vladimir Markoe764d2e2017-10-05 14:35:55 +010049 SsaLivenessAnalysis liveness(graph, &codegen, GetScopedAllocator());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010050 liveness.Analyze();
51
Vladimir Markofa6b93c2015-09-15 10:15:55 +010052 ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010053 for (size_t i = 0; i < number_of_blocks; ++i) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +010054 ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010055 }
56}
57
David Brazdil4833f5a2015-12-16 10:37:39 +000058TEST_F(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
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080072 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010073 Instruction::CONST_4 | 0 | 0,
74 Instruction::IF_EQ, 5,
75 Instruction::IF_EQ, 0xFFFE,
76 Instruction::GOTO | 0xFE00,
77 Instruction::RETURN_VOID);
78
Vladimir Markofa6b93c2015-09-15 10:15:55 +010079 const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
80 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010081}
82
David Brazdil4833f5a2015-12-16 10:37:39 +000083TEST_F(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
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080097 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010098 Instruction::CONST_4 | 0 | 0,
99 Instruction::IF_EQ, 3,
100 Instruction::RETURN_VOID,
101 Instruction::IF_EQ, 0xFFFD,
102 Instruction::GOTO | 0xFE00);
103
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100104 const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
105 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100106}
107
David Brazdil4833f5a2015-12-16 10:37:39 +0000108TEST_F(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 ++
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800123 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100124 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
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100131 const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
132 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100133}
134
David Brazdil4833f5a2015-12-16 10:37:39 +0000135TEST_F(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 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800153 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100154 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
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100161 const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
162 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100163}
164
David Brazdil4833f5a2015-12-16 10:37:39 +0000165TEST_F(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 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800183 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100184 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
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100191 const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
192 TestCode(data, blocks);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100193}
194
David Brazdil4833f5a2015-12-16 10:37:39 +0000195TEST_F(LinearizeTest, CFG6) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000196 // Block0
197 // |
198 // Block1
199 // |
200 // Block2 ++++++++++++++
201 // | +
202 // Block3 +
203 // / \ +
204 // Block8 Block4 +
205 // | / \ +
206 // Block5 <- Block9 Block6 +
207 // |
208 // Block7
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800209 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000210 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
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100217 const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
218 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000219}
220
David Brazdil4833f5a2015-12-16 10:37:39 +0000221TEST_F(LinearizeTest, CFG7) {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000222 // 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 //
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800237 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000238 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
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100245 const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
246 TestCode(data, blocks);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000247}
248
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100249} // namespace art