blob: a81a30e457ccac8216df804c57fed622674f4dca [file] [log] [blame]
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +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 "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010018#include "code_generator.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010019#include "code_generator_x86.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010020#include "dex_file.h"
21#include "dex_instruction.h"
22#include "nodes.h"
23#include "optimizing_unit_test.h"
24#include "ssa_liveness_analysis.h"
25#include "utils/arena_allocator.h"
26
27#include "gtest/gtest.h"
28
29namespace art {
30
31static HGraph* BuildGraph(const uint16_t* data, ArenaAllocator* allocator) {
32 HGraphBuilder builder(allocator);
33 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
34 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000035 // Suspend checks implementation may change in the future, and this test relies
36 // on how instructions are ordered.
37 RemoveSuspendChecks(graph);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038 graph->BuildDominatorTree();
39 graph->TransformToSSA();
40 graph->FindNaturalLoops();
41 return graph;
42}
43
44TEST(LiveRangesTest, CFG1) {
45 /*
46 * Test the following snippet:
47 * return 0;
48 *
49 * Which becomes the following graph (numbered by lifetime position):
50 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010051 * 4: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010052 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010053 * 8: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010054 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010055 * 12: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 */
57 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
58 Instruction::CONST_4 | 0 | 0,
59 Instruction::RETURN);
60
61 ArenaPool pool;
62 ArenaAllocator allocator(&pool);
63 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010064
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010065 x86::CodeGeneratorX86 codegen(graph);
66 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067 liveness.Analyze();
68
69 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010070 LiveRange* range = interval->GetFirstRange();
71 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010072 // Last use is the return instruction.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010073 ASSERT_EQ(9u, range->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010074 HBasicBlock* block = graph->GetBlocks().Get(1);
75 ASSERT_TRUE(block->GetLastInstruction()->AsReturn() != nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010076 ASSERT_EQ(8u, block->GetLastInstruction()->GetLifetimePosition());
77 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010078}
79
80TEST(LiveRangesTest, CFG2) {
81 /*
82 * Test the following snippet:
83 * var a = 0;
84 * if (0 == 0) {
85 * } else {
86 * }
87 * return a;
88 *
89 * Which becomes the following graph (numbered by lifetime position):
90 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010091 * 4: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010092 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010093 * 8: equal
94 * 10: if
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010095 * / \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010096 * 14: goto 18: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010097 * \ /
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010098 * 22: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010099 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100 * 26: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100101 */
102 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
103 Instruction::CONST_4 | 0 | 0,
104 Instruction::IF_EQ, 3,
105 Instruction::GOTO | 0x100,
106 Instruction::RETURN | 0 << 8);
107
108 ArenaPool pool;
109 ArenaAllocator allocator(&pool);
110 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100111 x86::CodeGeneratorX86 codegen(graph);
112 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100113 liveness.Analyze();
114
115 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100116 LiveRange* range = interval->GetFirstRange();
117 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100118 // Last use is the return instruction.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100119 ASSERT_EQ(23u, range->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100120 HBasicBlock* block = graph->GetBlocks().Get(3);
121 ASSERT_TRUE(block->GetLastInstruction()->AsReturn() != nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100122 ASSERT_EQ(22u, block->GetLastInstruction()->GetLifetimePosition());
123 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100124}
125
126TEST(LiveRangesTest, CFG3) {
127 /*
128 * Test the following snippet:
129 * var a = 0;
130 * if (0 == 0) {
131 * } else {
132 * a = 4;
133 * }
134 * return a;
135 *
136 * Which becomes the following graph (numbered by lifetime position):
137 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100138 * 4: constant4
139 * 6: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100140 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100141 * 10: equal
142 * 12: if
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100143 * / \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100144 * 16: goto 20: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100145 * \ /
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 * 22: phi
147 * 24: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100148 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100149 * 38: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100150 */
151 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
152 Instruction::CONST_4 | 0 | 0,
153 Instruction::IF_EQ, 3,
154 Instruction::CONST_4 | 4 << 12 | 0,
155 Instruction::RETURN | 0 << 8);
156
157 ArenaPool pool;
158 ArenaAllocator allocator(&pool);
159 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100160 x86::CodeGeneratorX86 codegen(graph);
161 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100162 liveness.Analyze();
163
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100164 // Test for the 4 constant.
165 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100166 LiveRange* range = interval->GetFirstRange();
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100167 ASSERT_EQ(4u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100168 // Last use is the phi at the return block so instruction is live until
169 // the end of the then block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100170 ASSERT_EQ(18u, range->GetEnd());
171 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100172
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100173 // Test for the 0 constant.
174 interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100175 // The then branch is a hole for this constant, therefore its interval has 2 ranges.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100176 // First range starts from the definition and ends at the if block.
177 range = interval->GetFirstRange();
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100178 ASSERT_EQ(2u, range->GetStart());
179 // 14 is the end of the if block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100180 ASSERT_EQ(14u, range->GetEnd());
181 // Second range is the else block.
182 range = range->GetNext();
183 ASSERT_EQ(18u, range->GetStart());
184 // Last use is the phi at the return block.
185 ASSERT_EQ(22u, range->GetEnd());
186 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100187
188 // Test for the phi.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100189 interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100190 range = interval->GetFirstRange();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100191 ASSERT_EQ(22u, liveness.GetInstructionFromSsaIndex(2)->GetLifetimePosition());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100192 ASSERT_EQ(22u, range->GetStart());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100193 ASSERT_EQ(25u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100194 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100195}
196
197TEST(LiveRangesTest, Loop) {
198 /*
199 * Test the following snippet:
200 * var a = 0;
201 * while (a == a) {
202 * a = 4;
203 * }
204 * return 5;
205 *
206 * Which becomes the following graph (numbered by lifetime position):
207 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100208 * 4: constant4
209 * 6: constant5
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100210 * 8: goto
211 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100212 * 12: goto
213 * |
214 * 14: phi
215 * 16: equal
216 * 18: if +++++
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100217 * | \ +
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100218 * | 22: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100219 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100220 * 26: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100221 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100222 * 30: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100223 */
224
225 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
226 Instruction::CONST_4 | 0 | 0,
227 Instruction::IF_EQ, 4,
228 Instruction::CONST_4 | 4 << 12 | 0,
229 Instruction::GOTO | 0xFD00,
230 Instruction::CONST_4 | 5 << 12 | 1 << 8,
231 Instruction::RETURN | 1 << 8);
232
233 ArenaPool pool;
234 ArenaAllocator allocator(&pool);
235 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100236 x86::CodeGeneratorX86 codegen(graph);
237 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100238 liveness.Analyze();
239
240 // Test for the 0 constant.
241 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100242 LiveRange* range = interval->GetFirstRange();
243 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100244 // Last use is the loop phi so instruction is live until
245 // the end of the pre loop header.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100246 ASSERT_EQ(14u, range->GetEnd());
247 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100248
249 // Test for the 4 constant.
250 interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100251 range = interval->GetFirstRange();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100252 // The instruction is live until the end of the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100253 ASSERT_EQ(4u, range->GetStart());
254 ASSERT_EQ(24u, range->GetEnd());
255 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100256
257 // Test for the 5 constant.
258 interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100259 range = interval->GetFirstRange();
260 // The instruction is live until the return instruction after the loop.
261 ASSERT_EQ(6u, range->GetStart());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100262 ASSERT_EQ(27u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100263 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100264
265 // Test for the phi.
266 interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100267 range = interval->GetFirstRange();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100268 // Instruction is consumed by the if.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100269 ASSERT_EQ(14u, range->GetStart());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100270 ASSERT_EQ(17u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100271 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100272}
273
274} // namespace art