blob: 8b75cc7c6512cd585bdc21e1629937443c7e3c0f [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
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
Nicolas Geoffray360231a2014-10-08 21:07:48 +010017#include <functional>
18
Ian Rogersd582fa42014-11-05 23:46:43 -080019#include "arch/instruction_set.h"
Alexandre Rames92730742014-10-01 12:55:56 +010020#include "base/macros.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "builder.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010022#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "code_generator_arm64.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010024#include "code_generator_x86.h"
25#include "code_generator_x86_64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000027#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "dex_instruction.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000030#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010031#include "prepare_for_register_allocation.h"
32#include "register_allocator.h"
33#include "ssa_liveness_analysis.h"
Roland Levillain55dcfb52014-10-24 18:09:09 +010034#include "utils.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
36#include "gtest/gtest.h"
37
38namespace art {
39
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041 public:
Ian Rogersd582fa42014-11-05 23:46:43 -080042 InternalCodeAllocator() : size_(0) { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000043
44 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000045 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000046 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000047 return memory_.get();
48 }
49
Nicolas Geoffray787c3072014-03-17 10:20:19 +000050 size_t GetSize() const { return size_; }
51 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000052
53 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +000054 size_t size_;
Ian Rogers700a4022014-05-19 16:49:03 -070055 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000056
Nicolas Geoffray787c3072014-03-17 10:20:19 +000057 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000058};
59
Roland Levillain55dcfb52014-10-24 18:09:09 +010060template <typename Expected>
Nicolas Geoffray8d486732014-07-16 16:23:40 +010061static void Run(const InternalCodeAllocator& allocator,
62 const CodeGenerator& codegen,
63 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +010064 Expected expected) {
65 typedef Expected (*fptr)();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010066 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -070067 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Nicolas Geoffray8d486732014-07-16 16:23:40 +010068 if (codegen.GetInstructionSet() == kThumb2) {
69 // For thumb we need the bottom bit set.
70 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
71 }
Roland Levillain55dcfb52014-10-24 18:09:09 +010072 Expected result = f();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010073 if (has_result) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +010074 ASSERT_EQ(result, expected);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010075 }
76}
77
Roland Levillain55dcfb52014-10-24 18:09:09 +010078template <typename Expected>
79static void RunCodeBaseline(HGraph* graph, bool has_result, Expected expected) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010081
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010082 x86::CodeGeneratorX86 codegenX86(graph);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010083 // We avoid doing a stack overflow check that requires the runtime being setup,
84 // by making sure the compiler knows the methods we are running are leaf methods.
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010085 codegenX86.CompileBaseline(&allocator, true);
86 if (kRuntimeISA == kX86) {
87 Run(allocator, codegenX86, has_result, expected);
88 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010089
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010090 arm::CodeGeneratorARM codegenARM(graph);
91 codegenARM.CompileBaseline(&allocator, true);
92 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
93 Run(allocator, codegenARM, has_result, expected);
94 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010095
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010096 x86_64::CodeGeneratorX86_64 codegenX86_64(graph);
97 codegenX86_64.CompileBaseline(&allocator, true);
98 if (kRuntimeISA == kX86_64) {
99 Run(allocator, codegenX86_64, has_result, expected);
100 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100101
102 arm64::CodeGeneratorARM64 codegenARM64(graph);
103 codegenARM64.CompileBaseline(&allocator, true);
104 if (kRuntimeISA == kArm64) {
105 Run(allocator, codegenARM64, has_result, expected);
106 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000107}
108
Roland Levillain55dcfb52014-10-24 18:09:09 +0100109template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100110static void RunCodeOptimized(CodeGenerator* codegen,
111 HGraph* graph,
112 std::function<void(HGraph*)> hook_before_codegen,
113 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100114 Expected expected) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100115 SsaLivenessAnalysis liveness(*graph, codegen);
116 liveness.Analyze();
117
118 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
119 register_allocator.AllocateRegisters();
120 hook_before_codegen(graph);
121
122 InternalCodeAllocator allocator;
123 codegen->CompileOptimized(&allocator);
124 Run(allocator, *codegen, has_result, expected);
125}
126
Roland Levillain55dcfb52014-10-24 18:09:09 +0100127template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100128static void RunCodeOptimized(HGraph* graph,
129 std::function<void(HGraph*)> hook_before_codegen,
130 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100131 Expected expected) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000132 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100133 arm::CodeGeneratorARM codegenARM(graph);
134 RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000135 } else if (kRuntimeISA == kArm64) {
136 arm64::CodeGeneratorARM64 codegenARM64(graph);
137 RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected);
138 } else if (kRuntimeISA == kX86) {
139 x86::CodeGeneratorX86 codegenX86(graph);
140 RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100141 } else if (kRuntimeISA == kX86_64) {
142 x86_64::CodeGeneratorX86_64 codegenX86_64(graph);
143 RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
144 }
145}
146
147static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
148 ArenaPool pool;
149 ArenaAllocator arena(&pool);
150 HGraphBuilder builder(&arena);
151 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
152 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100153 ASSERT_NE(graph, nullptr);
Calin Juravle039b6e22014-10-23 12:32:11 +0100154 // Remove suspend checks, they cannot be executed in this context.
Calin Juravle48dee042014-10-22 15:54:12 +0100155 RemoveSuspendChecks(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100156 RunCodeBaseline(graph, has_result, expected);
157}
158
Roland Levillain55dcfb52014-10-24 18:09:09 +0100159static void TestCodeLong(const uint16_t* data, bool has_result, int64_t expected) {
160 ArenaPool pool;
161 ArenaAllocator arena(&pool);
162 HGraphBuilder builder(&arena, Primitive::kPrimLong);
163 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
164 HGraph* graph = builder.BuildGraph(*item);
165 ASSERT_NE(graph, nullptr);
166 // Remove suspend checks, they cannot be executed in this context.
167 RemoveSuspendChecks(graph);
168 RunCodeBaseline(graph, has_result, expected);
169}
170
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000171TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000172 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
173 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000174}
175
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000176TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000177 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000178 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000179 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000180
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000181 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000182}
183
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000184TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000185 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000186 Instruction::GOTO | 0x100,
187 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000188 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000190 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000191}
192
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000193TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000194 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000195 Instruction::GOTO | 0x200,
196 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000197 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000198
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000199 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000200
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000201 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000202 Instruction::GOTO_16, 3,
203 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000204 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000205
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000206 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000207
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000208 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000209 Instruction::GOTO_32, 4, 0,
210 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000211 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000212
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000213 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000214}
215
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000216TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000217 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000218 Instruction::RETURN_VOID,
219 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000220 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000222 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000223}
224
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000225TEST(CodegenTest, CFG5) {
226 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
227 Instruction::CONST_4 | 0 | 0,
228 Instruction::IF_EQ, 3,
229 Instruction::GOTO | 0x100,
230 Instruction::RETURN_VOID);
231
232 TestCode(data);
233}
234
235TEST(CodegenTest, IntConstant) {
236 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
237 Instruction::CONST_4 | 0 | 0,
238 Instruction::RETURN_VOID);
239
240 TestCode(data);
241}
242
243TEST(CodegenTest, Return1) {
244 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
245 Instruction::CONST_4 | 0 | 0,
246 Instruction::RETURN | 0);
247
248 TestCode(data, true, 0);
249}
250
251TEST(CodegenTest, Return2) {
252 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
253 Instruction::CONST_4 | 0 | 0,
254 Instruction::CONST_4 | 0 | 1 << 8,
255 Instruction::RETURN | 1 << 8);
256
257 TestCode(data, true, 0);
258}
259
260TEST(CodegenTest, Return3) {
261 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
262 Instruction::CONST_4 | 0 | 0,
263 Instruction::CONST_4 | 1 << 8 | 1 << 12,
264 Instruction::RETURN | 1 << 8);
265
266 TestCode(data, true, 1);
267}
268
269TEST(CodegenTest, ReturnIf1) {
270 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
271 Instruction::CONST_4 | 0 | 0,
272 Instruction::CONST_4 | 1 << 8 | 1 << 12,
273 Instruction::IF_EQ, 3,
274 Instruction::RETURN | 0 << 8,
275 Instruction::RETURN | 1 << 8);
276
277 TestCode(data, true, 1);
278}
279
280TEST(CodegenTest, ReturnIf2) {
281 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
282 Instruction::CONST_4 | 0 | 0,
283 Instruction::CONST_4 | 1 << 8 | 1 << 12,
284 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
285 Instruction::RETURN | 0 << 8,
286 Instruction::RETURN | 1 << 8);
287
288 TestCode(data, true, 0);
289}
290
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100291// Exercise bit-wise (one's complement) not-int instruction.
292#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
293TEST(CodegenTest, TEST_NAME) { \
294 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100295 const uint16_t input_lo = Low16Bits(input); \
296 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100297 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
298 Instruction::CONST | 0 << 8, input_lo, input_hi, \
299 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
300 Instruction::RETURN | 1 << 8); \
301 \
302 TestCode(data, true, EXPECTED_OUTPUT); \
303}
304
305NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
306NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
307NOT_INT_TEST(ReturnNotInt0, 0, -1)
308NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100309NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
310NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
311NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
312NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100313
314#undef NOT_INT_TEST
315
Roland Levillain55dcfb52014-10-24 18:09:09 +0100316// Exercise bit-wise (one's complement) not-long instruction.
317#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
318TEST(CodegenTest, TEST_NAME) { \
319 const int64_t input = INPUT; \
320 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
321 const uint16_t word1 = High16Bits(Low32Bits(input)); \
322 const uint16_t word2 = Low16Bits(High32Bits(input)); \
323 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
324 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
325 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
326 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
327 Instruction::RETURN_WIDE | 2 << 8); \
328 \
329 TestCodeLong(data, true, EXPECTED_OUTPUT); \
330}
331
332NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
333NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
334NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
335NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
336
337NOT_LONG_TEST(ReturnNotLongINT32_MIN,
338 INT64_C(-2147483648),
339 INT64_C(2147483647)) // (2^31) - 1
340NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
341 INT64_C(-2147483647),
342 INT64_C(2147483646)) // (2^31) - 2
343NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
344 INT64_C(2147483646),
345 INT64_C(-2147483647)) // -(2^31) - 1
346NOT_LONG_TEST(ReturnNotLongINT32_MAX,
347 INT64_C(2147483647),
348 INT64_C(-2147483648)) // -(2^31)
349
350// Note that the C++ compiler won't accept
351// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
352// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
353NOT_LONG_TEST(ReturnNotINT64_MIN,
354 INT64_C(-9223372036854775807)-1,
355 INT64_C(9223372036854775807)); // (2^63) - 1
356NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
357 INT64_C(-9223372036854775807),
358 INT64_C(9223372036854775806)); // (2^63) - 2
359NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
360 INT64_C(9223372036854775806),
361 INT64_C(-9223372036854775807)); // -(2^63) - 1
362NOT_LONG_TEST(ReturnNotLongINT64_MAX,
363 INT64_C(9223372036854775807),
364 INT64_C(-9223372036854775807)-1); // -(2^63)
365
366#undef NOT_LONG_TEST
367
Roland Levillain946e1432014-11-11 17:35:19 +0000368TEST(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000369 const int64_t input = INT64_C(4294967296); // 2^32
370 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
371 const uint16_t word1 = High16Bits(Low32Bits(input));
372 const uint16_t word2 = Low16Bits(High32Bits(input));
373 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
374 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
375 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
376 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
377 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
378 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
379 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
380 Instruction::RETURN_WIDE | 2 << 8);
381
382 TestCodeLong(data, true, 1);
383}
384
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000385TEST(CodegenTest, ReturnAdd1) {
386 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
387 Instruction::CONST_4 | 3 << 12 | 0,
388 Instruction::CONST_4 | 4 << 12 | 1 << 8,
389 Instruction::ADD_INT, 1 << 8 | 0,
390 Instruction::RETURN);
391
392 TestCode(data, true, 7);
393}
394
395TEST(CodegenTest, ReturnAdd2) {
396 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
397 Instruction::CONST_4 | 3 << 12 | 0,
398 Instruction::CONST_4 | 4 << 12 | 1 << 8,
399 Instruction::ADD_INT_2ADDR | 1 << 12,
400 Instruction::RETURN);
401
402 TestCode(data, true, 7);
403}
404
405TEST(CodegenTest, ReturnAdd3) {
406 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
407 Instruction::CONST_4 | 4 << 12 | 0 << 8,
408 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
409 Instruction::RETURN);
410
411 TestCode(data, true, 7);
412}
413
414TEST(CodegenTest, ReturnAdd4) {
415 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
416 Instruction::CONST_4 | 4 << 12 | 0 << 8,
417 Instruction::ADD_INT_LIT16, 3,
418 Instruction::RETURN);
419
420 TestCode(data, true, 7);
421}
422
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100423TEST(CodegenTest, NonMaterializedCondition) {
424 ArenaPool pool;
425 ArenaAllocator allocator(&pool);
426
427 HGraph* graph = new (&allocator) HGraph(&allocator);
428 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
429 graph->AddBlock(entry);
430 graph->SetEntryBlock(entry);
431 entry->AddInstruction(new (&allocator) HGoto());
432
433 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
434 graph->AddBlock(first_block);
435 entry->AddSuccessor(first_block);
436 HIntConstant* constant0 = new (&allocator) HIntConstant(0);
437 entry->AddInstruction(constant0);
438 HIntConstant* constant1 = new (&allocator) HIntConstant(1);
439 entry->AddInstruction(constant1);
440 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
441 first_block->AddInstruction(equal);
442 first_block->AddInstruction(new (&allocator) HIf(equal));
443
444 HBasicBlock* then = new (&allocator) HBasicBlock(graph);
445 HBasicBlock* else_ = new (&allocator) HBasicBlock(graph);
446 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
447
448 graph->AddBlock(then);
449 graph->AddBlock(else_);
450 graph->AddBlock(exit);
451 first_block->AddSuccessor(then);
452 first_block->AddSuccessor(else_);
453 then->AddSuccessor(exit);
454 else_->AddSuccessor(exit);
455
456 exit->AddInstruction(new (&allocator) HExit());
457 then->AddInstruction(new (&allocator) HReturn(constant0));
458 else_->AddInstruction(new (&allocator) HReturn(constant1));
459
460 ASSERT_TRUE(equal->NeedsMaterialization());
461 graph->BuildDominatorTree();
462 PrepareForRegisterAllocation(graph).Run();
463 ASSERT_FALSE(equal->NeedsMaterialization());
464
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800465 auto hook_before_codegen = [](HGraph* graph_in) {
466 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
467 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100468 block->InsertInstructionBefore(move, block->GetLastInstruction());
469 };
470
471 RunCodeOptimized(graph, hook_before_codegen, true, 0);
472}
473
Calin Juravle34bacdf2014-10-07 20:23:36 +0100474#define MUL_TEST(TYPE, TEST_NAME) \
475 TEST(CodegenTest, Return ## TEST_NAME) { \
476 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
477 Instruction::CONST_4 | 3 << 12 | 0, \
478 Instruction::CONST_4 | 4 << 12 | 1 << 8, \
479 Instruction::MUL_ ## TYPE, 1 << 8 | 0, \
480 Instruction::RETURN); \
481 \
482 TestCode(data, true, 12); \
483 } \
484 \
485 TEST(CodegenTest, Return ## TEST_NAME ## 2addr) { \
486 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
487 Instruction::CONST_4 | 3 << 12 | 0, \
488 Instruction::CONST_4 | 4 << 12 | 1 << 8, \
489 Instruction::MUL_ ## TYPE ## _2ADDR | 1 << 12, \
490 Instruction::RETURN); \
491 \
492 TestCode(data, true, 12); \
493 }
494
495MUL_TEST(INT, MulInt);
496MUL_TEST(LONG, MulLong);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100497
498TEST(CodegenTest, ReturnMulIntLit8) {
499 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
500 Instruction::CONST_4 | 4 << 12 | 0 << 8,
501 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
502 Instruction::RETURN);
503
504 TestCode(data, true, 12);
505}
506
507TEST(CodegenTest, ReturnMulIntLit16) {
508 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
509 Instruction::CONST_4 | 4 << 12 | 0 << 8,
510 Instruction::MUL_INT_LIT16, 3,
511 Instruction::RETURN);
512
513 TestCode(data, true, 12);
514}
515
Alexandre Rames92730742014-10-01 12:55:56 +0100516TEST(CodegenTest, MaterializedCondition1) {
517 // Check that condition are materialized correctly. A materialized condition
518 // should yield `1` if it evaluated to true, and `0` otherwise.
519 // We force the materialization of comparisons for different combinations of
520 // inputs and check the results.
521
522 int lhs[] = {1, 2, -1, 2, 0xabc};
523 int rhs[] = {2, 1, 2, -1, 0xabc};
524
525 for (size_t i = 0; i < arraysize(lhs); i++) {
526 ArenaPool pool;
527 ArenaAllocator allocator(&pool);
528 HGraph* graph = new (&allocator) HGraph(&allocator);
529
530 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
531 graph->AddBlock(entry_block);
532 graph->SetEntryBlock(entry_block);
533 entry_block->AddInstruction(new (&allocator) HGoto());
534 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
535 graph->AddBlock(code_block);
536 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
537 graph->AddBlock(exit_block);
538 exit_block->AddInstruction(new (&allocator) HExit());
539
540 entry_block->AddSuccessor(code_block);
541 code_block->AddSuccessor(exit_block);
542 graph->SetExitBlock(exit_block);
543
544 HIntConstant cst_lhs(lhs[i]);
545 code_block->AddInstruction(&cst_lhs);
546 HIntConstant cst_rhs(rhs[i]);
547 code_block->AddInstruction(&cst_rhs);
548 HLessThan cmp_lt(&cst_lhs, &cst_rhs);
549 code_block->AddInstruction(&cmp_lt);
550 HReturn ret(&cmp_lt);
551 code_block->AddInstruction(&ret);
552
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800553 auto hook_before_codegen = [](HGraph* graph_in) {
554 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
555 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100556 block->InsertInstructionBefore(move, block->GetLastInstruction());
557 };
558
559 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
560 }
561}
562
563TEST(CodegenTest, MaterializedCondition2) {
564 // Check that HIf correctly interprets a materialized condition.
565 // We force the materialization of comparisons for different combinations of
566 // inputs. An HIf takes the materialized combination as input and returns a
567 // value that we verify.
568
569 int lhs[] = {1, 2, -1, 2, 0xabc};
570 int rhs[] = {2, 1, 2, -1, 0xabc};
571
572
573 for (size_t i = 0; i < arraysize(lhs); i++) {
574 ArenaPool pool;
575 ArenaAllocator allocator(&pool);
576 HGraph* graph = new (&allocator) HGraph(&allocator);
577
578 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
579 graph->AddBlock(entry_block);
580 graph->SetEntryBlock(entry_block);
581 entry_block->AddInstruction(new (&allocator) HGoto());
582
583 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
584 graph->AddBlock(if_block);
585 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
586 graph->AddBlock(if_true_block);
587 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
588 graph->AddBlock(if_false_block);
589 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
590 graph->AddBlock(exit_block);
591 exit_block->AddInstruction(new (&allocator) HExit());
592
593 graph->SetEntryBlock(entry_block);
594 entry_block->AddSuccessor(if_block);
595 if_block->AddSuccessor(if_true_block);
596 if_block->AddSuccessor(if_false_block);
597 if_true_block->AddSuccessor(exit_block);
598 if_false_block->AddSuccessor(exit_block);
599 graph->SetExitBlock(exit_block);
600
601 HIntConstant cst_lhs(lhs[i]);
602 if_block->AddInstruction(&cst_lhs);
603 HIntConstant cst_rhs(rhs[i]);
604 if_block->AddInstruction(&cst_rhs);
605 HLessThan cmp_lt(&cst_lhs, &cst_rhs);
606 if_block->AddInstruction(&cmp_lt);
607 // We insert a temporary to separate the HIf from the HLessThan and force
608 // the materialization of the condition.
609 HTemporary force_materialization(0);
610 if_block->AddInstruction(&force_materialization);
611 HIf if_lt(&cmp_lt);
612 if_block->AddInstruction(&if_lt);
613
614 HIntConstant cst_lt(1);
615 if_true_block->AddInstruction(&cst_lt);
616 HReturn ret_lt(&cst_lt);
617 if_true_block->AddInstruction(&ret_lt);
618 HIntConstant cst_ge(0);
619 if_false_block->AddInstruction(&cst_ge);
620 HReturn ret_ge(&cst_ge);
621 if_false_block->AddInstruction(&ret_ge);
622
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800623 auto hook_before_codegen = [](HGraph* graph_in) {
624 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
625 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100626 block->InsertInstructionBefore(move, block->GetLastInstruction());
627 };
628
629 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
630 }
631}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100632
Calin Juravled0d48522014-11-04 16:40:20 +0000633TEST(CodegenTest, ReturnDivIntLit8) {
634 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
635 Instruction::CONST_4 | 4 << 12 | 0 << 8,
636 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
637 Instruction::RETURN);
638
639 TestCode(data, true, 1);
640}
641
Calin Juravle865fc882014-11-06 17:09:03 +0000642TEST(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000643 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
644 Instruction::CONST_4 | 4 << 12 | 0,
645 Instruction::CONST_4 | 2 << 12 | 1 << 8,
646 Instruction::DIV_INT_2ADDR | 1 << 12,
647 Instruction::RETURN);
648
649 TestCode(data, true, 2);
650}
651
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000652} // namespace art