blob: 868fc5b8675fecc433e2e331887cf124b098ee96 [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"
Serban Constantinescu579885a2015-02-22 20:51:33 +000021#include "arch/arm64/instruction_set_features_arm64.h"
Alexandre Rames92730742014-10-01 12:55:56 +010022#include "base/macros.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000023#include "builder.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010024#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "code_generator_arm64.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010026#include "code_generator_x86.h"
27#include "code_generator_x86_64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000029#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000031#include "driver/compiler_options.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000033#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010034#include "prepare_for_register_allocation.h"
35#include "register_allocator.h"
36#include "ssa_liveness_analysis.h"
Roland Levillain55dcfb52014-10-24 18:09:09 +010037#include "utils.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038
39#include "gtest/gtest.h"
Nicolas Geoffraye6362282015-01-26 13:57:30 +000040
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace art {
42
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000043// Provide our own codegen, that ensures the C calling conventions
44// are preserved. Currently, ART and C do not match as R4 is caller-save
45// in ART, and callee-save in C. Alternatively, we could use or write
46// the stub that saves and restores all registers, but it is easier
47// to just overwrite the code generator.
48class TestCodeGeneratorARM : public arm::CodeGeneratorARM {
49 public:
50 TestCodeGeneratorARM(HGraph* graph,
51 const ArmInstructionSetFeatures& isa_features,
52 const CompilerOptions& compiler_options)
53 : arm::CodeGeneratorARM(graph, isa_features, compiler_options) {
54 AddAllocatedRegister(Location::RegisterLocation(6));
55 AddAllocatedRegister(Location::RegisterLocation(7));
56 }
57
58 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
59 arm::CodeGeneratorARM::SetupBlockedRegisters(is_baseline);
60 blocked_core_registers_[4] = true;
61 blocked_core_registers_[6] = false;
62 blocked_core_registers_[7] = false;
Nicolas Geoffraye6362282015-01-26 13:57:30 +000063 // Makes pair R6-R7 available.
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000064 blocked_register_pairs_[6 >> 1] = false;
65 }
66};
67
Nicolas Geoffray787c3072014-03-17 10:20:19 +000068class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000069 public:
Ian Rogersd582fa42014-11-05 23:46:43 -080070 InternalCodeAllocator() : size_(0) { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000071
72 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000073 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000074 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000075 return memory_.get();
76 }
77
Nicolas Geoffray787c3072014-03-17 10:20:19 +000078 size_t GetSize() const { return size_; }
79 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000080
81 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +000082 size_t size_;
Ian Rogers700a4022014-05-19 16:49:03 -070083 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000084
Nicolas Geoffray787c3072014-03-17 10:20:19 +000085 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000086};
87
Roland Levillain55dcfb52014-10-24 18:09:09 +010088template <typename Expected>
Nicolas Geoffray8d486732014-07-16 16:23:40 +010089static void Run(const InternalCodeAllocator& allocator,
90 const CodeGenerator& codegen,
91 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +010092 Expected expected) {
93 typedef Expected (*fptr)();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010094 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -070095 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Nicolas Geoffray8d486732014-07-16 16:23:40 +010096 if (codegen.GetInstructionSet() == kThumb2) {
97 // For thumb we need the bottom bit set.
98 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
99 }
Roland Levillain55dcfb52014-10-24 18:09:09 +0100100 Expected result = f();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100101 if (has_result) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100102 ASSERT_EQ(result, expected);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100103 }
104}
105
Roland Levillain55dcfb52014-10-24 18:09:09 +0100106template <typename Expected>
107static void RunCodeBaseline(HGraph* graph, bool has_result, Expected expected) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000108 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100109
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000110 CompilerOptions compiler_options;
111 x86::CodeGeneratorX86 codegenX86(graph, compiler_options);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100112 // We avoid doing a stack overflow check that requires the runtime being setup,
113 // by making sure the compiler knows the methods we are running are leaf methods.
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100114 codegenX86.CompileBaseline(&allocator, true);
115 if (kRuntimeISA == kX86) {
116 Run(allocator, codegenX86, has_result, expected);
117 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100118
Serban Constantinescu579885a2015-02-22 20:51:33 +0000119 std::unique_ptr<const ArmInstructionSetFeatures> features_arm(
Andreas Gampedf649502015-01-06 14:13:52 -0800120 ArmInstructionSetFeatures::FromCppDefines());
Serban Constantinescu579885a2015-02-22 20:51:33 +0000121 TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100122 codegenARM.CompileBaseline(&allocator, true);
123 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
124 Run(allocator, codegenARM, has_result, expected);
125 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100126
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000127 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100128 codegenX86_64.CompileBaseline(&allocator, true);
129 if (kRuntimeISA == kX86_64) {
130 Run(allocator, codegenX86_64, has_result, expected);
131 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100132
Serban Constantinescu579885a2015-02-22 20:51:33 +0000133 std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64(
134 Arm64InstructionSetFeatures::FromCppDefines());
135 arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100136 codegenARM64.CompileBaseline(&allocator, true);
137 if (kRuntimeISA == kArm64) {
138 Run(allocator, codegenARM64, has_result, expected);
139 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000140}
141
Roland Levillain55dcfb52014-10-24 18:09:09 +0100142template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100143static void RunCodeOptimized(CodeGenerator* codegen,
144 HGraph* graph,
145 std::function<void(HGraph*)> hook_before_codegen,
146 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100147 Expected expected) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100148 SsaLivenessAnalysis liveness(*graph, codegen);
149 liveness.Analyze();
150
151 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
152 register_allocator.AllocateRegisters();
153 hook_before_codegen(graph);
154
155 InternalCodeAllocator allocator;
156 codegen->CompileOptimized(&allocator);
157 Run(allocator, *codegen, has_result, expected);
158}
159
Roland Levillain55dcfb52014-10-24 18:09:09 +0100160template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100161static void RunCodeOptimized(HGraph* graph,
162 std::function<void(HGraph*)> hook_before_codegen,
163 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100164 Expected expected) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000165 CompilerOptions compiler_options;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000166 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000167 TestCodeGeneratorARM codegenARM(graph,
168 *ArmInstructionSetFeatures::FromCppDefines(),
169 compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100170 RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000171 } else if (kRuntimeISA == kArm64) {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000172 arm64::CodeGeneratorARM64 codegenARM64(graph,
173 *Arm64InstructionSetFeatures::FromCppDefines(),
174 compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000175 RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected);
176 } else if (kRuntimeISA == kX86) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000177 x86::CodeGeneratorX86 codegenX86(graph, compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000178 RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100179 } else if (kRuntimeISA == kX86_64) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000180 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100181 RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
182 }
183}
184
185static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
186 ArenaPool pool;
187 ArenaAllocator arena(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +0000188 HGraph* graph = new (&arena) HGraph(&arena);
189 HGraphBuilder builder(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100190 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000191 bool graph_built = builder.BuildGraph(*item);
192 ASSERT_TRUE(graph_built);
Calin Juravle039b6e22014-10-23 12:32:11 +0100193 // Remove suspend checks, they cannot be executed in this context.
Calin Juravle48dee042014-10-22 15:54:12 +0100194 RemoveSuspendChecks(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100195 RunCodeBaseline(graph, has_result, expected);
196}
197
Roland Levillain55dcfb52014-10-24 18:09:09 +0100198static void TestCodeLong(const uint16_t* data, bool has_result, int64_t expected) {
199 ArenaPool pool;
200 ArenaAllocator arena(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +0000201 HGraph* graph = new (&arena) HGraph(&arena);
202 HGraphBuilder builder(graph, Primitive::kPrimLong);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100203 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000204 bool graph_built = builder.BuildGraph(*item);
205 ASSERT_TRUE(graph_built);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100206 // Remove suspend checks, they cannot be executed in this context.
207 RemoveSuspendChecks(graph);
208 RunCodeBaseline(graph, has_result, expected);
209}
210
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000211TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000212 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
213 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000214}
215
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000216TEST(CodegenTest, CFG1) {
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::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000219 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000220
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000221 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000222}
223
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000224TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000226 Instruction::GOTO | 0x100,
227 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000228 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000229
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000230 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000231}
232
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000233TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000234 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000235 Instruction::GOTO | 0x200,
236 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000237 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000238
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000239 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000242 Instruction::GOTO_16, 3,
243 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000244 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000245
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000246 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000247
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000248 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000249 Instruction::GOTO_32, 4, 0,
250 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000251 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000252
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000253 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000254}
255
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000256TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000257 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000258 Instruction::RETURN_VOID,
259 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000260 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000261
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000262 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000263}
264
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000265TEST(CodegenTest, CFG5) {
266 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
267 Instruction::CONST_4 | 0 | 0,
268 Instruction::IF_EQ, 3,
269 Instruction::GOTO | 0x100,
270 Instruction::RETURN_VOID);
271
272 TestCode(data);
273}
274
275TEST(CodegenTest, IntConstant) {
276 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
277 Instruction::CONST_4 | 0 | 0,
278 Instruction::RETURN_VOID);
279
280 TestCode(data);
281}
282
283TEST(CodegenTest, Return1) {
284 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
285 Instruction::CONST_4 | 0 | 0,
286 Instruction::RETURN | 0);
287
288 TestCode(data, true, 0);
289}
290
291TEST(CodegenTest, Return2) {
292 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
293 Instruction::CONST_4 | 0 | 0,
294 Instruction::CONST_4 | 0 | 1 << 8,
295 Instruction::RETURN | 1 << 8);
296
297 TestCode(data, true, 0);
298}
299
300TEST(CodegenTest, Return3) {
301 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
302 Instruction::CONST_4 | 0 | 0,
303 Instruction::CONST_4 | 1 << 8 | 1 << 12,
304 Instruction::RETURN | 1 << 8);
305
306 TestCode(data, true, 1);
307}
308
309TEST(CodegenTest, ReturnIf1) {
310 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
311 Instruction::CONST_4 | 0 | 0,
312 Instruction::CONST_4 | 1 << 8 | 1 << 12,
313 Instruction::IF_EQ, 3,
314 Instruction::RETURN | 0 << 8,
315 Instruction::RETURN | 1 << 8);
316
317 TestCode(data, true, 1);
318}
319
320TEST(CodegenTest, ReturnIf2) {
321 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
322 Instruction::CONST_4 | 0 | 0,
323 Instruction::CONST_4 | 1 << 8 | 1 << 12,
324 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
325 Instruction::RETURN | 0 << 8,
326 Instruction::RETURN | 1 << 8);
327
328 TestCode(data, true, 0);
329}
330
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100331// Exercise bit-wise (one's complement) not-int instruction.
332#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
333TEST(CodegenTest, TEST_NAME) { \
334 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100335 const uint16_t input_lo = Low16Bits(input); \
336 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100337 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
338 Instruction::CONST | 0 << 8, input_lo, input_hi, \
339 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
340 Instruction::RETURN | 1 << 8); \
341 \
342 TestCode(data, true, EXPECTED_OUTPUT); \
343}
344
345NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
346NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
347NOT_INT_TEST(ReturnNotInt0, 0, -1)
348NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100349NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
350NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
351NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
352NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100353
354#undef NOT_INT_TEST
355
Roland Levillain55dcfb52014-10-24 18:09:09 +0100356// Exercise bit-wise (one's complement) not-long instruction.
357#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
358TEST(CodegenTest, TEST_NAME) { \
359 const int64_t input = INPUT; \
360 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
361 const uint16_t word1 = High16Bits(Low32Bits(input)); \
362 const uint16_t word2 = Low16Bits(High32Bits(input)); \
363 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
364 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
365 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
366 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
367 Instruction::RETURN_WIDE | 2 << 8); \
368 \
369 TestCodeLong(data, true, EXPECTED_OUTPUT); \
370}
371
372NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
373NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
374NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
375NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
376
377NOT_LONG_TEST(ReturnNotLongINT32_MIN,
378 INT64_C(-2147483648),
379 INT64_C(2147483647)) // (2^31) - 1
380NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
381 INT64_C(-2147483647),
382 INT64_C(2147483646)) // (2^31) - 2
383NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
384 INT64_C(2147483646),
385 INT64_C(-2147483647)) // -(2^31) - 1
386NOT_LONG_TEST(ReturnNotLongINT32_MAX,
387 INT64_C(2147483647),
388 INT64_C(-2147483648)) // -(2^31)
389
390// Note that the C++ compiler won't accept
391// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
392// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
393NOT_LONG_TEST(ReturnNotINT64_MIN,
394 INT64_C(-9223372036854775807)-1,
395 INT64_C(9223372036854775807)); // (2^63) - 1
396NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
397 INT64_C(-9223372036854775807),
398 INT64_C(9223372036854775806)); // (2^63) - 2
399NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
400 INT64_C(9223372036854775806),
401 INT64_C(-9223372036854775807)); // -(2^63) - 1
402NOT_LONG_TEST(ReturnNotLongINT64_MAX,
403 INT64_C(9223372036854775807),
404 INT64_C(-9223372036854775807)-1); // -(2^63)
405
406#undef NOT_LONG_TEST
407
Roland Levillain946e1432014-11-11 17:35:19 +0000408TEST(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000409 const int64_t input = INT64_C(4294967296); // 2^32
410 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
411 const uint16_t word1 = High16Bits(Low32Bits(input));
412 const uint16_t word2 = Low16Bits(High32Bits(input));
413 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
414 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
415 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
416 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
417 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
418 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
419 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
420 Instruction::RETURN_WIDE | 2 << 8);
421
422 TestCodeLong(data, true, 1);
423}
424
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000425TEST(CodegenTest, ReturnAdd1) {
426 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
427 Instruction::CONST_4 | 3 << 12 | 0,
428 Instruction::CONST_4 | 4 << 12 | 1 << 8,
429 Instruction::ADD_INT, 1 << 8 | 0,
430 Instruction::RETURN);
431
432 TestCode(data, true, 7);
433}
434
435TEST(CodegenTest, ReturnAdd2) {
436 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
437 Instruction::CONST_4 | 3 << 12 | 0,
438 Instruction::CONST_4 | 4 << 12 | 1 << 8,
439 Instruction::ADD_INT_2ADDR | 1 << 12,
440 Instruction::RETURN);
441
442 TestCode(data, true, 7);
443}
444
445TEST(CodegenTest, ReturnAdd3) {
446 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
447 Instruction::CONST_4 | 4 << 12 | 0 << 8,
448 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
449 Instruction::RETURN);
450
451 TestCode(data, true, 7);
452}
453
454TEST(CodegenTest, ReturnAdd4) {
455 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
456 Instruction::CONST_4 | 4 << 12 | 0 << 8,
457 Instruction::ADD_INT_LIT16, 3,
458 Instruction::RETURN);
459
460 TestCode(data, true, 7);
461}
462
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100463TEST(CodegenTest, NonMaterializedCondition) {
464 ArenaPool pool;
465 ArenaAllocator allocator(&pool);
466
467 HGraph* graph = new (&allocator) HGraph(&allocator);
468 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
469 graph->AddBlock(entry);
470 graph->SetEntryBlock(entry);
471 entry->AddInstruction(new (&allocator) HGoto());
472
473 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
474 graph->AddBlock(first_block);
475 entry->AddSuccessor(first_block);
476 HIntConstant* constant0 = new (&allocator) HIntConstant(0);
477 entry->AddInstruction(constant0);
478 HIntConstant* constant1 = new (&allocator) HIntConstant(1);
479 entry->AddInstruction(constant1);
480 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
481 first_block->AddInstruction(equal);
482 first_block->AddInstruction(new (&allocator) HIf(equal));
483
484 HBasicBlock* then = new (&allocator) HBasicBlock(graph);
485 HBasicBlock* else_ = new (&allocator) HBasicBlock(graph);
486 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
487
488 graph->AddBlock(then);
489 graph->AddBlock(else_);
490 graph->AddBlock(exit);
491 first_block->AddSuccessor(then);
492 first_block->AddSuccessor(else_);
493 then->AddSuccessor(exit);
494 else_->AddSuccessor(exit);
495
496 exit->AddInstruction(new (&allocator) HExit());
497 then->AddInstruction(new (&allocator) HReturn(constant0));
498 else_->AddInstruction(new (&allocator) HReturn(constant1));
499
500 ASSERT_TRUE(equal->NeedsMaterialization());
501 graph->BuildDominatorTree();
502 PrepareForRegisterAllocation(graph).Run();
503 ASSERT_FALSE(equal->NeedsMaterialization());
504
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800505 auto hook_before_codegen = [](HGraph* graph_in) {
506 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
507 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100508 block->InsertInstructionBefore(move, block->GetLastInstruction());
509 };
510
511 RunCodeOptimized(graph, hook_before_codegen, true, 0);
512}
513
Calin Juravle34bacdf2014-10-07 20:23:36 +0100514#define MUL_TEST(TYPE, TEST_NAME) \
515 TEST(CodegenTest, Return ## TEST_NAME) { \
516 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
517 Instruction::CONST_4 | 3 << 12 | 0, \
518 Instruction::CONST_4 | 4 << 12 | 1 << 8, \
519 Instruction::MUL_ ## TYPE, 1 << 8 | 0, \
520 Instruction::RETURN); \
521 \
522 TestCode(data, true, 12); \
523 } \
524 \
525 TEST(CodegenTest, Return ## TEST_NAME ## 2addr) { \
526 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
527 Instruction::CONST_4 | 3 << 12 | 0, \
528 Instruction::CONST_4 | 4 << 12 | 1 << 8, \
529 Instruction::MUL_ ## TYPE ## _2ADDR | 1 << 12, \
530 Instruction::RETURN); \
531 \
532 TestCode(data, true, 12); \
533 }
534
535MUL_TEST(INT, MulInt);
536MUL_TEST(LONG, MulLong);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100537
538TEST(CodegenTest, ReturnMulIntLit8) {
539 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
540 Instruction::CONST_4 | 4 << 12 | 0 << 8,
541 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
542 Instruction::RETURN);
543
544 TestCode(data, true, 12);
545}
546
547TEST(CodegenTest, ReturnMulIntLit16) {
548 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
549 Instruction::CONST_4 | 4 << 12 | 0 << 8,
550 Instruction::MUL_INT_LIT16, 3,
551 Instruction::RETURN);
552
553 TestCode(data, true, 12);
554}
555
Alexandre Rames92730742014-10-01 12:55:56 +0100556TEST(CodegenTest, MaterializedCondition1) {
557 // Check that condition are materialized correctly. A materialized condition
558 // should yield `1` if it evaluated to true, and `0` otherwise.
559 // We force the materialization of comparisons for different combinations of
560 // inputs and check the results.
561
562 int lhs[] = {1, 2, -1, 2, 0xabc};
563 int rhs[] = {2, 1, 2, -1, 0xabc};
564
565 for (size_t i = 0; i < arraysize(lhs); i++) {
566 ArenaPool pool;
567 ArenaAllocator allocator(&pool);
568 HGraph* graph = new (&allocator) HGraph(&allocator);
569
570 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
571 graph->AddBlock(entry_block);
572 graph->SetEntryBlock(entry_block);
573 entry_block->AddInstruction(new (&allocator) HGoto());
574 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
575 graph->AddBlock(code_block);
576 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
577 graph->AddBlock(exit_block);
578 exit_block->AddInstruction(new (&allocator) HExit());
579
580 entry_block->AddSuccessor(code_block);
581 code_block->AddSuccessor(exit_block);
582 graph->SetExitBlock(exit_block);
583
584 HIntConstant cst_lhs(lhs[i]);
585 code_block->AddInstruction(&cst_lhs);
586 HIntConstant cst_rhs(rhs[i]);
587 code_block->AddInstruction(&cst_rhs);
588 HLessThan cmp_lt(&cst_lhs, &cst_rhs);
589 code_block->AddInstruction(&cmp_lt);
590 HReturn ret(&cmp_lt);
591 code_block->AddInstruction(&ret);
592
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800593 auto hook_before_codegen = [](HGraph* graph_in) {
594 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
595 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100596 block->InsertInstructionBefore(move, block->GetLastInstruction());
597 };
598
599 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
600 }
601}
602
603TEST(CodegenTest, MaterializedCondition2) {
604 // Check that HIf correctly interprets a materialized condition.
605 // We force the materialization of comparisons for different combinations of
606 // inputs. An HIf takes the materialized combination as input and returns a
607 // value that we verify.
608
609 int lhs[] = {1, 2, -1, 2, 0xabc};
610 int rhs[] = {2, 1, 2, -1, 0xabc};
611
612
613 for (size_t i = 0; i < arraysize(lhs); i++) {
614 ArenaPool pool;
615 ArenaAllocator allocator(&pool);
616 HGraph* graph = new (&allocator) HGraph(&allocator);
617
618 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
619 graph->AddBlock(entry_block);
620 graph->SetEntryBlock(entry_block);
621 entry_block->AddInstruction(new (&allocator) HGoto());
622
623 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
624 graph->AddBlock(if_block);
625 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
626 graph->AddBlock(if_true_block);
627 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
628 graph->AddBlock(if_false_block);
629 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
630 graph->AddBlock(exit_block);
631 exit_block->AddInstruction(new (&allocator) HExit());
632
633 graph->SetEntryBlock(entry_block);
634 entry_block->AddSuccessor(if_block);
635 if_block->AddSuccessor(if_true_block);
636 if_block->AddSuccessor(if_false_block);
637 if_true_block->AddSuccessor(exit_block);
638 if_false_block->AddSuccessor(exit_block);
639 graph->SetExitBlock(exit_block);
640
641 HIntConstant cst_lhs(lhs[i]);
642 if_block->AddInstruction(&cst_lhs);
643 HIntConstant cst_rhs(rhs[i]);
644 if_block->AddInstruction(&cst_rhs);
645 HLessThan cmp_lt(&cst_lhs, &cst_rhs);
646 if_block->AddInstruction(&cmp_lt);
647 // We insert a temporary to separate the HIf from the HLessThan and force
648 // the materialization of the condition.
649 HTemporary force_materialization(0);
650 if_block->AddInstruction(&force_materialization);
651 HIf if_lt(&cmp_lt);
652 if_block->AddInstruction(&if_lt);
653
654 HIntConstant cst_lt(1);
655 if_true_block->AddInstruction(&cst_lt);
656 HReturn ret_lt(&cst_lt);
657 if_true_block->AddInstruction(&ret_lt);
658 HIntConstant cst_ge(0);
659 if_false_block->AddInstruction(&cst_ge);
660 HReturn ret_ge(&cst_ge);
661 if_false_block->AddInstruction(&ret_ge);
662
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800663 auto hook_before_codegen = [](HGraph* graph_in) {
664 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
665 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100666 block->InsertInstructionBefore(move, block->GetLastInstruction());
667 };
668
669 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
670 }
671}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100672
Calin Juravled0d48522014-11-04 16:40:20 +0000673TEST(CodegenTest, ReturnDivIntLit8) {
674 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
675 Instruction::CONST_4 | 4 << 12 | 0 << 8,
676 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
677 Instruction::RETURN);
678
679 TestCode(data, true, 1);
680}
681
Calin Juravle865fc882014-11-06 17:09:03 +0000682TEST(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000683 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
684 Instruction::CONST_4 | 4 << 12 | 0,
685 Instruction::CONST_4 | 2 << 12 | 1 << 8,
686 Instruction::DIV_INT_2ADDR | 1 << 12,
687 Instruction::RETURN);
688
689 TestCode(data, true, 2);
690}
691
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000692} // namespace art