blob: 322a577bbf4afd82c229bc7ea5c7408b62c07b13 [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"
Phil Wang751beff2015-08-28 15:17:15 +080038#include "code_simulator_container.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000040#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000042#include "driver/compiler_options.h"
David Brazdil58282f42016-01-14 12:45:10 +000043#include "graph_checker.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000044#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000045#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010046#include "prepare_for_register_allocation.h"
47#include "register_allocator.h"
48#include "ssa_liveness_analysis.h"
Roland Levillain55dcfb52014-10-24 18:09:09 +010049#include "utils.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010050#include "utils/arm/managed_register_arm.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020051#include "utils/mips/managed_register_mips.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070052#include "utils/mips64/managed_register_mips64.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010053#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000054
55#include "gtest/gtest.h"
Nicolas Geoffraye6362282015-01-26 13:57:30 +000056
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000057namespace art {
58
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000059// Provide our own codegen, that ensures the C calling conventions
60// are preserved. Currently, ART and C do not match as R4 is caller-save
61// in ART, and callee-save in C. Alternatively, we could use or write
62// the stub that saves and restores all registers, but it is easier
63// to just overwrite the code generator.
64class TestCodeGeneratorARM : public arm::CodeGeneratorARM {
65 public:
66 TestCodeGeneratorARM(HGraph* graph,
67 const ArmInstructionSetFeatures& isa_features,
68 const CompilerOptions& compiler_options)
69 : arm::CodeGeneratorARM(graph, isa_features, compiler_options) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +010070 AddAllocatedRegister(Location::RegisterLocation(arm::R6));
71 AddAllocatedRegister(Location::RegisterLocation(arm::R7));
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000072 }
73
David Brazdil58282f42016-01-14 12:45:10 +000074 void SetupBlockedRegisters() const OVERRIDE {
75 arm::CodeGeneratorARM::SetupBlockedRegisters();
Nicolas Geoffray5da21802015-04-20 09:29:18 +010076 blocked_core_registers_[arm::R4] = true;
77 blocked_core_registers_[arm::R6] = false;
78 blocked_core_registers_[arm::R7] = false;
Nicolas Geoffraye6362282015-01-26 13:57:30 +000079 // Makes pair R6-R7 available.
Nicolas Geoffray5da21802015-04-20 09:29:18 +010080 blocked_register_pairs_[arm::R6_R7] = false;
81 }
82};
83
84class TestCodeGeneratorX86 : public x86::CodeGeneratorX86 {
85 public:
86 TestCodeGeneratorX86(HGraph* graph,
87 const X86InstructionSetFeatures& isa_features,
88 const CompilerOptions& compiler_options)
89 : x86::CodeGeneratorX86(graph, isa_features, compiler_options) {
90 // Save edi, we need it for getting enough registers for long multiplication.
91 AddAllocatedRegister(Location::RegisterLocation(x86::EDI));
92 }
93
David Brazdil58282f42016-01-14 12:45:10 +000094 void SetupBlockedRegisters() const OVERRIDE {
95 x86::CodeGeneratorX86::SetupBlockedRegisters();
Nicolas Geoffray5da21802015-04-20 09:29:18 +010096 // ebx is a callee-save register in C, but caller-save for ART.
97 blocked_core_registers_[x86::EBX] = true;
98 blocked_register_pairs_[x86::EAX_EBX] = true;
99 blocked_register_pairs_[x86::EDX_EBX] = true;
100 blocked_register_pairs_[x86::ECX_EBX] = true;
101 blocked_register_pairs_[x86::EBX_EDI] = true;
102
103 // Make edi available.
104 blocked_core_registers_[x86::EDI] = false;
105 blocked_register_pairs_[x86::ECX_EDI] = false;
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000106 }
107};
108
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000109class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110 public:
Ian Rogersd582fa42014-11-05 23:46:43 -0800111 InternalCodeAllocator() : size_(0) { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112
113 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000114 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000115 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000116 return memory_.get();
117 }
118
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000119 size_t GetSize() const { return size_; }
120 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000121
122 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000123 size_t size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700124 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000125
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000126 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000127};
128
Phil Wang751beff2015-08-28 15:17:15 +0800129static bool CanExecuteOnHardware(InstructionSet target_isa) {
130 return (target_isa == kRuntimeISA)
131 // Handle the special case of ARM, with two instructions sets (ARM32 and Thumb-2).
132 || (kRuntimeISA == kArm && target_isa == kThumb2);
133}
134
135static bool CanExecute(InstructionSet target_isa) {
136 CodeSimulatorContainer simulator(target_isa);
137 return CanExecuteOnHardware(target_isa) || simulator.CanSimulate();
138}
139
140template <typename Expected>
141static Expected SimulatorExecute(CodeSimulator* simulator, Expected (*f)());
142
143template <>
144bool SimulatorExecute<bool>(CodeSimulator* simulator, bool (*f)()) {
145 simulator->RunFrom(reinterpret_cast<intptr_t>(f));
146 return simulator->GetCReturnBool();
147}
148
149template <>
150int32_t SimulatorExecute<int32_t>(CodeSimulator* simulator, int32_t (*f)()) {
151 simulator->RunFrom(reinterpret_cast<intptr_t>(f));
152 return simulator->GetCReturnInt32();
153}
154
155template <>
156int64_t SimulatorExecute<int64_t>(CodeSimulator* simulator, int64_t (*f)()) {
157 simulator->RunFrom(reinterpret_cast<intptr_t>(f));
158 return simulator->GetCReturnInt64();
159}
160
161template <typename Expected>
162static void VerifyGeneratedCode(InstructionSet target_isa,
163 Expected (*f)(),
164 bool has_result,
165 Expected expected) {
166 ASSERT_TRUE(CanExecute(target_isa)) << "Target isa is not executable.";
167
168 // Verify on simulator.
169 CodeSimulatorContainer simulator(target_isa);
170 if (simulator.CanSimulate()) {
171 Expected result = SimulatorExecute<Expected>(simulator.Get(), f);
172 if (has_result) {
173 ASSERT_EQ(expected, result);
174 }
175 }
176
177 // Verify on hardware.
178 if (CanExecuteOnHardware(target_isa)) {
179 Expected result = f();
180 if (has_result) {
181 ASSERT_EQ(expected, result);
182 }
183 }
184}
185
Roland Levillain55dcfb52014-10-24 18:09:09 +0100186template <typename Expected>
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100187static void Run(const InternalCodeAllocator& allocator,
188 const CodeGenerator& codegen,
189 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100190 Expected expected) {
Phil Wang751beff2015-08-28 15:17:15 +0800191 InstructionSet target_isa = codegen.GetInstructionSet();
192
Roland Levillain55dcfb52014-10-24 18:09:09 +0100193 typedef Expected (*fptr)();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100194 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -0700195 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Phil Wang751beff2015-08-28 15:17:15 +0800196 if (target_isa == kThumb2) {
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100197 // For thumb we need the bottom bit set.
198 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
199 }
Phil Wang751beff2015-08-28 15:17:15 +0800200 VerifyGeneratedCode(target_isa, f, has_result, expected);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100201}
202
Roland Levillain55dcfb52014-10-24 18:09:09 +0100203template <typename Expected>
David Brazdil58282f42016-01-14 12:45:10 +0000204static void RunCode(CodeGenerator* codegen,
205 HGraph* graph,
206 std::function<void(HGraph*)> hook_before_codegen,
207 bool has_result,
208 Expected expected) {
209 ASSERT_TRUE(graph->IsInSsaForm());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100210
David Brazdil58282f42016-01-14 12:45:10 +0000211 SSAChecker graph_checker(graph);
212 graph_checker.Run();
213 ASSERT_TRUE(graph_checker.IsValid());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100214
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100215 SsaLivenessAnalysis liveness(graph, codegen);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100216
David Brazdil58282f42016-01-14 12:45:10 +0000217 PrepareForRegisterAllocation(graph).Run();
218 liveness.Analyze();
219 RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100220 hook_before_codegen(graph);
221
222 InternalCodeAllocator allocator;
David Brazdil58282f42016-01-14 12:45:10 +0000223 codegen->Compile(&allocator);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100224 Run(allocator, *codegen, has_result, expected);
225}
226
Roland Levillain55dcfb52014-10-24 18:09:09 +0100227template <typename Expected>
David Brazdil58282f42016-01-14 12:45:10 +0000228static void RunCode(InstructionSet target_isa,
229 HGraph* graph,
230 std::function<void(HGraph*)> hook_before_codegen,
231 bool has_result,
232 Expected expected) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000233 CompilerOptions compiler_options;
Phil Wang751beff2015-08-28 15:17:15 +0800234 if (target_isa == kArm || target_isa == kThumb2) {
235 std::unique_ptr<const ArmInstructionSetFeatures> features_arm(
236 ArmInstructionSetFeatures::FromCppDefines());
237 TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000238 RunCode(&codegenARM, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800239 } else if (target_isa == kArm64) {
240 std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64(
241 Arm64InstructionSetFeatures::FromCppDefines());
242 arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000243 RunCode(&codegenARM64, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800244 } else if (target_isa == kX86) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400245 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
246 X86InstructionSetFeatures::FromCppDefines());
247 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000248 RunCode(&codegenX86, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800249 } else if (target_isa == kX86_64) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400250 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
251 X86_64InstructionSetFeatures::FromCppDefines());
252 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000253 RunCode(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800254 } else if (target_isa == kMips) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200255 std::unique_ptr<const MipsInstructionSetFeatures> features_mips(
256 MipsInstructionSetFeatures::FromCppDefines());
257 mips::CodeGeneratorMIPS codegenMIPS(graph, *features_mips.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000258 RunCode(&codegenMIPS, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800259 } else if (target_isa == kMips64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 std::unique_ptr<const Mips64InstructionSetFeatures> features_mips64(
261 Mips64InstructionSetFeatures::FromCppDefines());
262 mips64::CodeGeneratorMIPS64 codegenMIPS64(graph, *features_mips64.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000263 RunCode(&codegenMIPS64, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100264 }
265}
266
David Brazdil58282f42016-01-14 12:45:10 +0000267static ::std::vector<InstructionSet> GetTargetISAs() {
268 ::std::vector<InstructionSet> v;
269 // Add all ISAs that are executable on hardware or on simulator.
270 const ::std::vector<InstructionSet> executable_isa_candidates = {
271 kArm,
272 kArm64,
273 kThumb2,
274 kX86,
275 kX86_64,
276 kMips,
277 kMips64
278 };
279
280 for (auto target_isa : executable_isa_candidates) {
281 if (CanExecute(target_isa)) {
282 v.push_back(target_isa);
283 }
284 }
285
286 return v;
287}
288
289static void TestCode(const uint16_t* data,
Phil Wang751beff2015-08-28 15:17:15 +0800290 bool has_result = false,
291 int32_t expected = 0) {
David Brazdil58282f42016-01-14 12:45:10 +0000292 for (InstructionSet target_isa : GetTargetISAs()) {
293 ArenaPool pool;
294 ArenaAllocator arena(&pool);
295 HGraph* graph = CreateGraph(&arena);
296 HGraphBuilder builder(graph);
297 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
298 bool graph_built = builder.BuildGraph(*item);
299 ASSERT_TRUE(graph_built);
300 // Remove suspend checks, they cannot be executed in this context.
301 RemoveSuspendChecks(graph);
302 TransformToSsa(graph);
303 RunCode(target_isa, graph, [](HGraph*) {}, has_result, expected);
304 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100305}
306
David Brazdil58282f42016-01-14 12:45:10 +0000307static void TestCodeLong(const uint16_t* data,
Phil Wang751beff2015-08-28 15:17:15 +0800308 bool has_result,
309 int64_t expected) {
David Brazdil58282f42016-01-14 12:45:10 +0000310 for (InstructionSet target_isa : GetTargetISAs()) {
311 ArenaPool pool;
312 ArenaAllocator arena(&pool);
313 HGraph* graph = CreateGraph(&arena);
314 HGraphBuilder builder(graph, Primitive::kPrimLong);
315 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
316 bool graph_built = builder.BuildGraph(*item);
317 ASSERT_TRUE(graph_built);
318 // Remove suspend checks, they cannot be executed in this context.
319 RemoveSuspendChecks(graph);
320 TransformToSsa(graph);
321 RunCode(target_isa, graph, [](HGraph*) {}, has_result, expected);
322 }
Roland Levillain55dcfb52014-10-24 18:09:09 +0100323}
324
David Brazdil58282f42016-01-14 12:45:10 +0000325class CodegenTest : public CommonCompilerTest {};
Phil Wang751beff2015-08-28 15:17:15 +0800326
David Brazdil58282f42016-01-14 12:45:10 +0000327TEST_F(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000328 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
David Brazdil58282f42016-01-14 12:45:10 +0000329 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000330}
331
David Brazdil58282f42016-01-14 12:45:10 +0000332TEST_F(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000333 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000334 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000335 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000336
David Brazdil58282f42016-01-14 12:45:10 +0000337 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000338}
339
David Brazdil58282f42016-01-14 12:45:10 +0000340TEST_F(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000341 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000342 Instruction::GOTO | 0x100,
343 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000344 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000345
David Brazdil58282f42016-01-14 12:45:10 +0000346 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000347}
348
David Brazdil58282f42016-01-14 12:45:10 +0000349TEST_F(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000350 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000351 Instruction::GOTO | 0x200,
352 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000353 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000354
David Brazdil58282f42016-01-14 12:45:10 +0000355 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000356
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000357 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000358 Instruction::GOTO_16, 3,
359 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000360 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000361
David Brazdil58282f42016-01-14 12:45:10 +0000362 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000364 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000365 Instruction::GOTO_32, 4, 0,
366 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000367 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000368
David Brazdil58282f42016-01-14 12:45:10 +0000369 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370}
371
David Brazdil58282f42016-01-14 12:45:10 +0000372TEST_F(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000373 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000374 Instruction::RETURN_VOID,
375 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000376 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000377
David Brazdil58282f42016-01-14 12:45:10 +0000378 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000379}
380
David Brazdil58282f42016-01-14 12:45:10 +0000381TEST_F(CodegenTest, CFG5) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000382 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
383 Instruction::CONST_4 | 0 | 0,
384 Instruction::IF_EQ, 3,
385 Instruction::GOTO | 0x100,
386 Instruction::RETURN_VOID);
387
David Brazdil58282f42016-01-14 12:45:10 +0000388 TestCode(data);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000389}
390
David Brazdil58282f42016-01-14 12:45:10 +0000391TEST_F(CodegenTest, IntConstant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000392 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
393 Instruction::CONST_4 | 0 | 0,
394 Instruction::RETURN_VOID);
395
David Brazdil58282f42016-01-14 12:45:10 +0000396 TestCode(data);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000397}
398
David Brazdil58282f42016-01-14 12:45:10 +0000399TEST_F(CodegenTest, Return1) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000400 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
401 Instruction::CONST_4 | 0 | 0,
402 Instruction::RETURN | 0);
403
David Brazdil58282f42016-01-14 12:45:10 +0000404 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000405}
406
David Brazdil58282f42016-01-14 12:45:10 +0000407TEST_F(CodegenTest, Return2) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000408 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
409 Instruction::CONST_4 | 0 | 0,
410 Instruction::CONST_4 | 0 | 1 << 8,
411 Instruction::RETURN | 1 << 8);
412
David Brazdil58282f42016-01-14 12:45:10 +0000413 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000414}
415
David Brazdil58282f42016-01-14 12:45:10 +0000416TEST_F(CodegenTest, Return3) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000417 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
418 Instruction::CONST_4 | 0 | 0,
419 Instruction::CONST_4 | 1 << 8 | 1 << 12,
420 Instruction::RETURN | 1 << 8);
421
David Brazdil58282f42016-01-14 12:45:10 +0000422 TestCode(data, true, 1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000423}
424
David Brazdil58282f42016-01-14 12:45:10 +0000425TEST_F(CodegenTest, ReturnIf1) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000426 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
427 Instruction::CONST_4 | 0 | 0,
428 Instruction::CONST_4 | 1 << 8 | 1 << 12,
429 Instruction::IF_EQ, 3,
430 Instruction::RETURN | 0 << 8,
431 Instruction::RETURN | 1 << 8);
432
David Brazdil58282f42016-01-14 12:45:10 +0000433 TestCode(data, true, 1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000434}
435
David Brazdil58282f42016-01-14 12:45:10 +0000436TEST_F(CodegenTest, ReturnIf2) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000437 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
438 Instruction::CONST_4 | 0 | 0,
439 Instruction::CONST_4 | 1 << 8 | 1 << 12,
440 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
441 Instruction::RETURN | 0 << 8,
442 Instruction::RETURN | 1 << 8);
443
David Brazdil58282f42016-01-14 12:45:10 +0000444 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000445}
446
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100447// Exercise bit-wise (one's complement) not-int instruction.
448#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
David Brazdil58282f42016-01-14 12:45:10 +0000449TEST_F(CodegenTest, TEST_NAME) { \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100450 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100451 const uint16_t input_lo = Low16Bits(input); \
452 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100453 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
454 Instruction::CONST | 0 << 8, input_lo, input_hi, \
455 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
456 Instruction::RETURN | 1 << 8); \
457 \
David Brazdil58282f42016-01-14 12:45:10 +0000458 TestCode(data, true, EXPECTED_OUTPUT); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100459}
460
461NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
462NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
463NOT_INT_TEST(ReturnNotInt0, 0, -1)
464NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100465NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
466NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
467NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
468NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100469
470#undef NOT_INT_TEST
471
Roland Levillain55dcfb52014-10-24 18:09:09 +0100472// Exercise bit-wise (one's complement) not-long instruction.
473#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
David Brazdil58282f42016-01-14 12:45:10 +0000474TEST_F(CodegenTest, TEST_NAME) { \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100475 const int64_t input = INPUT; \
476 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
477 const uint16_t word1 = High16Bits(Low32Bits(input)); \
478 const uint16_t word2 = Low16Bits(High32Bits(input)); \
479 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
480 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
481 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
482 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
483 Instruction::RETURN_WIDE | 2 << 8); \
484 \
David Brazdil58282f42016-01-14 12:45:10 +0000485 TestCodeLong(data, true, EXPECTED_OUTPUT); \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100486}
487
488NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
489NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
490NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
491NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
492
493NOT_LONG_TEST(ReturnNotLongINT32_MIN,
494 INT64_C(-2147483648),
495 INT64_C(2147483647)) // (2^31) - 1
496NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
497 INT64_C(-2147483647),
498 INT64_C(2147483646)) // (2^31) - 2
499NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
500 INT64_C(2147483646),
501 INT64_C(-2147483647)) // -(2^31) - 1
502NOT_LONG_TEST(ReturnNotLongINT32_MAX,
503 INT64_C(2147483647),
504 INT64_C(-2147483648)) // -(2^31)
505
506// Note that the C++ compiler won't accept
507// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
508// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
509NOT_LONG_TEST(ReturnNotINT64_MIN,
510 INT64_C(-9223372036854775807)-1,
511 INT64_C(9223372036854775807)); // (2^63) - 1
512NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
513 INT64_C(-9223372036854775807),
514 INT64_C(9223372036854775806)); // (2^63) - 2
515NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
516 INT64_C(9223372036854775806),
517 INT64_C(-9223372036854775807)); // -(2^63) - 1
518NOT_LONG_TEST(ReturnNotLongINT64_MAX,
519 INT64_C(9223372036854775807),
520 INT64_C(-9223372036854775807)-1); // -(2^63)
521
522#undef NOT_LONG_TEST
523
David Brazdil58282f42016-01-14 12:45:10 +0000524TEST_F(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000525 const int64_t input = INT64_C(4294967296); // 2^32
526 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
527 const uint16_t word1 = High16Bits(Low32Bits(input));
528 const uint16_t word2 = Low16Bits(High32Bits(input));
529 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
530 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
531 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
532 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
533 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
534 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
535 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
536 Instruction::RETURN_WIDE | 2 << 8);
537
David Brazdil58282f42016-01-14 12:45:10 +0000538 TestCodeLong(data, true, 1);
Roland Levillain946e1432014-11-11 17:35:19 +0000539}
540
David Brazdil58282f42016-01-14 12:45:10 +0000541TEST_F(CodegenTest, ReturnAdd1) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000542 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
543 Instruction::CONST_4 | 3 << 12 | 0,
544 Instruction::CONST_4 | 4 << 12 | 1 << 8,
545 Instruction::ADD_INT, 1 << 8 | 0,
546 Instruction::RETURN);
547
David Brazdil58282f42016-01-14 12:45:10 +0000548 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000549}
550
David Brazdil58282f42016-01-14 12:45:10 +0000551TEST_F(CodegenTest, ReturnAdd2) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000552 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
553 Instruction::CONST_4 | 3 << 12 | 0,
554 Instruction::CONST_4 | 4 << 12 | 1 << 8,
555 Instruction::ADD_INT_2ADDR | 1 << 12,
556 Instruction::RETURN);
557
David Brazdil58282f42016-01-14 12:45:10 +0000558 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000559}
560
David Brazdil58282f42016-01-14 12:45:10 +0000561TEST_F(CodegenTest, ReturnAdd3) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000562 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
563 Instruction::CONST_4 | 4 << 12 | 0 << 8,
564 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
565 Instruction::RETURN);
566
David Brazdil58282f42016-01-14 12:45:10 +0000567 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000568}
569
David Brazdil58282f42016-01-14 12:45:10 +0000570TEST_F(CodegenTest, ReturnAdd4) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000571 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
572 Instruction::CONST_4 | 4 << 12 | 0 << 8,
573 Instruction::ADD_INT_LIT16, 3,
574 Instruction::RETURN);
575
David Brazdil58282f42016-01-14 12:45:10 +0000576 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000577}
578
David Brazdil58282f42016-01-14 12:45:10 +0000579TEST_F(CodegenTest, ReturnMulInt) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100580 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
581 Instruction::CONST_4 | 3 << 12 | 0,
582 Instruction::CONST_4 | 4 << 12 | 1 << 8,
583 Instruction::MUL_INT, 1 << 8 | 0,
584 Instruction::RETURN);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100585
David Brazdil58282f42016-01-14 12:45:10 +0000586 TestCode(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100587}
588
David Brazdil58282f42016-01-14 12:45:10 +0000589TEST_F(CodegenTest, ReturnMulInt2addr) {
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_2ADDR | 1 << 12,
594 Instruction::RETURN);
595
David Brazdil58282f42016-01-14 12:45:10 +0000596 TestCode(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100597}
598
David Brazdil58282f42016-01-14 12:45:10 +0000599TEST_F(CodegenTest, ReturnMulLong) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100600 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
David Brazdil58282f42016-01-14 12:45:10 +0000601 Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0,
602 Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0,
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100603 Instruction::MUL_LONG, 2 << 8 | 0,
604 Instruction::RETURN_WIDE);
605
David Brazdil58282f42016-01-14 12:45:10 +0000606 TestCodeLong(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100607}
608
David Brazdil58282f42016-01-14 12:45:10 +0000609TEST_F(CodegenTest, ReturnMulLong2addr) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100610 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
David Brazdil58282f42016-01-14 12:45:10 +0000611 Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0,
612 Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0,
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100613 Instruction::MUL_LONG_2ADDR | 2 << 12,
614 Instruction::RETURN_WIDE);
615
David Brazdil58282f42016-01-14 12:45:10 +0000616 TestCodeLong(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100617}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100618
David Brazdil58282f42016-01-14 12:45:10 +0000619TEST_F(CodegenTest, ReturnMulIntLit8) {
Calin Juravle34bacdf2014-10-07 20:23:36 +0100620 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
621 Instruction::CONST_4 | 4 << 12 | 0 << 8,
622 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
623 Instruction::RETURN);
624
David Brazdil58282f42016-01-14 12:45:10 +0000625 TestCode(data, true, 12);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100626}
627
David Brazdil58282f42016-01-14 12:45:10 +0000628TEST_F(CodegenTest, ReturnMulIntLit16) {
Calin Juravle34bacdf2014-10-07 20:23:36 +0100629 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
630 Instruction::CONST_4 | 4 << 12 | 0 << 8,
631 Instruction::MUL_INT_LIT16, 3,
632 Instruction::RETURN);
633
David Brazdil58282f42016-01-14 12:45:10 +0000634 TestCode(data, true, 12);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100635}
636
David Brazdil58282f42016-01-14 12:45:10 +0000637TEST_F(CodegenTest, NonMaterializedCondition) {
638 for (InstructionSet target_isa : GetTargetISAs()) {
Alexandre Rames92730742014-10-01 12:55:56 +0100639 ArenaPool pool;
640 ArenaAllocator allocator(&pool);
David Brazdil58282f42016-01-14 12:45:10 +0000641
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100642 HGraph* graph = CreateGraph(&allocator);
David Brazdil58282f42016-01-14 12:45:10 +0000643 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
644 graph->AddBlock(entry);
645 graph->SetEntryBlock(entry);
646 entry->AddInstruction(new (&allocator) HGoto());
Alexandre Rames92730742014-10-01 12:55:56 +0100647
David Brazdil58282f42016-01-14 12:45:10 +0000648 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
649 graph->AddBlock(first_block);
650 entry->AddSuccessor(first_block);
651 HIntConstant* constant0 = graph->GetIntConstant(0);
652 HIntConstant* constant1 = graph->GetIntConstant(1);
653 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
654 first_block->AddInstruction(equal);
655 first_block->AddInstruction(new (&allocator) HIf(equal));
656
657 HBasicBlock* then_block = new (&allocator) HBasicBlock(graph);
658 HBasicBlock* else_block = new (&allocator) HBasicBlock(graph);
Alexandre Rames92730742014-10-01 12:55:56 +0100659 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
Alexandre Rames92730742014-10-01 12:55:56 +0100660 graph->SetExitBlock(exit_block);
661
David Brazdil58282f42016-01-14 12:45:10 +0000662 graph->AddBlock(then_block);
663 graph->AddBlock(else_block);
664 graph->AddBlock(exit_block);
665 first_block->AddSuccessor(then_block);
666 first_block->AddSuccessor(else_block);
667 then_block->AddSuccessor(exit_block);
668 else_block->AddSuccessor(exit_block);
669
670 exit_block->AddInstruction(new (&allocator) HExit());
671 then_block->AddInstruction(new (&allocator) HReturn(constant0));
672 else_block->AddInstruction(new (&allocator) HReturn(constant1));
673
David Brazdilb11b0722016-01-28 16:22:40 +0000674 ASSERT_FALSE(equal->IsEmittedAtUseSite());
David Brazdil58282f42016-01-14 12:45:10 +0000675 TransformToSsa(graph);
676 PrepareForRegisterAllocation(graph).Run();
David Brazdilb11b0722016-01-28 16:22:40 +0000677 ASSERT_TRUE(equal->IsEmittedAtUseSite());
Alexandre Rames92730742014-10-01 12:55:56 +0100678
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800679 auto hook_before_codegen = [](HGraph* graph_in) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100680 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800681 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100682 block->InsertInstructionBefore(move, block->GetLastInstruction());
683 };
684
David Brazdil58282f42016-01-14 12:45:10 +0000685 RunCode(target_isa, graph, hook_before_codegen, true, 0);
Alexandre Rames92730742014-10-01 12:55:56 +0100686 }
687}
688
David Brazdil58282f42016-01-14 12:45:10 +0000689TEST_F(CodegenTest, MaterializedCondition1) {
690 for (InstructionSet target_isa : GetTargetISAs()) {
691 // Check that condition are materialized correctly. A materialized condition
692 // should yield `1` if it evaluated to true, and `0` otherwise.
693 // We force the materialization of comparisons for different combinations of
Alexandre Rames92730742014-10-01 12:55:56 +0100694
David Brazdil58282f42016-01-14 12:45:10 +0000695 // inputs and check the results.
Alexandre Rames92730742014-10-01 12:55:56 +0100696
David Brazdil58282f42016-01-14 12:45:10 +0000697 int lhs[] = {1, 2, -1, 2, 0xabc};
698 int rhs[] = {2, 1, 2, -1, 0xabc};
Alexandre Rames92730742014-10-01 12:55:56 +0100699
David Brazdil58282f42016-01-14 12:45:10 +0000700 for (size_t i = 0; i < arraysize(lhs); i++) {
701 ArenaPool pool;
702 ArenaAllocator allocator(&pool);
703 HGraph* graph = CreateGraph(&allocator);
Alexandre Rames92730742014-10-01 12:55:56 +0100704
David Brazdil58282f42016-01-14 12:45:10 +0000705 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
706 graph->AddBlock(entry_block);
707 graph->SetEntryBlock(entry_block);
708 entry_block->AddInstruction(new (&allocator) HGoto());
709 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
710 graph->AddBlock(code_block);
711 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
712 graph->AddBlock(exit_block);
713 exit_block->AddInstruction(new (&allocator) HExit());
Alexandre Rames92730742014-10-01 12:55:56 +0100714
David Brazdil58282f42016-01-14 12:45:10 +0000715 entry_block->AddSuccessor(code_block);
716 code_block->AddSuccessor(exit_block);
717 graph->SetExitBlock(exit_block);
Alexandre Rames92730742014-10-01 12:55:56 +0100718
David Brazdil58282f42016-01-14 12:45:10 +0000719 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
720 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
721 HLessThan cmp_lt(cst_lhs, cst_rhs);
722 code_block->AddInstruction(&cmp_lt);
723 HReturn ret(&cmp_lt);
724 code_block->AddInstruction(&ret);
Alexandre Rames92730742014-10-01 12:55:56 +0100725
David Brazdil58282f42016-01-14 12:45:10 +0000726 TransformToSsa(graph);
727 auto hook_before_codegen = [](HGraph* graph_in) {
728 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
729 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
730 block->InsertInstructionBefore(move, block->GetLastInstruction());
731 };
732 RunCode(target_isa, graph, hook_before_codegen, true, lhs[i] < rhs[i]);
733 }
Alexandre Rames92730742014-10-01 12:55:56 +0100734 }
735}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100736
David Brazdil58282f42016-01-14 12:45:10 +0000737TEST_F(CodegenTest, MaterializedCondition2) {
738 for (InstructionSet target_isa : GetTargetISAs()) {
739 // Check that HIf correctly interprets a materialized condition.
740 // We force the materialization of comparisons for different combinations of
741 // inputs. An HIf takes the materialized combination as input and returns a
742 // value that we verify.
743
744 int lhs[] = {1, 2, -1, 2, 0xabc};
745 int rhs[] = {2, 1, 2, -1, 0xabc};
746
747
748 for (size_t i = 0; i < arraysize(lhs); i++) {
749 ArenaPool pool;
750 ArenaAllocator allocator(&pool);
751 HGraph* graph = CreateGraph(&allocator);
752
753 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
754 graph->AddBlock(entry_block);
755 graph->SetEntryBlock(entry_block);
756 entry_block->AddInstruction(new (&allocator) HGoto());
757
758 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
759 graph->AddBlock(if_block);
760 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
761 graph->AddBlock(if_true_block);
762 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
763 graph->AddBlock(if_false_block);
764 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
765 graph->AddBlock(exit_block);
766 exit_block->AddInstruction(new (&allocator) HExit());
767
768 graph->SetEntryBlock(entry_block);
769 entry_block->AddSuccessor(if_block);
770 if_block->AddSuccessor(if_true_block);
771 if_block->AddSuccessor(if_false_block);
772 if_true_block->AddSuccessor(exit_block);
773 if_false_block->AddSuccessor(exit_block);
774 graph->SetExitBlock(exit_block);
775
776 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
777 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
778 HLessThan cmp_lt(cst_lhs, cst_rhs);
779 if_block->AddInstruction(&cmp_lt);
780 // We insert a temporary to separate the HIf from the HLessThan and force
781 // the materialization of the condition.
782 HTemporary force_materialization(0);
783 if_block->AddInstruction(&force_materialization);
784 HIf if_lt(&cmp_lt);
785 if_block->AddInstruction(&if_lt);
786
787 HIntConstant* cst_lt = graph->GetIntConstant(1);
788 HReturn ret_lt(cst_lt);
789 if_true_block->AddInstruction(&ret_lt);
790 HIntConstant* cst_ge = graph->GetIntConstant(0);
791 HReturn ret_ge(cst_ge);
792 if_false_block->AddInstruction(&ret_ge);
793
794 TransformToSsa(graph);
795 auto hook_before_codegen = [](HGraph* graph_in) {
796 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
797 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
798 block->InsertInstructionBefore(move, block->GetLastInstruction());
799 };
800 RunCode(target_isa, graph, hook_before_codegen, true, lhs[i] < rhs[i]);
801 }
802 }
803}
804
805TEST_F(CodegenTest, ReturnDivIntLit8) {
Calin Juravled0d48522014-11-04 16:40:20 +0000806 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
807 Instruction::CONST_4 | 4 << 12 | 0 << 8,
808 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
809 Instruction::RETURN);
810
David Brazdil58282f42016-01-14 12:45:10 +0000811 TestCode(data, true, 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000812}
813
David Brazdil58282f42016-01-14 12:45:10 +0000814TEST_F(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000815 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
816 Instruction::CONST_4 | 4 << 12 | 0,
817 Instruction::CONST_4 | 2 << 12 | 1 << 8,
818 Instruction::DIV_INT_2ADDR | 1 << 12,
819 Instruction::RETURN);
820
David Brazdil58282f42016-01-14 12:45:10 +0000821 TestCode(data, true, 2);
Calin Juravle865fc882014-11-06 17:09:03 +0000822}
823
Aart Bike9f37602015-10-09 11:15:55 -0700824// Helper method.
Phil Wang751beff2015-08-28 15:17:15 +0800825static void TestComparison(IfCondition condition,
826 int64_t i,
827 int64_t j,
828 Primitive::Type type,
829 const InstructionSet target_isa) {
Aart Bike9f37602015-10-09 11:15:55 -0700830 ArenaPool pool;
831 ArenaAllocator allocator(&pool);
832 HGraph* graph = CreateGraph(&allocator);
833
834 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
835 graph->AddBlock(entry_block);
836 graph->SetEntryBlock(entry_block);
837 entry_block->AddInstruction(new (&allocator) HGoto());
838
839 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
840 graph->AddBlock(block);
841
842 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
843 graph->AddBlock(exit_block);
844 graph->SetExitBlock(exit_block);
845 exit_block->AddInstruction(new (&allocator) HExit());
846
847 entry_block->AddSuccessor(block);
848 block->AddSuccessor(exit_block);
849
850 HInstruction* op1;
851 HInstruction* op2;
852 if (type == Primitive::kPrimInt) {
853 op1 = graph->GetIntConstant(i);
854 op2 = graph->GetIntConstant(j);
855 } else {
856 DCHECK_EQ(type, Primitive::kPrimLong);
857 op1 = graph->GetLongConstant(i);
858 op2 = graph->GetLongConstant(j);
859 }
860
861 HInstruction* comparison = nullptr;
862 bool expected_result = false;
863 const uint64_t x = i;
864 const uint64_t y = j;
865 switch (condition) {
866 case kCondEQ:
867 comparison = new (&allocator) HEqual(op1, op2);
868 expected_result = (i == j);
869 break;
870 case kCondNE:
871 comparison = new (&allocator) HNotEqual(op1, op2);
872 expected_result = (i != j);
873 break;
874 case kCondLT:
875 comparison = new (&allocator) HLessThan(op1, op2);
876 expected_result = (i < j);
877 break;
878 case kCondLE:
879 comparison = new (&allocator) HLessThanOrEqual(op1, op2);
880 expected_result = (i <= j);
881 break;
882 case kCondGT:
883 comparison = new (&allocator) HGreaterThan(op1, op2);
884 expected_result = (i > j);
885 break;
886 case kCondGE:
887 comparison = new (&allocator) HGreaterThanOrEqual(op1, op2);
888 expected_result = (i >= j);
889 break;
890 case kCondB:
891 comparison = new (&allocator) HBelow(op1, op2);
892 expected_result = (x < y);
893 break;
894 case kCondBE:
895 comparison = new (&allocator) HBelowOrEqual(op1, op2);
896 expected_result = (x <= y);
897 break;
898 case kCondA:
899 comparison = new (&allocator) HAbove(op1, op2);
900 expected_result = (x > y);
901 break;
902 case kCondAE:
903 comparison = new (&allocator) HAboveOrEqual(op1, op2);
904 expected_result = (x >= y);
905 break;
906 }
907 block->AddInstruction(comparison);
908 block->AddInstruction(new (&allocator) HReturn(comparison));
909
David Brazdil58282f42016-01-14 12:45:10 +0000910 TransformToSsa(graph);
911 RunCode(target_isa, graph, [](HGraph*) {}, true, expected_result);
Aart Bike9f37602015-10-09 11:15:55 -0700912}
913
David Brazdil58282f42016-01-14 12:45:10 +0000914TEST_F(CodegenTest, ComparisonsInt) {
915 for (InstructionSet target_isa : GetTargetISAs()) {
916 for (int64_t i = -1; i <= 1; i++) {
917 for (int64_t j = -1; j <= 1; j++) {
918 TestComparison(kCondEQ, i, j, Primitive::kPrimInt, target_isa);
919 TestComparison(kCondNE, i, j, Primitive::kPrimInt, target_isa);
920 TestComparison(kCondLT, i, j, Primitive::kPrimInt, target_isa);
921 TestComparison(kCondLE, i, j, Primitive::kPrimInt, target_isa);
922 TestComparison(kCondGT, i, j, Primitive::kPrimInt, target_isa);
923 TestComparison(kCondGE, i, j, Primitive::kPrimInt, target_isa);
924 TestComparison(kCondB, i, j, Primitive::kPrimInt, target_isa);
925 TestComparison(kCondBE, i, j, Primitive::kPrimInt, target_isa);
926 TestComparison(kCondA, i, j, Primitive::kPrimInt, target_isa);
927 TestComparison(kCondAE, i, j, Primitive::kPrimInt, target_isa);
928 }
Aart Bike9f37602015-10-09 11:15:55 -0700929 }
930 }
931}
932
David Brazdil58282f42016-01-14 12:45:10 +0000933TEST_F(CodegenTest, ComparisonsLong) {
Aart Bike9f37602015-10-09 11:15:55 -0700934 // TODO: make MIPS work for long
935 if (kRuntimeISA == kMips || kRuntimeISA == kMips64) {
936 return;
937 }
938
David Brazdil58282f42016-01-14 12:45:10 +0000939 for (InstructionSet target_isa : GetTargetISAs()) {
940 if (target_isa == kMips || target_isa == kMips64) {
941 continue;
942 }
Phil Wang751beff2015-08-28 15:17:15 +0800943
David Brazdil58282f42016-01-14 12:45:10 +0000944 for (int64_t i = -1; i <= 1; i++) {
945 for (int64_t j = -1; j <= 1; j++) {
946 TestComparison(kCondEQ, i, j, Primitive::kPrimLong, target_isa);
947 TestComparison(kCondNE, i, j, Primitive::kPrimLong, target_isa);
948 TestComparison(kCondLT, i, j, Primitive::kPrimLong, target_isa);
949 TestComparison(kCondLE, i, j, Primitive::kPrimLong, target_isa);
950 TestComparison(kCondGT, i, j, Primitive::kPrimLong, target_isa);
951 TestComparison(kCondGE, i, j, Primitive::kPrimLong, target_isa);
952 TestComparison(kCondB, i, j, Primitive::kPrimLong, target_isa);
953 TestComparison(kCondBE, i, j, Primitive::kPrimLong, target_isa);
954 TestComparison(kCondA, i, j, Primitive::kPrimLong, target_isa);
955 TestComparison(kCondAE, i, j, Primitive::kPrimLong, target_isa);
956 }
Aart Bike9f37602015-10-09 11:15:55 -0700957 }
958 }
959}
960
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000961} // namespace art