1 /*
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 "arch/x86/instruction_set_features_x86.h"
20 #include "base/arena_allocator.h"
21 #include "builder.h"
22 #include "code_generator.h"
23 #include "code_generator_x86.h"
24 #include "dex_file.h"
25 #include "dex_instruction.h"
26 #include "driver/compiler_options.h"
27 #include "graph_visualizer.h"
28 #include "nodes.h"
29 #include "optimizing_unit_test.h"
30 #include "pretty_printer.h"
31 #include "ssa_liveness_analysis.h"
32 
33 namespace art {
34 
35 class LinearizeTest : public CommonCompilerTest {};
36 
37 template <size_t number_of_blocks>
TestCode(const uint16_t * data,const uint32_t (& expected_order)[number_of_blocks])38 static void TestCode(const uint16_t* data, const uint32_t (&expected_order)[number_of_blocks]) {
39   ArenaPool pool;
40   ArenaAllocator allocator(&pool);
41   HGraph* graph = CreateCFG(&allocator, data);
42   std::unique_ptr<const X86InstructionSetFeatures> features_x86(
43       X86InstructionSetFeatures::FromCppDefines());
44   x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
45   SsaLivenessAnalysis liveness(graph, &codegen);
46   liveness.Analyze();
47 
48   ASSERT_EQ(graph->GetLinearOrder().size(), number_of_blocks);
49   for (size_t i = 0; i < number_of_blocks; ++i) {
50     ASSERT_EQ(graph->GetLinearOrder()[i]->GetBlockId(), expected_order[i]);
51   }
52 }
53 
TEST_F(LinearizeTest,CFG1)54 TEST_F(LinearizeTest, CFG1) {
55   // Structure of this graph (+ are back edges)
56   //            Block0
57   //              |
58   //            Block1
59   //              |
60   //            Block2 ++++++
61   //            /   \       +
62   //       Block5   Block7  +
63   //         |        |     +
64   //       Block6   Block3  +
65   //               + /   \  +
66   //           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 
75   const uint32_t blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
76   TestCode(data, blocks);
77 }
78 
TEST_F(LinearizeTest,CFG2)79 TEST_F(LinearizeTest, CFG2) {
80   // Structure of this graph (+ are back edges)
81   //            Block0
82   //              |
83   //            Block1
84   //              |
85   //            Block2 ++++++
86   //            /   \       +
87   //       Block3   Block7  +
88   //         |        |     +
89   //       Block6   Block4  +
90   //               + /   \  +
91   //           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 
100   const uint32_t blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
101   TestCode(data, blocks);
102 }
103 
TEST_F(LinearizeTest,CFG3)104 TEST_F(LinearizeTest, CFG3) {
105   // Structure of this graph (+ are back edges)
106   //            Block0
107   //              |
108   //            Block1
109   //              |
110   //            Block2 ++++++
111   //            /   \       +
112   //       Block3   Block8  +
113   //         |        |     +
114   //       Block7   Block5  +
115   //                 / +  \ +
116   //           Block6  + Block9
117   //             |     +
118   //           Block4 ++
119   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 
127   const uint32_t blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
128   TestCode(data, blocks);
129 }
130 
TEST_F(LinearizeTest,CFG4)131 TEST_F(LinearizeTest, CFG4) {
132   /* Structure of this graph (+ are back edges)
133   //            Block0
134   //              |
135   //            Block1
136   //              |
137   //            Block2
138   //            / +  \
139   //       Block6 + Block8
140   //         |    +   |
141   //       Block7 + Block3 +++++++
142   //              +  /  \        +
143   //           Block9   Block10  +
144   //                      |      +
145   //                    Block4   +
146   //                  + /    \   +
147   //                Block5  Block11
148   */
149   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 
157   const uint32_t blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
158   TestCode(data, blocks);
159 }
160 
TEST_F(LinearizeTest,CFG5)161 TEST_F(LinearizeTest, CFG5) {
162   /* Structure of this graph (+ are back edges)
163   //            Block0
164   //              |
165   //            Block1
166   //              |
167   //            Block2
168   //            / +  \
169   //       Block3 + Block8
170   //         |    +   |
171   //       Block7 + Block4 +++++++
172   //              +  /  \        +
173   //           Block9   Block10  +
174   //                      |      +
175   //                    Block5   +
176   //                   +/    \   +
177   //                Block6  Block11
178   */
179   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 
187   const uint32_t blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
188   TestCode(data, blocks);
189 }
190 
TEST_F(LinearizeTest,CFG6)191 TEST_F(LinearizeTest, CFG6) {
192   //            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 
213   const uint32_t blocks[] = {0, 1, 2, 3, 4, 6, 9, 8, 5, 7};
214   TestCode(data, blocks);
215 }
216 
TEST_F(LinearizeTest,CFG7)217 TEST_F(LinearizeTest, CFG7) {
218   // 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 
241   const uint32_t blocks[] = {0, 1, 2, 3, 4, 9, 8, 6, 5, 7};
242   TestCode(data, blocks);
243 }
244 
245 }  // namespace art
246