blob: 715eca41da3a11220c870f31b7f579fe95f92875 [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"
Alexey Frunze4dda3372015-06-01 18:31:49 -070023#include "arch/mips64/instruction_set_features_mips64.h"
24#include "arch/mips64/registers_mips64.h"
Mark Mendellfb8d2792015-03-31 22:16:59 -040025#include "arch/x86/instruction_set_features_x86.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010026#include "arch/x86/registers_x86.h"
Mark Mendellfb8d2792015-03-31 22:16:59 -040027#include "arch/x86_64/instruction_set_features_x86_64.h"
Alexandre Rames92730742014-10-01 12:55:56 +010028#include "base/macros.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "builder.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010030#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "code_generator_arm64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "code_generator_mips64.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010033#include "code_generator_x86.h"
34#include "code_generator_x86_64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000036#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000038#include "driver/compiler_options.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000040#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010041#include "prepare_for_register_allocation.h"
42#include "register_allocator.h"
Phil Wangc2e1a5e2015-07-29 15:14:09 +080043#include "simulator/code_simulator.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010044#include "ssa_liveness_analysis.h"
Roland Levillain55dcfb52014-10-24 18:09:09 +010045#include "utils.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010046#include "utils/arm/managed_register_arm.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070047#include "utils/mips64/managed_register_mips64.h"
Nicolas Geoffray5da21802015-04-20 09:29:18 +010048#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000049
50#include "gtest/gtest.h"
Nicolas Geoffraye6362282015-01-26 13:57:30 +000051
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000052namespace art {
53
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000054// Provide our own codegen, that ensures the C calling conventions
55// are preserved. Currently, ART and C do not match as R4 is caller-save
56// in ART, and callee-save in C. Alternatively, we could use or write
57// the stub that saves and restores all registers, but it is easier
58// to just overwrite the code generator.
59class TestCodeGeneratorARM : public arm::CodeGeneratorARM {
60 public:
61 TestCodeGeneratorARM(HGraph* graph,
62 const ArmInstructionSetFeatures& isa_features,
63 const CompilerOptions& compiler_options)
64 : arm::CodeGeneratorARM(graph, isa_features, compiler_options) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +010065 AddAllocatedRegister(Location::RegisterLocation(arm::R6));
66 AddAllocatedRegister(Location::RegisterLocation(arm::R7));
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +000067 }
68
69 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
70 arm::CodeGeneratorARM::SetupBlockedRegisters(is_baseline);
Nicolas Geoffray5da21802015-04-20 09:29:18 +010071 blocked_core_registers_[arm::R4] = true;
72 blocked_core_registers_[arm::R6] = false;
73 blocked_core_registers_[arm::R7] = false;
Nicolas Geoffraye6362282015-01-26 13:57:30 +000074 // Makes pair R6-R7 available.
Nicolas Geoffray5da21802015-04-20 09:29:18 +010075 blocked_register_pairs_[arm::R6_R7] = false;
76 }
77};
78
79class TestCodeGeneratorX86 : public x86::CodeGeneratorX86 {
80 public:
81 TestCodeGeneratorX86(HGraph* graph,
82 const X86InstructionSetFeatures& isa_features,
83 const CompilerOptions& compiler_options)
84 : x86::CodeGeneratorX86(graph, isa_features, compiler_options) {
85 // Save edi, we need it for getting enough registers for long multiplication.
86 AddAllocatedRegister(Location::RegisterLocation(x86::EDI));
87 }
88
89 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE {
90 x86::CodeGeneratorX86::SetupBlockedRegisters(is_baseline);
91 // ebx is a callee-save register in C, but caller-save for ART.
92 blocked_core_registers_[x86::EBX] = true;
93 blocked_register_pairs_[x86::EAX_EBX] = true;
94 blocked_register_pairs_[x86::EDX_EBX] = true;
95 blocked_register_pairs_[x86::ECX_EBX] = true;
96 blocked_register_pairs_[x86::EBX_EDI] = true;
97
98 // Make edi available.
99 blocked_core_registers_[x86::EDI] = false;
100 blocked_register_pairs_[x86::ECX_EDI] = false;
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000101 }
102};
103
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000104class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105 public:
Ian Rogersd582fa42014-11-05 23:46:43 -0800106 InternalCodeAllocator() : size_(0) { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000107
108 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000109 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000111 return memory_.get();
112 }
113
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000114 size_t GetSize() const { return size_; }
115 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000116
117 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000118 size_t size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700119 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000120
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000121 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000122};
123
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800124static bool CanExecute(InstructionSet target_isa) {
125 return (target_isa == kRuntimeISA) || CodeSimulator::CanSimulate(target_isa);
126}
127
128template <typename Expected>
129static Expected SimulatorExecute(CodeSimulator* simulator, Expected (*f)());
130
131template <>
132bool SimulatorExecute<bool>(CodeSimulator* simulator, bool (*f)()) {
133 simulator->RunFrom(reinterpret_cast<intptr_t>(f));
134 return simulator->GetCReturnBool();
135}
136
137template <>
138int32_t SimulatorExecute<int32_t>(CodeSimulator* simulator, int32_t (*f)()) {
139 simulator->RunFrom(reinterpret_cast<intptr_t>(f));
140 return simulator->GetCReturnInt32();
141}
142
143template <>
144int64_t SimulatorExecute<int64_t>(CodeSimulator* simulator, int64_t (*f)()) {
145 simulator->RunFrom(reinterpret_cast<intptr_t>(f));
146 return simulator->GetCReturnInt64();
147}
148
149template <typename Expected>
150static void VerifyGeneratedCode(InstructionSet target_isa,
151 Expected (*f)(),
152 bool has_result,
153 Expected expected) {
154 ASSERT_TRUE(CanExecute(target_isa)) << "Target isa is not executable.";
155
156 // Verify on simulator.
157 if (CodeSimulator::CanSimulate(target_isa)) {
158 std::unique_ptr<CodeSimulator> simulator(CodeSimulator::CreateCodeSimulator(target_isa));
159 Expected result = SimulatorExecute<Expected>(simulator.get(), f);
160 if (has_result) {
161 ASSERT_EQ(expected, result);
162 }
163 }
164
165 // Verify on hardware.
166 if (kRuntimeISA == target_isa) {
167 Expected result = f();
168 if (has_result) {
169 ASSERT_EQ(expected, result);
170 }
171 }
172}
173
Roland Levillain55dcfb52014-10-24 18:09:09 +0100174template <typename Expected>
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100175static void Run(const InternalCodeAllocator& allocator,
176 const CodeGenerator& codegen,
177 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100178 Expected expected) {
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800179 InstructionSet target_isa = codegen.GetInstructionSet();
180
Roland Levillain55dcfb52014-10-24 18:09:09 +0100181 typedef Expected (*fptr)();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100182 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -0700183 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800184 if (target_isa == kThumb2) {
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100185 // For thumb we need the bottom bit set.
186 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
187 }
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800188 VerifyGeneratedCode(target_isa, f, has_result, expected);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100189}
190
Roland Levillain55dcfb52014-10-24 18:09:09 +0100191template <typename Expected>
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800192static void RunCodeBaseline(InstructionSet target_isa,
193 HGraph* graph,
194 bool has_result,
195 Expected expected) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000196 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100197
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000198 CompilerOptions compiler_options;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400199 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
200 X86InstructionSetFeatures::FromCppDefines());
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100201 TestCodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100202 // We avoid doing a stack overflow check that requires the runtime being setup,
203 // by making sure the compiler knows the methods we are running are leaf methods.
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100204 codegenX86.CompileBaseline(&allocator, true);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800205 if (target_isa == kX86) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100206 Run(allocator, codegenX86, has_result, expected);
207 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100208
Serban Constantinescu579885a2015-02-22 20:51:33 +0000209 std::unique_ptr<const ArmInstructionSetFeatures> features_arm(
Andreas Gampedf649502015-01-06 14:13:52 -0800210 ArmInstructionSetFeatures::FromCppDefines());
Serban Constantinescu579885a2015-02-22 20:51:33 +0000211 TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100212 codegenARM.CompileBaseline(&allocator, true);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800213 if (target_isa == kArm || target_isa == kThumb2) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100214 Run(allocator, codegenARM, has_result, expected);
215 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100216
Mark Mendellfb8d2792015-03-31 22:16:59 -0400217 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
218 X86_64InstructionSetFeatures::FromCppDefines());
219 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100220 codegenX86_64.CompileBaseline(&allocator, true);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800221 if (target_isa == kX86_64) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100222 Run(allocator, codegenX86_64, has_result, expected);
223 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100224
Serban Constantinescu579885a2015-02-22 20:51:33 +0000225 std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64(
226 Arm64InstructionSetFeatures::FromCppDefines());
227 arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100228 codegenARM64.CompileBaseline(&allocator, true);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800229 if (target_isa == kArm64) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 Run(allocator, codegenARM64, has_result, expected);
231 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700232
233 std::unique_ptr<const Mips64InstructionSetFeatures> features_mips64(
234 Mips64InstructionSetFeatures::FromCppDefines());
235 mips64::CodeGeneratorMIPS64 codegenMIPS64(graph, *features_mips64.get(), compiler_options);
236 codegenMIPS64.CompileBaseline(&allocator, true);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800237 if (target_isa == kMips64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700238 Run(allocator, codegenMIPS64, has_result, expected);
239 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000240}
241
Roland Levillain55dcfb52014-10-24 18:09:09 +0100242template <typename Expected>
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100243static void RunCodeOptimized(CodeGenerator* codegen,
244 HGraph* graph,
245 std::function<void(HGraph*)> hook_before_codegen,
246 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100247 Expected expected) {
Nicolas Geoffrayea809422015-06-24 14:25:09 +0100248 // Tests may have already computed it.
249 if (graph->GetReversePostOrder().IsEmpty()) {
250 graph->BuildDominatorTree();
251 }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100252 SsaLivenessAnalysis liveness(graph, codegen);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100253 liveness.Analyze();
254
255 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
256 register_allocator.AllocateRegisters();
257 hook_before_codegen(graph);
258
259 InternalCodeAllocator allocator;
260 codegen->CompileOptimized(&allocator);
261 Run(allocator, *codegen, has_result, expected);
262}
263
Roland Levillain55dcfb52014-10-24 18:09:09 +0100264template <typename Expected>
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800265static void RunCodeOptimized(InstructionSet target_isa,
266 HGraph* graph,
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100267 std::function<void(HGraph*)> hook_before_codegen,
268 bool has_result,
Roland Levillain55dcfb52014-10-24 18:09:09 +0100269 Expected expected) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000270 CompilerOptions compiler_options;
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800271 if (target_isa == kArm || target_isa == kThumb2) {
272 std::unique_ptr<const ArmInstructionSetFeatures> features_arm(
273 ArmInstructionSetFeatures::FromCppDefines());
274 TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100275 RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800276 } else if (target_isa == kArm64) {
277 std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64(
278 Arm64InstructionSetFeatures::FromCppDefines());
279 arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000280 RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800281 } else if (target_isa == kX86) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400282 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
283 X86InstructionSetFeatures::FromCppDefines());
284 x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000285 RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800286 } else if (target_isa == kX86_64) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400287 std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64(
288 X86_64InstructionSetFeatures::FromCppDefines());
289 x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100290 RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800291 } else if (target_isa == kMips64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700292 std::unique_ptr<const Mips64InstructionSetFeatures> features_mips64(
293 Mips64InstructionSetFeatures::FromCppDefines());
294 mips64::CodeGeneratorMIPS64 codegenMIPS64(graph, *features_mips64.get(), compiler_options);
295 RunCodeOptimized(&codegenMIPS64, graph, hook_before_codegen, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100296 }
297}
298
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800299static void TestCode(InstructionSet target_isa,
300 const uint16_t* data,
301 bool has_result = false,
302 int32_t expected = 0) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100303 ArenaPool pool;
304 ArenaAllocator arena(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 HGraph* graph = CreateGraph(&arena);
David Brazdil5e8b1372015-01-23 14:39:08 +0000306 HGraphBuilder builder(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100307 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000308 bool graph_built = builder.BuildGraph(*item);
309 ASSERT_TRUE(graph_built);
Calin Juravle039b6e22014-10-23 12:32:11 +0100310 // Remove suspend checks, they cannot be executed in this context.
Calin Juravle48dee042014-10-22 15:54:12 +0100311 RemoveSuspendChecks(graph);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800312 RunCodeBaseline(target_isa, graph, has_result, expected);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100313}
314
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800315static void TestCodeLong(InstructionSet target_isa,
316 const uint16_t* data,
317 bool has_result,
318 int64_t expected) {
Roland Levillain55dcfb52014-10-24 18:09:09 +0100319 ArenaPool pool;
320 ArenaAllocator arena(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 HGraph* graph = CreateGraph(&arena);
David Brazdil5e8b1372015-01-23 14:39:08 +0000322 HGraphBuilder builder(graph, Primitive::kPrimLong);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100323 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000324 bool graph_built = builder.BuildGraph(*item);
325 ASSERT_TRUE(graph_built);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100326 // Remove suspend checks, they cannot be executed in this context.
327 RemoveSuspendChecks(graph);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800328 RunCodeBaseline(target_isa, graph, has_result, expected);
Roland Levillain55dcfb52014-10-24 18:09:09 +0100329}
330
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800331class CodegenTest: public ::testing::TestWithParam<InstructionSet> {};
332
333TEST_P(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000334 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800335 TestCode(GetParam(), data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000336}
337
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800338TEST_P(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000339 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000340 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000341 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000342
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800343 TestCode(GetParam(), data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000344}
345
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800346TEST_P(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000347 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000348 Instruction::GOTO | 0x100,
349 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000350 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000351
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800352 TestCode(GetParam(), data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000353}
354
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800355TEST_P(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000356 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000357 Instruction::GOTO | 0x200,
358 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000359 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000360
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800361 TestCode(GetParam(), data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000362
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000363 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364 Instruction::GOTO_16, 3,
365 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000366 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000367
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800368 TestCode(GetParam(), data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000369
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000370 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371 Instruction::GOTO_32, 4, 0,
372 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000373 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000374
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800375 TestCode(GetParam(), data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000376}
377
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800378TEST_P(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000379 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000380 Instruction::RETURN_VOID,
381 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000382 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000383
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800384 TestCode(GetParam(), data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000385}
386
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800387TEST_P(CodegenTest, CFG5) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000388 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
389 Instruction::CONST_4 | 0 | 0,
390 Instruction::IF_EQ, 3,
391 Instruction::GOTO | 0x100,
392 Instruction::RETURN_VOID);
393
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800394 TestCode(GetParam(), data);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000395}
396
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800397TEST_P(CodegenTest, IntConstant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000398 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
399 Instruction::CONST_4 | 0 | 0,
400 Instruction::RETURN_VOID);
401
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800402 TestCode(GetParam(), data);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000403}
404
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800405TEST_P(CodegenTest, Return1) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000406 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
407 Instruction::CONST_4 | 0 | 0,
408 Instruction::RETURN | 0);
409
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800410 TestCode(GetParam(), data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000411}
412
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800413TEST_P(CodegenTest, Return2) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000414 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
415 Instruction::CONST_4 | 0 | 0,
416 Instruction::CONST_4 | 0 | 1 << 8,
417 Instruction::RETURN | 1 << 8);
418
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800419 TestCode(GetParam(), data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000420}
421
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800422TEST_P(CodegenTest, Return3) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000423 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
424 Instruction::CONST_4 | 0 | 0,
425 Instruction::CONST_4 | 1 << 8 | 1 << 12,
426 Instruction::RETURN | 1 << 8);
427
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800428 TestCode(GetParam(), data, true, 1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000429}
430
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800431TEST_P(CodegenTest, ReturnIf1) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000432 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
433 Instruction::CONST_4 | 0 | 0,
434 Instruction::CONST_4 | 1 << 8 | 1 << 12,
435 Instruction::IF_EQ, 3,
436 Instruction::RETURN | 0 << 8,
437 Instruction::RETURN | 1 << 8);
438
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800439 TestCode(GetParam(), data, true, 1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000440}
441
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800442TEST_P(CodegenTest, ReturnIf2) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000443 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
444 Instruction::CONST_4 | 0 | 0,
445 Instruction::CONST_4 | 1 << 8 | 1 << 12,
446 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
447 Instruction::RETURN | 0 << 8,
448 Instruction::RETURN | 1 << 8);
449
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800450 TestCode(GetParam(), data, true, 0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000451}
452
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100453// Exercise bit-wise (one's complement) not-int instruction.
454#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800455TEST_P(CodegenTest, TEST_NAME) { \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100456 const int32_t input = INPUT; \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100457 const uint16_t input_lo = Low16Bits(input); \
458 const uint16_t input_hi = High16Bits(input); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100459 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \
460 Instruction::CONST | 0 << 8, input_lo, input_hi, \
461 Instruction::NOT_INT | 1 << 8 | 0 << 12 , \
462 Instruction::RETURN | 1 << 8); \
463 \
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800464 TestCode(GetParam(), data, true, EXPECTED_OUTPUT); \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100465}
466
467NOT_INT_TEST(ReturnNotIntMinus2, -2, 1)
468NOT_INT_TEST(ReturnNotIntMinus1, -1, 0)
469NOT_INT_TEST(ReturnNotInt0, 0, -1)
470NOT_INT_TEST(ReturnNotInt1, 1, -2)
Roland Levillain55dcfb52014-10-24 18:09:09 +0100471NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1
472NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2
473NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1
474NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31)
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100475
476#undef NOT_INT_TEST
477
Roland Levillain55dcfb52014-10-24 18:09:09 +0100478// Exercise bit-wise (one's complement) not-long instruction.
479#define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800480TEST_P(CodegenTest, TEST_NAME) { \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100481 const int64_t input = INPUT; \
482 const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \
483 const uint16_t word1 = High16Bits(Low32Bits(input)); \
484 const uint16_t word2 = Low16Bits(High32Bits(input)); \
485 const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \
486 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \
487 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \
488 Instruction::NOT_LONG | 2 << 8 | 0 << 12, \
489 Instruction::RETURN_WIDE | 2 << 8); \
490 \
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800491 TestCodeLong(GetParam(), data, true, EXPECTED_OUTPUT); \
Roland Levillain55dcfb52014-10-24 18:09:09 +0100492}
493
494NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1))
495NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0))
496NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1))
497NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2))
498
499NOT_LONG_TEST(ReturnNotLongINT32_MIN,
500 INT64_C(-2147483648),
501 INT64_C(2147483647)) // (2^31) - 1
502NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1,
503 INT64_C(-2147483647),
504 INT64_C(2147483646)) // (2^31) - 2
505NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1,
506 INT64_C(2147483646),
507 INT64_C(-2147483647)) // -(2^31) - 1
508NOT_LONG_TEST(ReturnNotLongINT32_MAX,
509 INT64_C(2147483647),
510 INT64_C(-2147483648)) // -(2^31)
511
512// Note that the C++ compiler won't accept
513// INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid
514// int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead.
515NOT_LONG_TEST(ReturnNotINT64_MIN,
516 INT64_C(-9223372036854775807)-1,
517 INT64_C(9223372036854775807)); // (2^63) - 1
518NOT_LONG_TEST(ReturnNotINT64_MINPlus1,
519 INT64_C(-9223372036854775807),
520 INT64_C(9223372036854775806)); // (2^63) - 2
521NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1,
522 INT64_C(9223372036854775806),
523 INT64_C(-9223372036854775807)); // -(2^63) - 1
524NOT_LONG_TEST(ReturnNotLongINT64_MAX,
525 INT64_C(9223372036854775807),
526 INT64_C(-9223372036854775807)-1); // -(2^63)
527
528#undef NOT_LONG_TEST
529
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800530TEST_P(CodegenTest, IntToLongOfLongToInt) {
Roland Levillain946e1432014-11-11 17:35:19 +0000531 const int64_t input = INT64_C(4294967296); // 2^32
532 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
533 const uint16_t word1 = High16Bits(Low32Bits(input));
534 const uint16_t word2 = Low16Bits(High32Bits(input));
535 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
536 const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM(
537 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
538 Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0,
539 Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1
540 Instruction::LONG_TO_INT | 4 << 8 | 0 << 12,
541 Instruction::INT_TO_LONG | 2 << 8 | 4 << 12,
542 Instruction::RETURN_WIDE | 2 << 8);
543
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800544 TestCodeLong(GetParam(), data, true, 1);
Roland Levillain946e1432014-11-11 17:35:19 +0000545}
546
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800547TEST_P(CodegenTest, ReturnAdd1) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000548 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
549 Instruction::CONST_4 | 3 << 12 | 0,
550 Instruction::CONST_4 | 4 << 12 | 1 << 8,
551 Instruction::ADD_INT, 1 << 8 | 0,
552 Instruction::RETURN);
553
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800554 TestCode(GetParam(), data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000555}
556
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800557TEST_P(CodegenTest, ReturnAdd2) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000558 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
559 Instruction::CONST_4 | 3 << 12 | 0,
560 Instruction::CONST_4 | 4 << 12 | 1 << 8,
561 Instruction::ADD_INT_2ADDR | 1 << 12,
562 Instruction::RETURN);
563
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800564 TestCode(GetParam(), data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000565}
566
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800567TEST_P(CodegenTest, ReturnAdd3) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000568 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
569 Instruction::CONST_4 | 4 << 12 | 0 << 8,
570 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
571 Instruction::RETURN);
572
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800573 TestCode(GetParam(), data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000574}
575
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800576TEST_P(CodegenTest, ReturnAdd4) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000577 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
578 Instruction::CONST_4 | 4 << 12 | 0 << 8,
579 Instruction::ADD_INT_LIT16, 3,
580 Instruction::RETURN);
581
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800582 TestCode(GetParam(), data, true, 7);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000583}
584
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800585TEST_P(CodegenTest, NonMaterializedCondition) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100586 ArenaPool pool;
587 ArenaAllocator allocator(&pool);
588
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100589 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100590 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
591 graph->AddBlock(entry);
592 graph->SetEntryBlock(entry);
593 entry->AddInstruction(new (&allocator) HGoto());
594
595 HBasicBlock* first_block = new (&allocator) HBasicBlock(graph);
596 graph->AddBlock(first_block);
597 entry->AddSuccessor(first_block);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000598 HIntConstant* constant0 = graph->GetIntConstant(0);
599 HIntConstant* constant1 = graph->GetIntConstant(1);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100600 HEqual* equal = new (&allocator) HEqual(constant0, constant0);
601 first_block->AddInstruction(equal);
602 first_block->AddInstruction(new (&allocator) HIf(equal));
603
604 HBasicBlock* then = new (&allocator) HBasicBlock(graph);
605 HBasicBlock* else_ = new (&allocator) HBasicBlock(graph);
606 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
607
608 graph->AddBlock(then);
609 graph->AddBlock(else_);
610 graph->AddBlock(exit);
611 first_block->AddSuccessor(then);
612 first_block->AddSuccessor(else_);
613 then->AddSuccessor(exit);
614 else_->AddSuccessor(exit);
615
616 exit->AddInstruction(new (&allocator) HExit());
617 then->AddInstruction(new (&allocator) HReturn(constant0));
618 else_->AddInstruction(new (&allocator) HReturn(constant1));
619
620 ASSERT_TRUE(equal->NeedsMaterialization());
621 graph->BuildDominatorTree();
622 PrepareForRegisterAllocation(graph).Run();
623 ASSERT_FALSE(equal->NeedsMaterialization());
624
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800625 auto hook_before_codegen = [](HGraph* graph_in) {
626 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
627 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100628 block->InsertInstructionBefore(move, block->GetLastInstruction());
629 };
630
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800631 RunCodeOptimized(GetParam(), graph, hook_before_codegen, true, 0);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100632}
633
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800634TEST_P(CodegenTest, ReturnMulInt) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100635 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
636 Instruction::CONST_4 | 3 << 12 | 0,
637 Instruction::CONST_4 | 4 << 12 | 1 << 8,
638 Instruction::MUL_INT, 1 << 8 | 0,
639 Instruction::RETURN);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100640
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800641 TestCode(GetParam(), data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100642}
643
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800644TEST_P(CodegenTest, ReturnMulInt2addr) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100645 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
646 Instruction::CONST_4 | 3 << 12 | 0,
647 Instruction::CONST_4 | 4 << 12 | 1 << 8,
648 Instruction::MUL_INT_2ADDR | 1 << 12,
649 Instruction::RETURN);
650
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800651 TestCode(GetParam(), data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100652}
653
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800654TEST_P(CodegenTest, ReturnMulLong) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100655 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
656 Instruction::CONST_4 | 3 << 12 | 0,
657 Instruction::CONST_4 | 0 << 12 | 1 << 8,
658 Instruction::CONST_4 | 4 << 12 | 2 << 8,
659 Instruction::CONST_4 | 0 << 12 | 3 << 8,
660 Instruction::MUL_LONG, 2 << 8 | 0,
661 Instruction::RETURN_WIDE);
662
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800663 TestCodeLong(GetParam(), data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100664}
665
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800666TEST_P(CodegenTest, ReturnMulLong2addr) {
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100667 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
668 Instruction::CONST_4 | 3 << 12 | 0 << 8,
669 Instruction::CONST_4 | 0 << 12 | 1 << 8,
670 Instruction::CONST_4 | 4 << 12 | 2 << 8,
671 Instruction::CONST_4 | 0 << 12 | 3 << 8,
672 Instruction::MUL_LONG_2ADDR | 2 << 12,
673 Instruction::RETURN_WIDE);
674
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800675 TestCodeLong(GetParam(), data, true, 12);
Nicolas Geoffray5da21802015-04-20 09:29:18 +0100676}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100677
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800678TEST_P(CodegenTest, ReturnMulIntLit8) {
Calin Juravle34bacdf2014-10-07 20:23:36 +0100679 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
680 Instruction::CONST_4 | 4 << 12 | 0 << 8,
681 Instruction::MUL_INT_LIT8, 3 << 8 | 0,
682 Instruction::RETURN);
683
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800684 TestCode(GetParam(), data, true, 12);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100685}
686
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800687TEST_P(CodegenTest, ReturnMulIntLit16) {
Calin Juravle34bacdf2014-10-07 20:23:36 +0100688 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
689 Instruction::CONST_4 | 4 << 12 | 0 << 8,
690 Instruction::MUL_INT_LIT16, 3,
691 Instruction::RETURN);
692
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800693 TestCode(GetParam(), data, true, 12);
Calin Juravle34bacdf2014-10-07 20:23:36 +0100694}
695
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800696TEST_P(CodegenTest, MaterializedCondition1) {
Alexandre Rames92730742014-10-01 12:55:56 +0100697 // Check that condition are materialized correctly. A materialized condition
698 // should yield `1` if it evaluated to true, and `0` otherwise.
699 // We force the materialization of comparisons for different combinations of
700 // inputs and check the results.
701
702 int lhs[] = {1, 2, -1, 2, 0xabc};
703 int rhs[] = {2, 1, 2, -1, 0xabc};
704
705 for (size_t i = 0; i < arraysize(lhs); i++) {
706 ArenaPool pool;
707 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100708 HGraph* graph = CreateGraph(&allocator);
Alexandre Rames92730742014-10-01 12:55:56 +0100709
710 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
711 graph->AddBlock(entry_block);
712 graph->SetEntryBlock(entry_block);
713 entry_block->AddInstruction(new (&allocator) HGoto());
714 HBasicBlock* code_block = new (&allocator) HBasicBlock(graph);
715 graph->AddBlock(code_block);
716 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
717 graph->AddBlock(exit_block);
718 exit_block->AddInstruction(new (&allocator) HExit());
719
720 entry_block->AddSuccessor(code_block);
721 code_block->AddSuccessor(exit_block);
722 graph->SetExitBlock(exit_block);
723
David Brazdil8d5b8b22015-03-24 10:51:52 +0000724 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
725 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
726 HLessThan cmp_lt(cst_lhs, cst_rhs);
Alexandre Rames92730742014-10-01 12:55:56 +0100727 code_block->AddInstruction(&cmp_lt);
728 HReturn ret(&cmp_lt);
729 code_block->AddInstruction(&ret);
730
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800731 auto hook_before_codegen = [](HGraph* graph_in) {
732 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
733 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100734 block->InsertInstructionBefore(move, block->GetLastInstruction());
735 };
736
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800737 RunCodeOptimized(GetParam(), graph, hook_before_codegen, true, lhs[i] < rhs[i]);
Alexandre Rames92730742014-10-01 12:55:56 +0100738 }
739}
740
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800741TEST_P(CodegenTest, MaterializedCondition2) {
Alexandre Rames92730742014-10-01 12:55:56 +0100742 // Check that HIf correctly interprets a materialized condition.
743 // We force the materialization of comparisons for different combinations of
744 // inputs. An HIf takes the materialized combination as input and returns a
745 // value that we verify.
746
747 int lhs[] = {1, 2, -1, 2, 0xabc};
748 int rhs[] = {2, 1, 2, -1, 0xabc};
749
750
751 for (size_t i = 0; i < arraysize(lhs); i++) {
752 ArenaPool pool;
753 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100754 HGraph* graph = CreateGraph(&allocator);
Alexandre Rames92730742014-10-01 12:55:56 +0100755
756 HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph);
757 graph->AddBlock(entry_block);
758 graph->SetEntryBlock(entry_block);
759 entry_block->AddInstruction(new (&allocator) HGoto());
760
761 HBasicBlock* if_block = new (&allocator) HBasicBlock(graph);
762 graph->AddBlock(if_block);
763 HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph);
764 graph->AddBlock(if_true_block);
765 HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph);
766 graph->AddBlock(if_false_block);
767 HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph);
768 graph->AddBlock(exit_block);
769 exit_block->AddInstruction(new (&allocator) HExit());
770
771 graph->SetEntryBlock(entry_block);
772 entry_block->AddSuccessor(if_block);
773 if_block->AddSuccessor(if_true_block);
774 if_block->AddSuccessor(if_false_block);
775 if_true_block->AddSuccessor(exit_block);
776 if_false_block->AddSuccessor(exit_block);
777 graph->SetExitBlock(exit_block);
778
David Brazdil8d5b8b22015-03-24 10:51:52 +0000779 HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]);
780 HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]);
781 HLessThan cmp_lt(cst_lhs, cst_rhs);
Alexandre Rames92730742014-10-01 12:55:56 +0100782 if_block->AddInstruction(&cmp_lt);
783 // We insert a temporary to separate the HIf from the HLessThan and force
784 // the materialization of the condition.
785 HTemporary force_materialization(0);
786 if_block->AddInstruction(&force_materialization);
787 HIf if_lt(&cmp_lt);
788 if_block->AddInstruction(&if_lt);
789
David Brazdil8d5b8b22015-03-24 10:51:52 +0000790 HIntConstant* cst_lt = graph->GetIntConstant(1);
791 HReturn ret_lt(cst_lt);
Alexandre Rames92730742014-10-01 12:55:56 +0100792 if_true_block->AddInstruction(&ret_lt);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000793 HIntConstant* cst_ge = graph->GetIntConstant(0);
794 HReturn ret_ge(cst_ge);
Alexandre Rames92730742014-10-01 12:55:56 +0100795 if_false_block->AddInstruction(&ret_ge);
796
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800797 auto hook_before_codegen = [](HGraph* graph_in) {
798 HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0);
799 HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena());
Alexandre Rames92730742014-10-01 12:55:56 +0100800 block->InsertInstructionBefore(move, block->GetLastInstruction());
801 };
802
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800803 RunCodeOptimized(GetParam(), graph, hook_before_codegen, true, lhs[i] < rhs[i]);
Alexandre Rames92730742014-10-01 12:55:56 +0100804 }
805}
Calin Juravle34bacdf2014-10-07 20:23:36 +0100806
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800807TEST_P(CodegenTest, ReturnDivIntLit8) {
Calin Juravled0d48522014-11-04 16:40:20 +0000808 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
809 Instruction::CONST_4 | 4 << 12 | 0 << 8,
810 Instruction::DIV_INT_LIT8, 3 << 8 | 0,
811 Instruction::RETURN);
812
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800813 TestCode(GetParam(), data, true, 1);
Calin Juravled0d48522014-11-04 16:40:20 +0000814}
815
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800816TEST_P(CodegenTest, ReturnDivInt2Addr) {
Calin Juravle865fc882014-11-06 17:09:03 +0000817 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
818 Instruction::CONST_4 | 4 << 12 | 0,
819 Instruction::CONST_4 | 2 << 12 | 1 << 8,
820 Instruction::DIV_INT_2ADDR | 1 << 12,
821 Instruction::RETURN);
822
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800823 TestCode(GetParam(), data, true, 2);
Calin Juravle865fc882014-11-06 17:09:03 +0000824}
825
Phil Wangc2e1a5e2015-07-29 15:14:09 +0800826static ::std::vector<InstructionSet> GetTargetISAs() {
827 ::std::vector<InstructionSet> v;
828 // Add all ISAs that are executable on hardware or on simulator.
829 const ::std::vector<InstructionSet> executable_isa_candidates = {
830 kArm,
831 kArm64,
832 kThumb2,
833 kX86,
834 kX86_64,
835 kMips,
836 kMips64
837 };
838
839 for (auto target_isa : executable_isa_candidates) {
840 if (CanExecute(target_isa)) {
841 v.push_back(target_isa);
842 }
843 }
844
845 return v;
846}
847
848INSTANTIATE_TEST_CASE_P(MultipleTargets,
849 CodegenTest,
850 ::testing::ValuesIn(GetTargetISAs()));
851
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000852} // namespace art