blob: 57de41f557b5b344be9898d9f4cf6edcc9ee9ca1 [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"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020023#include "arch/mips/instruction_set_features_mips.h"
24#include "arch/mips/registers_mips.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070025#include "arch/mips64/instruction_set_features_mips64.h"
26#include "arch/mips64/registers_mips64.h"
Mark Mendellfb8d2792015-03-31 22:16:59 -040027#include "arch/x86/instruction_set_features_x86.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010028#include "arch/x86/registers_x86.h"
Mark Mendellfb8d2792015-03-31 22:16:59 -040029#include "arch/x86_64/instruction_set_features_x86_64.h"
Alexandre Rames92730742014-10-01 12:55:56 +010030#include "base/macros.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "builder.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010032#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "code_generator_arm64.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020034#include "code_generator_mips.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070035#include "code_generator_mips64.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010036#include "code_generator_x86.h"
37#include "code_generator_x86_64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000039#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000041#include "driver/compiler_options.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010044#include "prepare_for_register_allocation.h"
45#include "register_allocator.h"
46#include "ssa_liveness_analysis.h"
Roland Levillain55dcfb52014-10-24 18:09:09 +010047#include "utils.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010048#include "utils/arm/managed_register_arm.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020049#include "utils/mips/managed_register_mips.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070050#include "utils/mips64/managed_register_mips64.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010051#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000052
53#include "gtest/gtest.h"
Nicolas Geoffraye6362282015-01-26 13:57:30 +000054
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000055namespace art {
56
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000057// Provide our own codegen, that ensures the C calling conventions
58// are preserved. Currently, ART and C do not match as R4 is caller-save
59// in ART, and callee-save in C. Alternatively, we could use or write
60// the stub that saves and restores all registers, but it is easier
61// to just overwrite the code generator.
62class TestCodeGeneratorARM : public arm::CodeGeneratorARM {
63 public:
64 TestCodeGeneratorARM(HGraph* graph,
65 const ArmInstructionSetFeatures& isa_features,
66 const CompilerOptions& compiler_options)
67 : arm::CodeGeneratorARM(graph, isa_features, compiler_options) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +010068 AddAllocatedRegister(Location::RegisterLocation(arm::R6));
69 AddAllocatedRegister(Location::RegisterLocation(arm::R7));
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000070 }
71
72 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
73 arm::CodeGeneratorARM::SetupBlockedRegisters(is_baseline);
Nicolas Geoffray5da21802015-04-20 09:29:18 +010074 blocked_core_registers_[arm::R4] = true;
75 blocked_core_registers_[arm::R6] = false;
76 blocked_core_registers_[arm::R7] = false;
Nicolas Geoffraye6362282015-01-26 13:57:30 +000077 // Makes pair R6-R7 available.
Nicolas Geoffray5da21802015-04-20 09:29:18 +010078 blocked_register_pairs_[arm::R6_R7] = false;
79 }
80};
81
82class TestCodeGeneratorX86 : public x86::CodeGeneratorX86 {
83 public:
84 TestCodeGeneratorX86(HGraph* graph,
85 const X86InstructionSetFeatures& isa_features,
86 const CompilerOptions& compiler_options)
87 : x86::CodeGeneratorX86(graph, isa_features, compiler_options) {
88 // Save edi, we need it for getting enough registers for long multiplication.
89 AddAllocatedRegister(Location::RegisterLocation(x86::EDI));
90 }
91
92 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
93 x86::CodeGeneratorX86::SetupBlockedRegisters(is_baseline);
94 // ebx is a callee-save register in C, but caller-save for ART.
95 blocked_core_registers_[x86::EBX] = true;
96 blocked_register_pairs_[x86::EAX_EBX] = true;
97 blocked_register_pairs_[x86::EDX_EBX] = true;
98 blocked_register_pairs_[x86::ECX_EBX] = true;
99 blocked_register_pairs_[x86::EBX_EDI] = true;
100
101 // Make edi available.
102 blocked_core_registers_[x86::EDI] = false;
103 blocked_register_pairs_[x86::ECX_EDI] = false;
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000104 }
105};
106
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108 public:
Ian Rogersd582fa42014-11-05 23:46:43 -0800109 InternalCodeAllocator() : size_(0) { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110
111 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000112 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000113 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000114 return memory_.get();
115 }
116
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000117 size_t GetSize() const { return size_; }
118 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000119
120 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000121 size_t size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700122 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000123
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000125};
126
Roland Levillain55dcfb52014-10-24 18:09:09 +0100127template <typename Expected>
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100128static void Run(const InternalCodeAllocator& allocator,
129 const CodeGenerator& codegen,
130 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100131 Expected expected) {
132 typedef Expected (*fptr)();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100133 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -0700134 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Alex Light4cd27d62015-08-18 23:03:42 +0000135 if (codegen.GetInstructionSet() == kThumb2) {
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100136 // For thumb we need the bottom bit set.
137 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
138 }
Alex Light4cd27d62015-08-18 23:03:42 +0000139 Expected result = f();
140 if (has_result) {
141 ASSERT_EQ(expected, result);
142 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100143}
144
Roland Levillain55dcfb52014-10-24 18:09:09 +0100145template <typename Expected>
Alex Light4cd27d62015-08-18 23:03:42 +0000146static void RunCodeBaseline(HGraph* graph, bool has_result, Expected expected) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000147 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100148
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000149 CompilerOptions compiler_options;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400150 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
151 X86InstructionSetFeatures::FromCppDefines());
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100152 TestCodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100153 // We avoid doing a stack overflow check that requires the runtime being setup,
154 // by making sure the compiler knows the methods we are running are leaf methods.
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100155 codegenX86.CompileBaseline(&allocator, true);
Alex Light4cd27d62015-08-18 23:03:42 +0000156 if (kRuntimeISA == kX86) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100157 Run(allocator, codegenX86, has_result, expected);
158 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100159
Serban Constantinescu579885a2015-02-22 20:51:33 +0000160 std::unique_ptr<const ArmInstructionSetFeatures> features_arm(
Andreas Gampedf649502015-01-06 14:13:52 -0800161 ArmInstructionSetFeatures::FromCppDefines());
Serban Constantinescu579885a2015-02-22 20:51:33 +0000162 TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100163 codegenARM.CompileBaseline(&allocator, true);
Alex Light4cd27d62015-08-18 23:03:42 +0000164 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100165 Run(allocator, codegenARM, has_result, expected);
166 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100167
Mark Mendellfb8d2792015-03-31 22:16:59 -0400168 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
169 X86_64InstructionSetFeatures::FromCppDefines());
170 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100171 codegenX86_64.CompileBaseline(&allocator, true);
Alex Light4cd27d62015-08-18 23:03:42 +0000172 if (kRuntimeISA == kX86_64) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100173 Run(allocator, codegenX86_64, has_result, expected);
174 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100175
Serban Constantinescu579885a2015-02-22 20:51:33 +0000176 std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64(
177 Arm64InstructionSetFeatures::FromCppDefines());
178 arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100179 codegenARM64.CompileBaseline(&allocator, true);
Alex Light4cd27d62015-08-18 23:03:42 +0000180 if (kRuntimeISA == kArm64) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100181 Run(allocator, codegenARM64, has_result, expected);
182 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700183
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200184 std::unique_ptr<const MipsInstructionSetFeatures> features_mips(
185 MipsInstructionSetFeatures::FromCppDefines());
186 mips::CodeGeneratorMIPS codegenMIPS(graph, *features_mips.get(), compiler_options);
187 codegenMIPS.CompileBaseline(&allocator, true);
188 if (kRuntimeISA == kMips) {
189 Run(allocator, codegenMIPS, has_result, expected);
190 }
191
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 std::unique_ptr<const Mips64InstructionSetFeatures> features_mips64(
193 Mips64InstructionSetFeatures::FromCppDefines());
194 mips64::CodeGeneratorMIPS64 codegenMIPS64(graph, *features_mips64.get(), compiler_options);
195 codegenMIPS64.CompileBaseline(&allocator, true);
Alex Light4cd27d62015-08-18 23:03:42 +0000196 if (kRuntimeISA == kMips64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700197 Run(allocator, codegenMIPS64, has_result, expected);
198 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000199}
200
Roland Levillain55dcfb52014-10-24 18:09:09 +0100201template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100202static void RunCodeOptimized(CodeGenerator* codegen,
203 HGraph* graph,
204 std::function<void(HGraph*)> hook_before_codegen,
205 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100206 Expected expected) {
Nicolas Geoffrayea809422015-06-24 14:25:09 +0100207 // Tests may have already computed it.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100208 if (graph->GetReversePostOrder().empty()) {
Nicolas Geoffrayea809422015-06-24 14:25:09 +0100209 graph->BuildDominatorTree();
210 }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100211 SsaLivenessAnalysis liveness(graph, codegen);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100212 liveness.Analyze();
213
214 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
215 register_allocator.AllocateRegisters();
216 hook_before_codegen(graph);
217
218 InternalCodeAllocator allocator;
219 codegen->CompileOptimized(&allocator);
220 Run(allocator, *codegen, has_result, expected);
221}
222
Roland Levillain55dcfb52014-10-24 18:09:09 +0100223template <typename Expected>
Alex Light4cd27d62015-08-18 23:03:42 +0000224static void RunCodeOptimized(HGraph* graph,
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100225 std::function<void(HGraph*)> hook_before_codegen,
226 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100227 Expected expected) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000228 CompilerOptions compiler_options;
Alex Light4cd27d62015-08-18 23:03:42 +0000229 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) {
230 TestCodeGeneratorARM codegenARM(graph,
231 *ArmInstructionSetFeatures::FromCppDefines(),
232 compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100233 RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected);
Alex Light4cd27d62015-08-18 23:03:42 +0000234 } else if (kRuntimeISA == kArm64) {
235 arm64::CodeGeneratorARM64 codegenARM64(graph,
236 *Arm64InstructionSetFeatures::FromCppDefines(),
237 compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000238 RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected);
Alex Light4cd27d62015-08-18 23:03:42 +0000239 } else if (kRuntimeISA == kX86) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400240 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
241 X86InstructionSetFeatures::FromCppDefines());
242 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000243 RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected);
Alex Light4cd27d62015-08-18 23:03:42 +0000244 } else if (kRuntimeISA == kX86_64) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400245 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
246 X86_64InstructionSetFeatures::FromCppDefines());
247 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100248 RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200249 } else if (kRuntimeISA == kMips) {
250 std::unique_ptr<const MipsInstructionSetFeatures> features_mips(
251 MipsInstructionSetFeatures::FromCppDefines());
252 mips::CodeGeneratorMIPS codegenMIPS(graph, *features_mips.get(), compiler_options);
253 RunCodeOptimized(&codegenMIPS, graph, hook_before_codegen, has_result, expected);
Alex Light4cd27d62015-08-18 23:03:42 +0000254 } else if (kRuntimeISA == kMips64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700255 std::unique_ptr<const Mips64InstructionSetFeatures> features_mips64(
256 Mips64InstructionSetFeatures::FromCppDefines());
257 mips64::CodeGeneratorMIPS64 codegenMIPS64(graph, *features_mips64.get(), compiler_options);
258 RunCodeOptimized(&codegenMIPS64, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100259 }
260}
261
Alex Light4cd27d62015-08-18 23:03:42 +0000262static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100263 ArenaPool pool;
264 ArenaAllocator arena(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100265 HGraph* graph = CreateGraph(&arena);
David Brazdil5e8b1372015-01-23 14:39:08 +0000266 HGraphBuilder builder(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100267 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000268 bool graph_built = builder.BuildGraph(*item);
269 ASSERT_TRUE(graph_built);
Calin Juravle039b6e22014-10-23 12:32:11 +0100270 // Remove suspend checks, they cannot be executed in this context.
Calin Juravle48dee042014-10-22 15:54:12 +0100271 RemoveSuspendChecks(graph);
Alex Light4cd27d62015-08-18 23:03:42 +0000272 RunCodeBaseline(graph, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100273}
274
Alex Light4cd27d62015-08-18 23:03:42 +0000275static void TestCodeLong(const uint16_t* data, bool has_result, int64_t expected) {
Roland Levillain55dcfb52014-10-24 18:09:09 +0100276 ArenaPool pool;
277 ArenaAllocator arena(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 HGraph* graph = CreateGraph(&arena);
David Brazdil5e8b1372015-01-23 14:39:08 +0000279 HGraphBuilder builder(graph, Primitive::kPrimLong);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100280 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000281 bool graph_built = builder.BuildGraph(*item);
282 ASSERT_TRUE(graph_built);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100283 // Remove suspend checks, they cannot be executed in this context.
284 RemoveSuspendChecks(graph);
Alex Light4cd27d62015-08-18 23:03:42 +0000285 RunCodeBaseline(graph, has_result, expected);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100286}
287
Alex Light4cd27d62015-08-18 23:03:42 +0000288TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000289 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
Alex Light4cd27d62015-08-18 23:03:42 +0000290 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000291}
292
Alex Light4cd27d62015-08-18 23:03:42 +0000293TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000294 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000295 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000296 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000297
Alex Light4cd27d62015-08-18 23:03:42 +0000298 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000299}
300
Alex Light4cd27d62015-08-18 23:03:42 +0000301TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000302 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000303 Instruction::GOTO | 0x100,
304 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000305 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000306
Alex Light4cd27d62015-08-18 23:03:42 +0000307 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000308}
309
Alex Light4cd27d62015-08-18 23:03:42 +0000310TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000311 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000312 Instruction::GOTO | 0x200,
313 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000314 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000315
Alex Light4cd27d62015-08-18 23:03:42 +0000316 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000317
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000318 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000319 Instruction::GOTO_16, 3,
320 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000321 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000322
Alex Light4cd27d62015-08-18 23:03:42 +0000323 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000324
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000325 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000326 Instruction::GOTO_32, 4, 0,
327 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000328 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000329
Alex Light4cd27d62015-08-18 23:03:42 +0000330 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331}
332
Alex Light4cd27d62015-08-18 23:03:42 +0000333TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000334 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000335 Instruction::RETURN_VOID,
336 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000337 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000338
Alex Light4cd27d62015-08-18 23:03:42 +0000339 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000340}
341
Alex Light4cd27d62015-08-18 23:03:42 +0000342TEST(CodegenTest, CFG5) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000343 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
344 Instruction::CONST_4 | 0 | 0,
345 Instruction::IF_EQ, 3,
346 Instruction::GOTO | 0x100,
347 Instruction::RETURN_VOID);
348
Alex Light4cd27d62015-08-18 23:03:42 +0000349 TestCode(data);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000350}
351
Alex Light4cd27d62015-08-18 23:03:42 +0000352TEST(CodegenTest, IntConstant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000353 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
354 Instruction::CONST_4 | 0 | 0,
355 Instruction::RETURN_VOID);
356
Alex Light4cd27d62015-08-18 23:03:42 +0000357 TestCode(data);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000358}
359
Alex Light4cd27d62015-08-18 23:03:42 +0000360TEST(CodegenTest, Return1) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000361 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
362 Instruction::CONST_4 | 0 | 0,
363 Instruction::RETURN | 0);
364
Alex Light4cd27d62015-08-18 23:03:42 +0000365 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000366}
367
Alex Light4cd27d62015-08-18 23:03:42 +0000368TEST(CodegenTest, Return2) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000369 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
370 Instruction::CONST_4 | 0 | 0,
371 Instruction::CONST_4 | 0 | 1 << 8,
372 Instruction::RETURN | 1 << 8);
373
Alex Light4cd27d62015-08-18 23:03:42 +0000374 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000375}
376
Alex Light4cd27d62015-08-18 23:03:42 +0000377TEST(CodegenTest, Return3) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000378 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
379 Instruction::CONST_4 | 0 | 0,
380 Instruction::CONST_4 | 1 << 8 | 1 << 12,
381 Instruction::RETURN | 1 << 8);
382
Alex Light4cd27d62015-08-18 23:03:42 +0000383 TestCode(data, true, 1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000384}
385
Alex Light4cd27d62015-08-18 23:03:42 +0000386TEST(CodegenTest, ReturnIf1) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000387 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
388 Instruction::CONST_4 | 0 | 0,
389 Instruction::CONST_4 | 1 << 8 | 1 << 12,
390 Instruction::IF_EQ, 3,
391 Instruction::RETURN | 0 << 8,
392 Instruction::RETURN | 1 << 8);
393
Alex Light4cd27d62015-08-18 23:03:42 +0000394 TestCode(data, true, 1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000395}
396
Alex Light4cd27d62015-08-18 23:03:42 +0000397TEST(CodegenTest, ReturnIf2) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000398 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
399 Instruction::CONST_4 | 0 | 0,
400 Instruction::CONST_4 | 1 << 8 | 1 << 12,
401 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
402 Instruction::RETURN | 0 << 8,
403 Instruction::RETURN | 1 << 8);
404
Alex Light4cd27d62015-08-18 23:03:42 +0000405 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000406}
407
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100408// Exercise bit-wise (one's complement) not-int instruction.
409#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
Alex Light4cd27d62015-08-18 23:03:42 +0000410TEST(CodegenTest, TEST_NAME) { \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100411 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100412 const uint16_t input_lo = Low16Bits(input); \
413 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100414 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
415 Instruction::CONST | 0 << 8, input_lo, input_hi, \
416 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
417 Instruction::RETURN | 1 << 8); \
418 \
Alex Light4cd27d62015-08-18 23:03:42 +0000419 TestCode(data, true, EXPECTED_OUTPUT); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100420}
421
422NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
423NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
424NOT_INT_TEST(ReturnNotInt0, 0, -1)
425NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100426NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
427NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
428NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
429NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100430
431#undef NOT_INT_TEST
432
Roland Levillain55dcfb52014-10-24 18:09:09 +0100433// Exercise bit-wise (one's complement) not-long instruction.
434#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
Alex Light4cd27d62015-08-18 23:03:42 +0000435TEST(CodegenTest, TEST_NAME) { \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100436 const int64_t input = INPUT; \
437 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
438 const uint16_t word1 = High16Bits(Low32Bits(input)); \
439 const uint16_t word2 = Low16Bits(High32Bits(input)); \
440 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
441 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
442 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
443 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
444 Instruction::RETURN_WIDE | 2 << 8); \
445 \
Alex Light4cd27d62015-08-18 23:03:42 +0000446 TestCodeLong(data, true, EXPECTED_OUTPUT); \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100447}
448
449NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
450NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
451NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
452NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
453
454NOT_LONG_TEST(ReturnNotLongINT32_MIN,
455 INT64_C(-2147483648),
456 INT64_C(2147483647)) // (2^31) - 1
457NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
458 INT64_C(-2147483647),
459 INT64_C(2147483646)) // (2^31) - 2
460NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
461 INT64_C(2147483646),
462 INT64_C(-2147483647)) // -(2^31) - 1
463NOT_LONG_TEST(ReturnNotLongINT32_MAX,
464 INT64_C(2147483647),
465 INT64_C(-2147483648)) // -(2^31)
466
467// Note that the C++ compiler won't accept
468// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
469// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
470NOT_LONG_TEST(ReturnNotINT64_MIN,
471 INT64_C(-9223372036854775807)-1,
472 INT64_C(9223372036854775807)); // (2^63) - 1
473NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
474 INT64_C(-9223372036854775807),
475 INT64_C(9223372036854775806)); // (2^63) - 2
476NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
477 INT64_C(9223372036854775806),
478 INT64_C(-9223372036854775807)); // -(2^63) - 1
479NOT_LONG_TEST(ReturnNotLongINT64_MAX,
480 INT64_C(9223372036854775807),
481 INT64_C(-9223372036854775807)-1); // -(2^63)
482
483#undef NOT_LONG_TEST
484
Alex Light4cd27d62015-08-18 23:03:42 +0000485TEST(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000486 const int64_t input = INT64_C(4294967296); // 2^32
487 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
488 const uint16_t word1 = High16Bits(Low32Bits(input));
489 const uint16_t word2 = Low16Bits(High32Bits(input));
490 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
491 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
492 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
493 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
494 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
495 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
496 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
497 Instruction::RETURN_WIDE | 2 << 8);
498
Alex Light4cd27d62015-08-18 23:03:42 +0000499 TestCodeLong(data, true, 1);
Roland Levillain946e1432014-11-11 17:35:19 +0000500}
501
Alex Light4cd27d62015-08-18 23:03:42 +0000502TEST(CodegenTest, ReturnAdd1) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000503 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
504 Instruction::CONST_4 | 3 << 12 | 0,
505 Instruction::CONST_4 | 4 << 12 | 1 << 8,
506 Instruction::ADD_INT, 1 << 8 | 0,
507 Instruction::RETURN);
508
Alex Light4cd27d62015-08-18 23:03:42 +0000509 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000510}
511
Alex Light4cd27d62015-08-18 23:03:42 +0000512TEST(CodegenTest, ReturnAdd2) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000513 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
514 Instruction::CONST_4 | 3 << 12 | 0,
515 Instruction::CONST_4 | 4 << 12 | 1 << 8,
516 Instruction::ADD_INT_2ADDR | 1 << 12,
517 Instruction::RETURN);
518
Alex Light4cd27d62015-08-18 23:03:42 +0000519 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000520}
521
Alex Light4cd27d62015-08-18 23:03:42 +0000522TEST(CodegenTest, ReturnAdd3) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000523 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
524 Instruction::CONST_4 | 4 << 12 | 0 << 8,
525 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
526 Instruction::RETURN);
527
Alex Light4cd27d62015-08-18 23:03:42 +0000528 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000529}
530
Alex Light4cd27d62015-08-18 23:03:42 +0000531TEST(CodegenTest, ReturnAdd4) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000532 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
533 Instruction::CONST_4 | 4 << 12 | 0 << 8,
534 Instruction::ADD_INT_LIT16, 3,
535 Instruction::RETURN);
536
Alex Light4cd27d62015-08-18 23:03:42 +0000537 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000538}
539
Alex Light4cd27d62015-08-18 23:03:42 +0000540TEST(CodegenTest, NonMaterializedCondition) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100541 ArenaPool pool;
542 ArenaAllocator allocator(&pool);
543
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100544 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100545 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
546 graph->AddBlock(entry);
547 graph->SetEntryBlock(entry);
548 entry->AddInstruction(new (&allocator) HGoto());
549
550 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
551 graph->AddBlock(first_block);
552 entry->AddSuccessor(first_block);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000553 HIntConstant* constant0 = graph->GetIntConstant(0);
554 HIntConstant* constant1 = graph->GetIntConstant(1);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100555 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
556 first_block->AddInstruction(equal);
557 first_block->AddInstruction(new (&allocator) HIf(equal));
558
559 HBasicBlock* then = new (&allocator) HBasicBlock(graph);
560 HBasicBlock* else_ = new (&allocator) HBasicBlock(graph);
561 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
562
563 graph->AddBlock(then);
564 graph->AddBlock(else_);
565 graph->AddBlock(exit);
566 first_block->AddSuccessor(then);
567 first_block->AddSuccessor(else_);
568 then->AddSuccessor(exit);
569 else_->AddSuccessor(exit);
570
571 exit->AddInstruction(new (&allocator) HExit());
572 then->AddInstruction(new (&allocator) HReturn(constant0));
573 else_->AddInstruction(new (&allocator) HReturn(constant1));
574
575 ASSERT_TRUE(equal->NeedsMaterialization());
576 graph->BuildDominatorTree();
577 PrepareForRegisterAllocation(graph).Run();
578 ASSERT_FALSE(equal->NeedsMaterialization());
579
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800580 auto hook_before_codegen = [](HGraph* graph_in) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100581 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800582 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100583 block->InsertInstructionBefore(move, block->GetLastInstruction());
584 };
585
Alex Light4cd27d62015-08-18 23:03:42 +0000586 RunCodeOptimized(graph, hook_before_codegen, true, 0);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100587}
588
Alex Light4cd27d62015-08-18 23:03:42 +0000589TEST(CodegenTest, ReturnMulInt) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100590 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
591 Instruction::CONST_4 | 3 << 12 | 0,
592 Instruction::CONST_4 | 4 << 12 | 1 << 8,
593 Instruction::MUL_INT, 1 << 8 | 0,
594 Instruction::RETURN);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100595
Alex Light4cd27d62015-08-18 23:03:42 +0000596 TestCode(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100597}
598
Alex Light4cd27d62015-08-18 23:03:42 +0000599TEST(CodegenTest, ReturnMulInt2addr) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100600 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
601 Instruction::CONST_4 | 3 << 12 | 0,
602 Instruction::CONST_4 | 4 << 12 | 1 << 8,
603 Instruction::MUL_INT_2ADDR | 1 << 12,
604 Instruction::RETURN);
605
Alex Light4cd27d62015-08-18 23:03:42 +0000606 TestCode(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100607}
608
Alex Light4cd27d62015-08-18 23:03:42 +0000609TEST(CodegenTest, ReturnMulLong) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100610 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
611 Instruction::CONST_4 | 3 << 12 | 0,
612 Instruction::CONST_4 | 0 << 12 | 1 << 8,
613 Instruction::CONST_4 | 4 << 12 | 2 << 8,
614 Instruction::CONST_4 | 0 << 12 | 3 << 8,
615 Instruction::MUL_LONG, 2 << 8 | 0,
616 Instruction::RETURN_WIDE);
617
Alex Light4cd27d62015-08-18 23:03:42 +0000618 TestCodeLong(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100619}
620
Alex Light4cd27d62015-08-18 23:03:42 +0000621TEST(CodegenTest, ReturnMulLong2addr) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100622 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
623 Instruction::CONST_4 | 3 << 12 | 0 << 8,
624 Instruction::CONST_4 | 0 << 12 | 1 << 8,
625 Instruction::CONST_4 | 4 << 12 | 2 << 8,
626 Instruction::CONST_4 | 0 << 12 | 3 << 8,
627 Instruction::MUL_LONG_2ADDR | 2 << 12,
628 Instruction::RETURN_WIDE);
629
Alex Light4cd27d62015-08-18 23:03:42 +0000630 TestCodeLong(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100631}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100632
Alex Light4cd27d62015-08-18 23:03:42 +0000633TEST(CodegenTest, ReturnMulIntLit8) {
Calin Juravle34bacdf2014-10-07 20:23:36 +0100634 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
635 Instruction::CONST_4 | 4 << 12 | 0 << 8,
636 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
637 Instruction::RETURN);
638
Alex Light4cd27d62015-08-18 23:03:42 +0000639 TestCode(data, true, 12);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100640}
641
Alex Light4cd27d62015-08-18 23:03:42 +0000642TEST(CodegenTest, ReturnMulIntLit16) {
Calin Juravle34bacdf2014-10-07 20:23:36 +0100643 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
644 Instruction::CONST_4 | 4 << 12 | 0 << 8,
645 Instruction::MUL_INT_LIT16, 3,
646 Instruction::RETURN);
647
Alex Light4cd27d62015-08-18 23:03:42 +0000648 TestCode(data, true, 12);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100649}
650
Alex Light4cd27d62015-08-18 23:03:42 +0000651TEST(CodegenTest, MaterializedCondition1) {
Alexandre Rames92730742014-10-01 12:55:56 +0100652 // Check that condition are materialized correctly. A materialized condition
653 // should yield `1` if it evaluated to true, and `0` otherwise.
654 // We force the materialization of comparisons for different combinations of
655 // inputs and check the results.
656
657 int lhs[] = {1, 2, -1, 2, 0xabc};
658 int rhs[] = {2, 1, 2, -1, 0xabc};
659
660 for (size_t i = 0; i < arraysize(lhs); i++) {
661 ArenaPool pool;
662 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100663 HGraph* graph = CreateGraph(&allocator);
Alexandre Rames92730742014-10-01 12:55:56 +0100664
665 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
666 graph->AddBlock(entry_block);
667 graph->SetEntryBlock(entry_block);
668 entry_block->AddInstruction(new (&allocator) HGoto());
669 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
670 graph->AddBlock(code_block);
671 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
672 graph->AddBlock(exit_block);
673 exit_block->AddInstruction(new (&allocator) HExit());
674
675 entry_block->AddSuccessor(code_block);
676 code_block->AddSuccessor(exit_block);
677 graph->SetExitBlock(exit_block);
678
David Brazdil8d5b8b22015-03-24 10:51:52 +0000679 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
680 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
681 HLessThan cmp_lt(cst_lhs, cst_rhs);
Alexandre Rames92730742014-10-01 12:55:56 +0100682 code_block->AddInstruction(&cmp_lt);
683 HReturn ret(&cmp_lt);
684 code_block->AddInstruction(&ret);
685
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800686 auto hook_before_codegen = [](HGraph* graph_in) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100687 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800688 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100689 block->InsertInstructionBefore(move, block->GetLastInstruction());
690 };
691
Alex Light4cd27d62015-08-18 23:03:42 +0000692 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
Alexandre Rames92730742014-10-01 12:55:56 +0100693 }
694}
695
Alex Light4cd27d62015-08-18 23:03:42 +0000696TEST(CodegenTest, MaterializedCondition2) {
Alexandre Rames92730742014-10-01 12:55:56 +0100697 // Check that HIf correctly interprets a materialized condition.
698 // We force the materialization of comparisons for different combinations of
699 // inputs. An HIf takes the materialized combination as input and returns a
700 // value that we verify.
701
702 int lhs[] = {1, 2, -1, 2, 0xabc};
703 int rhs[] = {2, 1, 2, -1, 0xabc};
704
705
706 for (size_t i = 0; i < arraysize(lhs); i++) {
707 ArenaPool pool;
708 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100709 HGraph* graph = CreateGraph(&allocator);
Alexandre Rames92730742014-10-01 12:55:56 +0100710
711 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
712 graph->AddBlock(entry_block);
713 graph->SetEntryBlock(entry_block);
714 entry_block->AddInstruction(new (&allocator) HGoto());
715
716 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
717 graph->AddBlock(if_block);
718 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
719 graph->AddBlock(if_true_block);
720 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
721 graph->AddBlock(if_false_block);
722 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
723 graph->AddBlock(exit_block);
724 exit_block->AddInstruction(new (&allocator) HExit());
725
726 graph->SetEntryBlock(entry_block);
727 entry_block->AddSuccessor(if_block);
728 if_block->AddSuccessor(if_true_block);
729 if_block->AddSuccessor(if_false_block);
730 if_true_block->AddSuccessor(exit_block);
731 if_false_block->AddSuccessor(exit_block);
732 graph->SetExitBlock(exit_block);
733
David Brazdil8d5b8b22015-03-24 10:51:52 +0000734 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
735 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
736 HLessThan cmp_lt(cst_lhs, cst_rhs);
Alexandre Rames92730742014-10-01 12:55:56 +0100737 if_block->AddInstruction(&cmp_lt);
738 // We insert a temporary to separate the HIf from the HLessThan and force
739 // the materialization of the condition.
740 HTemporary force_materialization(0);
741 if_block->AddInstruction(&force_materialization);
742 HIf if_lt(&cmp_lt);
743 if_block->AddInstruction(&if_lt);
744
David Brazdil8d5b8b22015-03-24 10:51:52 +0000745 HIntConstant* cst_lt = graph->GetIntConstant(1);
746 HReturn ret_lt(cst_lt);
Alexandre Rames92730742014-10-01 12:55:56 +0100747 if_true_block->AddInstruction(&ret_lt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000748 HIntConstant* cst_ge = graph->GetIntConstant(0);
749 HReturn ret_ge(cst_ge);
Alexandre Rames92730742014-10-01 12:55:56 +0100750 if_false_block->AddInstruction(&ret_ge);
751
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800752 auto hook_before_codegen = [](HGraph* graph_in) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100753 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800754 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100755 block->InsertInstructionBefore(move, block->GetLastInstruction());
756 };
757
Alex Light4cd27d62015-08-18 23:03:42 +0000758 RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]);
Alexandre Rames92730742014-10-01 12:55:56 +0100759 }
760}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100761
Alex Light4cd27d62015-08-18 23:03:42 +0000762TEST(CodegenTest, ReturnDivIntLit8) {
Calin Juravled0d48522014-11-04 16:40:20 +0000763 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
764 Instruction::CONST_4 | 4 << 12 | 0 << 8,
765 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
766 Instruction::RETURN);
767
Alex Light4cd27d62015-08-18 23:03:42 +0000768 TestCode(data, true, 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000769}
770
Alex Light4cd27d62015-08-18 23:03:42 +0000771TEST(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000772 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
773 Instruction::CONST_4 | 4 << 12 | 0,
774 Instruction::CONST_4 | 2 << 12 | 1 << 8,
775 Instruction::DIV_INT_2ADDR | 1 << 12,
776 Instruction::RETURN);
777
Alex Light4cd27d62015-08-18 23:03:42 +0000778 TestCode(data, true, 2);
Calin Juravle865fc882014-11-06 17:09:03 +0000779}
780
Aart Bike9f37602015-10-09 11:15:55 -0700781// Helper method.
782static void TestComparison(IfCondition condition, int64_t i, int64_t j, Primitive::Type type) {
783 ArenaPool pool;
784 ArenaAllocator allocator(&pool);
785 HGraph* graph = CreateGraph(&allocator);
786
787 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
788 graph->AddBlock(entry_block);
789 graph->SetEntryBlock(entry_block);
790 entry_block->AddInstruction(new (&allocator) HGoto());
791
792 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
793 graph->AddBlock(block);
794
795 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
796 graph->AddBlock(exit_block);
797 graph->SetExitBlock(exit_block);
798 exit_block->AddInstruction(new (&allocator) HExit());
799
800 entry_block->AddSuccessor(block);
801 block->AddSuccessor(exit_block);
802
803 HInstruction* op1;
804 HInstruction* op2;
805 if (type == Primitive::kPrimInt) {
806 op1 = graph->GetIntConstant(i);
807 op2 = graph->GetIntConstant(j);
808 } else {
809 DCHECK_EQ(type, Primitive::kPrimLong);
810 op1 = graph->GetLongConstant(i);
811 op2 = graph->GetLongConstant(j);
812 }
813
814 HInstruction* comparison = nullptr;
815 bool expected_result = false;
816 const uint64_t x = i;
817 const uint64_t y = j;
818 switch (condition) {
819 case kCondEQ:
820 comparison = new (&allocator) HEqual(op1, op2);
821 expected_result = (i == j);
822 break;
823 case kCondNE:
824 comparison = new (&allocator) HNotEqual(op1, op2);
825 expected_result = (i != j);
826 break;
827 case kCondLT:
828 comparison = new (&allocator) HLessThan(op1, op2);
829 expected_result = (i < j);
830 break;
831 case kCondLE:
832 comparison = new (&allocator) HLessThanOrEqual(op1, op2);
833 expected_result = (i <= j);
834 break;
835 case kCondGT:
836 comparison = new (&allocator) HGreaterThan(op1, op2);
837 expected_result = (i > j);
838 break;
839 case kCondGE:
840 comparison = new (&allocator) HGreaterThanOrEqual(op1, op2);
841 expected_result = (i >= j);
842 break;
843 case kCondB:
844 comparison = new (&allocator) HBelow(op1, op2);
845 expected_result = (x < y);
846 break;
847 case kCondBE:
848 comparison = new (&allocator) HBelowOrEqual(op1, op2);
849 expected_result = (x <= y);
850 break;
851 case kCondA:
852 comparison = new (&allocator) HAbove(op1, op2);
853 expected_result = (x > y);
854 break;
855 case kCondAE:
856 comparison = new (&allocator) HAboveOrEqual(op1, op2);
857 expected_result = (x >= y);
858 break;
859 }
860 block->AddInstruction(comparison);
861 block->AddInstruction(new (&allocator) HReturn(comparison));
862
863 auto hook_before_codegen = [](HGraph*) {
864 };
865 RunCodeOptimized(graph, hook_before_codegen, true, expected_result);
866}
867
868TEST(CodegenTest, ComparisonsInt) {
869 for (int64_t i = -1; i <= 1; i++) {
870 for (int64_t j = -1; j <= 1; j++) {
871 TestComparison(kCondEQ, i, j, Primitive::kPrimInt);
872 TestComparison(kCondNE, i, j, Primitive::kPrimInt);
873 TestComparison(kCondLT, i, j, Primitive::kPrimInt);
874 TestComparison(kCondLE, i, j, Primitive::kPrimInt);
875 TestComparison(kCondGT, i, j, Primitive::kPrimInt);
876 TestComparison(kCondGE, i, j, Primitive::kPrimInt);
877 TestComparison(kCondB, i, j, Primitive::kPrimInt);
878 TestComparison(kCondBE, i, j, Primitive::kPrimInt);
879 TestComparison(kCondA, i, j, Primitive::kPrimInt);
880 TestComparison(kCondAE, i, j, Primitive::kPrimInt);
881 }
882 }
883}
884
885TEST(CodegenTest, ComparisonsLong) {
886 // TODO: make MIPS work for long
887 if (kRuntimeISA == kMips || kRuntimeISA == kMips64) {
888 return;
889 }
890
891 for (int64_t i = -1; i <= 1; i++) {
892 for (int64_t j = -1; j <= 1; j++) {
893 TestComparison(kCondEQ, i, j, Primitive::kPrimLong);
894 TestComparison(kCondNE, i, j, Primitive::kPrimLong);
895 TestComparison(kCondLT, i, j, Primitive::kPrimLong);
896 TestComparison(kCondLE, i, j, Primitive::kPrimLong);
897 TestComparison(kCondGT, i, j, Primitive::kPrimLong);
898 TestComparison(kCondGE, i, j, Primitive::kPrimLong);
899 TestComparison(kCondB, i, j, Primitive::kPrimLong);
900 TestComparison(kCondBE, i, j, Primitive::kPrimLong);
901 TestComparison(kCondA, i, j, Primitive::kPrimLong);
902 TestComparison(kCondAE, i, j, Primitive::kPrimLong);
903 }
904 }
905}
906
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000907} // namespace art