blob: 94f56e5d3e809098f71d9305e437a7b21e2ced2d [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"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010021#include "arch/arm/registers_arm.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000022#include "arch/arm64/instruction_set_features_arm64.h"
Mark Mendellfb8d2792015-03-31 22:16:59 -040023#include "arch/x86/instruction_set_features_x86.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010024#include "arch/x86/registers_x86.h"
Mark Mendellfb8d2792015-03-31 22:16:59 -040025#include "arch/x86_64/instruction_set_features_x86_64.h"
Alexandre Rames92730742014-10-01 12:55:56 +010026#include "base/macros.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "builder.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010028#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "code_generator_arm64.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010030#include "code_generator_x86.h"
31#include "code_generator_x86_64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000033#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000035#include "driver/compiler_options.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000037#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010038#include "prepare_for_register_allocation.h"
39#include "register_allocator.h"
40#include "ssa_liveness_analysis.h"
Roland Levillain55dcfb52014-10-24 18:09:09 +010041#include "utils.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010042#include "utils/arm/managed_register_arm.h"
43#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000044
45#include "gtest/gtest.h"
Nicolas Geoffraye6362282015-01-26 13:57:30 +000046
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000047namespace art {
48
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000049// Provide our own codegen, that ensures the C calling conventions
50// are preserved. Currently, ART and C do not match as R4 is caller-save
51// in ART, and callee-save in C. Alternatively, we could use or write
52// the stub that saves and restores all registers, but it is easier
53// to just overwrite the code generator.
54class TestCodeGeneratorARM : public arm::CodeGeneratorARM {
55 public:
56 TestCodeGeneratorARM(HGraph* graph,
57 const ArmInstructionSetFeatures& isa_features,
58 const CompilerOptions& compiler_options)
59 : arm::CodeGeneratorARM(graph, isa_features, compiler_options) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +010060 AddAllocatedRegister(Location::RegisterLocation(arm::R6));
61 AddAllocatedRegister(Location::RegisterLocation(arm::R7));
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000062 }
63
64 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
65 arm::CodeGeneratorARM::SetupBlockedRegisters(is_baseline);
Nicolas Geoffray5da21802015-04-20 09:29:18 +010066 blocked_core_registers_[arm::R4] = true;
67 blocked_core_registers_[arm::R6] = false;
68 blocked_core_registers_[arm::R7] = false;
Nicolas Geoffraye6362282015-01-26 13:57:30 +000069 // Makes pair R6-R7 available.
Nicolas Geoffray5da21802015-04-20 09:29:18 +010070 blocked_register_pairs_[arm::R6_R7] = false;
71 }
72};
73
74class TestCodeGeneratorX86 : public x86::CodeGeneratorX86 {
75 public:
76 TestCodeGeneratorX86(HGraph* graph,
77 const X86InstructionSetFeatures& isa_features,
78 const CompilerOptions& compiler_options)
79 : x86::CodeGeneratorX86(graph, isa_features, compiler_options) {
80 // Save edi, we need it for getting enough registers for long multiplication.
81 AddAllocatedRegister(Location::RegisterLocation(x86::EDI));
82 }
83
84 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
85 x86::CodeGeneratorX86::SetupBlockedRegisters(is_baseline);
86 // ebx is a callee-save register in C, but caller-save for ART.
87 blocked_core_registers_[x86::EBX] = true;
88 blocked_register_pairs_[x86::EAX_EBX] = true;
89 blocked_register_pairs_[x86::EDX_EBX] = true;
90 blocked_register_pairs_[x86::ECX_EBX] = true;
91 blocked_register_pairs_[x86::EBX_EDI] = true;
92
93 // Make edi available.
94 blocked_core_registers_[x86::EDI] = false;
95 blocked_register_pairs_[x86::ECX_EDI] = false;
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000096 }
97};
98
Nicolas Geoffray787c3072014-03-17 10:20:19 +000099class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000100 public:
Ian Rogersd582fa42014-11-05 23:46:43 -0800101 InternalCodeAllocator() : size_(0) { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102
103 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000104 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000106 return memory_.get();
107 }
108
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000109 size_t GetSize() const { return size_; }
110 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000111
112 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000113 size_t size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700114 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000115
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000117};
118
Roland Levillain55dcfb52014-10-24 18:09:09 +0100119template <typename Expected>
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100120static void Run(const InternalCodeAllocator& allocator,
121 const CodeGenerator& codegen,
122 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100123 Expected expected) {
124 typedef Expected (*fptr)();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100125 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -0700126 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100127 if (codegen.GetInstructionSet() == kThumb2) {
128 // For thumb we need the bottom bit set.
129 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
130 }
Roland Levillain55dcfb52014-10-24 18:09:09 +0100131 Expected result = f();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100132 if (has_result) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100133 ASSERT_EQ(expected, result);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100134 }
135}
136
Roland Levillain55dcfb52014-10-24 18:09:09 +0100137template <typename Expected>
138static void RunCodeBaseline(HGraph* graph, bool has_result, Expected expected) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100140
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000141 CompilerOptions compiler_options;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400142 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
143 X86InstructionSetFeatures::FromCppDefines());
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100144 TestCodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100145 // We avoid doing a stack overflow check that requires the runtime being setup,
146 // by making sure the compiler knows the methods we are running are leaf methods.
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100147 codegenX86.CompileBaseline(&allocator, true);
148 if (kRuntimeISA == kX86) {
149 Run(allocator, codegenX86, has_result, expected);
150 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100151
Serban Constantinescu579885a2015-02-22 20:51:33 +0000152 std::unique_ptr<const ArmInstructionSetFeatures> features_arm(
Andreas Gampedf649502015-01-06 14:13:52 -0800153 ArmInstructionSetFeatures::FromCppDefines());
Serban Constantinescu579885a2015-02-22 20:51:33 +0000154 TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100155 codegenARM.CompileBaseline(&allocator, true);
156 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
157 Run(allocator, codegenARM, has_result, expected);
158 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100159
Mark Mendellfb8d2792015-03-31 22:16:59 -0400160 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
161 X86_64InstructionSetFeatures::FromCppDefines());
162 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100163 codegenX86_64.CompileBaseline(&allocator, true);
164 if (kRuntimeISA == kX86_64) {
165 Run(allocator, codegenX86_64, has_result, expected);
166 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100167
Serban Constantinescu579885a2015-02-22 20:51:33 +0000168 std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64(
169 Arm64InstructionSetFeatures::FromCppDefines());
170 arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100171 codegenARM64.CompileBaseline(&allocator, true);
172 if (kRuntimeISA == kArm64) {
173 Run(allocator, codegenARM64, has_result, expected);
174 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175}
176
Roland Levillain55dcfb52014-10-24 18:09:09 +0100177template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100178static void RunCodeOptimized(CodeGenerator* codegen,
179 HGraph* graph,
180 std::function<void(HGraph*)> hook_before_codegen,
181 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100182 Expected expected) {
David Brazdil10f56cb2015-03-24 18:49:14 +0000183 graph->BuildDominatorTree();
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100184 SsaLivenessAnalysis liveness(graph, codegen);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100185 liveness.Analyze();
186
187 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
188 register_allocator.AllocateRegisters();
189 hook_before_codegen(graph);
190
191 InternalCodeAllocator allocator;
192 codegen->CompileOptimized(&allocator);
193 Run(allocator, *codegen, has_result, expected);
194}
195
Roland Levillain55dcfb52014-10-24 18:09:09 +0100196template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100197static void RunCodeOptimized(HGraph* graph,
198 std::function<void(HGraph*)> hook_before_codegen,
199 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100200 Expected expected) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000201 CompilerOptions compiler_options;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000202 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000203 TestCodeGeneratorARM codegenARM(graph,
204 *ArmInstructionSetFeatures::FromCppDefines(),
205 compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100206 RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000207 } else if (kRuntimeISA == kArm64) {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000208 arm64::CodeGeneratorARM64 codegenARM64(graph,
209 *Arm64InstructionSetFeatures::FromCppDefines(),
210 compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000211 RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected);
212 } else if (kRuntimeISA == kX86) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400213 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
214 X86InstructionSetFeatures::FromCppDefines());
215 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000216 RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100217 } else if (kRuntimeISA == kX86_64) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400218 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
219 X86_64InstructionSetFeatures::FromCppDefines());
220 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100221 RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
222 }
223}
224
225static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
226 ArenaPool pool;
227 ArenaAllocator arena(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +0000228 HGraph* graph = new (&arena) HGraph(&arena);
229 HGraphBuilder builder(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100230 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000231 bool graph_built = builder.BuildGraph(*item);
232 ASSERT_TRUE(graph_built);
Calin Juravle039b6e22014-10-23 12:32:11 +0100233 // Remove suspend checks, they cannot be executed in this context.
Calin Juravle48dee042014-10-22 15:54:12 +0100234 RemoveSuspendChecks(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100235 RunCodeBaseline(graph, has_result, expected);
236}
237
Roland Levillain55dcfb52014-10-24 18:09:09 +0100238static void TestCodeLong(const uint16_t* data, bool has_result, int64_t expected) {
239 ArenaPool pool;
240 ArenaAllocator arena(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +0000241 HGraph* graph = new (&arena) HGraph(&arena);
242 HGraphBuilder builder(graph, Primitive::kPrimLong);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100243 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000244 bool graph_built = builder.BuildGraph(*item);
245 ASSERT_TRUE(graph_built);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100246 // Remove suspend checks, they cannot be executed in this context.
247 RemoveSuspendChecks(graph);
248 RunCodeBaseline(graph, has_result, expected);
249}
250
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000251TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000252 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
253 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000254}
255
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000256TEST(CodegenTest, CFG1) {
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::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000260
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000261 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000262}
263
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000264TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000265 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000266 Instruction::GOTO | 0x100,
267 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000268 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000269
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000270 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000271}
272
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000273TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000274 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000275 Instruction::GOTO | 0x200,
276 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000277 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000278
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000279 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000280
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000281 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000282 Instruction::GOTO_16, 3,
283 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000284 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000285
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000286 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000287
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000288 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000289 Instruction::GOTO_32, 4, 0,
290 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000291 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000292
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000293 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000294}
295
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000296TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000297 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000298 Instruction::RETURN_VOID,
299 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000300 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000301
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000302 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000303}
304
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000305TEST(CodegenTest, CFG5) {
306 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
307 Instruction::CONST_4 | 0 | 0,
308 Instruction::IF_EQ, 3,
309 Instruction::GOTO | 0x100,
310 Instruction::RETURN_VOID);
311
312 TestCode(data);
313}
314
315TEST(CodegenTest, IntConstant) {
316 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
317 Instruction::CONST_4 | 0 | 0,
318 Instruction::RETURN_VOID);
319
320 TestCode(data);
321}
322
323TEST(CodegenTest, Return1) {
324 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
325 Instruction::CONST_4 | 0 | 0,
326 Instruction::RETURN | 0);
327
328 TestCode(data, true, 0);
329}
330
331TEST(CodegenTest, Return2) {
332 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
333 Instruction::CONST_4 | 0 | 0,
334 Instruction::CONST_4 | 0 | 1 << 8,
335 Instruction::RETURN | 1 << 8);
336
337 TestCode(data, true, 0);
338}
339
340TEST(CodegenTest, Return3) {
341 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
342 Instruction::CONST_4 | 0 | 0,
343 Instruction::CONST_4 | 1 << 8 | 1 << 12,
344 Instruction::RETURN | 1 << 8);
345
346 TestCode(data, true, 1);
347}
348
349TEST(CodegenTest, ReturnIf1) {
350 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
351 Instruction::CONST_4 | 0 | 0,
352 Instruction::CONST_4 | 1 << 8 | 1 << 12,
353 Instruction::IF_EQ, 3,
354 Instruction::RETURN | 0 << 8,
355 Instruction::RETURN | 1 << 8);
356
357 TestCode(data, true, 1);
358}
359
360TEST(CodegenTest, ReturnIf2) {
361 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
362 Instruction::CONST_4 | 0 | 0,
363 Instruction::CONST_4 | 1 << 8 | 1 << 12,
364 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
365 Instruction::RETURN | 0 << 8,
366 Instruction::RETURN | 1 << 8);
367
368 TestCode(data, true, 0);
369}
370
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100371// Exercise bit-wise (one's complement) not-int instruction.
372#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
373TEST(CodegenTest, TEST_NAME) { \
374 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100375 const uint16_t input_lo = Low16Bits(input); \
376 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100377 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
378 Instruction::CONST | 0 << 8, input_lo, input_hi, \
379 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
380 Instruction::RETURN | 1 << 8); \
381 \
382 TestCode(data, true, EXPECTED_OUTPUT); \
383}
384
385NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
386NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
387NOT_INT_TEST(ReturnNotInt0, 0, -1)
388NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100389NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
390NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
391NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
392NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100393
394#undef NOT_INT_TEST
395
Roland Levillain55dcfb52014-10-24 18:09:09 +0100396// Exercise bit-wise (one's complement) not-long instruction.
397#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
398TEST(CodegenTest, TEST_NAME) { \
399 const int64_t input = INPUT; \
400 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
401 const uint16_t word1 = High16Bits(Low32Bits(input)); \
402 const uint16_t word2 = Low16Bits(High32Bits(input)); \
403 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
404 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
405 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
406 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
407 Instruction::RETURN_WIDE | 2 << 8); \
408 \
409 TestCodeLong(data, true, EXPECTED_OUTPUT); \
410}
411
412NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
413NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
414NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
415NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
416
417NOT_LONG_TEST(ReturnNotLongINT32_MIN,
418 INT64_C(-2147483648),
419 INT64_C(2147483647)) // (2^31) - 1
420NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
421 INT64_C(-2147483647),
422 INT64_C(2147483646)) // (2^31) - 2
423NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
424 INT64_C(2147483646),
425 INT64_C(-2147483647)) // -(2^31) - 1
426NOT_LONG_TEST(ReturnNotLongINT32_MAX,
427 INT64_C(2147483647),
428 INT64_C(-2147483648)) // -(2^31)
429
430// Note that the C++ compiler won't accept
431// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
432// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
433NOT_LONG_TEST(ReturnNotINT64_MIN,
434 INT64_C(-9223372036854775807)-1,
435 INT64_C(9223372036854775807)); // (2^63) - 1
436NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
437 INT64_C(-9223372036854775807),
438 INT64_C(9223372036854775806)); // (2^63) - 2
439NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
440 INT64_C(9223372036854775806),
441 INT64_C(-9223372036854775807)); // -(2^63) - 1
442NOT_LONG_TEST(ReturnNotLongINT64_MAX,
443 INT64_C(9223372036854775807),
444 INT64_C(-9223372036854775807)-1); // -(2^63)
445
446#undef NOT_LONG_TEST
447
Roland Levillain946e1432014-11-11 17:35:19 +0000448TEST(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000449 const int64_t input = INT64_C(4294967296); // 2^32
450 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
451 const uint16_t word1 = High16Bits(Low32Bits(input));
452 const uint16_t word2 = Low16Bits(High32Bits(input));
453 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
454 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
455 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
456 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
457 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
458 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
459 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
460 Instruction::RETURN_WIDE | 2 << 8);
461
462 TestCodeLong(data, true, 1);
463}
464
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000465TEST(CodegenTest, ReturnAdd1) {
466 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
467 Instruction::CONST_4 | 3 << 12 | 0,
468 Instruction::CONST_4 | 4 << 12 | 1 << 8,
469 Instruction::ADD_INT, 1 << 8 | 0,
470 Instruction::RETURN);
471
472 TestCode(data, true, 7);
473}
474
475TEST(CodegenTest, ReturnAdd2) {
476 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
477 Instruction::CONST_4 | 3 << 12 | 0,
478 Instruction::CONST_4 | 4 << 12 | 1 << 8,
479 Instruction::ADD_INT_2ADDR | 1 << 12,
480 Instruction::RETURN);
481
482 TestCode(data, true, 7);
483}
484
485TEST(CodegenTest, ReturnAdd3) {
486 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
487 Instruction::CONST_4 | 4 << 12 | 0 << 8,
488 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
489 Instruction::RETURN);
490
491 TestCode(data, true, 7);
492}
493
494TEST(CodegenTest, ReturnAdd4) {
495 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
496 Instruction::CONST_4 | 4 << 12 | 0 << 8,
497 Instruction::ADD_INT_LIT16, 3,
498 Instruction::RETURN);
499
500 TestCode(data, true, 7);
501}
502
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100503TEST(CodegenTest, NonMaterializedCondition) {
504 ArenaPool pool;
505 ArenaAllocator allocator(&pool);
506
507 HGraph* graph = new (&allocator) HGraph(&allocator);
508 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
509 graph->AddBlock(entry);
510 graph->SetEntryBlock(entry);
511 entry->AddInstruction(new (&allocator) HGoto());
512
513 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
514 graph->AddBlock(first_block);
515 entry->AddSuccessor(first_block);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000516 HIntConstant* constant0 = graph->GetIntConstant(0);
517 HIntConstant* constant1 = graph->GetIntConstant(1);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
519 first_block->AddInstruction(equal);
520 first_block->AddInstruction(new (&allocator) HIf(equal));
521
522 HBasicBlock* then = new (&allocator) HBasicBlock(graph);
523 HBasicBlock* else_ = new (&allocator) HBasicBlock(graph);
524 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
525
526 graph->AddBlock(then);
527 graph->AddBlock(else_);
528 graph->AddBlock(exit);
529 first_block->AddSuccessor(then);
530 first_block->AddSuccessor(else_);
531 then->AddSuccessor(exit);
532 else_->AddSuccessor(exit);
533
534 exit->AddInstruction(new (&allocator) HExit());
535 then->AddInstruction(new (&allocator) HReturn(constant0));
536 else_->AddInstruction(new (&allocator) HReturn(constant1));
537
538 ASSERT_TRUE(equal->NeedsMaterialization());
539 graph->BuildDominatorTree();
540 PrepareForRegisterAllocation(graph).Run();
541 ASSERT_FALSE(equal->NeedsMaterialization());
542
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800543 auto hook_before_codegen = [](HGraph* graph_in) {
544 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
545 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100546 block->InsertInstructionBefore(move, block->GetLastInstruction());
547 };
548
549 RunCodeOptimized(graph, hook_before_codegen, true, 0);
550}
551
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100552TEST(CodegenTest, ReturnMulInt) {
553 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
554 Instruction::CONST_4 | 3 << 12 | 0,
555 Instruction::CONST_4 | 4 << 12 | 1 << 8,
556 Instruction::MUL_INT, 1 << 8 | 0,
557 Instruction::RETURN);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100558
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100559 TestCode(data, true, 12);
560}
561
562TEST(CodegenTest, ReturnMulInt2addr) {
563 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
564 Instruction::CONST_4 | 3 << 12 | 0,
565 Instruction::CONST_4 | 4 << 12 | 1 << 8,
566 Instruction::MUL_INT_2ADDR | 1 << 12,
567 Instruction::RETURN);
568
569 TestCode(data, true, 12);
570}
571
572TEST(CodegenTest, ReturnMulLong) {
573 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
574 Instruction::CONST_4 | 3 << 12 | 0,
575 Instruction::CONST_4 | 0 << 12 | 1 << 8,
576 Instruction::CONST_4 | 4 << 12 | 2 << 8,
577 Instruction::CONST_4 | 0 << 12 | 3 << 8,
578 Instruction::MUL_LONG, 2 << 8 | 0,
579 Instruction::RETURN_WIDE);
580
581 TestCodeLong(data, true, 12);
582}
583
584TEST(CodegenTest, ReturnMulLong2addr) {
585 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
586 Instruction::CONST_4 | 3 << 12 | 0 << 8,
587 Instruction::CONST_4 | 0 << 12 | 1 << 8,
588 Instruction::CONST_4 | 4 << 12 | 2 << 8,
589 Instruction::CONST_4 | 0 << 12 | 3 << 8,
590 Instruction::MUL_LONG_2ADDR | 2 << 12,
591 Instruction::RETURN_WIDE);
592
593 TestCodeLong(data, true, 12);
594}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100595
596TEST(CodegenTest, ReturnMulIntLit8) {
597 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
598 Instruction::CONST_4 | 4 << 12 | 0 << 8,
599 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
600 Instruction::RETURN);
601
602 TestCode(data, true, 12);
603}
604
605TEST(CodegenTest, ReturnMulIntLit16) {
606 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
607 Instruction::CONST_4 | 4 << 12 | 0 << 8,
608 Instruction::MUL_INT_LIT16, 3,
609 Instruction::RETURN);
610
611 TestCode(data, true, 12);
612}
613
Alexandre Rames92730742014-10-01 12:55:56 +0100614TEST(CodegenTest, MaterializedCondition1) {
615 // Check that condition are materialized correctly. A materialized condition
616 // should yield `1` if it evaluated to true, and `0` otherwise.
617 // We force the materialization of comparisons for different combinations of
618 // inputs and check the results.
619
620 int lhs[] = {1, 2, -1, 2, 0xabc};
621 int rhs[] = {2, 1, 2, -1, 0xabc};
622
623 for (size_t i = 0; i < arraysize(lhs); i++) {
624 ArenaPool pool;
625 ArenaAllocator allocator(&pool);
626 HGraph* graph = new (&allocator) HGraph(&allocator);
627
628 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
629 graph->AddBlock(entry_block);
630 graph->SetEntryBlock(entry_block);
631 entry_block->AddInstruction(new (&allocator) HGoto());
632 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
633 graph->AddBlock(code_block);
634 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
635 graph->AddBlock(exit_block);
636 exit_block->AddInstruction(new (&allocator) HExit());
637
638 entry_block->AddSuccessor(code_block);
639 code_block->AddSuccessor(exit_block);
640 graph->SetExitBlock(exit_block);
641
David Brazdil8d5b8b22015-03-24 10:51:52 +0000642 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
643 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
644 HLessThan cmp_lt(cst_lhs, cst_rhs);
Alexandre Rames92730742014-10-01 12:55:56 +0100645 code_block->AddInstruction(&cmp_lt);
646 HReturn ret(&cmp_lt);
647 code_block->AddInstruction(&ret);
648
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800649 auto hook_before_codegen = [](HGraph* graph_in) {
650 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
651 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100652 block->InsertInstructionBefore(move, block->GetLastInstruction());
653 };
654
655 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
656 }
657}
658
659TEST(CodegenTest, MaterializedCondition2) {
660 // Check that HIf correctly interprets a materialized condition.
661 // We force the materialization of comparisons for different combinations of
662 // inputs. An HIf takes the materialized combination as input and returns a
663 // value that we verify.
664
665 int lhs[] = {1, 2, -1, 2, 0xabc};
666 int rhs[] = {2, 1, 2, -1, 0xabc};
667
668
669 for (size_t i = 0; i < arraysize(lhs); i++) {
670 ArenaPool pool;
671 ArenaAllocator allocator(&pool);
672 HGraph* graph = new (&allocator) HGraph(&allocator);
673
674 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
675 graph->AddBlock(entry_block);
676 graph->SetEntryBlock(entry_block);
677 entry_block->AddInstruction(new (&allocator) HGoto());
678
679 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
680 graph->AddBlock(if_block);
681 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
682 graph->AddBlock(if_true_block);
683 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
684 graph->AddBlock(if_false_block);
685 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
686 graph->AddBlock(exit_block);
687 exit_block->AddInstruction(new (&allocator) HExit());
688
689 graph->SetEntryBlock(entry_block);
690 entry_block->AddSuccessor(if_block);
691 if_block->AddSuccessor(if_true_block);
692 if_block->AddSuccessor(if_false_block);
693 if_true_block->AddSuccessor(exit_block);
694 if_false_block->AddSuccessor(exit_block);
695 graph->SetExitBlock(exit_block);
696
David Brazdil8d5b8b22015-03-24 10:51:52 +0000697 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
698 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
699 HLessThan cmp_lt(cst_lhs, cst_rhs);
Alexandre Rames92730742014-10-01 12:55:56 +0100700 if_block->AddInstruction(&cmp_lt);
701 // We insert a temporary to separate the HIf from the HLessThan and force
702 // the materialization of the condition.
703 HTemporary force_materialization(0);
704 if_block->AddInstruction(&force_materialization);
705 HIf if_lt(&cmp_lt);
706 if_block->AddInstruction(&if_lt);
707
David Brazdil8d5b8b22015-03-24 10:51:52 +0000708 HIntConstant* cst_lt = graph->GetIntConstant(1);
709 HReturn ret_lt(cst_lt);
Alexandre Rames92730742014-10-01 12:55:56 +0100710 if_true_block->AddInstruction(&ret_lt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000711 HIntConstant* cst_ge = graph->GetIntConstant(0);
712 HReturn ret_ge(cst_ge);
Alexandre Rames92730742014-10-01 12:55:56 +0100713 if_false_block->AddInstruction(&ret_ge);
714
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800715 auto hook_before_codegen = [](HGraph* graph_in) {
716 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
717 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100718 block->InsertInstructionBefore(move, block->GetLastInstruction());
719 };
720
721 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
722 }
723}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100724
Calin Juravled0d48522014-11-04 16:40:20 +0000725TEST(CodegenTest, ReturnDivIntLit8) {
726 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
727 Instruction::CONST_4 | 4 << 12 | 0 << 8,
728 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
729 Instruction::RETURN);
730
731 TestCode(data, true, 1);
732}
733
Calin Juravle865fc882014-11-06 17:09:03 +0000734TEST(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000735 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
736 Instruction::CONST_4 | 4 << 12 | 0,
737 Instruction::CONST_4 | 2 << 12 | 1 << 8,
738 Instruction::DIV_INT_2ADDR | 1 << 12,
739 Instruction::RETURN);
740
741 TestCode(data, true, 2);
742}
743
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000744} // namespace art