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" |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 23 | #include "arch/mips/instruction_set_features_mips.h" |
| 24 | #include "arch/mips/registers_mips.h" |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 25 | #include "arch/mips64/instruction_set_features_mips64.h" |
| 26 | #include "arch/mips64/registers_mips64.h" |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 27 | #include "arch/x86/instruction_set_features_x86.h" |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 28 | #include "arch/x86/registers_x86.h" |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 29 | #include "arch/x86_64/instruction_set_features_x86_64.h" |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 30 | #include "base/macros.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 31 | #include "builder.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 32 | #include "code_generator_arm.h" |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 33 | #include "code_generator_arm64.h" |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 34 | #include "code_generator_mips.h" |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 35 | #include "code_generator_mips64.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 36 | #include "code_generator_x86.h" |
| 37 | #include "code_generator_x86_64.h" |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 38 | #include "code_simulator_container.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 39 | #include "common_compiler_test.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 40 | #include "dex_file.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 41 | #include "dex_instruction.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 42 | #include "driver/compiler_options.h" |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 43 | #include "graph_checker.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 44 | #include "nodes.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 45 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 46 | #include "prepare_for_register_allocation.h" |
| 47 | #include "register_allocator.h" |
| 48 | #include "ssa_liveness_analysis.h" |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 49 | #include "utils.h" |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 50 | #include "utils/arm/managed_register_arm.h" |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 51 | #include "utils/mips/managed_register_mips.h" |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 52 | #include "utils/mips64/managed_register_mips64.h" |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 53 | #include "utils/x86/managed_register_x86.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 54 | |
| 55 | #include "gtest/gtest.h" |
Nicolas Geoffray | e636228 | 2015-01-26 13:57:30 +0000 | [diff] [blame] | 56 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 57 | namespace art { |
| 58 | |
Nicolas Geoffray | a0bb2bd | 2015-01-26 12:49:35 +0000 | [diff] [blame] | 59 | // Provide our own codegen, that ensures the C calling conventions |
| 60 | // are preserved. Currently, ART and C do not match as R4 is caller-save |
| 61 | // in ART, and callee-save in C. Alternatively, we could use or write |
| 62 | // the stub that saves and restores all registers, but it is easier |
| 63 | // to just overwrite the code generator. |
| 64 | class TestCodeGeneratorARM : public arm::CodeGeneratorARM { |
| 65 | public: |
| 66 | TestCodeGeneratorARM(HGraph* graph, |
| 67 | const ArmInstructionSetFeatures& isa_features, |
| 68 | const CompilerOptions& compiler_options) |
| 69 | : arm::CodeGeneratorARM(graph, isa_features, compiler_options) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 70 | AddAllocatedRegister(Location::RegisterLocation(arm::R6)); |
| 71 | AddAllocatedRegister(Location::RegisterLocation(arm::R7)); |
Nicolas Geoffray | a0bb2bd | 2015-01-26 12:49:35 +0000 | [diff] [blame] | 72 | } |
| 73 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 74 | void SetupBlockedRegisters() const OVERRIDE { |
| 75 | arm::CodeGeneratorARM::SetupBlockedRegisters(); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 76 | blocked_core_registers_[arm::R4] = true; |
| 77 | blocked_core_registers_[arm::R6] = false; |
| 78 | blocked_core_registers_[arm::R7] = false; |
Nicolas Geoffray | e636228 | 2015-01-26 13:57:30 +0000 | [diff] [blame] | 79 | // Makes pair R6-R7 available. |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 80 | blocked_register_pairs_[arm::R6_R7] = false; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | class TestCodeGeneratorX86 : public x86::CodeGeneratorX86 { |
| 85 | public: |
| 86 | TestCodeGeneratorX86(HGraph* graph, |
| 87 | const X86InstructionSetFeatures& isa_features, |
| 88 | const CompilerOptions& compiler_options) |
| 89 | : x86::CodeGeneratorX86(graph, isa_features, compiler_options) { |
| 90 | // Save edi, we need it for getting enough registers for long multiplication. |
| 91 | AddAllocatedRegister(Location::RegisterLocation(x86::EDI)); |
| 92 | } |
| 93 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 94 | void SetupBlockedRegisters() const OVERRIDE { |
| 95 | x86::CodeGeneratorX86::SetupBlockedRegisters(); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 96 | // ebx is a callee-save register in C, but caller-save for ART. |
| 97 | blocked_core_registers_[x86::EBX] = true; |
| 98 | blocked_register_pairs_[x86::EAX_EBX] = true; |
| 99 | blocked_register_pairs_[x86::EDX_EBX] = true; |
| 100 | blocked_register_pairs_[x86::ECX_EBX] = true; |
| 101 | blocked_register_pairs_[x86::EBX_EDI] = true; |
| 102 | |
| 103 | // Make edi available. |
| 104 | blocked_core_registers_[x86::EDI] = false; |
| 105 | blocked_register_pairs_[x86::ECX_EDI] = false; |
Nicolas Geoffray | a0bb2bd | 2015-01-26 12:49:35 +0000 | [diff] [blame] | 106 | } |
| 107 | }; |
| 108 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 109 | class InternalCodeAllocator : public CodeAllocator { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 110 | public: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 111 | InternalCodeAllocator() : size_(0) { } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 112 | |
| 113 | virtual uint8_t* Allocate(size_t size) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 114 | size_ = size; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 115 | memory_.reset(new uint8_t[size]); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 116 | return memory_.get(); |
| 117 | } |
| 118 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 119 | size_t GetSize() const { return size_; } |
| 120 | uint8_t* GetMemory() const { return memory_.get(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 121 | |
| 122 | private: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 123 | size_t size_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 124 | std::unique_ptr<uint8_t[]> memory_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 125 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 126 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 129 | static bool CanExecuteOnHardware(InstructionSet target_isa) { |
| 130 | return (target_isa == kRuntimeISA) |
| 131 | // Handle the special case of ARM, with two instructions sets (ARM32 and Thumb-2). |
| 132 | || (kRuntimeISA == kArm && target_isa == kThumb2); |
| 133 | } |
| 134 | |
| 135 | static bool CanExecute(InstructionSet target_isa) { |
| 136 | CodeSimulatorContainer simulator(target_isa); |
| 137 | return CanExecuteOnHardware(target_isa) || simulator.CanSimulate(); |
| 138 | } |
| 139 | |
| 140 | template <typename Expected> |
| 141 | static Expected SimulatorExecute(CodeSimulator* simulator, Expected (*f)()); |
| 142 | |
| 143 | template <> |
| 144 | bool SimulatorExecute<bool>(CodeSimulator* simulator, bool (*f)()) { |
| 145 | simulator->RunFrom(reinterpret_cast<intptr_t>(f)); |
| 146 | return simulator->GetCReturnBool(); |
| 147 | } |
| 148 | |
| 149 | template <> |
| 150 | int32_t SimulatorExecute<int32_t>(CodeSimulator* simulator, int32_t (*f)()) { |
| 151 | simulator->RunFrom(reinterpret_cast<intptr_t>(f)); |
| 152 | return simulator->GetCReturnInt32(); |
| 153 | } |
| 154 | |
| 155 | template <> |
| 156 | int64_t SimulatorExecute<int64_t>(CodeSimulator* simulator, int64_t (*f)()) { |
| 157 | simulator->RunFrom(reinterpret_cast<intptr_t>(f)); |
| 158 | return simulator->GetCReturnInt64(); |
| 159 | } |
| 160 | |
| 161 | template <typename Expected> |
| 162 | static void VerifyGeneratedCode(InstructionSet target_isa, |
| 163 | Expected (*f)(), |
| 164 | bool has_result, |
| 165 | Expected expected) { |
| 166 | ASSERT_TRUE(CanExecute(target_isa)) << "Target isa is not executable."; |
| 167 | |
| 168 | // Verify on simulator. |
| 169 | CodeSimulatorContainer simulator(target_isa); |
| 170 | if (simulator.CanSimulate()) { |
| 171 | Expected result = SimulatorExecute<Expected>(simulator.Get(), f); |
| 172 | if (has_result) { |
| 173 | ASSERT_EQ(expected, result); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Verify on hardware. |
| 178 | if (CanExecuteOnHardware(target_isa)) { |
| 179 | Expected result = f(); |
| 180 | if (has_result) { |
| 181 | ASSERT_EQ(expected, result); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 186 | template <typename Expected> |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 187 | static void Run(const InternalCodeAllocator& allocator, |
| 188 | const CodeGenerator& codegen, |
| 189 | bool has_result, |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 190 | Expected expected) { |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 191 | InstructionSet target_isa = codegen.GetInstructionSet(); |
| 192 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 193 | typedef Expected (*fptr)(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 194 | CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 195 | fptr f = reinterpret_cast<fptr>(allocator.GetMemory()); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 196 | if (target_isa == kThumb2) { |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 197 | // For thumb we need the bottom bit set. |
| 198 | f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1); |
| 199 | } |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 200 | VerifyGeneratedCode(target_isa, f, has_result, expected); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 203 | template <typename Expected> |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 204 | static void RunCode(CodeGenerator* codegen, |
| 205 | HGraph* graph, |
| 206 | std::function<void(HGraph*)> hook_before_codegen, |
| 207 | bool has_result, |
| 208 | Expected expected) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 209 | GraphChecker graph_checker(graph); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 210 | graph_checker.Run(); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 211 | if (!graph_checker.IsValid()) { |
| 212 | for (auto error : graph_checker.GetErrors()) { |
| 213 | std::cout << error << std::endl; |
| 214 | } |
| 215 | } |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 216 | ASSERT_TRUE(graph_checker.IsValid()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 217 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 218 | SsaLivenessAnalysis liveness(graph, codegen); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 219 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 220 | PrepareForRegisterAllocation(graph).Run(); |
| 221 | liveness.Analyze(); |
| 222 | RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 223 | hook_before_codegen(graph); |
| 224 | |
| 225 | InternalCodeAllocator allocator; |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 226 | codegen->Compile(&allocator); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 227 | Run(allocator, *codegen, has_result, expected); |
| 228 | } |
| 229 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 230 | template <typename Expected> |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 231 | static void RunCode(InstructionSet target_isa, |
| 232 | HGraph* graph, |
| 233 | std::function<void(HGraph*)> hook_before_codegen, |
| 234 | bool has_result, |
| 235 | Expected expected) { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 236 | CompilerOptions compiler_options; |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 237 | if (target_isa == kArm || target_isa == kThumb2) { |
| 238 | std::unique_ptr<const ArmInstructionSetFeatures> features_arm( |
| 239 | ArmInstructionSetFeatures::FromCppDefines()); |
| 240 | TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 241 | RunCode(&codegenARM, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 242 | } else if (target_isa == kArm64) { |
| 243 | std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64( |
| 244 | Arm64InstructionSetFeatures::FromCppDefines()); |
| 245 | arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 246 | RunCode(&codegenARM64, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 247 | } else if (target_isa == kX86) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 248 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 249 | X86InstructionSetFeatures::FromCppDefines()); |
| 250 | x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 251 | RunCode(&codegenX86, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 252 | } else if (target_isa == kX86_64) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 253 | std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64( |
| 254 | X86_64InstructionSetFeatures::FromCppDefines()); |
| 255 | x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 256 | RunCode(&codegenX86_64, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 257 | } else if (target_isa == kMips) { |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 258 | std::unique_ptr<const MipsInstructionSetFeatures> features_mips( |
| 259 | MipsInstructionSetFeatures::FromCppDefines()); |
| 260 | mips::CodeGeneratorMIPS codegenMIPS(graph, *features_mips.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 261 | RunCode(&codegenMIPS, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 262 | } else if (target_isa == kMips64) { |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 263 | std::unique_ptr<const Mips64InstructionSetFeatures> features_mips64( |
| 264 | Mips64InstructionSetFeatures::FromCppDefines()); |
| 265 | mips64::CodeGeneratorMIPS64 codegenMIPS64(graph, *features_mips64.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 266 | RunCode(&codegenMIPS64, graph, hook_before_codegen, has_result, expected); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 270 | static ::std::vector<InstructionSet> GetTargetISAs() { |
| 271 | ::std::vector<InstructionSet> v; |
| 272 | // Add all ISAs that are executable on hardware or on simulator. |
| 273 | const ::std::vector<InstructionSet> executable_isa_candidates = { |
| 274 | kArm, |
| 275 | kArm64, |
| 276 | kThumb2, |
| 277 | kX86, |
| 278 | kX86_64, |
| 279 | kMips, |
| 280 | kMips64 |
| 281 | }; |
| 282 | |
| 283 | for (auto target_isa : executable_isa_candidates) { |
| 284 | if (CanExecute(target_isa)) { |
| 285 | v.push_back(target_isa); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return v; |
| 290 | } |
| 291 | |
| 292 | static void TestCode(const uint16_t* data, |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 293 | bool has_result = false, |
| 294 | int32_t expected = 0) { |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 295 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 296 | ArenaPool pool; |
| 297 | ArenaAllocator arena(&pool); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 298 | HGraph* graph = CreateCFG(&arena, data); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 299 | // Remove suspend checks, they cannot be executed in this context. |
| 300 | RemoveSuspendChecks(graph); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 301 | RunCode(target_isa, graph, [](HGraph*) {}, has_result, expected); |
| 302 | } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 303 | } |
| 304 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 305 | static void TestCodeLong(const uint16_t* data, |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 306 | bool has_result, |
| 307 | int64_t expected) { |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 308 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 309 | ArenaPool pool; |
| 310 | ArenaAllocator arena(&pool); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 311 | HGraph* graph = CreateCFG(&arena, data, Primitive::kPrimLong); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 312 | // Remove suspend checks, they cannot be executed in this context. |
| 313 | RemoveSuspendChecks(graph); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 314 | RunCode(target_isa, graph, [](HGraph*) {}, has_result, expected); |
| 315 | } |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 316 | } |
| 317 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 318 | class CodegenTest : public CommonCompilerTest {}; |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 319 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 320 | TEST_F(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 321 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 322 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 323 | } |
| 324 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 325 | TEST_F(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 326 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 327 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 328 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 329 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 330 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 331 | } |
| 332 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 333 | TEST_F(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 334 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 335 | Instruction::GOTO | 0x100, |
| 336 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 337 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 338 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 339 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 340 | } |
| 341 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 342 | TEST_F(CodegenTest, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 343 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 344 | Instruction::GOTO | 0x200, |
| 345 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 346 | Instruction::GOTO | 0xFF00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 347 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 348 | TestCode(data1); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 349 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 350 | const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 351 | Instruction::GOTO_16, 3, |
| 352 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 353 | Instruction::GOTO_16, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 354 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 355 | TestCode(data2); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 356 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 357 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 358 | Instruction::GOTO_32, 4, 0, |
| 359 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 360 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 361 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 362 | TestCode(data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 363 | } |
| 364 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 365 | TEST_F(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 366 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 367 | Instruction::RETURN_VOID, |
| 368 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 369 | Instruction::GOTO | 0xFE00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 370 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 371 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 372 | } |
| 373 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 374 | TEST_F(CodegenTest, CFG5) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 375 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 376 | Instruction::CONST_4 | 0 | 0, |
| 377 | Instruction::IF_EQ, 3, |
| 378 | Instruction::GOTO | 0x100, |
| 379 | Instruction::RETURN_VOID); |
| 380 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 381 | TestCode(data); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 382 | } |
| 383 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 384 | TEST_F(CodegenTest, IntConstant) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 385 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 386 | Instruction::CONST_4 | 0 | 0, |
| 387 | Instruction::RETURN_VOID); |
| 388 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 389 | TestCode(data); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 390 | } |
| 391 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 392 | TEST_F(CodegenTest, Return1) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 393 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 394 | Instruction::CONST_4 | 0 | 0, |
| 395 | Instruction::RETURN | 0); |
| 396 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 397 | TestCode(data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 398 | } |
| 399 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 400 | TEST_F(CodegenTest, Return2) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 401 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 402 | Instruction::CONST_4 | 0 | 0, |
| 403 | Instruction::CONST_4 | 0 | 1 << 8, |
| 404 | Instruction::RETURN | 1 << 8); |
| 405 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 406 | TestCode(data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 407 | } |
| 408 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 409 | TEST_F(CodegenTest, Return3) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 410 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 411 | Instruction::CONST_4 | 0 | 0, |
| 412 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 413 | Instruction::RETURN | 1 << 8); |
| 414 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 415 | TestCode(data, true, 1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 416 | } |
| 417 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 418 | TEST_F(CodegenTest, ReturnIf1) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 419 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 420 | Instruction::CONST_4 | 0 | 0, |
| 421 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 422 | Instruction::IF_EQ, 3, |
| 423 | Instruction::RETURN | 0 << 8, |
| 424 | Instruction::RETURN | 1 << 8); |
| 425 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 426 | TestCode(data, true, 1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 427 | } |
| 428 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 429 | TEST_F(CodegenTest, ReturnIf2) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 430 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 431 | Instruction::CONST_4 | 0 | 0, |
| 432 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 433 | Instruction::IF_EQ | 0 << 4 | 1 << 8, 3, |
| 434 | Instruction::RETURN | 0 << 8, |
| 435 | Instruction::RETURN | 1 << 8); |
| 436 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 437 | TestCode(data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 440 | // Exercise bit-wise (one's complement) not-int instruction. |
| 441 | #define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 442 | TEST_F(CodegenTest, TEST_NAME) { \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 443 | const int32_t input = INPUT; \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 444 | const uint16_t input_lo = Low16Bits(input); \ |
| 445 | const uint16_t input_hi = High16Bits(input); \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 446 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \ |
| 447 | Instruction::CONST | 0 << 8, input_lo, input_hi, \ |
| 448 | Instruction::NOT_INT | 1 << 8 | 0 << 12 , \ |
| 449 | Instruction::RETURN | 1 << 8); \ |
| 450 | \ |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 451 | TestCode(data, true, EXPECTED_OUTPUT); \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | NOT_INT_TEST(ReturnNotIntMinus2, -2, 1) |
| 455 | NOT_INT_TEST(ReturnNotIntMinus1, -1, 0) |
| 456 | NOT_INT_TEST(ReturnNotInt0, 0, -1) |
| 457 | NOT_INT_TEST(ReturnNotInt1, 1, -2) |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 458 | NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1 |
| 459 | NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2 |
| 460 | NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1 |
| 461 | NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31) |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 462 | |
| 463 | #undef NOT_INT_TEST |
| 464 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 465 | // Exercise bit-wise (one's complement) not-long instruction. |
| 466 | #define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 467 | TEST_F(CodegenTest, TEST_NAME) { \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 468 | const int64_t input = INPUT; \ |
| 469 | const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \ |
| 470 | const uint16_t word1 = High16Bits(Low32Bits(input)); \ |
| 471 | const uint16_t word2 = Low16Bits(High32Bits(input)); \ |
| 472 | const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \ |
| 473 | const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \ |
| 474 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \ |
| 475 | Instruction::NOT_LONG | 2 << 8 | 0 << 12, \ |
| 476 | Instruction::RETURN_WIDE | 2 << 8); \ |
| 477 | \ |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 478 | TestCodeLong(data, true, EXPECTED_OUTPUT); \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1)) |
| 482 | NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0)) |
| 483 | NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1)) |
| 484 | NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2)) |
| 485 | |
| 486 | NOT_LONG_TEST(ReturnNotLongINT32_MIN, |
| 487 | INT64_C(-2147483648), |
| 488 | INT64_C(2147483647)) // (2^31) - 1 |
| 489 | NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1, |
| 490 | INT64_C(-2147483647), |
| 491 | INT64_C(2147483646)) // (2^31) - 2 |
| 492 | NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1, |
| 493 | INT64_C(2147483646), |
| 494 | INT64_C(-2147483647)) // -(2^31) - 1 |
| 495 | NOT_LONG_TEST(ReturnNotLongINT32_MAX, |
| 496 | INT64_C(2147483647), |
| 497 | INT64_C(-2147483648)) // -(2^31) |
| 498 | |
| 499 | // Note that the C++ compiler won't accept |
| 500 | // INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid |
| 501 | // int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead. |
| 502 | NOT_LONG_TEST(ReturnNotINT64_MIN, |
| 503 | INT64_C(-9223372036854775807)-1, |
| 504 | INT64_C(9223372036854775807)); // (2^63) - 1 |
| 505 | NOT_LONG_TEST(ReturnNotINT64_MINPlus1, |
| 506 | INT64_C(-9223372036854775807), |
| 507 | INT64_C(9223372036854775806)); // (2^63) - 2 |
| 508 | NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1, |
| 509 | INT64_C(9223372036854775806), |
| 510 | INT64_C(-9223372036854775807)); // -(2^63) - 1 |
| 511 | NOT_LONG_TEST(ReturnNotLongINT64_MAX, |
| 512 | INT64_C(9223372036854775807), |
| 513 | INT64_C(-9223372036854775807)-1); // -(2^63) |
| 514 | |
| 515 | #undef NOT_LONG_TEST |
| 516 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 517 | TEST_F(CodegenTest, IntToLongOfLongToInt) { |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 518 | const int64_t input = INT64_C(4294967296); // 2^32 |
| 519 | const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW. |
| 520 | const uint16_t word1 = High16Bits(Low32Bits(input)); |
| 521 | const uint16_t word2 = Low16Bits(High32Bits(input)); |
| 522 | const uint16_t word3 = High16Bits(High32Bits(input)); // MSW. |
| 523 | const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM( |
| 524 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, |
| 525 | Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0, |
| 526 | Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1 |
| 527 | Instruction::LONG_TO_INT | 4 << 8 | 0 << 12, |
| 528 | Instruction::INT_TO_LONG | 2 << 8 | 4 << 12, |
| 529 | Instruction::RETURN_WIDE | 2 << 8); |
| 530 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 531 | TestCodeLong(data, true, 1); |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 532 | } |
| 533 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 534 | TEST_F(CodegenTest, ReturnAdd1) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 535 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 536 | Instruction::CONST_4 | 3 << 12 | 0, |
| 537 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 538 | Instruction::ADD_INT, 1 << 8 | 0, |
| 539 | Instruction::RETURN); |
| 540 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 541 | TestCode(data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 542 | } |
| 543 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 544 | TEST_F(CodegenTest, ReturnAdd2) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 545 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 546 | Instruction::CONST_4 | 3 << 12 | 0, |
| 547 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 548 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 549 | Instruction::RETURN); |
| 550 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 551 | TestCode(data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 552 | } |
| 553 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 554 | TEST_F(CodegenTest, ReturnAdd3) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 555 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 556 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 557 | Instruction::ADD_INT_LIT8, 3 << 8 | 0, |
| 558 | Instruction::RETURN); |
| 559 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 560 | TestCode(data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 561 | } |
| 562 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 563 | TEST_F(CodegenTest, ReturnAdd4) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 564 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 565 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 566 | Instruction::ADD_INT_LIT16, 3, |
| 567 | Instruction::RETURN); |
| 568 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 569 | TestCode(data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 570 | } |
| 571 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 572 | TEST_F(CodegenTest, ReturnMulInt) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 573 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 574 | Instruction::CONST_4 | 3 << 12 | 0, |
| 575 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 576 | Instruction::MUL_INT, 1 << 8 | 0, |
| 577 | Instruction::RETURN); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 578 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 579 | TestCode(data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 580 | } |
| 581 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 582 | TEST_F(CodegenTest, ReturnMulInt2addr) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 583 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 584 | Instruction::CONST_4 | 3 << 12 | 0, |
| 585 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 586 | Instruction::MUL_INT_2ADDR | 1 << 12, |
| 587 | Instruction::RETURN); |
| 588 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 589 | TestCode(data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 590 | } |
| 591 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 592 | TEST_F(CodegenTest, ReturnMulLong) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 593 | const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 594 | Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0, |
| 595 | Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0, |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 596 | Instruction::MUL_LONG, 2 << 8 | 0, |
| 597 | Instruction::RETURN_WIDE); |
| 598 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 599 | TestCodeLong(data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 600 | } |
| 601 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 602 | TEST_F(CodegenTest, ReturnMulLong2addr) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 603 | const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 604 | Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0, |
| 605 | Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0, |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 606 | Instruction::MUL_LONG_2ADDR | 2 << 12, |
| 607 | Instruction::RETURN_WIDE); |
| 608 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 609 | TestCodeLong(data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 610 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 611 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 612 | TEST_F(CodegenTest, ReturnMulIntLit8) { |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 613 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 614 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 615 | Instruction::MUL_INT_LIT8, 3 << 8 | 0, |
| 616 | Instruction::RETURN); |
| 617 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 618 | TestCode(data, true, 12); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 619 | } |
| 620 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 621 | TEST_F(CodegenTest, ReturnMulIntLit16) { |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 622 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 623 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 624 | Instruction::MUL_INT_LIT16, 3, |
| 625 | Instruction::RETURN); |
| 626 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 627 | TestCode(data, true, 12); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 628 | } |
| 629 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 630 | TEST_F(CodegenTest, NonMaterializedCondition) { |
| 631 | for (InstructionSet target_isa : GetTargetISAs()) { |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 632 | ArenaPool pool; |
| 633 | ArenaAllocator allocator(&pool); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 634 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 635 | HGraph* graph = CreateGraph(&allocator); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 636 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 637 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 638 | graph->AddBlock(entry); |
| 639 | graph->SetEntryBlock(entry); |
| 640 | entry->AddInstruction(new (&allocator) HGoto()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 641 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 642 | HBasicBlock* first_block = new (&allocator) HBasicBlock(graph); |
| 643 | graph->AddBlock(first_block); |
| 644 | entry->AddSuccessor(first_block); |
| 645 | HIntConstant* constant0 = graph->GetIntConstant(0); |
| 646 | HIntConstant* constant1 = graph->GetIntConstant(1); |
| 647 | HEqual* equal = new (&allocator) HEqual(constant0, constant0); |
| 648 | first_block->AddInstruction(equal); |
| 649 | first_block->AddInstruction(new (&allocator) HIf(equal)); |
| 650 | |
| 651 | HBasicBlock* then_block = new (&allocator) HBasicBlock(graph); |
| 652 | HBasicBlock* else_block = new (&allocator) HBasicBlock(graph); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 653 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 654 | graph->SetExitBlock(exit_block); |
| 655 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 656 | graph->AddBlock(then_block); |
| 657 | graph->AddBlock(else_block); |
| 658 | graph->AddBlock(exit_block); |
| 659 | first_block->AddSuccessor(then_block); |
| 660 | first_block->AddSuccessor(else_block); |
| 661 | then_block->AddSuccessor(exit_block); |
| 662 | else_block->AddSuccessor(exit_block); |
| 663 | |
| 664 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 665 | then_block->AddInstruction(new (&allocator) HReturn(constant0)); |
| 666 | else_block->AddInstruction(new (&allocator) HReturn(constant1)); |
| 667 | |
David Brazdil | b11b072 | 2016-01-28 16:22:40 +0000 | [diff] [blame] | 668 | ASSERT_FALSE(equal->IsEmittedAtUseSite()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 669 | graph->BuildDominatorTree(); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 670 | PrepareForRegisterAllocation(graph).Run(); |
David Brazdil | b11b072 | 2016-01-28 16:22:40 +0000 | [diff] [blame] | 671 | ASSERT_TRUE(equal->IsEmittedAtUseSite()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 672 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 673 | auto hook_before_codegen = [](HGraph* graph_in) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 674 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 675 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 676 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 677 | }; |
| 678 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 679 | RunCode(target_isa, graph, hook_before_codegen, true, 0); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 683 | TEST_F(CodegenTest, MaterializedCondition1) { |
| 684 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 685 | // Check that condition are materialized correctly. A materialized condition |
| 686 | // should yield `1` if it evaluated to true, and `0` otherwise. |
| 687 | // We force the materialization of comparisons for different combinations of |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 688 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 689 | // inputs and check the results. |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 690 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 691 | int lhs[] = {1, 2, -1, 2, 0xabc}; |
| 692 | int rhs[] = {2, 1, 2, -1, 0xabc}; |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 693 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 694 | for (size_t i = 0; i < arraysize(lhs); i++) { |
| 695 | ArenaPool pool; |
| 696 | ArenaAllocator allocator(&pool); |
| 697 | HGraph* graph = CreateGraph(&allocator); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 698 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 699 | HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph); |
| 700 | graph->AddBlock(entry_block); |
| 701 | graph->SetEntryBlock(entry_block); |
| 702 | entry_block->AddInstruction(new (&allocator) HGoto()); |
| 703 | HBasicBlock* code_block = new (&allocator) HBasicBlock(graph); |
| 704 | graph->AddBlock(code_block); |
| 705 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 706 | graph->AddBlock(exit_block); |
| 707 | exit_block->AddInstruction(new (&allocator) HExit()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 708 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 709 | entry_block->AddSuccessor(code_block); |
| 710 | code_block->AddSuccessor(exit_block); |
| 711 | graph->SetExitBlock(exit_block); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 712 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 713 | HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]); |
| 714 | HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]); |
| 715 | HLessThan cmp_lt(cst_lhs, cst_rhs); |
| 716 | code_block->AddInstruction(&cmp_lt); |
| 717 | HReturn ret(&cmp_lt); |
| 718 | code_block->AddInstruction(&ret); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 719 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 720 | graph->BuildDominatorTree(); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 721 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 722 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
| 723 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
| 724 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 725 | }; |
| 726 | RunCode(target_isa, graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
| 727 | } |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 728 | } |
| 729 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 730 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 731 | TEST_F(CodegenTest, MaterializedCondition2) { |
| 732 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 733 | // Check that HIf correctly interprets a materialized condition. |
| 734 | // We force the materialization of comparisons for different combinations of |
| 735 | // inputs. An HIf takes the materialized combination as input and returns a |
| 736 | // value that we verify. |
| 737 | |
| 738 | int lhs[] = {1, 2, -1, 2, 0xabc}; |
| 739 | int rhs[] = {2, 1, 2, -1, 0xabc}; |
| 740 | |
| 741 | |
| 742 | for (size_t i = 0; i < arraysize(lhs); i++) { |
| 743 | ArenaPool pool; |
| 744 | ArenaAllocator allocator(&pool); |
| 745 | HGraph* graph = CreateGraph(&allocator); |
| 746 | |
| 747 | HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph); |
| 748 | graph->AddBlock(entry_block); |
| 749 | graph->SetEntryBlock(entry_block); |
| 750 | entry_block->AddInstruction(new (&allocator) HGoto()); |
| 751 | |
| 752 | HBasicBlock* if_block = new (&allocator) HBasicBlock(graph); |
| 753 | graph->AddBlock(if_block); |
| 754 | HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph); |
| 755 | graph->AddBlock(if_true_block); |
| 756 | HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph); |
| 757 | graph->AddBlock(if_false_block); |
| 758 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 759 | graph->AddBlock(exit_block); |
| 760 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 761 | |
| 762 | graph->SetEntryBlock(entry_block); |
| 763 | entry_block->AddSuccessor(if_block); |
| 764 | if_block->AddSuccessor(if_true_block); |
| 765 | if_block->AddSuccessor(if_false_block); |
| 766 | if_true_block->AddSuccessor(exit_block); |
| 767 | if_false_block->AddSuccessor(exit_block); |
| 768 | graph->SetExitBlock(exit_block); |
| 769 | |
| 770 | HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]); |
| 771 | HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]); |
| 772 | HLessThan cmp_lt(cst_lhs, cst_rhs); |
| 773 | if_block->AddInstruction(&cmp_lt); |
David Brazdil | 6e33252 | 2016-02-02 16:15:27 +0000 | [diff] [blame] | 774 | // We insert a dummy instruction to separate the HIf from the HLessThan |
| 775 | // and force the materialization of the condition. |
| 776 | HMemoryBarrier force_materialization(MemBarrierKind::kAnyAny, 0); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 777 | if_block->AddInstruction(&force_materialization); |
| 778 | HIf if_lt(&cmp_lt); |
| 779 | if_block->AddInstruction(&if_lt); |
| 780 | |
| 781 | HIntConstant* cst_lt = graph->GetIntConstant(1); |
| 782 | HReturn ret_lt(cst_lt); |
| 783 | if_true_block->AddInstruction(&ret_lt); |
| 784 | HIntConstant* cst_ge = graph->GetIntConstant(0); |
| 785 | HReturn ret_ge(cst_ge); |
| 786 | if_false_block->AddInstruction(&ret_ge); |
| 787 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 788 | graph->BuildDominatorTree(); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 789 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 790 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
| 791 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
| 792 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 793 | }; |
| 794 | RunCode(target_isa, graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | TEST_F(CodegenTest, ReturnDivIntLit8) { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 800 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 801 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 802 | Instruction::DIV_INT_LIT8, 3 << 8 | 0, |
| 803 | Instruction::RETURN); |
| 804 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 805 | TestCode(data, true, 1); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 806 | } |
| 807 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 808 | TEST_F(CodegenTest, ReturnDivInt2Addr) { |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 809 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 810 | Instruction::CONST_4 | 4 << 12 | 0, |
| 811 | Instruction::CONST_4 | 2 << 12 | 1 << 8, |
| 812 | Instruction::DIV_INT_2ADDR | 1 << 12, |
| 813 | Instruction::RETURN); |
| 814 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 815 | TestCode(data, true, 2); |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 818 | // Helper method. |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 819 | static void TestComparison(IfCondition condition, |
| 820 | int64_t i, |
| 821 | int64_t j, |
| 822 | Primitive::Type type, |
| 823 | const InstructionSet target_isa) { |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 824 | ArenaPool pool; |
| 825 | ArenaAllocator allocator(&pool); |
| 826 | HGraph* graph = CreateGraph(&allocator); |
| 827 | |
| 828 | HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph); |
| 829 | graph->AddBlock(entry_block); |
| 830 | graph->SetEntryBlock(entry_block); |
| 831 | entry_block->AddInstruction(new (&allocator) HGoto()); |
| 832 | |
| 833 | HBasicBlock* block = new (&allocator) HBasicBlock(graph); |
| 834 | graph->AddBlock(block); |
| 835 | |
| 836 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 837 | graph->AddBlock(exit_block); |
| 838 | graph->SetExitBlock(exit_block); |
| 839 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 840 | |
| 841 | entry_block->AddSuccessor(block); |
| 842 | block->AddSuccessor(exit_block); |
| 843 | |
| 844 | HInstruction* op1; |
| 845 | HInstruction* op2; |
| 846 | if (type == Primitive::kPrimInt) { |
| 847 | op1 = graph->GetIntConstant(i); |
| 848 | op2 = graph->GetIntConstant(j); |
| 849 | } else { |
| 850 | DCHECK_EQ(type, Primitive::kPrimLong); |
| 851 | op1 = graph->GetLongConstant(i); |
| 852 | op2 = graph->GetLongConstant(j); |
| 853 | } |
| 854 | |
| 855 | HInstruction* comparison = nullptr; |
| 856 | bool expected_result = false; |
| 857 | const uint64_t x = i; |
| 858 | const uint64_t y = j; |
| 859 | switch (condition) { |
| 860 | case kCondEQ: |
| 861 | comparison = new (&allocator) HEqual(op1, op2); |
| 862 | expected_result = (i == j); |
| 863 | break; |
| 864 | case kCondNE: |
| 865 | comparison = new (&allocator) HNotEqual(op1, op2); |
| 866 | expected_result = (i != j); |
| 867 | break; |
| 868 | case kCondLT: |
| 869 | comparison = new (&allocator) HLessThan(op1, op2); |
| 870 | expected_result = (i < j); |
| 871 | break; |
| 872 | case kCondLE: |
| 873 | comparison = new (&allocator) HLessThanOrEqual(op1, op2); |
| 874 | expected_result = (i <= j); |
| 875 | break; |
| 876 | case kCondGT: |
| 877 | comparison = new (&allocator) HGreaterThan(op1, op2); |
| 878 | expected_result = (i > j); |
| 879 | break; |
| 880 | case kCondGE: |
| 881 | comparison = new (&allocator) HGreaterThanOrEqual(op1, op2); |
| 882 | expected_result = (i >= j); |
| 883 | break; |
| 884 | case kCondB: |
| 885 | comparison = new (&allocator) HBelow(op1, op2); |
| 886 | expected_result = (x < y); |
| 887 | break; |
| 888 | case kCondBE: |
| 889 | comparison = new (&allocator) HBelowOrEqual(op1, op2); |
| 890 | expected_result = (x <= y); |
| 891 | break; |
| 892 | case kCondA: |
| 893 | comparison = new (&allocator) HAbove(op1, op2); |
| 894 | expected_result = (x > y); |
| 895 | break; |
| 896 | case kCondAE: |
| 897 | comparison = new (&allocator) HAboveOrEqual(op1, op2); |
| 898 | expected_result = (x >= y); |
| 899 | break; |
| 900 | } |
| 901 | block->AddInstruction(comparison); |
| 902 | block->AddInstruction(new (&allocator) HReturn(comparison)); |
| 903 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 904 | graph->BuildDominatorTree(); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 905 | RunCode(target_isa, graph, [](HGraph*) {}, true, expected_result); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 906 | } |
| 907 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 908 | TEST_F(CodegenTest, ComparisonsInt) { |
| 909 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 910 | for (int64_t i = -1; i <= 1; i++) { |
| 911 | for (int64_t j = -1; j <= 1; j++) { |
| 912 | TestComparison(kCondEQ, i, j, Primitive::kPrimInt, target_isa); |
| 913 | TestComparison(kCondNE, i, j, Primitive::kPrimInt, target_isa); |
| 914 | TestComparison(kCondLT, i, j, Primitive::kPrimInt, target_isa); |
| 915 | TestComparison(kCondLE, i, j, Primitive::kPrimInt, target_isa); |
| 916 | TestComparison(kCondGT, i, j, Primitive::kPrimInt, target_isa); |
| 917 | TestComparison(kCondGE, i, j, Primitive::kPrimInt, target_isa); |
| 918 | TestComparison(kCondB, i, j, Primitive::kPrimInt, target_isa); |
| 919 | TestComparison(kCondBE, i, j, Primitive::kPrimInt, target_isa); |
| 920 | TestComparison(kCondA, i, j, Primitive::kPrimInt, target_isa); |
| 921 | TestComparison(kCondAE, i, j, Primitive::kPrimInt, target_isa); |
| 922 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 927 | TEST_F(CodegenTest, ComparisonsLong) { |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 928 | // TODO: make MIPS work for long |
| 929 | if (kRuntimeISA == kMips || kRuntimeISA == kMips64) { |
| 930 | return; |
| 931 | } |
| 932 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 933 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 934 | if (target_isa == kMips || target_isa == kMips64) { |
| 935 | continue; |
| 936 | } |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 937 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 938 | for (int64_t i = -1; i <= 1; i++) { |
| 939 | for (int64_t j = -1; j <= 1; j++) { |
| 940 | TestComparison(kCondEQ, i, j, Primitive::kPrimLong, target_isa); |
| 941 | TestComparison(kCondNE, i, j, Primitive::kPrimLong, target_isa); |
| 942 | TestComparison(kCondLT, i, j, Primitive::kPrimLong, target_isa); |
| 943 | TestComparison(kCondLE, i, j, Primitive::kPrimLong, target_isa); |
| 944 | TestComparison(kCondGT, i, j, Primitive::kPrimLong, target_isa); |
| 945 | TestComparison(kCondGE, i, j, Primitive::kPrimLong, target_isa); |
| 946 | TestComparison(kCondB, i, j, Primitive::kPrimLong, target_isa); |
| 947 | TestComparison(kCondBE, i, j, Primitive::kPrimLong, target_isa); |
| 948 | TestComparison(kCondA, i, j, Primitive::kPrimLong, target_isa); |
| 949 | TestComparison(kCondAE, i, j, Primitive::kPrimLong, target_isa); |
| 950 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 955 | } // namespace art |