blob: 2be117bf387747a10ac3553a88a4413898acaa1e [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"
Mark Mendellfb8d2792015-03-31 22:16:59 -040022#include "arch/x86/instruction_set_features_x86.h"
23#include "arch/x86_64/instruction_set_features_x86_64.h"
Alexandre Rames92730742014-10-01 12:55:56 +010024#include "base/macros.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "builder.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010026#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "code_generator_arm64.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010028#include "code_generator_x86.h"
29#include "code_generator_x86_64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000031#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000033#include "driver/compiler_options.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000035#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010036#include "prepare_for_register_allocation.h"
37#include "register_allocator.h"
38#include "ssa_liveness_analysis.h"
Roland Levillain55dcfb52014-10-24 18:09:09 +010039#include "utils.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040
41#include "gtest/gtest.h"
Nicolas Geoffraye6362282015-01-26 13:57:30 +000042
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000043namespace art {
44
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000045// Provide our own codegen, that ensures the C calling conventions
46// are preserved. Currently, ART and C do not match as R4 is caller-save
47// in ART, and callee-save in C. Alternatively, we could use or write
48// the stub that saves and restores all registers, but it is easier
49// to just overwrite the code generator.
50class TestCodeGeneratorARM : public arm::CodeGeneratorARM {
51 public:
52 TestCodeGeneratorARM(HGraph* graph,
53 const ArmInstructionSetFeatures& isa_features,
54 const CompilerOptions& compiler_options)
55 : arm::CodeGeneratorARM(graph, isa_features, compiler_options) {
56 AddAllocatedRegister(Location::RegisterLocation(6));
57 AddAllocatedRegister(Location::RegisterLocation(7));
58 }
59
60 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
61 arm::CodeGeneratorARM::SetupBlockedRegisters(is_baseline);
62 blocked_core_registers_[4] = true;
63 blocked_core_registers_[6] = false;
64 blocked_core_registers_[7] = false;
Nicolas Geoffraye6362282015-01-26 13:57:30 +000065 // Makes pair R6-R7 available.
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000066 blocked_register_pairs_[6 >> 1] = false;
67 }
68};
69
Nicolas Geoffray787c3072014-03-17 10:20:19 +000070class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000071 public:
Ian Rogersd582fa42014-11-05 23:46:43 -080072 InternalCodeAllocator() : size_(0) { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000073
74 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000075 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000077 return memory_.get();
78 }
79
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080 size_t GetSize() const { return size_; }
81 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000082
83 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +000084 size_t size_;
Ian Rogers700a4022014-05-19 16:49:03 -070085 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000086
Nicolas Geoffray787c3072014-03-17 10:20:19 +000087 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000088};
89
Roland Levillain55dcfb52014-10-24 18:09:09 +010090template <typename Expected>
Nicolas Geoffray8d486732014-07-16 16:23:40 +010091static void Run(const InternalCodeAllocator& allocator,
92 const CodeGenerator& codegen,
93 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +010094 Expected expected) {
95 typedef Expected (*fptr)();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010096 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -070097 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Nicolas Geoffray8d486732014-07-16 16:23:40 +010098 if (codegen.GetInstructionSet() == kThumb2) {
99 // For thumb we need the bottom bit set.
100 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
101 }
Roland Levillain55dcfb52014-10-24 18:09:09 +0100102 Expected result = f();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100103 if (has_result) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100104 ASSERT_EQ(result, expected);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100105 }
106}
107
Roland Levillain55dcfb52014-10-24 18:09:09 +0100108template <typename Expected>
109static void RunCodeBaseline(HGraph* graph, bool has_result, Expected expected) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100111
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000112 CompilerOptions compiler_options;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400113 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
114 X86InstructionSetFeatures::FromCppDefines());
115 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100116 // We avoid doing a stack overflow check that requires the runtime being setup,
117 // by making sure the compiler knows the methods we are running are leaf methods.
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100118 codegenX86.CompileBaseline(&allocator, true);
119 if (kRuntimeISA == kX86) {
120 Run(allocator, codegenX86, has_result, expected);
121 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100122
Serban Constantinescu579885a2015-02-22 20:51:33 +0000123 std::unique_ptr<const ArmInstructionSetFeatures> features_arm(
Andreas Gampedf649502015-01-06 14:13:52 -0800124 ArmInstructionSetFeatures::FromCppDefines());
Serban Constantinescu579885a2015-02-22 20:51:33 +0000125 TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100126 codegenARM.CompileBaseline(&allocator, true);
127 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
128 Run(allocator, codegenARM, has_result, expected);
129 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100130
Mark Mendellfb8d2792015-03-31 22:16:59 -0400131 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
132 X86_64InstructionSetFeatures::FromCppDefines());
133 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100134 codegenX86_64.CompileBaseline(&allocator, true);
135 if (kRuntimeISA == kX86_64) {
136 Run(allocator, codegenX86_64, has_result, expected);
137 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100138
Serban Constantinescu579885a2015-02-22 20:51:33 +0000139 std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64(
140 Arm64InstructionSetFeatures::FromCppDefines());
141 arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100142 codegenARM64.CompileBaseline(&allocator, true);
143 if (kRuntimeISA == kArm64) {
144 Run(allocator, codegenARM64, has_result, expected);
145 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000146}
147
Roland Levillain55dcfb52014-10-24 18:09:09 +0100148template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100149static void RunCodeOptimized(CodeGenerator* codegen,
150 HGraph* graph,
151 std::function<void(HGraph*)> hook_before_codegen,
152 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100153 Expected expected) {
David Brazdil10f56cb2015-03-24 18:49:14 +0000154 graph->BuildDominatorTree();
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100155 SsaLivenessAnalysis liveness(*graph, codegen);
156 liveness.Analyze();
157
158 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
159 register_allocator.AllocateRegisters();
160 hook_before_codegen(graph);
161
162 InternalCodeAllocator allocator;
163 codegen->CompileOptimized(&allocator);
164 Run(allocator, *codegen, has_result, expected);
165}
166
Roland Levillain55dcfb52014-10-24 18:09:09 +0100167template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100168static void RunCodeOptimized(HGraph* graph,
169 std::function<void(HGraph*)> hook_before_codegen,
170 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100171 Expected expected) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000172 CompilerOptions compiler_options;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000173 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000174 TestCodeGeneratorARM codegenARM(graph,
175 *ArmInstructionSetFeatures::FromCppDefines(),
176 compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100177 RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000178 } else if (kRuntimeISA == kArm64) {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000179 arm64::CodeGeneratorARM64 codegenARM64(graph,
180 *Arm64InstructionSetFeatures::FromCppDefines(),
181 compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000182 RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected);
183 } else if (kRuntimeISA == kX86) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400184 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
185 X86InstructionSetFeatures::FromCppDefines());
186 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000187 RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100188 } else if (kRuntimeISA == kX86_64) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400189 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
190 X86_64InstructionSetFeatures::FromCppDefines());
191 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100192 RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
193 }
194}
195
196static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
197 ArenaPool pool;
198 ArenaAllocator arena(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +0000199 HGraph* graph = new (&arena) HGraph(&arena);
200 HGraphBuilder builder(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100201 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000202 bool graph_built = builder.BuildGraph(*item);
203 ASSERT_TRUE(graph_built);
Calin Juravle039b6e22014-10-23 12:32:11 +0100204 // Remove suspend checks, they cannot be executed in this context.
Calin Juravle48dee042014-10-22 15:54:12 +0100205 RemoveSuspendChecks(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100206 RunCodeBaseline(graph, has_result, expected);
207}
208
Roland Levillain55dcfb52014-10-24 18:09:09 +0100209static void TestCodeLong(const uint16_t* data, bool has_result, int64_t expected) {
210 ArenaPool pool;
211 ArenaAllocator arena(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +0000212 HGraph* graph = new (&arena) HGraph(&arena);
213 HGraphBuilder builder(graph, Primitive::kPrimLong);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100214 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000215 bool graph_built = builder.BuildGraph(*item);
216 ASSERT_TRUE(graph_built);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100217 // Remove suspend checks, they cannot be executed in this context.
218 RemoveSuspendChecks(graph);
219 RunCodeBaseline(graph, has_result, expected);
220}
221
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000222TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
224 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000225}
226
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000227TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000228 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000229 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000230 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000231
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000232 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000233}
234
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000235TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000236 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000237 Instruction::GOTO | 0x100,
238 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000239 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000242}
243
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000244TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000245 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000246 Instruction::GOTO | 0x200,
247 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000248 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000249
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000250 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000251
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000252 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000253 Instruction::GOTO_16, 3,
254 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000255 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000256
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000257 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000258
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000260 Instruction::GOTO_32, 4, 0,
261 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000262 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000263
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000264 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000265}
266
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000267TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000268 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000269 Instruction::RETURN_VOID,
270 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000271 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000272
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000273 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000274}
275
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000276TEST(CodegenTest, CFG5) {
277 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
278 Instruction::CONST_4 | 0 | 0,
279 Instruction::IF_EQ, 3,
280 Instruction::GOTO | 0x100,
281 Instruction::RETURN_VOID);
282
283 TestCode(data);
284}
285
286TEST(CodegenTest, IntConstant) {
287 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
288 Instruction::CONST_4 | 0 | 0,
289 Instruction::RETURN_VOID);
290
291 TestCode(data);
292}
293
294TEST(CodegenTest, Return1) {
295 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
296 Instruction::CONST_4 | 0 | 0,
297 Instruction::RETURN | 0);
298
299 TestCode(data, true, 0);
300}
301
302TEST(CodegenTest, Return2) {
303 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
304 Instruction::CONST_4 | 0 | 0,
305 Instruction::CONST_4 | 0 | 1 << 8,
306 Instruction::RETURN | 1 << 8);
307
308 TestCode(data, true, 0);
309}
310
311TEST(CodegenTest, Return3) {
312 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
313 Instruction::CONST_4 | 0 | 0,
314 Instruction::CONST_4 | 1 << 8 | 1 << 12,
315 Instruction::RETURN | 1 << 8);
316
317 TestCode(data, true, 1);
318}
319
320TEST(CodegenTest, ReturnIf1) {
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, 3,
325 Instruction::RETURN | 0 << 8,
326 Instruction::RETURN | 1 << 8);
327
328 TestCode(data, true, 1);
329}
330
331TEST(CodegenTest, ReturnIf2) {
332 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
333 Instruction::CONST_4 | 0 | 0,
334 Instruction::CONST_4 | 1 << 8 | 1 << 12,
335 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
336 Instruction::RETURN | 0 << 8,
337 Instruction::RETURN | 1 << 8);
338
339 TestCode(data, true, 0);
340}
341
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100342// Exercise bit-wise (one's complement) not-int instruction.
343#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
344TEST(CodegenTest, TEST_NAME) { \
345 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100346 const uint16_t input_lo = Low16Bits(input); \
347 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100348 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
349 Instruction::CONST | 0 << 8, input_lo, input_hi, \
350 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
351 Instruction::RETURN | 1 << 8); \
352 \
353 TestCode(data, true, EXPECTED_OUTPUT); \
354}
355
356NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
357NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
358NOT_INT_TEST(ReturnNotInt0, 0, -1)
359NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100360NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
361NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
362NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
363NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100364
365#undef NOT_INT_TEST
366
Roland Levillain55dcfb52014-10-24 18:09:09 +0100367// Exercise bit-wise (one's complement) not-long instruction.
368#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
369TEST(CodegenTest, TEST_NAME) { \
370 const int64_t input = INPUT; \
371 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
372 const uint16_t word1 = High16Bits(Low32Bits(input)); \
373 const uint16_t word2 = Low16Bits(High32Bits(input)); \
374 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
375 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
376 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
377 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
378 Instruction::RETURN_WIDE | 2 << 8); \
379 \
380 TestCodeLong(data, true, EXPECTED_OUTPUT); \
381}
382
383NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
384NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
385NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
386NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
387
388NOT_LONG_TEST(ReturnNotLongINT32_MIN,
389 INT64_C(-2147483648),
390 INT64_C(2147483647)) // (2^31) - 1
391NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
392 INT64_C(-2147483647),
393 INT64_C(2147483646)) // (2^31) - 2
394NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
395 INT64_C(2147483646),
396 INT64_C(-2147483647)) // -(2^31) - 1
397NOT_LONG_TEST(ReturnNotLongINT32_MAX,
398 INT64_C(2147483647),
399 INT64_C(-2147483648)) // -(2^31)
400
401// Note that the C++ compiler won't accept
402// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
403// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
404NOT_LONG_TEST(ReturnNotINT64_MIN,
405 INT64_C(-9223372036854775807)-1,
406 INT64_C(9223372036854775807)); // (2^63) - 1
407NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
408 INT64_C(-9223372036854775807),
409 INT64_C(9223372036854775806)); // (2^63) - 2
410NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
411 INT64_C(9223372036854775806),
412 INT64_C(-9223372036854775807)); // -(2^63) - 1
413NOT_LONG_TEST(ReturnNotLongINT64_MAX,
414 INT64_C(9223372036854775807),
415 INT64_C(-9223372036854775807)-1); // -(2^63)
416
417#undef NOT_LONG_TEST
418
Roland Levillain946e1432014-11-11 17:35:19 +0000419TEST(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000420 const int64_t input = INT64_C(4294967296); // 2^32
421 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
422 const uint16_t word1 = High16Bits(Low32Bits(input));
423 const uint16_t word2 = Low16Bits(High32Bits(input));
424 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
425 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
426 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
427 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
428 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
429 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
430 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
431 Instruction::RETURN_WIDE | 2 << 8);
432
433 TestCodeLong(data, true, 1);
434}
435
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000436TEST(CodegenTest, ReturnAdd1) {
437 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
438 Instruction::CONST_4 | 3 << 12 | 0,
439 Instruction::CONST_4 | 4 << 12 | 1 << 8,
440 Instruction::ADD_INT, 1 << 8 | 0,
441 Instruction::RETURN);
442
443 TestCode(data, true, 7);
444}
445
446TEST(CodegenTest, ReturnAdd2) {
447 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
448 Instruction::CONST_4 | 3 << 12 | 0,
449 Instruction::CONST_4 | 4 << 12 | 1 << 8,
450 Instruction::ADD_INT_2ADDR | 1 << 12,
451 Instruction::RETURN);
452
453 TestCode(data, true, 7);
454}
455
456TEST(CodegenTest, ReturnAdd3) {
457 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
458 Instruction::CONST_4 | 4 << 12 | 0 << 8,
459 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
460 Instruction::RETURN);
461
462 TestCode(data, true, 7);
463}
464
465TEST(CodegenTest, ReturnAdd4) {
466 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
467 Instruction::CONST_4 | 4 << 12 | 0 << 8,
468 Instruction::ADD_INT_LIT16, 3,
469 Instruction::RETURN);
470
471 TestCode(data, true, 7);
472}
473
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100474TEST(CodegenTest, NonMaterializedCondition) {
475 ArenaPool pool;
476 ArenaAllocator allocator(&pool);
477
478 HGraph* graph = new (&allocator) HGraph(&allocator);
479 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
480 graph->AddBlock(entry);
481 graph->SetEntryBlock(entry);
482 entry->AddInstruction(new (&allocator) HGoto());
483
484 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
485 graph->AddBlock(first_block);
486 entry->AddSuccessor(first_block);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000487 HIntConstant* constant0 = graph->GetIntConstant(0);
488 HIntConstant* constant1 = graph->GetIntConstant(1);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100489 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
490 first_block->AddInstruction(equal);
491 first_block->AddInstruction(new (&allocator) HIf(equal));
492
493 HBasicBlock* then = new (&allocator) HBasicBlock(graph);
494 HBasicBlock* else_ = new (&allocator) HBasicBlock(graph);
495 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
496
497 graph->AddBlock(then);
498 graph->AddBlock(else_);
499 graph->AddBlock(exit);
500 first_block->AddSuccessor(then);
501 first_block->AddSuccessor(else_);
502 then->AddSuccessor(exit);
503 else_->AddSuccessor(exit);
504
505 exit->AddInstruction(new (&allocator) HExit());
506 then->AddInstruction(new (&allocator) HReturn(constant0));
507 else_->AddInstruction(new (&allocator) HReturn(constant1));
508
509 ASSERT_TRUE(equal->NeedsMaterialization());
510 graph->BuildDominatorTree();
511 PrepareForRegisterAllocation(graph).Run();
512 ASSERT_FALSE(equal->NeedsMaterialization());
513
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800514 auto hook_before_codegen = [](HGraph* graph_in) {
515 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
516 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100517 block->InsertInstructionBefore(move, block->GetLastInstruction());
518 };
519
520 RunCodeOptimized(graph, hook_before_codegen, true, 0);
521}
522
Calin Juravle34bacdf2014-10-07 20:23:36 +0100523#define MUL_TEST(TYPE, TEST_NAME) \
524 TEST(CodegenTest, Return ## TEST_NAME) { \
525 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
526 Instruction::CONST_4 | 3 << 12 | 0, \
527 Instruction::CONST_4 | 4 << 12 | 1 << 8, \
528 Instruction::MUL_ ## TYPE, 1 << 8 | 0, \
529 Instruction::RETURN); \
530 \
531 TestCode(data, true, 12); \
532 } \
533 \
534 TEST(CodegenTest, Return ## TEST_NAME ## 2addr) { \
535 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
536 Instruction::CONST_4 | 3 << 12 | 0, \
537 Instruction::CONST_4 | 4 << 12 | 1 << 8, \
538 Instruction::MUL_ ## TYPE ## _2ADDR | 1 << 12, \
539 Instruction::RETURN); \
540 \
541 TestCode(data, true, 12); \
542 }
543
544MUL_TEST(INT, MulInt);
545MUL_TEST(LONG, MulLong);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100546
547TEST(CodegenTest, ReturnMulIntLit8) {
548 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
549 Instruction::CONST_4 | 4 << 12 | 0 << 8,
550 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
551 Instruction::RETURN);
552
553 TestCode(data, true, 12);
554}
555
556TEST(CodegenTest, ReturnMulIntLit16) {
557 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
558 Instruction::CONST_4 | 4 << 12 | 0 << 8,
559 Instruction::MUL_INT_LIT16, 3,
560 Instruction::RETURN);
561
562 TestCode(data, true, 12);
563}
564
Alexandre Rames92730742014-10-01 12:55:56 +0100565TEST(CodegenTest, MaterializedCondition1) {
566 // Check that condition are materialized correctly. A materialized condition
567 // should yield `1` if it evaluated to true, and `0` otherwise.
568 // We force the materialization of comparisons for different combinations of
569 // inputs and check the results.
570
571 int lhs[] = {1, 2, -1, 2, 0xabc};
572 int rhs[] = {2, 1, 2, -1, 0xabc};
573
574 for (size_t i = 0; i < arraysize(lhs); i++) {
575 ArenaPool pool;
576 ArenaAllocator allocator(&pool);
577 HGraph* graph = new (&allocator) HGraph(&allocator);
578
579 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
580 graph->AddBlock(entry_block);
581 graph->SetEntryBlock(entry_block);
582 entry_block->AddInstruction(new (&allocator) HGoto());
583 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
584 graph->AddBlock(code_block);
585 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
586 graph->AddBlock(exit_block);
587 exit_block->AddInstruction(new (&allocator) HExit());
588
589 entry_block->AddSuccessor(code_block);
590 code_block->AddSuccessor(exit_block);
591 graph->SetExitBlock(exit_block);
592
David Brazdil8d5b8b22015-03-24 10:51:52 +0000593 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
594 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
595 HLessThan cmp_lt(cst_lhs, cst_rhs);
Alexandre Rames92730742014-10-01 12:55:56 +0100596 code_block->AddInstruction(&cmp_lt);
597 HReturn ret(&cmp_lt);
598 code_block->AddInstruction(&ret);
599
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800600 auto hook_before_codegen = [](HGraph* graph_in) {
601 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
602 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100603 block->InsertInstructionBefore(move, block->GetLastInstruction());
604 };
605
606 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
607 }
608}
609
610TEST(CodegenTest, MaterializedCondition2) {
611 // Check that HIf correctly interprets a materialized condition.
612 // We force the materialization of comparisons for different combinations of
613 // inputs. An HIf takes the materialized combination as input and returns a
614 // value that we verify.
615
616 int lhs[] = {1, 2, -1, 2, 0xabc};
617 int rhs[] = {2, 1, 2, -1, 0xabc};
618
619
620 for (size_t i = 0; i < arraysize(lhs); i++) {
621 ArenaPool pool;
622 ArenaAllocator allocator(&pool);
623 HGraph* graph = new (&allocator) HGraph(&allocator);
624
625 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
626 graph->AddBlock(entry_block);
627 graph->SetEntryBlock(entry_block);
628 entry_block->AddInstruction(new (&allocator) HGoto());
629
630 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
631 graph->AddBlock(if_block);
632 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
633 graph->AddBlock(if_true_block);
634 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
635 graph->AddBlock(if_false_block);
636 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
637 graph->AddBlock(exit_block);
638 exit_block->AddInstruction(new (&allocator) HExit());
639
640 graph->SetEntryBlock(entry_block);
641 entry_block->AddSuccessor(if_block);
642 if_block->AddSuccessor(if_true_block);
643 if_block->AddSuccessor(if_false_block);
644 if_true_block->AddSuccessor(exit_block);
645 if_false_block->AddSuccessor(exit_block);
646 graph->SetExitBlock(exit_block);
647
David Brazdil8d5b8b22015-03-24 10:51:52 +0000648 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
649 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
650 HLessThan cmp_lt(cst_lhs, cst_rhs);
Alexandre Rames92730742014-10-01 12:55:56 +0100651 if_block->AddInstruction(&cmp_lt);
652 // We insert a temporary to separate the HIf from the HLessThan and force
653 // the materialization of the condition.
654 HTemporary force_materialization(0);
655 if_block->AddInstruction(&force_materialization);
656 HIf if_lt(&cmp_lt);
657 if_block->AddInstruction(&if_lt);
658
David Brazdil8d5b8b22015-03-24 10:51:52 +0000659 HIntConstant* cst_lt = graph->GetIntConstant(1);
660 HReturn ret_lt(cst_lt);
Alexandre Rames92730742014-10-01 12:55:56 +0100661 if_true_block->AddInstruction(&ret_lt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000662 HIntConstant* cst_ge = graph->GetIntConstant(0);
663 HReturn ret_ge(cst_ge);
Alexandre Rames92730742014-10-01 12:55:56 +0100664 if_false_block->AddInstruction(&ret_ge);
665
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800666 auto hook_before_codegen = [](HGraph* graph_in) {
667 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
668 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100669 block->InsertInstructionBefore(move, block->GetLastInstruction());
670 };
671
672 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
673 }
674}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100675
Calin Juravled0d48522014-11-04 16:40:20 +0000676TEST(CodegenTest, ReturnDivIntLit8) {
677 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
678 Instruction::CONST_4 | 4 << 12 | 0 << 8,
679 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
680 Instruction::RETURN);
681
682 TestCode(data, true, 1);
683}
684
Calin Juravle865fc882014-11-06 17:09:03 +0000685TEST(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000686 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
687 Instruction::CONST_4 | 4 << 12 | 0,
688 Instruction::CONST_4 | 2 << 12 | 1 << 8,
689 Instruction::DIV_INT_2ADDR | 1 << 12,
690 Instruction::RETURN);
691
692 TestCode(data, true, 2);
693}
694
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000695} // namespace art