1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include <fstream>
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "base/arena_allocator.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
21*795d594fSAndroid Build Coastguard Worker #include "builder.h"
22*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
23*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
24*795d594fSAndroid Build Coastguard Worker #include "dex/dex_instruction.h"
25*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
26*795d594fSAndroid Build Coastguard Worker #include "graph_visualizer.h"
27*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
28*795d594fSAndroid Build Coastguard Worker #include "optimizing_unit_test.h"
29*795d594fSAndroid Build Coastguard Worker #include "pretty_printer.h"
30*795d594fSAndroid Build Coastguard Worker #include "ssa_liveness_analysis.h"
31*795d594fSAndroid Build Coastguard Worker
32*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker class LinearizeTest : public CommonCompilerTest, public OptimizingUnitTestHelper {
35*795d594fSAndroid Build Coastguard Worker protected:
36*795d594fSAndroid Build Coastguard Worker template <size_t number_of_blocks>
37*795d594fSAndroid Build Coastguard Worker void TestCode(const std::vector<uint16_t>& data,
38*795d594fSAndroid Build Coastguard Worker const uint32_t (&expected_order)[number_of_blocks]);
39*795d594fSAndroid Build Coastguard Worker };
40*795d594fSAndroid Build Coastguard Worker
41*795d594fSAndroid Build Coastguard Worker template <size_t number_of_blocks>
TestCode(const std::vector<uint16_t> & data,const uint32_t (& expected_order)[number_of_blocks])42*795d594fSAndroid Build Coastguard Worker void LinearizeTest::TestCode(const std::vector<uint16_t>& data,
43*795d594fSAndroid Build Coastguard Worker const uint32_t (&expected_order)[number_of_blocks]) {
44*795d594fSAndroid Build Coastguard Worker HGraph* graph = CreateCFG(data);
45*795d594fSAndroid Build Coastguard Worker std::unique_ptr<CompilerOptions> compiler_options =
46*795d594fSAndroid Build Coastguard Worker CommonCompilerTest::CreateCompilerOptions(kRuntimeISA, "default");
47*795d594fSAndroid Build Coastguard Worker std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options);
48*795d594fSAndroid Build Coastguard Worker SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
49*795d594fSAndroid Build Coastguard Worker liveness.Analyze();
50*795d594fSAndroid Build Coastguard Worker
51*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks);
52*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < number_of_blocks; ++i) {
53*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]);
54*795d594fSAndroid Build Coastguard Worker }
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker
TEST_F(LinearizeTest,CFG1)57*795d594fSAndroid Build Coastguard Worker TEST_F(LinearizeTest, CFG1) {
58*795d594fSAndroid Build Coastguard Worker // Structure of this graph (+ are back edges)
59*795d594fSAndroid Build Coastguard Worker // Block0
60*795d594fSAndroid Build Coastguard Worker // |
61*795d594fSAndroid Build Coastguard Worker // Block1
62*795d594fSAndroid Build Coastguard Worker // |
63*795d594fSAndroid Build Coastguard Worker // Block2 ++++++
64*795d594fSAndroid Build Coastguard Worker // / \ +
65*795d594fSAndroid Build Coastguard Worker // Block5 Block7 +
66*795d594fSAndroid Build Coastguard Worker // | | +
67*795d594fSAndroid Build Coastguard Worker // Block6 Block3 +
68*795d594fSAndroid Build Coastguard Worker // + / \ +
69*795d594fSAndroid Build Coastguard Worker // Block4 Block8
70*795d594fSAndroid Build Coastguard Worker
71*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
72*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 | 0,
73*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 5,
74*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0xFFFE,
75*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0xFE00,
76*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID);
77*795d594fSAndroid Build Coastguard Worker
78*795d594fSAndroid Build Coastguard Worker const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
79*795d594fSAndroid Build Coastguard Worker TestCode(data, blocks);
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker
TEST_F(LinearizeTest,CFG2)82*795d594fSAndroid Build Coastguard Worker TEST_F(LinearizeTest, CFG2) {
83*795d594fSAndroid Build Coastguard Worker // Structure of this graph (+ are back edges)
84*795d594fSAndroid Build Coastguard Worker // Block0
85*795d594fSAndroid Build Coastguard Worker // |
86*795d594fSAndroid Build Coastguard Worker // Block1
87*795d594fSAndroid Build Coastguard Worker // |
88*795d594fSAndroid Build Coastguard Worker // Block2 ++++++
89*795d594fSAndroid Build Coastguard Worker // / \ +
90*795d594fSAndroid Build Coastguard Worker // Block3 Block7 +
91*795d594fSAndroid Build Coastguard Worker // | | +
92*795d594fSAndroid Build Coastguard Worker // Block6 Block4 +
93*795d594fSAndroid Build Coastguard Worker // + / \ +
94*795d594fSAndroid Build Coastguard Worker // Block5 Block8
95*795d594fSAndroid Build Coastguard Worker
96*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
97*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 | 0,
98*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 3,
99*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID,
100*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0xFFFD,
101*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0xFE00);
102*795d594fSAndroid Build Coastguard Worker
103*795d594fSAndroid Build Coastguard Worker const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
104*795d594fSAndroid Build Coastguard Worker TestCode(data, blocks);
105*795d594fSAndroid Build Coastguard Worker }
106*795d594fSAndroid Build Coastguard Worker
TEST_F(LinearizeTest,CFG3)107*795d594fSAndroid Build Coastguard Worker TEST_F(LinearizeTest, CFG3) {
108*795d594fSAndroid Build Coastguard Worker // Structure of this graph (+ are back edges)
109*795d594fSAndroid Build Coastguard Worker // Block0
110*795d594fSAndroid Build Coastguard Worker // |
111*795d594fSAndroid Build Coastguard Worker // Block1
112*795d594fSAndroid Build Coastguard Worker // |
113*795d594fSAndroid Build Coastguard Worker // Block2 ++++++
114*795d594fSAndroid Build Coastguard Worker // / \ +
115*795d594fSAndroid Build Coastguard Worker // Block3 Block8 +
116*795d594fSAndroid Build Coastguard Worker // | | +
117*795d594fSAndroid Build Coastguard Worker // Block7 Block5 +
118*795d594fSAndroid Build Coastguard Worker // / + \ +
119*795d594fSAndroid Build Coastguard Worker // Block6 + Block9
120*795d594fSAndroid Build Coastguard Worker // | +
121*795d594fSAndroid Build Coastguard Worker // Block4 ++
122*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
123*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 | 0,
124*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 4,
125*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID,
126*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0x0100,
127*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0xFFFC,
128*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0xFD00);
129*795d594fSAndroid Build Coastguard Worker
130*795d594fSAndroid Build Coastguard Worker const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
131*795d594fSAndroid Build Coastguard Worker TestCode(data, blocks);
132*795d594fSAndroid Build Coastguard Worker }
133*795d594fSAndroid Build Coastguard Worker
TEST_F(LinearizeTest,CFG4)134*795d594fSAndroid Build Coastguard Worker TEST_F(LinearizeTest, CFG4) {
135*795d594fSAndroid Build Coastguard Worker /* Structure of this graph (+ are back edges)
136*795d594fSAndroid Build Coastguard Worker // Block0
137*795d594fSAndroid Build Coastguard Worker // |
138*795d594fSAndroid Build Coastguard Worker // Block1
139*795d594fSAndroid Build Coastguard Worker // |
140*795d594fSAndroid Build Coastguard Worker // Block2
141*795d594fSAndroid Build Coastguard Worker // / + \
142*795d594fSAndroid Build Coastguard Worker // Block6 + Block8
143*795d594fSAndroid Build Coastguard Worker // | + |
144*795d594fSAndroid Build Coastguard Worker // Block7 + Block3 +++++++
145*795d594fSAndroid Build Coastguard Worker // + / \ +
146*795d594fSAndroid Build Coastguard Worker // Block9 Block10 +
147*795d594fSAndroid Build Coastguard Worker // | +
148*795d594fSAndroid Build Coastguard Worker // Block4 +
149*795d594fSAndroid Build Coastguard Worker // + / \ +
150*795d594fSAndroid Build Coastguard Worker // Block5 Block11
151*795d594fSAndroid Build Coastguard Worker */
152*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
153*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 | 0,
154*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 7,
155*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0xFFFE,
156*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0xFFFE,
157*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0xFE00,
158*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID);
159*795d594fSAndroid Build Coastguard Worker
160*795d594fSAndroid Build Coastguard Worker const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
161*795d594fSAndroid Build Coastguard Worker TestCode(data, blocks);
162*795d594fSAndroid Build Coastguard Worker }
163*795d594fSAndroid Build Coastguard Worker
TEST_F(LinearizeTest,CFG5)164*795d594fSAndroid Build Coastguard Worker TEST_F(LinearizeTest, CFG5) {
165*795d594fSAndroid Build Coastguard Worker /* Structure of this graph (+ are back edges)
166*795d594fSAndroid Build Coastguard Worker // Block0
167*795d594fSAndroid Build Coastguard Worker // |
168*795d594fSAndroid Build Coastguard Worker // Block1
169*795d594fSAndroid Build Coastguard Worker // |
170*795d594fSAndroid Build Coastguard Worker // Block2
171*795d594fSAndroid Build Coastguard Worker // / + \
172*795d594fSAndroid Build Coastguard Worker // Block3 + Block8
173*795d594fSAndroid Build Coastguard Worker // | + |
174*795d594fSAndroid Build Coastguard Worker // Block7 + Block4 +++++++
175*795d594fSAndroid Build Coastguard Worker // + / \ +
176*795d594fSAndroid Build Coastguard Worker // Block9 Block10 +
177*795d594fSAndroid Build Coastguard Worker // | +
178*795d594fSAndroid Build Coastguard Worker // Block5 +
179*795d594fSAndroid Build Coastguard Worker // +/ \ +
180*795d594fSAndroid Build Coastguard Worker // Block6 Block11
181*795d594fSAndroid Build Coastguard Worker */
182*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
183*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 | 0,
184*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 3,
185*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID,
186*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0xFFFD,
187*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0xFFFE,
188*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0xFE00);
189*795d594fSAndroid Build Coastguard Worker
190*795d594fSAndroid Build Coastguard Worker const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
191*795d594fSAndroid Build Coastguard Worker TestCode(data, blocks);
192*795d594fSAndroid Build Coastguard Worker }
193*795d594fSAndroid Build Coastguard Worker
TEST_F(LinearizeTest,CFG6)194*795d594fSAndroid Build Coastguard Worker TEST_F(LinearizeTest, CFG6) {
195*795d594fSAndroid Build Coastguard Worker // Block0
196*795d594fSAndroid Build Coastguard Worker // |
197*795d594fSAndroid Build Coastguard Worker // Block1
198*795d594fSAndroid Build Coastguard Worker // |
199*795d594fSAndroid Build Coastguard Worker // Block2 ++++++++++++++
200*795d594fSAndroid Build Coastguard Worker // | +
201*795d594fSAndroid Build Coastguard Worker // Block3 +
202*795d594fSAndroid Build Coastguard Worker // / \ +
203*795d594fSAndroid Build Coastguard Worker // Block8 Block4 +
204*795d594fSAndroid Build Coastguard Worker // | / \ +
205*795d594fSAndroid Build Coastguard Worker // Block5 <- Block9 Block6 +
206*795d594fSAndroid Build Coastguard Worker // |
207*795d594fSAndroid Build Coastguard Worker // Block7
208*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
209*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 | 0,
210*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0x0100,
211*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0x0004,
212*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0x0003,
213*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID,
214*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0xFA00);
215*795d594fSAndroid Build Coastguard Worker
216*795d594fSAndroid Build Coastguard Worker const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
217*795d594fSAndroid Build Coastguard Worker TestCode(data, blocks);
218*795d594fSAndroid Build Coastguard Worker }
219*795d594fSAndroid Build Coastguard Worker
TEST_F(LinearizeTest,CFG7)220*795d594fSAndroid Build Coastguard Worker TEST_F(LinearizeTest, CFG7) {
221*795d594fSAndroid Build Coastguard Worker // Structure of this graph (+ are back edges)
222*795d594fSAndroid Build Coastguard Worker // Block0
223*795d594fSAndroid Build Coastguard Worker // |
224*795d594fSAndroid Build Coastguard Worker // Block1
225*795d594fSAndroid Build Coastguard Worker // |
226*795d594fSAndroid Build Coastguard Worker // Block2 ++++++++
227*795d594fSAndroid Build Coastguard Worker // | +
228*795d594fSAndroid Build Coastguard Worker // Block3 +
229*795d594fSAndroid Build Coastguard Worker // / \ +
230*795d594fSAndroid Build Coastguard Worker // Block4 Block8 +
231*795d594fSAndroid Build Coastguard Worker // / \ | +
232*795d594fSAndroid Build Coastguard Worker // Block5 Block9 - Block6 +
233*795d594fSAndroid Build Coastguard Worker // |
234*795d594fSAndroid Build Coastguard Worker // Block7
235*795d594fSAndroid Build Coastguard Worker //
236*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
237*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 | 0,
238*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0x0100,
239*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0x0005,
240*795d594fSAndroid Build Coastguard Worker Instruction::IF_EQ, 0x0003,
241*795d594fSAndroid Build Coastguard Worker Instruction::RETURN_VOID,
242*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0xFA00);
243*795d594fSAndroid Build Coastguard Worker
244*795d594fSAndroid Build Coastguard Worker const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
245*795d594fSAndroid Build Coastguard Worker TestCode(data, blocks);
246*795d594fSAndroid Build Coastguard Worker }
247*795d594fSAndroid Build Coastguard Worker
248*795d594fSAndroid Build Coastguard Worker } // namespace art
249