blob: 6be79fa75cb60532193edf5f35039fca806c58fb [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) {
David Brazdilbadd8262016-02-02 16:28:56 +0000209 GraphChecker graph_checker(graph);
David Brazdil58282f42016-01-14 12:45:10 +0000210 graph_checker.Run();
David Brazdilbadd8262016-02-02 16:28:56 +0000211 if (!graph_checker.IsValid()) {
212 for (auto error : graph_checker.GetErrors()) {
213 std::cout << error << std::endl;
214 }
215 }
David Brazdil58282f42016-01-14 12:45:10 +0000216 ASSERT_TRUE(graph_checker.IsValid());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100217
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100218 SsaLivenessAnalysis liveness(graph, codegen);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100219
David Brazdil58282f42016-01-14 12:45:10 +0000220 PrepareForRegisterAllocation(graph).Run();
221 liveness.Analyze();
222 RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100223 hook_before_codegen(graph);
224
225 InternalCodeAllocator allocator;
David Brazdil58282f42016-01-14 12:45:10 +0000226 codegen->Compile(&allocator);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100227 Run(allocator, *codegen, has_result, expected);
228}
229
Roland Levillain55dcfb52014-10-24 18:09:09 +0100230template <typename Expected>
David Brazdil58282f42016-01-14 12:45:10 +0000231static void RunCode(InstructionSet target_isa,
232 HGraph* graph,
233 std::function<void(HGraph*)> hook_before_codegen,
234 bool has_result,
235 Expected expected) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000236 CompilerOptions compiler_options;
Phil Wang751beff2015-08-28 15:17:15 +0800237 if (target_isa == kArm || target_isa == kThumb2) {
238 std::unique_ptr<const ArmInstructionSetFeatures> features_arm(
239 ArmInstructionSetFeatures::FromCppDefines());
240 TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000241 RunCode(&codegenARM, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800242 } else if (target_isa == kArm64) {
243 std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64(
244 Arm64InstructionSetFeatures::FromCppDefines());
245 arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000246 RunCode(&codegenARM64, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800247 } else if (target_isa == kX86) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400248 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
249 X86InstructionSetFeatures::FromCppDefines());
250 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000251 RunCode(&codegenX86, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800252 } else if (target_isa == kX86_64) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400253 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
254 X86_64InstructionSetFeatures::FromCppDefines());
255 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000256 RunCode(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800257 } else if (target_isa == kMips) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200258 std::unique_ptr<const MipsInstructionSetFeatures> features_mips(
259 MipsInstructionSetFeatures::FromCppDefines());
260 mips::CodeGeneratorMIPS codegenMIPS(graph, *features_mips.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000261 RunCode(&codegenMIPS, graph, hook_before_codegen, has_result, expected);
Phil Wang751beff2015-08-28 15:17:15 +0800262 } else if (target_isa == kMips64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700263 std::unique_ptr<const Mips64InstructionSetFeatures> features_mips64(
264 Mips64InstructionSetFeatures::FromCppDefines());
265 mips64::CodeGeneratorMIPS64 codegenMIPS64(graph, *features_mips64.get(), compiler_options);
David Brazdil58282f42016-01-14 12:45:10 +0000266 RunCode(&codegenMIPS64, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100267 }
268}
269
David Brazdil58282f42016-01-14 12:45:10 +0000270static ::std::vector<InstructionSet> GetTargetISAs() {
271 ::std::vector<InstructionSet> v;
272 // Add all ISAs that are executable on hardware or on simulator.
273 const ::std::vector<InstructionSet> executable_isa_candidates = {
274 kArm,
275 kArm64,
276 kThumb2,
277 kX86,
278 kX86_64,
279 kMips,
280 kMips64
281 };
282
283 for (auto target_isa : executable_isa_candidates) {
284 if (CanExecute(target_isa)) {
285 v.push_back(target_isa);
286 }
287 }
288
289 return v;
290}
291
292static void TestCode(const uint16_t* data,
Phil Wang751beff2015-08-28 15:17:15 +0800293 bool has_result = false,
294 int32_t expected = 0) {
David Brazdil58282f42016-01-14 12:45:10 +0000295 for (InstructionSet target_isa : GetTargetISAs()) {
296 ArenaPool pool;
297 ArenaAllocator arena(&pool);
David Brazdilbadd8262016-02-02 16:28:56 +0000298 HGraph* graph = CreateCFG(&arena, data);
David Brazdil58282f42016-01-14 12:45:10 +0000299 // Remove suspend checks, they cannot be executed in this context.
300 RemoveSuspendChecks(graph);
David Brazdil58282f42016-01-14 12:45:10 +0000301 RunCode(target_isa, graph, [](HGraph*) {}, has_result, expected);
302 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100303}
304
David Brazdil58282f42016-01-14 12:45:10 +0000305static void TestCodeLong(const uint16_t* data,
Phil Wang751beff2015-08-28 15:17:15 +0800306 bool has_result,
307 int64_t expected) {
David Brazdil58282f42016-01-14 12:45:10 +0000308 for (InstructionSet target_isa : GetTargetISAs()) {
309 ArenaPool pool;
310 ArenaAllocator arena(&pool);
David Brazdilbadd8262016-02-02 16:28:56 +0000311 HGraph* graph = CreateCFG(&arena, data, Primitive::kPrimLong);
David Brazdil58282f42016-01-14 12:45:10 +0000312 // Remove suspend checks, they cannot be executed in this context.
313 RemoveSuspendChecks(graph);
David Brazdil58282f42016-01-14 12:45:10 +0000314 RunCode(target_isa, graph, [](HGraph*) {}, has_result, expected);
315 }
Roland Levillain55dcfb52014-10-24 18:09:09 +0100316}
317
David Brazdil58282f42016-01-14 12:45:10 +0000318class CodegenTest : public CommonCompilerTest {};
Phil Wang751beff2015-08-28 15:17:15 +0800319
David Brazdil58282f42016-01-14 12:45:10 +0000320TEST_F(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000321 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
David Brazdil58282f42016-01-14 12:45:10 +0000322 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000323}
324
David Brazdil58282f42016-01-14 12:45:10 +0000325TEST_F(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000326 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000327 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000328 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000329
David Brazdil58282f42016-01-14 12:45:10 +0000330 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331}
332
David Brazdil58282f42016-01-14 12:45:10 +0000333TEST_F(CodegenTest, CFG2) {
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::GOTO | 0x100,
336 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000337 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000338
David Brazdil58282f42016-01-14 12:45:10 +0000339 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000340}
341
David Brazdil58282f42016-01-14 12:45:10 +0000342TEST_F(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000343 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000344 Instruction::GOTO | 0x200,
345 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000346 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000347
David Brazdil58282f42016-01-14 12:45:10 +0000348 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000349
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000350 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000351 Instruction::GOTO_16, 3,
352 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000353 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000354
David Brazdil58282f42016-01-14 12:45:10 +0000355 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000356
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000357 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000358 Instruction::GOTO_32, 4, 0,
359 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000360 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000361
David Brazdil58282f42016-01-14 12:45:10 +0000362 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363}
364
David Brazdil58282f42016-01-14 12:45:10 +0000365TEST_F(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000366 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000367 Instruction::RETURN_VOID,
368 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000369 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370
David Brazdil58282f42016-01-14 12:45:10 +0000371 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000372}
373
David Brazdil58282f42016-01-14 12:45:10 +0000374TEST_F(CodegenTest, CFG5) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000375 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
376 Instruction::CONST_4 | 0 | 0,
377 Instruction::IF_EQ, 3,
378 Instruction::GOTO | 0x100,
379 Instruction::RETURN_VOID);
380
David Brazdil58282f42016-01-14 12:45:10 +0000381 TestCode(data);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000382}
383
David Brazdil58282f42016-01-14 12:45:10 +0000384TEST_F(CodegenTest, IntConstant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000385 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
386 Instruction::CONST_4 | 0 | 0,
387 Instruction::RETURN_VOID);
388
David Brazdil58282f42016-01-14 12:45:10 +0000389 TestCode(data);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000390}
391
David Brazdil58282f42016-01-14 12:45:10 +0000392TEST_F(CodegenTest, Return1) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000393 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
394 Instruction::CONST_4 | 0 | 0,
395 Instruction::RETURN | 0);
396
David Brazdil58282f42016-01-14 12:45:10 +0000397 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000398}
399
David Brazdil58282f42016-01-14 12:45:10 +0000400TEST_F(CodegenTest, Return2) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000401 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
402 Instruction::CONST_4 | 0 | 0,
403 Instruction::CONST_4 | 0 | 1 << 8,
404 Instruction::RETURN | 1 << 8);
405
David Brazdil58282f42016-01-14 12:45:10 +0000406 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000407}
408
David Brazdil58282f42016-01-14 12:45:10 +0000409TEST_F(CodegenTest, Return3) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000410 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
411 Instruction::CONST_4 | 0 | 0,
412 Instruction::CONST_4 | 1 << 8 | 1 << 12,
413 Instruction::RETURN | 1 << 8);
414
David Brazdil58282f42016-01-14 12:45:10 +0000415 TestCode(data, true, 1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000416}
417
David Brazdil58282f42016-01-14 12:45:10 +0000418TEST_F(CodegenTest, ReturnIf1) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000419 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
420 Instruction::CONST_4 | 0 | 0,
421 Instruction::CONST_4 | 1 << 8 | 1 << 12,
422 Instruction::IF_EQ, 3,
423 Instruction::RETURN | 0 << 8,
424 Instruction::RETURN | 1 << 8);
425
David Brazdil58282f42016-01-14 12:45:10 +0000426 TestCode(data, true, 1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000427}
428
David Brazdil58282f42016-01-14 12:45:10 +0000429TEST_F(CodegenTest, ReturnIf2) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000430 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
431 Instruction::CONST_4 | 0 | 0,
432 Instruction::CONST_4 | 1 << 8 | 1 << 12,
433 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
434 Instruction::RETURN | 0 << 8,
435 Instruction::RETURN | 1 << 8);
436
David Brazdil58282f42016-01-14 12:45:10 +0000437 TestCode(data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000438}
439
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100440// Exercise bit-wise (one's complement) not-int instruction.
441#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
David Brazdil58282f42016-01-14 12:45:10 +0000442TEST_F(CodegenTest, TEST_NAME) { \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100443 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100444 const uint16_t input_lo = Low16Bits(input); \
445 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100446 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
447 Instruction::CONST | 0 << 8, input_lo, input_hi, \
448 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
449 Instruction::RETURN | 1 << 8); \
450 \
David Brazdil58282f42016-01-14 12:45:10 +0000451 TestCode(data, true, EXPECTED_OUTPUT); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100452}
453
454NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
455NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
456NOT_INT_TEST(ReturnNotInt0, 0, -1)
457NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100458NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
459NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
460NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
461NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100462
463#undef NOT_INT_TEST
464
Roland Levillain55dcfb52014-10-24 18:09:09 +0100465// Exercise bit-wise (one's complement) not-long instruction.
466#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
David Brazdil58282f42016-01-14 12:45:10 +0000467TEST_F(CodegenTest, TEST_NAME) { \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100468 const int64_t input = INPUT; \
469 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
470 const uint16_t word1 = High16Bits(Low32Bits(input)); \
471 const uint16_t word2 = Low16Bits(High32Bits(input)); \
472 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
473 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
474 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
475 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
476 Instruction::RETURN_WIDE | 2 << 8); \
477 \
David Brazdil58282f42016-01-14 12:45:10 +0000478 TestCodeLong(data, true, EXPECTED_OUTPUT); \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100479}
480
481NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
482NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
483NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
484NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
485
486NOT_LONG_TEST(ReturnNotLongINT32_MIN,
487 INT64_C(-2147483648),
488 INT64_C(2147483647)) // (2^31) - 1
489NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
490 INT64_C(-2147483647),
491 INT64_C(2147483646)) // (2^31) - 2
492NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
493 INT64_C(2147483646),
494 INT64_C(-2147483647)) // -(2^31) - 1
495NOT_LONG_TEST(ReturnNotLongINT32_MAX,
496 INT64_C(2147483647),
497 INT64_C(-2147483648)) // -(2^31)
498
499// Note that the C++ compiler won't accept
500// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
501// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
502NOT_LONG_TEST(ReturnNotINT64_MIN,
503 INT64_C(-9223372036854775807)-1,
504 INT64_C(9223372036854775807)); // (2^63) - 1
505NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
506 INT64_C(-9223372036854775807),
507 INT64_C(9223372036854775806)); // (2^63) - 2
508NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
509 INT64_C(9223372036854775806),
510 INT64_C(-9223372036854775807)); // -(2^63) - 1
511NOT_LONG_TEST(ReturnNotLongINT64_MAX,
512 INT64_C(9223372036854775807),
513 INT64_C(-9223372036854775807)-1); // -(2^63)
514
515#undef NOT_LONG_TEST
516
David Brazdil58282f42016-01-14 12:45:10 +0000517TEST_F(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000518 const int64_t input = INT64_C(4294967296); // 2^32
519 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
520 const uint16_t word1 = High16Bits(Low32Bits(input));
521 const uint16_t word2 = Low16Bits(High32Bits(input));
522 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
523 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
524 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
525 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
526 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
527 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
528 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
529 Instruction::RETURN_WIDE | 2 << 8);
530
David Brazdil58282f42016-01-14 12:45:10 +0000531 TestCodeLong(data, true, 1);
Roland Levillain946e1432014-11-11 17:35:19 +0000532}
533
David Brazdil58282f42016-01-14 12:45:10 +0000534TEST_F(CodegenTest, ReturnAdd1) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000535 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
536 Instruction::CONST_4 | 3 << 12 | 0,
537 Instruction::CONST_4 | 4 << 12 | 1 << 8,
538 Instruction::ADD_INT, 1 << 8 | 0,
539 Instruction::RETURN);
540
David Brazdil58282f42016-01-14 12:45:10 +0000541 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000542}
543
David Brazdil58282f42016-01-14 12:45:10 +0000544TEST_F(CodegenTest, ReturnAdd2) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000545 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
546 Instruction::CONST_4 | 3 << 12 | 0,
547 Instruction::CONST_4 | 4 << 12 | 1 << 8,
548 Instruction::ADD_INT_2ADDR | 1 << 12,
549 Instruction::RETURN);
550
David Brazdil58282f42016-01-14 12:45:10 +0000551 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000552}
553
David Brazdil58282f42016-01-14 12:45:10 +0000554TEST_F(CodegenTest, ReturnAdd3) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000555 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
556 Instruction::CONST_4 | 4 << 12 | 0 << 8,
557 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
558 Instruction::RETURN);
559
David Brazdil58282f42016-01-14 12:45:10 +0000560 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000561}
562
David Brazdil58282f42016-01-14 12:45:10 +0000563TEST_F(CodegenTest, ReturnAdd4) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000564 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
565 Instruction::CONST_4 | 4 << 12 | 0 << 8,
566 Instruction::ADD_INT_LIT16, 3,
567 Instruction::RETURN);
568
David Brazdil58282f42016-01-14 12:45:10 +0000569 TestCode(data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000570}
571
David Brazdil58282f42016-01-14 12:45:10 +0000572TEST_F(CodegenTest, ReturnMulInt) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100573 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
574 Instruction::CONST_4 | 3 << 12 | 0,
575 Instruction::CONST_4 | 4 << 12 | 1 << 8,
576 Instruction::MUL_INT, 1 << 8 | 0,
577 Instruction::RETURN);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100578
David Brazdil58282f42016-01-14 12:45:10 +0000579 TestCode(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100580}
581
David Brazdil58282f42016-01-14 12:45:10 +0000582TEST_F(CodegenTest, ReturnMulInt2addr) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100583 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
584 Instruction::CONST_4 | 3 << 12 | 0,
585 Instruction::CONST_4 | 4 << 12 | 1 << 8,
586 Instruction::MUL_INT_2ADDR | 1 << 12,
587 Instruction::RETURN);
588
David Brazdil58282f42016-01-14 12:45:10 +0000589 TestCode(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100590}
591
David Brazdil58282f42016-01-14 12:45:10 +0000592TEST_F(CodegenTest, ReturnMulLong) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100593 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
David Brazdil58282f42016-01-14 12:45:10 +0000594 Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0,
595 Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0,
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100596 Instruction::MUL_LONG, 2 << 8 | 0,
597 Instruction::RETURN_WIDE);
598
David Brazdil58282f42016-01-14 12:45:10 +0000599 TestCodeLong(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100600}
601
David Brazdil58282f42016-01-14 12:45:10 +0000602TEST_F(CodegenTest, ReturnMulLong2addr) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100603 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
David Brazdil58282f42016-01-14 12:45:10 +0000604 Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0,
605 Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0,
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100606 Instruction::MUL_LONG_2ADDR | 2 << 12,
607 Instruction::RETURN_WIDE);
608
David Brazdil58282f42016-01-14 12:45:10 +0000609 TestCodeLong(data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100610}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100611
David Brazdil58282f42016-01-14 12:45:10 +0000612TEST_F(CodegenTest, ReturnMulIntLit8) {
Calin Juravle34bacdf2014-10-07 20:23:36 +0100613 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
614 Instruction::CONST_4 | 4 << 12 | 0 << 8,
615 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
616 Instruction::RETURN);
617
David Brazdil58282f42016-01-14 12:45:10 +0000618 TestCode(data, true, 12);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100619}
620
David Brazdil58282f42016-01-14 12:45:10 +0000621TEST_F(CodegenTest, ReturnMulIntLit16) {
Calin Juravle34bacdf2014-10-07 20:23:36 +0100622 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
623 Instruction::CONST_4 | 4 << 12 | 0 << 8,
624 Instruction::MUL_INT_LIT16, 3,
625 Instruction::RETURN);
626
David Brazdil58282f42016-01-14 12:45:10 +0000627 TestCode(data, true, 12);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100628}
629
David Brazdil58282f42016-01-14 12:45:10 +0000630TEST_F(CodegenTest, NonMaterializedCondition) {
631 for (InstructionSet target_isa : GetTargetISAs()) {
Alexandre Rames92730742014-10-01 12:55:56 +0100632 ArenaPool pool;
633 ArenaAllocator allocator(&pool);
David Brazdil58282f42016-01-14 12:45:10 +0000634
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100635 HGraph* graph = CreateGraph(&allocator);
David Brazdilbadd8262016-02-02 16:28:56 +0000636
David Brazdil58282f42016-01-14 12:45:10 +0000637 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
638 graph->AddBlock(entry);
639 graph->SetEntryBlock(entry);
640 entry->AddInstruction(new (&allocator) HGoto());
Alexandre Rames92730742014-10-01 12:55:56 +0100641
David Brazdil58282f42016-01-14 12:45:10 +0000642 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
643 graph->AddBlock(first_block);
644 entry->AddSuccessor(first_block);
645 HIntConstant* constant0 = graph->GetIntConstant(0);
646 HIntConstant* constant1 = graph->GetIntConstant(1);
647 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
648 first_block->AddInstruction(equal);
649 first_block->AddInstruction(new (&allocator) HIf(equal));
650
651 HBasicBlock* then_block = new (&allocator) HBasicBlock(graph);
652 HBasicBlock* else_block = new (&allocator) HBasicBlock(graph);
Alexandre Rames92730742014-10-01 12:55:56 +0100653 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
Alexandre Rames92730742014-10-01 12:55:56 +0100654 graph->SetExitBlock(exit_block);
655
David Brazdil58282f42016-01-14 12:45:10 +0000656 graph->AddBlock(then_block);
657 graph->AddBlock(else_block);
658 graph->AddBlock(exit_block);
659 first_block->AddSuccessor(then_block);
660 first_block->AddSuccessor(else_block);
661 then_block->AddSuccessor(exit_block);
662 else_block->AddSuccessor(exit_block);
663
664 exit_block->AddInstruction(new (&allocator) HExit());
665 then_block->AddInstruction(new (&allocator) HReturn(constant0));
666 else_block->AddInstruction(new (&allocator) HReturn(constant1));
667
David Brazdilb11b0722016-01-28 16:22:40 +0000668 ASSERT_FALSE(equal->IsEmittedAtUseSite());
David Brazdilbadd8262016-02-02 16:28:56 +0000669 graph->BuildDominatorTree();
David Brazdil58282f42016-01-14 12:45:10 +0000670 PrepareForRegisterAllocation(graph).Run();
David Brazdilb11b0722016-01-28 16:22:40 +0000671 ASSERT_TRUE(equal->IsEmittedAtUseSite());
Alexandre Rames92730742014-10-01 12:55:56 +0100672
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800673 auto hook_before_codegen = [](HGraph* graph_in) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100674 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800675 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100676 block->InsertInstructionBefore(move, block->GetLastInstruction());
677 };
678
David Brazdil58282f42016-01-14 12:45:10 +0000679 RunCode(target_isa, graph, hook_before_codegen, true, 0);
Alexandre Rames92730742014-10-01 12:55:56 +0100680 }
681}
682
David Brazdil58282f42016-01-14 12:45:10 +0000683TEST_F(CodegenTest, MaterializedCondition1) {
684 for (InstructionSet target_isa : GetTargetISAs()) {
685 // Check that condition are materialized correctly. A materialized condition
686 // should yield `1` if it evaluated to true, and `0` otherwise.
687 // We force the materialization of comparisons for different combinations of
Alexandre Rames92730742014-10-01 12:55:56 +0100688
David Brazdil58282f42016-01-14 12:45:10 +0000689 // inputs and check the results.
Alexandre Rames92730742014-10-01 12:55:56 +0100690
David Brazdil58282f42016-01-14 12:45:10 +0000691 int lhs[] = {1, 2, -1, 2, 0xabc};
692 int rhs[] = {2, 1, 2, -1, 0xabc};
Alexandre Rames92730742014-10-01 12:55:56 +0100693
David Brazdil58282f42016-01-14 12:45:10 +0000694 for (size_t i = 0; i < arraysize(lhs); i++) {
695 ArenaPool pool;
696 ArenaAllocator allocator(&pool);
697 HGraph* graph = CreateGraph(&allocator);
Alexandre Rames92730742014-10-01 12:55:56 +0100698
David Brazdil58282f42016-01-14 12:45:10 +0000699 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
700 graph->AddBlock(entry_block);
701 graph->SetEntryBlock(entry_block);
702 entry_block->AddInstruction(new (&allocator) HGoto());
703 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
704 graph->AddBlock(code_block);
705 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
706 graph->AddBlock(exit_block);
707 exit_block->AddInstruction(new (&allocator) HExit());
Alexandre Rames92730742014-10-01 12:55:56 +0100708
David Brazdil58282f42016-01-14 12:45:10 +0000709 entry_block->AddSuccessor(code_block);
710 code_block->AddSuccessor(exit_block);
711 graph->SetExitBlock(exit_block);
Alexandre Rames92730742014-10-01 12:55:56 +0100712
David Brazdil58282f42016-01-14 12:45:10 +0000713 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
714 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
715 HLessThan cmp_lt(cst_lhs, cst_rhs);
716 code_block->AddInstruction(&cmp_lt);
717 HReturn ret(&cmp_lt);
718 code_block->AddInstruction(&ret);
Alexandre Rames92730742014-10-01 12:55:56 +0100719
David Brazdilbadd8262016-02-02 16:28:56 +0000720 graph->BuildDominatorTree();
David Brazdil58282f42016-01-14 12:45:10 +0000721 auto hook_before_codegen = [](HGraph* graph_in) {
722 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
723 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
724 block->InsertInstructionBefore(move, block->GetLastInstruction());
725 };
726 RunCode(target_isa, graph, hook_before_codegen, true, lhs[i] < rhs[i]);
727 }
Alexandre Rames92730742014-10-01 12:55:56 +0100728 }
729}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100730
David Brazdil58282f42016-01-14 12:45:10 +0000731TEST_F(CodegenTest, MaterializedCondition2) {
732 for (InstructionSet target_isa : GetTargetISAs()) {
733 // Check that HIf correctly interprets a materialized condition.
734 // We force the materialization of comparisons for different combinations of
735 // inputs. An HIf takes the materialized combination as input and returns a
736 // value that we verify.
737
738 int lhs[] = {1, 2, -1, 2, 0xabc};
739 int rhs[] = {2, 1, 2, -1, 0xabc};
740
741
742 for (size_t i = 0; i < arraysize(lhs); i++) {
743 ArenaPool pool;
744 ArenaAllocator allocator(&pool);
745 HGraph* graph = CreateGraph(&allocator);
746
747 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
748 graph->AddBlock(entry_block);
749 graph->SetEntryBlock(entry_block);
750 entry_block->AddInstruction(new (&allocator) HGoto());
751
752 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
753 graph->AddBlock(if_block);
754 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
755 graph->AddBlock(if_true_block);
756 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
757 graph->AddBlock(if_false_block);
758 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
759 graph->AddBlock(exit_block);
760 exit_block->AddInstruction(new (&allocator) HExit());
761
762 graph->SetEntryBlock(entry_block);
763 entry_block->AddSuccessor(if_block);
764 if_block->AddSuccessor(if_true_block);
765 if_block->AddSuccessor(if_false_block);
766 if_true_block->AddSuccessor(exit_block);
767 if_false_block->AddSuccessor(exit_block);
768 graph->SetExitBlock(exit_block);
769
770 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
771 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
772 HLessThan cmp_lt(cst_lhs, cst_rhs);
773 if_block->AddInstruction(&cmp_lt);
David Brazdil6e332522016-02-02 16:15:27 +0000774 // We insert a dummy instruction to separate the HIf from the HLessThan
775 // and force the materialization of the condition.
776 HMemoryBarrier force_materialization(MemBarrierKind::kAnyAny, 0);
David Brazdil58282f42016-01-14 12:45:10 +0000777 if_block->AddInstruction(&force_materialization);
778 HIf if_lt(&cmp_lt);
779 if_block->AddInstruction(&if_lt);
780
781 HIntConstant* cst_lt = graph->GetIntConstant(1);
782 HReturn ret_lt(cst_lt);
783 if_true_block->AddInstruction(&ret_lt);
784 HIntConstant* cst_ge = graph->GetIntConstant(0);
785 HReturn ret_ge(cst_ge);
786 if_false_block->AddInstruction(&ret_ge);
787
David Brazdilbadd8262016-02-02 16:28:56 +0000788 graph->BuildDominatorTree();
David Brazdil58282f42016-01-14 12:45:10 +0000789 auto hook_before_codegen = [](HGraph* graph_in) {
790 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0];
791 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
792 block->InsertInstructionBefore(move, block->GetLastInstruction());
793 };
794 RunCode(target_isa, graph, hook_before_codegen, true, lhs[i] < rhs[i]);
795 }
796 }
797}
798
799TEST_F(CodegenTest, ReturnDivIntLit8) {
Calin Juravled0d48522014-11-04 16:40:20 +0000800 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
801 Instruction::CONST_4 | 4 << 12 | 0 << 8,
802 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
803 Instruction::RETURN);
804
David Brazdil58282f42016-01-14 12:45:10 +0000805 TestCode(data, true, 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000806}
807
David Brazdil58282f42016-01-14 12:45:10 +0000808TEST_F(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000809 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
810 Instruction::CONST_4 | 4 << 12 | 0,
811 Instruction::CONST_4 | 2 << 12 | 1 << 8,
812 Instruction::DIV_INT_2ADDR | 1 << 12,
813 Instruction::RETURN);
814
David Brazdil58282f42016-01-14 12:45:10 +0000815 TestCode(data, true, 2);
Calin Juravle865fc882014-11-06 17:09:03 +0000816}
817
Aart Bike9f37602015-10-09 11:15:55 -0700818// Helper method.
Phil Wang751beff2015-08-28 15:17:15 +0800819static void TestComparison(IfCondition condition,
820 int64_t i,
821 int64_t j,
822 Primitive::Type type,
823 const InstructionSet target_isa) {
Aart Bike9f37602015-10-09 11:15:55 -0700824 ArenaPool pool;
825 ArenaAllocator allocator(&pool);
826 HGraph* graph = CreateGraph(&allocator);
827
828 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
829 graph->AddBlock(entry_block);
830 graph->SetEntryBlock(entry_block);
831 entry_block->AddInstruction(new (&allocator) HGoto());
832
833 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
834 graph->AddBlock(block);
835
836 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
837 graph->AddBlock(exit_block);
838 graph->SetExitBlock(exit_block);
839 exit_block->AddInstruction(new (&allocator) HExit());
840
841 entry_block->AddSuccessor(block);
842 block->AddSuccessor(exit_block);
843
844 HInstruction* op1;
845 HInstruction* op2;
846 if (type == Primitive::kPrimInt) {
847 op1 = graph->GetIntConstant(i);
848 op2 = graph->GetIntConstant(j);
849 } else {
850 DCHECK_EQ(type, Primitive::kPrimLong);
851 op1 = graph->GetLongConstant(i);
852 op2 = graph->GetLongConstant(j);
853 }
854
855 HInstruction* comparison = nullptr;
856 bool expected_result = false;
857 const uint64_t x = i;
858 const uint64_t y = j;
859 switch (condition) {
860 case kCondEQ:
861 comparison = new (&allocator) HEqual(op1, op2);
862 expected_result = (i == j);
863 break;
864 case kCondNE:
865 comparison = new (&allocator) HNotEqual(op1, op2);
866 expected_result = (i != j);
867 break;
868 case kCondLT:
869 comparison = new (&allocator) HLessThan(op1, op2);
870 expected_result = (i < j);
871 break;
872 case kCondLE:
873 comparison = new (&allocator) HLessThanOrEqual(op1, op2);
874 expected_result = (i <= j);
875 break;
876 case kCondGT:
877 comparison = new (&allocator) HGreaterThan(op1, op2);
878 expected_result = (i > j);
879 break;
880 case kCondGE:
881 comparison = new (&allocator) HGreaterThanOrEqual(op1, op2);
882 expected_result = (i >= j);
883 break;
884 case kCondB:
885 comparison = new (&allocator) HBelow(op1, op2);
886 expected_result = (x < y);
887 break;
888 case kCondBE:
889 comparison = new (&allocator) HBelowOrEqual(op1, op2);
890 expected_result = (x <= y);
891 break;
892 case kCondA:
893 comparison = new (&allocator) HAbove(op1, op2);
894 expected_result = (x > y);
895 break;
896 case kCondAE:
897 comparison = new (&allocator) HAboveOrEqual(op1, op2);
898 expected_result = (x >= y);
899 break;
900 }
901 block->AddInstruction(comparison);
902 block->AddInstruction(new (&allocator) HReturn(comparison));
903
David Brazdilbadd8262016-02-02 16:28:56 +0000904 graph->BuildDominatorTree();
David Brazdil58282f42016-01-14 12:45:10 +0000905 RunCode(target_isa, graph, [](HGraph*) {}, true, expected_result);
Aart Bike9f37602015-10-09 11:15:55 -0700906}
907
David Brazdil58282f42016-01-14 12:45:10 +0000908TEST_F(CodegenTest, ComparisonsInt) {
909 for (InstructionSet target_isa : GetTargetISAs()) {
910 for (int64_t i = -1; i <= 1; i++) {
911 for (int64_t j = -1; j <= 1; j++) {
912 TestComparison(kCondEQ, i, j, Primitive::kPrimInt, target_isa);
913 TestComparison(kCondNE, i, j, Primitive::kPrimInt, target_isa);
914 TestComparison(kCondLT, i, j, Primitive::kPrimInt, target_isa);
915 TestComparison(kCondLE, i, j, Primitive::kPrimInt, target_isa);
916 TestComparison(kCondGT, i, j, Primitive::kPrimInt, target_isa);
917 TestComparison(kCondGE, i, j, Primitive::kPrimInt, target_isa);
918 TestComparison(kCondB, i, j, Primitive::kPrimInt, target_isa);
919 TestComparison(kCondBE, i, j, Primitive::kPrimInt, target_isa);
920 TestComparison(kCondA, i, j, Primitive::kPrimInt, target_isa);
921 TestComparison(kCondAE, i, j, Primitive::kPrimInt, target_isa);
922 }
Aart Bike9f37602015-10-09 11:15:55 -0700923 }
924 }
925}
926
David Brazdil58282f42016-01-14 12:45:10 +0000927TEST_F(CodegenTest, ComparisonsLong) {
Aart Bike9f37602015-10-09 11:15:55 -0700928 // TODO: make MIPS work for long
929 if (kRuntimeISA == kMips || kRuntimeISA == kMips64) {
930 return;
931 }
932
David Brazdil58282f42016-01-14 12:45:10 +0000933 for (InstructionSet target_isa : GetTargetISAs()) {
934 if (target_isa == kMips || target_isa == kMips64) {
935 continue;
936 }
Phil Wang751beff2015-08-28 15:17:15 +0800937
David Brazdil58282f42016-01-14 12:45:10 +0000938 for (int64_t i = -1; i <= 1; i++) {
939 for (int64_t j = -1; j <= 1; j++) {
940 TestComparison(kCondEQ, i, j, Primitive::kPrimLong, target_isa);
941 TestComparison(kCondNE, i, j, Primitive::kPrimLong, target_isa);
942 TestComparison(kCondLT, i, j, Primitive::kPrimLong, target_isa);
943 TestComparison(kCondLE, i, j, Primitive::kPrimLong, target_isa);
944 TestComparison(kCondGT, i, j, Primitive::kPrimLong, target_isa);
945 TestComparison(kCondGE, i, j, Primitive::kPrimLong, target_isa);
946 TestComparison(kCondB, i, j, Primitive::kPrimLong, target_isa);
947 TestComparison(kCondBE, i, j, Primitive::kPrimLong, target_isa);
948 TestComparison(kCondA, i, j, Primitive::kPrimLong, target_isa);
949 TestComparison(kCondAE, i, j, Primitive::kPrimLong, target_isa);
950 }
Aart Bike9f37602015-10-09 11:15:55 -0700951 }
952 }
953}
954
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000955} // namespace art