blob: 6dd4207795204d6b6cd47a08f3abd2645cbcacd0 [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
19#include "base/stringprintf.h"
20#include "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010021#include "code_generator.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010022#include "code_generator_x86.h"
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010023#include "dex_file.h"
24#include "dex_instruction.h"
25#include "graph_visualizer.h"
26#include "nodes.h"
27#include "optimizing_unit_test.h"
28#include "pretty_printer.h"
29#include "ssa_builder.h"
30#include "ssa_liveness_analysis.h"
31#include "utils/arena_allocator.h"
32
33#include "gtest/gtest.h"
34
35namespace art {
36
37static void TestCode(const uint16_t* data, const int* expected_order, size_t number_of_blocks) {
38 ArenaPool pool;
39 ArenaAllocator allocator(&pool);
40 HGraphBuilder builder(&allocator);
41 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
42 HGraph* graph = builder.BuildGraph(*item);
43 ASSERT_NE(graph, nullptr);
44
45 graph->BuildDominatorTree();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010046 graph->TransformToSSA();
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010047 graph->FindNaturalLoops();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010048
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010049 x86::CodeGeneratorX86 codegen(graph);
50 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010051 liveness.Analyze();
52
53 ASSERT_EQ(liveness.GetLinearPostOrder().Size(), number_of_blocks);
54 for (size_t i = 0; i < number_of_blocks; ++i) {
55 ASSERT_EQ(liveness.GetLinearPostOrder().Get(number_of_blocks - i - 1)->GetBlockId(),
56 expected_order[i]);
57 }
58}
59
60TEST(LinearizeTest, CFG1) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010061 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010062 // Block0
63 // |
64 // Block1
65 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010066 // Block2 ++++++
67 // / \ +
68 // Block5 Block7 +
69 // | | +
70 // Block6 Block3 +
71 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010072 // Block4 Block8
73
74 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
75 Instruction::CONST_4 | 0 | 0,
76 Instruction::IF_EQ, 5,
77 Instruction::IF_EQ, 0xFFFE,
78 Instruction::GOTO | 0xFE00,
79 Instruction::RETURN_VOID);
80
81 const int blocks[] = {0, 1, 2, 7, 3, 4, 8, 5, 6};
82 TestCode(data, blocks, 9);
83}
84
85TEST(LinearizeTest, CFG2) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010086 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010087 // Block0
88 // |
89 // Block1
90 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +010091 // Block2 ++++++
92 // / \ +
93 // Block3 Block7 +
94 // | | +
95 // Block6 Block4 +
96 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010097 // Block5 Block8
98
99 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
100 Instruction::CONST_4 | 0 | 0,
101 Instruction::IF_EQ, 3,
102 Instruction::RETURN_VOID,
103 Instruction::IF_EQ, 0xFFFD,
104 Instruction::GOTO | 0xFE00);
105
106 const int blocks[] = {0, 1, 2, 7, 4, 5, 8, 3, 6};
107 TestCode(data, blocks, 9);
108}
109
110TEST(LinearizeTest, CFG3) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100111 // Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100112 // Block0
113 // |
114 // Block1
115 // |
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100116 // Block2 ++++++
117 // / \ +
118 // Block3 Block8 +
119 // | | +
120 // Block7 Block5 +
121 // / + \ +
122 // Block6 + Block9
123 // | +
124 // Block4 ++
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100125 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
126 Instruction::CONST_4 | 0 | 0,
127 Instruction::IF_EQ, 4,
128 Instruction::RETURN_VOID,
129 Instruction::GOTO | 0x0100,
130 Instruction::IF_EQ, 0xFFFC,
131 Instruction::GOTO | 0xFD00);
132
133 const int blocks[] = {0, 1, 2, 8, 5, 6, 4, 9, 3, 7};
134 TestCode(data, blocks, 10);
135}
136
137TEST(LinearizeTest, CFG4) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100138 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100139 // Block0
140 // |
141 // Block1
142 // |
143 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100144 // / + \
145 // Block6 + Block8
146 // | + |
147 // Block7 + Block3 +++++++
148 // + / \ +
149 // Block9 Block10 +
150 // | +
151 // Block4 +
152 // + / \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100153 // Block5 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100154 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100155 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
156 Instruction::CONST_4 | 0 | 0,
157 Instruction::IF_EQ, 7,
158 Instruction::IF_EQ, 0xFFFE,
159 Instruction::IF_EQ, 0xFFFE,
160 Instruction::GOTO | 0xFE00,
161 Instruction::RETURN_VOID);
162
163 const int blocks[] = {0, 1, 2, 8, 3, 10, 4, 5, 11, 9, 6, 7};
164 TestCode(data, blocks, 12);
165}
166
167TEST(LinearizeTest, CFG5) {
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100168 /* Structure of this graph (+ are back edges)
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100169 // Block0
170 // |
171 // Block1
172 // |
173 // Block2
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100174 // / + \
175 // Block3 + Block8
176 // | + |
177 // Block7 + Block4 +++++++
178 // + / \ +
179 // Block9 Block10 +
180 // | +
181 // Block5 +
182 // +/ \ +
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100183 // Block6 Block11
Nicolas Geoffray8f1a4d42014-05-16 09:36:00 +0100184 */
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100185 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
186 Instruction::CONST_4 | 0 | 0,
187 Instruction::IF_EQ, 3,
188 Instruction::RETURN_VOID,
189 Instruction::IF_EQ, 0xFFFD,
190 Instruction::IF_EQ, 0xFFFE,
191 Instruction::GOTO | 0xFE00);
192
193 const int blocks[] = {0, 1, 2, 8, 4, 10, 5, 6, 11, 9, 3, 7};
194 TestCode(data, blocks, 12);
195}
196
197} // namespace art