Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 17 | #include <functional> |
| 18 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 19 | #include "arch/instruction_set.h" |
Calin Juravle | 3416601 | 2014-12-19 17:22:29 +0000 | [diff] [blame] | 20 | #include "arch/arm/instruction_set_features_arm.h" |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 21 | #include "arch/arm/registers_arm.h" |
Serban Constantinescu | 579885a | 2015-02-22 20:51:33 +0000 | [diff] [blame] | 22 | #include "arch/arm64/instruction_set_features_arm64.h" |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 23 | #include "arch/mips64/instruction_set_features_mips64.h" |
| 24 | #include "arch/mips64/registers_mips64.h" |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 25 | #include "arch/x86/instruction_set_features_x86.h" |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 26 | #include "arch/x86/registers_x86.h" |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 27 | #include "arch/x86_64/instruction_set_features_x86_64.h" |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 28 | #include "base/macros.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 29 | #include "builder.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 30 | #include "code_generator_arm.h" |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 31 | #include "code_generator_arm64.h" |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 32 | #include "code_generator_mips64.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 33 | #include "code_generator_x86.h" |
| 34 | #include "code_generator_x86_64.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 35 | #include "common_compiler_test.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 36 | #include "dex_file.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 37 | #include "dex_instruction.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 38 | #include "driver/compiler_options.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 39 | #include "nodes.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 40 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 41 | #include "prepare_for_register_allocation.h" |
| 42 | #include "register_allocator.h" |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 43 | #include "simulator/code_simulator.h" |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 44 | #include "ssa_liveness_analysis.h" |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 45 | #include "utils.h" |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 46 | #include "utils/arm/managed_register_arm.h" |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 47 | #include "utils/mips64/managed_register_mips64.h" |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 48 | #include "utils/x86/managed_register_x86.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 49 | |
| 50 | #include "gtest/gtest.h" |
Nicolas Geoffray | e636228 | 2015-01-26 13:57:30 +0000 | [diff] [blame] | 51 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 52 | namespace art { |
| 53 | |
Nicolas Geoffray | a0bb2bd | 2015-01-26 12:49:35 +0000 | [diff] [blame] | 54 | // 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. |
| 59 | class 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 Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 65 | AddAllocatedRegister(Location::RegisterLocation(arm::R6)); |
| 66 | AddAllocatedRegister(Location::RegisterLocation(arm::R7)); |
Nicolas Geoffray | a0bb2bd | 2015-01-26 12:49:35 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void SetupBlockedRegisters(bool is_baseline) const OVERRIDE { |
| 70 | arm::CodeGeneratorARM::SetupBlockedRegisters(is_baseline); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 71 | blocked_core_registers_[arm::R4] = true; |
| 72 | blocked_core_registers_[arm::R6] = false; |
| 73 | blocked_core_registers_[arm::R7] = false; |
Nicolas Geoffray | e636228 | 2015-01-26 13:57:30 +0000 | [diff] [blame] | 74 | // Makes pair R6-R7 available. |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 75 | blocked_register_pairs_[arm::R6_R7] = false; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | class 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 Geoffray | a0bb2bd | 2015-01-26 12:49:35 +0000 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 104 | class InternalCodeAllocator : public CodeAllocator { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 105 | public: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 106 | InternalCodeAllocator() : size_(0) { } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 107 | |
| 108 | virtual uint8_t* Allocate(size_t size) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 109 | size_ = size; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 110 | memory_.reset(new uint8_t[size]); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 111 | return memory_.get(); |
| 112 | } |
| 113 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 114 | size_t GetSize() const { return size_; } |
| 115 | uint8_t* GetMemory() const { return memory_.get(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 116 | |
| 117 | private: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 118 | size_t size_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 119 | std::unique_ptr<uint8_t[]> memory_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 120 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 121 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 124 | static bool CanExecute(InstructionSet target_isa) { |
| 125 | return (target_isa == kRuntimeISA) || CodeSimulator::CanSimulate(target_isa); |
| 126 | } |
| 127 | |
| 128 | template <typename Expected> |
| 129 | static Expected SimulatorExecute(CodeSimulator* simulator, Expected (*f)()); |
| 130 | |
| 131 | template <> |
| 132 | bool SimulatorExecute<bool>(CodeSimulator* simulator, bool (*f)()) { |
| 133 | simulator->RunFrom(reinterpret_cast<intptr_t>(f)); |
| 134 | return simulator->GetCReturnBool(); |
| 135 | } |
| 136 | |
| 137 | template <> |
| 138 | int32_t SimulatorExecute<int32_t>(CodeSimulator* simulator, int32_t (*f)()) { |
| 139 | simulator->RunFrom(reinterpret_cast<intptr_t>(f)); |
| 140 | return simulator->GetCReturnInt32(); |
| 141 | } |
| 142 | |
| 143 | template <> |
| 144 | int64_t SimulatorExecute<int64_t>(CodeSimulator* simulator, int64_t (*f)()) { |
| 145 | simulator->RunFrom(reinterpret_cast<intptr_t>(f)); |
| 146 | return simulator->GetCReturnInt64(); |
| 147 | } |
| 148 | |
| 149 | template <typename Expected> |
| 150 | static 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 Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 174 | template <typename Expected> |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 175 | static void Run(const InternalCodeAllocator& allocator, |
| 176 | const CodeGenerator& codegen, |
| 177 | bool has_result, |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 178 | Expected expected) { |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 179 | InstructionSet target_isa = codegen.GetInstructionSet(); |
| 180 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 181 | typedef Expected (*fptr)(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 182 | CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 183 | fptr f = reinterpret_cast<fptr>(allocator.GetMemory()); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 184 | if (target_isa == kThumb2) { |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 185 | // For thumb we need the bottom bit set. |
| 186 | f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1); |
| 187 | } |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 188 | VerifyGeneratedCode(target_isa, f, has_result, expected); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 191 | template <typename Expected> |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 192 | static void RunCodeBaseline(InstructionSet target_isa, |
| 193 | HGraph* graph, |
| 194 | bool has_result, |
| 195 | Expected expected) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 196 | InternalCodeAllocator allocator; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 197 | |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 198 | CompilerOptions compiler_options; |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 199 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 200 | X86InstructionSetFeatures::FromCppDefines()); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 201 | TestCodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 202 | // 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 Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 204 | codegenX86.CompileBaseline(&allocator, true); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 205 | if (target_isa == kX86) { |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 206 | Run(allocator, codegenX86, has_result, expected); |
| 207 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 208 | |
Serban Constantinescu | 579885a | 2015-02-22 20:51:33 +0000 | [diff] [blame] | 209 | std::unique_ptr<const ArmInstructionSetFeatures> features_arm( |
Andreas Gampe | df64950 | 2015-01-06 14:13:52 -0800 | [diff] [blame] | 210 | ArmInstructionSetFeatures::FromCppDefines()); |
Serban Constantinescu | 579885a | 2015-02-22 20:51:33 +0000 | [diff] [blame] | 211 | TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 212 | codegenARM.CompileBaseline(&allocator, true); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 213 | if (target_isa == kArm || target_isa == kThumb2) { |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 214 | Run(allocator, codegenARM, has_result, expected); |
| 215 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 216 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 217 | 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 Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 220 | codegenX86_64.CompileBaseline(&allocator, true); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 221 | if (target_isa == kX86_64) { |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 222 | Run(allocator, codegenX86_64, has_result, expected); |
| 223 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 224 | |
Serban Constantinescu | 579885a | 2015-02-22 20:51:33 +0000 | [diff] [blame] | 225 | std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64( |
| 226 | Arm64InstructionSetFeatures::FromCppDefines()); |
| 227 | arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 228 | codegenARM64.CompileBaseline(&allocator, true); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 229 | if (target_isa == kArm64) { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 230 | Run(allocator, codegenARM64, has_result, expected); |
| 231 | } |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 232 | |
| 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 237 | if (target_isa == kMips64) { |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 238 | Run(allocator, codegenMIPS64, has_result, expected); |
| 239 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 242 | template <typename Expected> |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 243 | static void RunCodeOptimized(CodeGenerator* codegen, |
| 244 | HGraph* graph, |
| 245 | std::function<void(HGraph*)> hook_before_codegen, |
| 246 | bool has_result, |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 247 | Expected expected) { |
Nicolas Geoffray | ea80942 | 2015-06-24 14:25:09 +0100 | [diff] [blame] | 248 | // Tests may have already computed it. |
| 249 | if (graph->GetReversePostOrder().IsEmpty()) { |
| 250 | graph->BuildDominatorTree(); |
| 251 | } |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 252 | SsaLivenessAnalysis liveness(graph, codegen); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 253 | 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 Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 264 | template <typename Expected> |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 265 | static void RunCodeOptimized(InstructionSet target_isa, |
| 266 | HGraph* graph, |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 267 | std::function<void(HGraph*)> hook_before_codegen, |
| 268 | bool has_result, |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 269 | Expected expected) { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 270 | CompilerOptions compiler_options; |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 271 | 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 Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 275 | RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 276 | } 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 Rames | 3e69f16 | 2014-12-10 10:36:50 +0000 | [diff] [blame] | 280 | RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 281 | } else if (target_isa == kX86) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 282 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 283 | X86InstructionSetFeatures::FromCppDefines()); |
| 284 | x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options); |
Alexandre Rames | 3e69f16 | 2014-12-10 10:36:50 +0000 | [diff] [blame] | 285 | RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 286 | } else if (target_isa == kX86_64) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 287 | 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 Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 290 | RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 291 | } else if (target_isa == kMips64) { |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 292 | 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 Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 299 | static void TestCode(InstructionSet target_isa, |
| 300 | const uint16_t* data, |
| 301 | bool has_result = false, |
| 302 | int32_t expected = 0) { |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 303 | ArenaPool pool; |
| 304 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 305 | HGraph* graph = CreateGraph(&arena); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 306 | HGraphBuilder builder(graph); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 307 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 308 | bool graph_built = builder.BuildGraph(*item); |
| 309 | ASSERT_TRUE(graph_built); |
Calin Juravle | 039b6e2 | 2014-10-23 12:32:11 +0100 | [diff] [blame] | 310 | // Remove suspend checks, they cannot be executed in this context. |
Calin Juravle | 48dee04 | 2014-10-22 15:54:12 +0100 | [diff] [blame] | 311 | RemoveSuspendChecks(graph); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 312 | RunCodeBaseline(target_isa, graph, has_result, expected); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 315 | static void TestCodeLong(InstructionSet target_isa, |
| 316 | const uint16_t* data, |
| 317 | bool has_result, |
| 318 | int64_t expected) { |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 319 | ArenaPool pool; |
| 320 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 321 | HGraph* graph = CreateGraph(&arena); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 322 | HGraphBuilder builder(graph, Primitive::kPrimLong); |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 323 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 324 | bool graph_built = builder.BuildGraph(*item); |
| 325 | ASSERT_TRUE(graph_built); |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 326 | // Remove suspend checks, they cannot be executed in this context. |
| 327 | RemoveSuspendChecks(graph); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 328 | RunCodeBaseline(target_isa, graph, has_result, expected); |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 331 | class CodegenTest: public ::testing::TestWithParam<InstructionSet> {}; |
| 332 | |
| 333 | TEST_P(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 334 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 335 | TestCode(GetParam(), data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 338 | TEST_P(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 339 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 340 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 341 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 342 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 343 | TestCode(GetParam(), data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 346 | TEST_P(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 347 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 348 | Instruction::GOTO | 0x100, |
| 349 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 350 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 351 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 352 | TestCode(GetParam(), data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 355 | TEST_P(CodegenTest, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 356 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 357 | Instruction::GOTO | 0x200, |
| 358 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 359 | Instruction::GOTO | 0xFF00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 360 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 361 | TestCode(GetParam(), data1); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 362 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 363 | const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 364 | Instruction::GOTO_16, 3, |
| 365 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 366 | Instruction::GOTO_16, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 367 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 368 | TestCode(GetParam(), data2); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 369 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 370 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 371 | Instruction::GOTO_32, 4, 0, |
| 372 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 373 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 374 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 375 | TestCode(GetParam(), data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 378 | TEST_P(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 379 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 380 | Instruction::RETURN_VOID, |
| 381 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 382 | Instruction::GOTO | 0xFE00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 383 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 384 | TestCode(GetParam(), data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 387 | TEST_P(CodegenTest, CFG5) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 388 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 394 | TestCode(GetParam(), data); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 397 | TEST_P(CodegenTest, IntConstant) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 398 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 399 | Instruction::CONST_4 | 0 | 0, |
| 400 | Instruction::RETURN_VOID); |
| 401 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 402 | TestCode(GetParam(), data); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 405 | TEST_P(CodegenTest, Return1) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 406 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 407 | Instruction::CONST_4 | 0 | 0, |
| 408 | Instruction::RETURN | 0); |
| 409 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 410 | TestCode(GetParam(), data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 413 | TEST_P(CodegenTest, Return2) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 414 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 419 | TestCode(GetParam(), data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 422 | TEST_P(CodegenTest, Return3) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 423 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 428 | TestCode(GetParam(), data, true, 1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 431 | TEST_P(CodegenTest, ReturnIf1) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 432 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 439 | TestCode(GetParam(), data, true, 1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 442 | TEST_P(CodegenTest, ReturnIf2) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 443 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 450 | TestCode(GetParam(), data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 453 | // Exercise bit-wise (one's complement) not-int instruction. |
| 454 | #define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 455 | TEST_P(CodegenTest, TEST_NAME) { \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 456 | const int32_t input = INPUT; \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 457 | const uint16_t input_lo = Low16Bits(input); \ |
| 458 | const uint16_t input_hi = High16Bits(input); \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 459 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 464 | TestCode(GetParam(), data, true, EXPECTED_OUTPUT); \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | NOT_INT_TEST(ReturnNotIntMinus2, -2, 1) |
| 468 | NOT_INT_TEST(ReturnNotIntMinus1, -1, 0) |
| 469 | NOT_INT_TEST(ReturnNotInt0, 0, -1) |
| 470 | NOT_INT_TEST(ReturnNotInt1, 1, -2) |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 471 | NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1 |
| 472 | NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2 |
| 473 | NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1 |
| 474 | NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31) |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 475 | |
| 476 | #undef NOT_INT_TEST |
| 477 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 478 | // Exercise bit-wise (one's complement) not-long instruction. |
| 479 | #define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 480 | TEST_P(CodegenTest, TEST_NAME) { \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 481 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 491 | TestCodeLong(GetParam(), data, true, EXPECTED_OUTPUT); \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1)) |
| 495 | NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0)) |
| 496 | NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1)) |
| 497 | NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2)) |
| 498 | |
| 499 | NOT_LONG_TEST(ReturnNotLongINT32_MIN, |
| 500 | INT64_C(-2147483648), |
| 501 | INT64_C(2147483647)) // (2^31) - 1 |
| 502 | NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1, |
| 503 | INT64_C(-2147483647), |
| 504 | INT64_C(2147483646)) // (2^31) - 2 |
| 505 | NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1, |
| 506 | INT64_C(2147483646), |
| 507 | INT64_C(-2147483647)) // -(2^31) - 1 |
| 508 | NOT_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. |
| 515 | NOT_LONG_TEST(ReturnNotINT64_MIN, |
| 516 | INT64_C(-9223372036854775807)-1, |
| 517 | INT64_C(9223372036854775807)); // (2^63) - 1 |
| 518 | NOT_LONG_TEST(ReturnNotINT64_MINPlus1, |
| 519 | INT64_C(-9223372036854775807), |
| 520 | INT64_C(9223372036854775806)); // (2^63) - 2 |
| 521 | NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1, |
| 522 | INT64_C(9223372036854775806), |
| 523 | INT64_C(-9223372036854775807)); // -(2^63) - 1 |
| 524 | NOT_LONG_TEST(ReturnNotLongINT64_MAX, |
| 525 | INT64_C(9223372036854775807), |
| 526 | INT64_C(-9223372036854775807)-1); // -(2^63) |
| 527 | |
| 528 | #undef NOT_LONG_TEST |
| 529 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 530 | TEST_P(CodegenTest, IntToLongOfLongToInt) { |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 531 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 544 | TestCodeLong(GetParam(), data, true, 1); |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 547 | TEST_P(CodegenTest, ReturnAdd1) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 548 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 554 | TestCode(GetParam(), data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 557 | TEST_P(CodegenTest, ReturnAdd2) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 558 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 564 | TestCode(GetParam(), data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 567 | TEST_P(CodegenTest, ReturnAdd3) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 568 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 573 | TestCode(GetParam(), data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 576 | TEST_P(CodegenTest, ReturnAdd4) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 577 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 582 | TestCode(GetParam(), data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 585 | TEST_P(CodegenTest, NonMaterializedCondition) { |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 586 | ArenaPool pool; |
| 587 | ArenaAllocator allocator(&pool); |
| 588 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 589 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 590 | 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 Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 598 | HIntConstant* constant0 = graph->GetIntConstant(0); |
| 599 | HIntConstant* constant1 = graph->GetIntConstant(1); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 600 | 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 Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 625 | 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 Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 628 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 629 | }; |
| 630 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 631 | RunCodeOptimized(GetParam(), graph, hook_before_codegen, true, 0); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 634 | TEST_P(CodegenTest, ReturnMulInt) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 635 | 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 Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 640 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 641 | TestCode(GetParam(), data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 644 | TEST_P(CodegenTest, ReturnMulInt2addr) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 645 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 651 | TestCode(GetParam(), data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 654 | TEST_P(CodegenTest, ReturnMulLong) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 655 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 663 | TestCodeLong(GetParam(), data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 666 | TEST_P(CodegenTest, ReturnMulLong2addr) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 667 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 675 | TestCodeLong(GetParam(), data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 676 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 677 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 678 | TEST_P(CodegenTest, ReturnMulIntLit8) { |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 679 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 684 | TestCode(GetParam(), data, true, 12); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 687 | TEST_P(CodegenTest, ReturnMulIntLit16) { |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 688 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 693 | TestCode(GetParam(), data, true, 12); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 696 | TEST_P(CodegenTest, MaterializedCondition1) { |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 697 | // 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 Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 708 | HGraph* graph = CreateGraph(&allocator); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 709 | |
| 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 Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 724 | HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]); |
| 725 | HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]); |
| 726 | HLessThan cmp_lt(cst_lhs, cst_rhs); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 727 | code_block->AddInstruction(&cmp_lt); |
| 728 | HReturn ret(&cmp_lt); |
| 729 | code_block->AddInstruction(&ret); |
| 730 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 731 | 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 Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 734 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 735 | }; |
| 736 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 737 | RunCodeOptimized(GetParam(), graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 741 | TEST_P(CodegenTest, MaterializedCondition2) { |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 742 | // 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 Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 754 | HGraph* graph = CreateGraph(&allocator); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 755 | |
| 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 Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 779 | HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]); |
| 780 | HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]); |
| 781 | HLessThan cmp_lt(cst_lhs, cst_rhs); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 782 | 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 Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 790 | HIntConstant* cst_lt = graph->GetIntConstant(1); |
| 791 | HReturn ret_lt(cst_lt); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 792 | if_true_block->AddInstruction(&ret_lt); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 793 | HIntConstant* cst_ge = graph->GetIntConstant(0); |
| 794 | HReturn ret_ge(cst_ge); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 795 | if_false_block->AddInstruction(&ret_ge); |
| 796 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 797 | 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 Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 800 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 801 | }; |
| 802 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 803 | RunCodeOptimized(GetParam(), graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 804 | } |
| 805 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 806 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 807 | TEST_P(CodegenTest, ReturnDivIntLit8) { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 808 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 813 | TestCode(GetParam(), data, true, 1); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 816 | TEST_P(CodegenTest, ReturnDivInt2Addr) { |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 817 | 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 Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 823 | TestCode(GetParam(), data, true, 2); |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Phil Wang | c2e1a5e | 2015-07-29 15:14:09 +0800 | [diff] [blame] | 826 | static ::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 | |
| 848 | INSTANTIATE_TEST_CASE_P(MultipleTargets, |
| 849 | CodegenTest, |
| 850 | ::testing::ValuesIn(GetTargetISAs())); |
| 851 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 852 | } // namespace art |