blob: e0e0b4c3e8a20dd72b98842ffc149b9044da9c00 [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"
Calin Juravle34166012014-12-19 17:22:29 +000020#include "arch/arm/instruction_set_features_arm.h"
Alexandre Rames92730742014-10-01 12:55:56 +010021#include "base/macros.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000022#include "builder.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010023#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "code_generator_arm64.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010025#include "code_generator_x86.h"
26#include "code_generator_x86_64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000028#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "driver/compiler_options.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010033#include "prepare_for_register_allocation.h"
34#include "register_allocator.h"
35#include "ssa_liveness_analysis.h"
Roland Levillain55dcfb52014-10-24 18:09:09 +010036#include "utils.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037
38#include "gtest/gtest.h"
Nicolas Geoffraye6362282015-01-26 13:57:30 +000039
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace art {
41
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000042// Provide our own codegen, that ensures the C calling conventions
43// are preserved. Currently, ART and C do not match as R4 is caller-save
44// in ART, and callee-save in C. Alternatively, we could use or write
45// the stub that saves and restores all registers, but it is easier
46// to just overwrite the code generator.
47class TestCodeGeneratorARM : public arm::CodeGeneratorARM {
48 public:
49 TestCodeGeneratorARM(HGraph* graph,
50 const ArmInstructionSetFeatures& isa_features,
51 const CompilerOptions& compiler_options)
52 : arm::CodeGeneratorARM(graph, isa_features, compiler_options) {
53 AddAllocatedRegister(Location::RegisterLocation(6));
54 AddAllocatedRegister(Location::RegisterLocation(7));
55 }
56
57 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
58 arm::CodeGeneratorARM::SetupBlockedRegisters(is_baseline);
59 blocked_core_registers_[4] = true;
60 blocked_core_registers_[6] = false;
61 blocked_core_registers_[7] = false;
Nicolas Geoffraye6362282015-01-26 13:57:30 +000062 // Makes pair R6-R7 available.
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000063 blocked_register_pairs_[6 >> 1] = false;
64 }
65};
66
Nicolas Geoffray787c3072014-03-17 10:20:19 +000067class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000068 public:
Ian Rogersd582fa42014-11-05 23:46:43 -080069 InternalCodeAllocator() : size_(0) { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000070
71 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000072 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000073 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000074 return memory_.get();
75 }
76
Nicolas Geoffray787c3072014-03-17 10:20:19 +000077 size_t GetSize() const { return size_; }
78 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000079
80 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +000081 size_t size_;
Ian Rogers700a4022014-05-19 16:49:03 -070082 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000083
Nicolas Geoffray787c3072014-03-17 10:20:19 +000084 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000085};
86
Roland Levillain55dcfb52014-10-24 18:09:09 +010087template <typename Expected>
Nicolas Geoffray8d486732014-07-16 16:23:40 +010088static void Run(const InternalCodeAllocator& allocator,
89 const CodeGenerator& codegen,
90 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +010091 Expected expected) {
92 typedef Expected (*fptr)();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010093 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -070094 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Nicolas Geoffray8d486732014-07-16 16:23:40 +010095 if (codegen.GetInstructionSet() == kThumb2) {
96 // For thumb we need the bottom bit set.
97 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
98 }
Roland Levillain55dcfb52014-10-24 18:09:09 +010099 Expected result = f();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100100 if (has_result) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100101 ASSERT_EQ(result, expected);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100102 }
103}
104
Roland Levillain55dcfb52014-10-24 18:09:09 +0100105template <typename Expected>
106static void RunCodeBaseline(HGraph* graph, bool has_result, Expected expected) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100108
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000109 CompilerOptions compiler_options;
110 x86::CodeGeneratorX86 codegenX86(graph, compiler_options);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100111 // We avoid doing a stack overflow check that requires the runtime being setup,
112 // by making sure the compiler knows the methods we are running are leaf methods.
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100113 codegenX86.CompileBaseline(&allocator, true);
114 if (kRuntimeISA == kX86) {
115 Run(allocator, codegenX86, has_result, expected);
116 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100117
Andreas Gampedf649502015-01-06 14:13:52 -0800118 std::unique_ptr<const ArmInstructionSetFeatures> features(
119 ArmInstructionSetFeatures::FromCppDefines());
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000120 TestCodeGeneratorARM codegenARM(graph, *features.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100121 codegenARM.CompileBaseline(&allocator, true);
122 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
123 Run(allocator, codegenARM, has_result, expected);
124 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100125
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000126 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100127 codegenX86_64.CompileBaseline(&allocator, true);
128 if (kRuntimeISA == kX86_64) {
129 Run(allocator, codegenX86_64, has_result, expected);
130 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100131
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000132 arm64::CodeGeneratorARM64 codegenARM64(graph, compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100133 codegenARM64.CompileBaseline(&allocator, true);
134 if (kRuntimeISA == kArm64) {
135 Run(allocator, codegenARM64, has_result, expected);
136 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000137}
138
Roland Levillain55dcfb52014-10-24 18:09:09 +0100139template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100140static void RunCodeOptimized(CodeGenerator* codegen,
141 HGraph* graph,
142 std::function<void(HGraph*)> hook_before_codegen,
143 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100144 Expected expected) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100145 SsaLivenessAnalysis liveness(*graph, codegen);
146 liveness.Analyze();
147
148 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
149 register_allocator.AllocateRegisters();
150 hook_before_codegen(graph);
151
152 InternalCodeAllocator allocator;
153 codegen->CompileOptimized(&allocator);
154 Run(allocator, *codegen, has_result, expected);
155}
156
Roland Levillain55dcfb52014-10-24 18:09:09 +0100157template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100158static void RunCodeOptimized(HGraph* graph,
159 std::function<void(HGraph*)> hook_before_codegen,
160 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100161 Expected expected) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000162 CompilerOptions compiler_options;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000163 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000164 TestCodeGeneratorARM codegenARM(graph,
165 *ArmInstructionSetFeatures::FromCppDefines(),
166 compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100167 RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000168 } else if (kRuntimeISA == kArm64) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000169 arm64::CodeGeneratorARM64 codegenARM64(graph, compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000170 RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected);
171 } else if (kRuntimeISA == kX86) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000172 x86::CodeGeneratorX86 codegenX86(graph, compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000173 RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100174 } else if (kRuntimeISA == kX86_64) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000175 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100176 RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
177 }
178}
179
180static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
181 ArenaPool pool;
182 ArenaAllocator arena(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +0000183 HGraph* graph = new (&arena) HGraph(&arena);
184 HGraphBuilder builder(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100185 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000186 bool graph_built = builder.BuildGraph(*item);
187 ASSERT_TRUE(graph_built);
Calin Juravle039b6e22014-10-23 12:32:11 +0100188 // Remove suspend checks, they cannot be executed in this context.
Calin Juravle48dee042014-10-22 15:54:12 +0100189 RemoveSuspendChecks(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100190 RunCodeBaseline(graph, has_result, expected);
191}
192
Roland Levillain55dcfb52014-10-24 18:09:09 +0100193static void TestCodeLong(const uint16_t* data, bool has_result, int64_t expected) {
194 ArenaPool pool;
195 ArenaAllocator arena(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +0000196 HGraph* graph = new (&arena) HGraph(&arena);
197 HGraphBuilder builder(graph, Primitive::kPrimLong);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100198 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000199 bool graph_built = builder.BuildGraph(*item);
200 ASSERT_TRUE(graph_built);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100201 // Remove suspend checks, they cannot be executed in this context.
202 RemoveSuspendChecks(graph);
203 RunCodeBaseline(graph, has_result, expected);
204}
205
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000206TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000207 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
208 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000209}
210
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000211TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000212 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000213 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000214 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000215
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000216 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000217}
218
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000219TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000220 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000221 Instruction::GOTO | 0x100,
222 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000224
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000226}
227
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000228TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000229 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000230 Instruction::GOTO | 0x200,
231 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000232 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000233
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000234 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000235
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000236 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000237 Instruction::GOTO_16, 3,
238 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000239 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000242
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000243 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000244 Instruction::GOTO_32, 4, 0,
245 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000246 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000247
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000248 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000249}
250
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000251TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000252 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000253 Instruction::RETURN_VOID,
254 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000255 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000256
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000257 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000258}
259
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000260TEST(CodegenTest, CFG5) {
261 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
262 Instruction::CONST_4 | 0 | 0,
263 Instruction::IF_EQ, 3,
264 Instruction::GOTO | 0x100,
265 Instruction::RETURN_VOID);
266
267 TestCode(data);
268}
269
270TEST(CodegenTest, IntConstant) {
271 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
272 Instruction::CONST_4 | 0 | 0,
273 Instruction::RETURN_VOID);
274
275 TestCode(data);
276}
277
278TEST(CodegenTest, Return1) {
279 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
280 Instruction::CONST_4 | 0 | 0,
281 Instruction::RETURN | 0);
282
283 TestCode(data, true, 0);
284}
285
286TEST(CodegenTest, Return2) {
287 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
288 Instruction::CONST_4 | 0 | 0,
289 Instruction::CONST_4 | 0 | 1 << 8,
290 Instruction::RETURN | 1 << 8);
291
292 TestCode(data, true, 0);
293}
294
295TEST(CodegenTest, Return3) {
296 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
297 Instruction::CONST_4 | 0 | 0,
298 Instruction::CONST_4 | 1 << 8 | 1 << 12,
299 Instruction::RETURN | 1 << 8);
300
301 TestCode(data, true, 1);
302}
303
304TEST(CodegenTest, ReturnIf1) {
305 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
306 Instruction::CONST_4 | 0 | 0,
307 Instruction::CONST_4 | 1 << 8 | 1 << 12,
308 Instruction::IF_EQ, 3,
309 Instruction::RETURN | 0 << 8,
310 Instruction::RETURN | 1 << 8);
311
312 TestCode(data, true, 1);
313}
314
315TEST(CodegenTest, ReturnIf2) {
316 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
317 Instruction::CONST_4 | 0 | 0,
318 Instruction::CONST_4 | 1 << 8 | 1 << 12,
319 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
320 Instruction::RETURN | 0 << 8,
321 Instruction::RETURN | 1 << 8);
322
323 TestCode(data, true, 0);
324}
325
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100326// Exercise bit-wise (one's complement) not-int instruction.
327#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
328TEST(CodegenTest, TEST_NAME) { \
329 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100330 const uint16_t input_lo = Low16Bits(input); \
331 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100332 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
333 Instruction::CONST | 0 << 8, input_lo, input_hi, \
334 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
335 Instruction::RETURN | 1 << 8); \
336 \
337 TestCode(data, true, EXPECTED_OUTPUT); \
338}
339
340NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
341NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
342NOT_INT_TEST(ReturnNotInt0, 0, -1)
343NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100344NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
345NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
346NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
347NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100348
349#undef NOT_INT_TEST
350
Roland Levillain55dcfb52014-10-24 18:09:09 +0100351// Exercise bit-wise (one's complement) not-long instruction.
352#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
353TEST(CodegenTest, TEST_NAME) { \
354 const int64_t input = INPUT; \
355 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
356 const uint16_t word1 = High16Bits(Low32Bits(input)); \
357 const uint16_t word2 = Low16Bits(High32Bits(input)); \
358 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
359 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
360 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
361 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
362 Instruction::RETURN_WIDE | 2 << 8); \
363 \
364 TestCodeLong(data, true, EXPECTED_OUTPUT); \
365}
366
367NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
368NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
369NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
370NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
371
372NOT_LONG_TEST(ReturnNotLongINT32_MIN,
373 INT64_C(-2147483648),
374 INT64_C(2147483647)) // (2^31) - 1
375NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
376 INT64_C(-2147483647),
377 INT64_C(2147483646)) // (2^31) - 2
378NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
379 INT64_C(2147483646),
380 INT64_C(-2147483647)) // -(2^31) - 1
381NOT_LONG_TEST(ReturnNotLongINT32_MAX,
382 INT64_C(2147483647),
383 INT64_C(-2147483648)) // -(2^31)
384
385// Note that the C++ compiler won't accept
386// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
387// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
388NOT_LONG_TEST(ReturnNotINT64_MIN,
389 INT64_C(-9223372036854775807)-1,
390 INT64_C(9223372036854775807)); // (2^63) - 1
391NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
392 INT64_C(-9223372036854775807),
393 INT64_C(9223372036854775806)); // (2^63) - 2
394NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
395 INT64_C(9223372036854775806),
396 INT64_C(-9223372036854775807)); // -(2^63) - 1
397NOT_LONG_TEST(ReturnNotLongINT64_MAX,
398 INT64_C(9223372036854775807),
399 INT64_C(-9223372036854775807)-1); // -(2^63)
400
401#undef NOT_LONG_TEST
402
Roland Levillain946e1432014-11-11 17:35:19 +0000403TEST(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000404 const int64_t input = INT64_C(4294967296); // 2^32
405 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
406 const uint16_t word1 = High16Bits(Low32Bits(input));
407 const uint16_t word2 = Low16Bits(High32Bits(input));
408 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
409 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
410 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
411 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
412 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
413 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
414 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
415 Instruction::RETURN_WIDE | 2 << 8);
416
417 TestCodeLong(data, true, 1);
418}
419
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000420TEST(CodegenTest, ReturnAdd1) {
421 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
422 Instruction::CONST_4 | 3 << 12 | 0,
423 Instruction::CONST_4 | 4 << 12 | 1 << 8,
424 Instruction::ADD_INT, 1 << 8 | 0,
425 Instruction::RETURN);
426
427 TestCode(data, true, 7);
428}
429
430TEST(CodegenTest, ReturnAdd2) {
431 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
432 Instruction::CONST_4 | 3 << 12 | 0,
433 Instruction::CONST_4 | 4 << 12 | 1 << 8,
434 Instruction::ADD_INT_2ADDR | 1 << 12,
435 Instruction::RETURN);
436
437 TestCode(data, true, 7);
438}
439
440TEST(CodegenTest, ReturnAdd3) {
441 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
442 Instruction::CONST_4 | 4 << 12 | 0 << 8,
443 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
444 Instruction::RETURN);
445
446 TestCode(data, true, 7);
447}
448
449TEST(CodegenTest, ReturnAdd4) {
450 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
451 Instruction::CONST_4 | 4 << 12 | 0 << 8,
452 Instruction::ADD_INT_LIT16, 3,
453 Instruction::RETURN);
454
455 TestCode(data, true, 7);
456}
457
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100458TEST(CodegenTest, NonMaterializedCondition) {
459 ArenaPool pool;
460 ArenaAllocator allocator(&pool);
461
462 HGraph* graph = new (&allocator) HGraph(&allocator);
463 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
464 graph->AddBlock(entry);
465 graph->SetEntryBlock(entry);
466 entry->AddInstruction(new (&allocator) HGoto());
467
468 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
469 graph->AddBlock(first_block);
470 entry->AddSuccessor(first_block);
471 HIntConstant* constant0 = new (&allocator) HIntConstant(0);
472 entry->AddInstruction(constant0);
473 HIntConstant* constant1 = new (&allocator) HIntConstant(1);
474 entry->AddInstruction(constant1);
475 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
476 first_block->AddInstruction(equal);
477 first_block->AddInstruction(new (&allocator) HIf(equal));
478
479 HBasicBlock* then = new (&allocator) HBasicBlock(graph);
480 HBasicBlock* else_ = new (&allocator) HBasicBlock(graph);
481 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
482
483 graph->AddBlock(then);
484 graph->AddBlock(else_);
485 graph->AddBlock(exit);
486 first_block->AddSuccessor(then);
487 first_block->AddSuccessor(else_);
488 then->AddSuccessor(exit);
489 else_->AddSuccessor(exit);
490
491 exit->AddInstruction(new (&allocator) HExit());
492 then->AddInstruction(new (&allocator) HReturn(constant0));
493 else_->AddInstruction(new (&allocator) HReturn(constant1));
494
495 ASSERT_TRUE(equal->NeedsMaterialization());
496 graph->BuildDominatorTree();
497 PrepareForRegisterAllocation(graph).Run();
498 ASSERT_FALSE(equal->NeedsMaterialization());
499
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800500 auto hook_before_codegen = [](HGraph* graph_in) {
501 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
502 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100503 block->InsertInstructionBefore(move, block->GetLastInstruction());
504 };
505
506 RunCodeOptimized(graph, hook_before_codegen, true, 0);
507}
508
Calin Juravle34bacdf2014-10-07 20:23:36 +0100509#define MUL_TEST(TYPE, TEST_NAME) \
510 TEST(CodegenTest, Return ## TEST_NAME) { \
511 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
512 Instruction::CONST_4 | 3 << 12 | 0, \
513 Instruction::CONST_4 | 4 << 12 | 1 << 8, \
514 Instruction::MUL_ ## TYPE, 1 << 8 | 0, \
515 Instruction::RETURN); \
516 \
517 TestCode(data, true, 12); \
518 } \
519 \
520 TEST(CodegenTest, Return ## TEST_NAME ## 2addr) { \
521 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
522 Instruction::CONST_4 | 3 << 12 | 0, \
523 Instruction::CONST_4 | 4 << 12 | 1 << 8, \
524 Instruction::MUL_ ## TYPE ## _2ADDR | 1 << 12, \
525 Instruction::RETURN); \
526 \
527 TestCode(data, true, 12); \
528 }
529
530MUL_TEST(INT, MulInt);
531MUL_TEST(LONG, MulLong);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100532
533TEST(CodegenTest, ReturnMulIntLit8) {
534 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
535 Instruction::CONST_4 | 4 << 12 | 0 << 8,
536 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
537 Instruction::RETURN);
538
539 TestCode(data, true, 12);
540}
541
542TEST(CodegenTest, ReturnMulIntLit16) {
543 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
544 Instruction::CONST_4 | 4 << 12 | 0 << 8,
545 Instruction::MUL_INT_LIT16, 3,
546 Instruction::RETURN);
547
548 TestCode(data, true, 12);
549}
550
Alexandre Rames92730742014-10-01 12:55:56 +0100551TEST(CodegenTest, MaterializedCondition1) {
552 // Check that condition are materialized correctly. A materialized condition
553 // should yield `1` if it evaluated to true, and `0` otherwise.
554 // We force the materialization of comparisons for different combinations of
555 // inputs and check the results.
556
557 int lhs[] = {1, 2, -1, 2, 0xabc};
558 int rhs[] = {2, 1, 2, -1, 0xabc};
559
560 for (size_t i = 0; i < arraysize(lhs); i++) {
561 ArenaPool pool;
562 ArenaAllocator allocator(&pool);
563 HGraph* graph = new (&allocator) HGraph(&allocator);
564
565 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
566 graph->AddBlock(entry_block);
567 graph->SetEntryBlock(entry_block);
568 entry_block->AddInstruction(new (&allocator) HGoto());
569 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
570 graph->AddBlock(code_block);
571 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
572 graph->AddBlock(exit_block);
573 exit_block->AddInstruction(new (&allocator) HExit());
574
575 entry_block->AddSuccessor(code_block);
576 code_block->AddSuccessor(exit_block);
577 graph->SetExitBlock(exit_block);
578
579 HIntConstant cst_lhs(lhs[i]);
580 code_block->AddInstruction(&cst_lhs);
581 HIntConstant cst_rhs(rhs[i]);
582 code_block->AddInstruction(&cst_rhs);
583 HLessThan cmp_lt(&cst_lhs, &cst_rhs);
584 code_block->AddInstruction(&cmp_lt);
585 HReturn ret(&cmp_lt);
586 code_block->AddInstruction(&ret);
587
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800588 auto hook_before_codegen = [](HGraph* graph_in) {
589 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
590 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100591 block->InsertInstructionBefore(move, block->GetLastInstruction());
592 };
593
594 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
595 }
596}
597
598TEST(CodegenTest, MaterializedCondition2) {
599 // Check that HIf correctly interprets a materialized condition.
600 // We force the materialization of comparisons for different combinations of
601 // inputs. An HIf takes the materialized combination as input and returns a
602 // value that we verify.
603
604 int lhs[] = {1, 2, -1, 2, 0xabc};
605 int rhs[] = {2, 1, 2, -1, 0xabc};
606
607
608 for (size_t i = 0; i < arraysize(lhs); i++) {
609 ArenaPool pool;
610 ArenaAllocator allocator(&pool);
611 HGraph* graph = new (&allocator) HGraph(&allocator);
612
613 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
614 graph->AddBlock(entry_block);
615 graph->SetEntryBlock(entry_block);
616 entry_block->AddInstruction(new (&allocator) HGoto());
617
618 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
619 graph->AddBlock(if_block);
620 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
621 graph->AddBlock(if_true_block);
622 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
623 graph->AddBlock(if_false_block);
624 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
625 graph->AddBlock(exit_block);
626 exit_block->AddInstruction(new (&allocator) HExit());
627
628 graph->SetEntryBlock(entry_block);
629 entry_block->AddSuccessor(if_block);
630 if_block->AddSuccessor(if_true_block);
631 if_block->AddSuccessor(if_false_block);
632 if_true_block->AddSuccessor(exit_block);
633 if_false_block->AddSuccessor(exit_block);
634 graph->SetExitBlock(exit_block);
635
636 HIntConstant cst_lhs(lhs[i]);
637 if_block->AddInstruction(&cst_lhs);
638 HIntConstant cst_rhs(rhs[i]);
639 if_block->AddInstruction(&cst_rhs);
640 HLessThan cmp_lt(&cst_lhs, &cst_rhs);
641 if_block->AddInstruction(&cmp_lt);
642 // We insert a temporary to separate the HIf from the HLessThan and force
643 // the materialization of the condition.
644 HTemporary force_materialization(0);
645 if_block->AddInstruction(&force_materialization);
646 HIf if_lt(&cmp_lt);
647 if_block->AddInstruction(&if_lt);
648
649 HIntConstant cst_lt(1);
650 if_true_block->AddInstruction(&cst_lt);
651 HReturn ret_lt(&cst_lt);
652 if_true_block->AddInstruction(&ret_lt);
653 HIntConstant cst_ge(0);
654 if_false_block->AddInstruction(&cst_ge);
655 HReturn ret_ge(&cst_ge);
656 if_false_block->AddInstruction(&ret_ge);
657
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800658 auto hook_before_codegen = [](HGraph* graph_in) {
659 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
660 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100661 block->InsertInstructionBefore(move, block->GetLastInstruction());
662 };
663
664 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
665 }
666}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100667
Calin Juravled0d48522014-11-04 16:40:20 +0000668TEST(CodegenTest, ReturnDivIntLit8) {
669 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
670 Instruction::CONST_4 | 4 << 12 | 0 << 8,
671 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
672 Instruction::RETURN);
673
674 TestCode(data, true, 1);
675}
676
Calin Juravle865fc882014-11-06 17:09:03 +0000677TEST(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000678 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
679 Instruction::CONST_4 | 4 << 12 | 0,
680 Instruction::CONST_4 | 2 << 12 | 1 << 8,
681 Instruction::DIV_INT_2ADDR | 1 << 12,
682 Instruction::RETURN);
683
684 TestCode(data, true, 2);
685}
686
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000687} // namespace art