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) { |
| 209 | ASSERT_TRUE(graph->IsInSsaForm()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 210 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 211 | SSAChecker graph_checker(graph); |
| 212 | graph_checker.Run(); |
| 213 | ASSERT_TRUE(graph_checker.IsValid()); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 214 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 215 | SsaLivenessAnalysis liveness(graph, codegen); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 216 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 217 | PrepareForRegisterAllocation(graph).Run(); |
| 218 | liveness.Analyze(); |
| 219 | RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 220 | hook_before_codegen(graph); |
| 221 | |
| 222 | InternalCodeAllocator allocator; |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 223 | codegen->Compile(&allocator); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 224 | Run(allocator, *codegen, has_result, expected); |
| 225 | } |
| 226 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 227 | template <typename Expected> |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 228 | static void RunCode(InstructionSet target_isa, |
| 229 | HGraph* graph, |
| 230 | std::function<void(HGraph*)> hook_before_codegen, |
| 231 | bool has_result, |
| 232 | Expected expected) { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 233 | CompilerOptions compiler_options; |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 234 | if (target_isa == kArm || target_isa == kThumb2) { |
| 235 | std::unique_ptr<const ArmInstructionSetFeatures> features_arm( |
| 236 | ArmInstructionSetFeatures::FromCppDefines()); |
| 237 | TestCodeGeneratorARM codegenARM(graph, *features_arm.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 238 | RunCode(&codegenARM, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 239 | } else if (target_isa == kArm64) { |
| 240 | std::unique_ptr<const Arm64InstructionSetFeatures> features_arm64( |
| 241 | Arm64InstructionSetFeatures::FromCppDefines()); |
| 242 | arm64::CodeGeneratorARM64 codegenARM64(graph, *features_arm64.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 243 | RunCode(&codegenARM64, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 244 | } else if (target_isa == kX86) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 245 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 246 | X86InstructionSetFeatures::FromCppDefines()); |
| 247 | x86::CodeGeneratorX86 codegenX86(graph, *features_x86.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 248 | RunCode(&codegenX86, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 249 | } else if (target_isa == kX86_64) { |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 250 | std::unique_ptr<const X86_64InstructionSetFeatures> features_x86_64( |
| 251 | X86_64InstructionSetFeatures::FromCppDefines()); |
| 252 | x86_64::CodeGeneratorX86_64 codegenX86_64(graph, *features_x86_64.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 253 | RunCode(&codegenX86_64, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 254 | } else if (target_isa == kMips) { |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 255 | std::unique_ptr<const MipsInstructionSetFeatures> features_mips( |
| 256 | MipsInstructionSetFeatures::FromCppDefines()); |
| 257 | mips::CodeGeneratorMIPS codegenMIPS(graph, *features_mips.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 258 | RunCode(&codegenMIPS, graph, hook_before_codegen, has_result, expected); |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 259 | } else if (target_isa == kMips64) { |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 260 | std::unique_ptr<const Mips64InstructionSetFeatures> features_mips64( |
| 261 | Mips64InstructionSetFeatures::FromCppDefines()); |
| 262 | mips64::CodeGeneratorMIPS64 codegenMIPS64(graph, *features_mips64.get(), compiler_options); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 263 | RunCode(&codegenMIPS64, graph, hook_before_codegen, has_result, expected); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 267 | static ::std::vector<InstructionSet> GetTargetISAs() { |
| 268 | ::std::vector<InstructionSet> v; |
| 269 | // Add all ISAs that are executable on hardware or on simulator. |
| 270 | const ::std::vector<InstructionSet> executable_isa_candidates = { |
| 271 | kArm, |
| 272 | kArm64, |
| 273 | kThumb2, |
| 274 | kX86, |
| 275 | kX86_64, |
| 276 | kMips, |
| 277 | kMips64 |
| 278 | }; |
| 279 | |
| 280 | for (auto target_isa : executable_isa_candidates) { |
| 281 | if (CanExecute(target_isa)) { |
| 282 | v.push_back(target_isa); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return v; |
| 287 | } |
| 288 | |
| 289 | static void TestCode(const uint16_t* data, |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 290 | bool has_result = false, |
| 291 | int32_t expected = 0) { |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 292 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 293 | ArenaPool pool; |
| 294 | ArenaAllocator arena(&pool); |
| 295 | HGraph* graph = CreateGraph(&arena); |
| 296 | HGraphBuilder builder(graph); |
| 297 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 298 | bool graph_built = builder.BuildGraph(*item); |
| 299 | ASSERT_TRUE(graph_built); |
| 300 | // Remove suspend checks, they cannot be executed in this context. |
| 301 | RemoveSuspendChecks(graph); |
| 302 | TransformToSsa(graph); |
| 303 | RunCode(target_isa, graph, [](HGraph*) {}, has_result, expected); |
| 304 | } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 305 | } |
| 306 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 307 | static void TestCodeLong(const uint16_t* data, |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 308 | bool has_result, |
| 309 | int64_t expected) { |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 310 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 311 | ArenaPool pool; |
| 312 | ArenaAllocator arena(&pool); |
| 313 | HGraph* graph = CreateGraph(&arena); |
| 314 | HGraphBuilder builder(graph, Primitive::kPrimLong); |
| 315 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 316 | bool graph_built = builder.BuildGraph(*item); |
| 317 | ASSERT_TRUE(graph_built); |
| 318 | // Remove suspend checks, they cannot be executed in this context. |
| 319 | RemoveSuspendChecks(graph); |
| 320 | TransformToSsa(graph); |
| 321 | RunCode(target_isa, graph, [](HGraph*) {}, has_result, expected); |
| 322 | } |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 323 | } |
| 324 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 325 | class CodegenTest : public CommonCompilerTest {}; |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 326 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 327 | TEST_F(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 328 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 329 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 330 | } |
| 331 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 332 | TEST_F(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 333 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 334 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 335 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 336 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 337 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 338 | } |
| 339 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 340 | TEST_F(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 341 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 342 | Instruction::GOTO | 0x100, |
| 343 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 344 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 345 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 346 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 347 | } |
| 348 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 349 | TEST_F(CodegenTest, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 350 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 351 | Instruction::GOTO | 0x200, |
| 352 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 353 | Instruction::GOTO | 0xFF00); |
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(data1); |
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 data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 358 | Instruction::GOTO_16, 3, |
| 359 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 360 | Instruction::GOTO_16, 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(data2); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 363 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 364 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 365 | Instruction::GOTO_32, 4, 0, |
| 366 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 367 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 368 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 369 | TestCode(data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 370 | } |
| 371 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 372 | TEST_F(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 373 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 374 | Instruction::RETURN_VOID, |
| 375 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 376 | Instruction::GOTO | 0xFE00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 377 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 378 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 379 | } |
| 380 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 381 | TEST_F(CodegenTest, CFG5) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 382 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 383 | Instruction::CONST_4 | 0 | 0, |
| 384 | Instruction::IF_EQ, 3, |
| 385 | Instruction::GOTO | 0x100, |
| 386 | Instruction::RETURN_VOID); |
| 387 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 388 | TestCode(data); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 389 | } |
| 390 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 391 | TEST_F(CodegenTest, IntConstant) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 392 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 393 | Instruction::CONST_4 | 0 | 0, |
| 394 | Instruction::RETURN_VOID); |
| 395 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 396 | TestCode(data); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 397 | } |
| 398 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 399 | TEST_F(CodegenTest, Return1) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 400 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 401 | Instruction::CONST_4 | 0 | 0, |
| 402 | Instruction::RETURN | 0); |
| 403 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 404 | TestCode(data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 405 | } |
| 406 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 407 | TEST_F(CodegenTest, Return2) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 408 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 409 | Instruction::CONST_4 | 0 | 0, |
| 410 | Instruction::CONST_4 | 0 | 1 << 8, |
| 411 | Instruction::RETURN | 1 << 8); |
| 412 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 413 | TestCode(data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 414 | } |
| 415 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 416 | TEST_F(CodegenTest, Return3) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 417 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 418 | Instruction::CONST_4 | 0 | 0, |
| 419 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 420 | Instruction::RETURN | 1 << 8); |
| 421 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 422 | TestCode(data, true, 1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 423 | } |
| 424 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 425 | TEST_F(CodegenTest, ReturnIf1) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 426 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 427 | Instruction::CONST_4 | 0 | 0, |
| 428 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 429 | Instruction::IF_EQ, 3, |
| 430 | Instruction::RETURN | 0 << 8, |
| 431 | Instruction::RETURN | 1 << 8); |
| 432 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 433 | TestCode(data, true, 1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 434 | } |
| 435 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 436 | TEST_F(CodegenTest, ReturnIf2) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 437 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 438 | Instruction::CONST_4 | 0 | 0, |
| 439 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 440 | Instruction::IF_EQ | 0 << 4 | 1 << 8, 3, |
| 441 | Instruction::RETURN | 0 << 8, |
| 442 | Instruction::RETURN | 1 << 8); |
| 443 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 444 | TestCode(data, true, 0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 447 | // Exercise bit-wise (one's complement) not-int instruction. |
| 448 | #define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 449 | TEST_F(CodegenTest, TEST_NAME) { \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 450 | const int32_t input = INPUT; \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 451 | const uint16_t input_lo = Low16Bits(input); \ |
| 452 | const uint16_t input_hi = High16Bits(input); \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 453 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \ |
| 454 | Instruction::CONST | 0 << 8, input_lo, input_hi, \ |
| 455 | Instruction::NOT_INT | 1 << 8 | 0 << 12 , \ |
| 456 | Instruction::RETURN | 1 << 8); \ |
| 457 | \ |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 458 | TestCode(data, true, EXPECTED_OUTPUT); \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | NOT_INT_TEST(ReturnNotIntMinus2, -2, 1) |
| 462 | NOT_INT_TEST(ReturnNotIntMinus1, -1, 0) |
| 463 | NOT_INT_TEST(ReturnNotInt0, 0, -1) |
| 464 | NOT_INT_TEST(ReturnNotInt1, 1, -2) |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 465 | NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1 |
| 466 | NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2 |
| 467 | NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1 |
| 468 | NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31) |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 469 | |
| 470 | #undef NOT_INT_TEST |
| 471 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 472 | // Exercise bit-wise (one's complement) not-long instruction. |
| 473 | #define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 474 | TEST_F(CodegenTest, TEST_NAME) { \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 475 | const int64_t input = INPUT; \ |
| 476 | const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \ |
| 477 | const uint16_t word1 = High16Bits(Low32Bits(input)); \ |
| 478 | const uint16_t word2 = Low16Bits(High32Bits(input)); \ |
| 479 | const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \ |
| 480 | const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \ |
| 481 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \ |
| 482 | Instruction::NOT_LONG | 2 << 8 | 0 << 12, \ |
| 483 | Instruction::RETURN_WIDE | 2 << 8); \ |
| 484 | \ |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 485 | TestCodeLong(data, true, EXPECTED_OUTPUT); \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1)) |
| 489 | NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0)) |
| 490 | NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1)) |
| 491 | NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2)) |
| 492 | |
| 493 | NOT_LONG_TEST(ReturnNotLongINT32_MIN, |
| 494 | INT64_C(-2147483648), |
| 495 | INT64_C(2147483647)) // (2^31) - 1 |
| 496 | NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1, |
| 497 | INT64_C(-2147483647), |
| 498 | INT64_C(2147483646)) // (2^31) - 2 |
| 499 | NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1, |
| 500 | INT64_C(2147483646), |
| 501 | INT64_C(-2147483647)) // -(2^31) - 1 |
| 502 | NOT_LONG_TEST(ReturnNotLongINT32_MAX, |
| 503 | INT64_C(2147483647), |
| 504 | INT64_C(-2147483648)) // -(2^31) |
| 505 | |
| 506 | // Note that the C++ compiler won't accept |
| 507 | // INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid |
| 508 | // int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead. |
| 509 | NOT_LONG_TEST(ReturnNotINT64_MIN, |
| 510 | INT64_C(-9223372036854775807)-1, |
| 511 | INT64_C(9223372036854775807)); // (2^63) - 1 |
| 512 | NOT_LONG_TEST(ReturnNotINT64_MINPlus1, |
| 513 | INT64_C(-9223372036854775807), |
| 514 | INT64_C(9223372036854775806)); // (2^63) - 2 |
| 515 | NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1, |
| 516 | INT64_C(9223372036854775806), |
| 517 | INT64_C(-9223372036854775807)); // -(2^63) - 1 |
| 518 | NOT_LONG_TEST(ReturnNotLongINT64_MAX, |
| 519 | INT64_C(9223372036854775807), |
| 520 | INT64_C(-9223372036854775807)-1); // -(2^63) |
| 521 | |
| 522 | #undef NOT_LONG_TEST |
| 523 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 524 | TEST_F(CodegenTest, IntToLongOfLongToInt) { |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 525 | const int64_t input = INT64_C(4294967296); // 2^32 |
| 526 | const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW. |
| 527 | const uint16_t word1 = High16Bits(Low32Bits(input)); |
| 528 | const uint16_t word2 = Low16Bits(High32Bits(input)); |
| 529 | const uint16_t word3 = High16Bits(High32Bits(input)); // MSW. |
| 530 | const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM( |
| 531 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, |
| 532 | Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0, |
| 533 | Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1 |
| 534 | Instruction::LONG_TO_INT | 4 << 8 | 0 << 12, |
| 535 | Instruction::INT_TO_LONG | 2 << 8 | 4 << 12, |
| 536 | Instruction::RETURN_WIDE | 2 << 8); |
| 537 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 538 | TestCodeLong(data, true, 1); |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 539 | } |
| 540 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 541 | TEST_F(CodegenTest, ReturnAdd1) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 542 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 543 | Instruction::CONST_4 | 3 << 12 | 0, |
| 544 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 545 | Instruction::ADD_INT, 1 << 8 | 0, |
| 546 | Instruction::RETURN); |
| 547 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 548 | TestCode(data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 549 | } |
| 550 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 551 | TEST_F(CodegenTest, ReturnAdd2) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 552 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 553 | Instruction::CONST_4 | 3 << 12 | 0, |
| 554 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 555 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 556 | Instruction::RETURN); |
| 557 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 558 | TestCode(data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 559 | } |
| 560 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 561 | TEST_F(CodegenTest, ReturnAdd3) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 562 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 563 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 564 | Instruction::ADD_INT_LIT8, 3 << 8 | 0, |
| 565 | Instruction::RETURN); |
| 566 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 567 | TestCode(data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 568 | } |
| 569 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 570 | TEST_F(CodegenTest, ReturnAdd4) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 571 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 572 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 573 | Instruction::ADD_INT_LIT16, 3, |
| 574 | Instruction::RETURN); |
| 575 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 576 | TestCode(data, true, 7); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 577 | } |
| 578 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 579 | TEST_F(CodegenTest, ReturnMulInt) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 580 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 581 | Instruction::CONST_4 | 3 << 12 | 0, |
| 582 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 583 | Instruction::MUL_INT, 1 << 8 | 0, |
| 584 | Instruction::RETURN); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 585 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 586 | TestCode(data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 587 | } |
| 588 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 589 | TEST_F(CodegenTest, ReturnMulInt2addr) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 590 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 591 | Instruction::CONST_4 | 3 << 12 | 0, |
| 592 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 593 | Instruction::MUL_INT_2ADDR | 1 << 12, |
| 594 | Instruction::RETURN); |
| 595 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 596 | TestCode(data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 597 | } |
| 598 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 599 | TEST_F(CodegenTest, ReturnMulLong) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 600 | const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 601 | Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0, |
| 602 | Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0, |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 603 | Instruction::MUL_LONG, 2 << 8 | 0, |
| 604 | Instruction::RETURN_WIDE); |
| 605 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 606 | TestCodeLong(data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 607 | } |
| 608 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 609 | TEST_F(CodegenTest, ReturnMulLong2addr) { |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 610 | const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 611 | Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0, |
| 612 | Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0, |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 613 | Instruction::MUL_LONG_2ADDR | 2 << 12, |
| 614 | Instruction::RETURN_WIDE); |
| 615 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 616 | TestCodeLong(data, true, 12); |
Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 617 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 618 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 619 | TEST_F(CodegenTest, ReturnMulIntLit8) { |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 620 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 621 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 622 | Instruction::MUL_INT_LIT8, 3 << 8 | 0, |
| 623 | Instruction::RETURN); |
| 624 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 625 | TestCode(data, true, 12); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 626 | } |
| 627 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 628 | TEST_F(CodegenTest, ReturnMulIntLit16) { |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 629 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 630 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 631 | Instruction::MUL_INT_LIT16, 3, |
| 632 | Instruction::RETURN); |
| 633 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 634 | TestCode(data, true, 12); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 635 | } |
| 636 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 637 | TEST_F(CodegenTest, NonMaterializedCondition) { |
| 638 | for (InstructionSet target_isa : GetTargetISAs()) { |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 639 | ArenaPool pool; |
| 640 | ArenaAllocator allocator(&pool); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 641 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 642 | HGraph* graph = CreateGraph(&allocator); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 643 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 644 | graph->AddBlock(entry); |
| 645 | graph->SetEntryBlock(entry); |
| 646 | entry->AddInstruction(new (&allocator) HGoto()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 647 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 648 | HBasicBlock* first_block = new (&allocator) HBasicBlock(graph); |
| 649 | graph->AddBlock(first_block); |
| 650 | entry->AddSuccessor(first_block); |
| 651 | HIntConstant* constant0 = graph->GetIntConstant(0); |
| 652 | HIntConstant* constant1 = graph->GetIntConstant(1); |
| 653 | HEqual* equal = new (&allocator) HEqual(constant0, constant0); |
| 654 | first_block->AddInstruction(equal); |
| 655 | first_block->AddInstruction(new (&allocator) HIf(equal)); |
| 656 | |
| 657 | HBasicBlock* then_block = new (&allocator) HBasicBlock(graph); |
| 658 | HBasicBlock* else_block = new (&allocator) HBasicBlock(graph); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 659 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 660 | graph->SetExitBlock(exit_block); |
| 661 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 662 | graph->AddBlock(then_block); |
| 663 | graph->AddBlock(else_block); |
| 664 | graph->AddBlock(exit_block); |
| 665 | first_block->AddSuccessor(then_block); |
| 666 | first_block->AddSuccessor(else_block); |
| 667 | then_block->AddSuccessor(exit_block); |
| 668 | else_block->AddSuccessor(exit_block); |
| 669 | |
| 670 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 671 | then_block->AddInstruction(new (&allocator) HReturn(constant0)); |
| 672 | else_block->AddInstruction(new (&allocator) HReturn(constant1)); |
| 673 | |
David Brazdil | b11b072 | 2016-01-28 16:22:40 +0000 | [diff] [blame^] | 674 | ASSERT_FALSE(equal->IsEmittedAtUseSite()); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 675 | TransformToSsa(graph); |
| 676 | PrepareForRegisterAllocation(graph).Run(); |
David Brazdil | b11b072 | 2016-01-28 16:22:40 +0000 | [diff] [blame^] | 677 | ASSERT_TRUE(equal->IsEmittedAtUseSite()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 678 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 679 | auto hook_before_codegen = [](HGraph* graph_in) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 680 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 681 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 682 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 683 | }; |
| 684 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 685 | RunCode(target_isa, graph, hook_before_codegen, true, 0); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 689 | TEST_F(CodegenTest, MaterializedCondition1) { |
| 690 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 691 | // Check that condition are materialized correctly. A materialized condition |
| 692 | // should yield `1` if it evaluated to true, and `0` otherwise. |
| 693 | // We force the materialization of comparisons for different combinations of |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 694 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 695 | // inputs and check the results. |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 696 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 697 | int lhs[] = {1, 2, -1, 2, 0xabc}; |
| 698 | int rhs[] = {2, 1, 2, -1, 0xabc}; |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 699 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 700 | for (size_t i = 0; i < arraysize(lhs); i++) { |
| 701 | ArenaPool pool; |
| 702 | ArenaAllocator allocator(&pool); |
| 703 | HGraph* graph = CreateGraph(&allocator); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 704 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 705 | HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph); |
| 706 | graph->AddBlock(entry_block); |
| 707 | graph->SetEntryBlock(entry_block); |
| 708 | entry_block->AddInstruction(new (&allocator) HGoto()); |
| 709 | HBasicBlock* code_block = new (&allocator) HBasicBlock(graph); |
| 710 | graph->AddBlock(code_block); |
| 711 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 712 | graph->AddBlock(exit_block); |
| 713 | exit_block->AddInstruction(new (&allocator) HExit()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 714 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 715 | entry_block->AddSuccessor(code_block); |
| 716 | code_block->AddSuccessor(exit_block); |
| 717 | graph->SetExitBlock(exit_block); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 718 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 719 | HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]); |
| 720 | HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]); |
| 721 | HLessThan cmp_lt(cst_lhs, cst_rhs); |
| 722 | code_block->AddInstruction(&cmp_lt); |
| 723 | HReturn ret(&cmp_lt); |
| 724 | code_block->AddInstruction(&ret); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 725 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 726 | TransformToSsa(graph); |
| 727 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 728 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
| 729 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
| 730 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 731 | }; |
| 732 | RunCode(target_isa, graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
| 733 | } |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 734 | } |
| 735 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 736 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 737 | TEST_F(CodegenTest, MaterializedCondition2) { |
| 738 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 739 | // Check that HIf correctly interprets a materialized condition. |
| 740 | // We force the materialization of comparisons for different combinations of |
| 741 | // inputs. An HIf takes the materialized combination as input and returns a |
| 742 | // value that we verify. |
| 743 | |
| 744 | int lhs[] = {1, 2, -1, 2, 0xabc}; |
| 745 | int rhs[] = {2, 1, 2, -1, 0xabc}; |
| 746 | |
| 747 | |
| 748 | for (size_t i = 0; i < arraysize(lhs); i++) { |
| 749 | ArenaPool pool; |
| 750 | ArenaAllocator allocator(&pool); |
| 751 | HGraph* graph = CreateGraph(&allocator); |
| 752 | |
| 753 | HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph); |
| 754 | graph->AddBlock(entry_block); |
| 755 | graph->SetEntryBlock(entry_block); |
| 756 | entry_block->AddInstruction(new (&allocator) HGoto()); |
| 757 | |
| 758 | HBasicBlock* if_block = new (&allocator) HBasicBlock(graph); |
| 759 | graph->AddBlock(if_block); |
| 760 | HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph); |
| 761 | graph->AddBlock(if_true_block); |
| 762 | HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph); |
| 763 | graph->AddBlock(if_false_block); |
| 764 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 765 | graph->AddBlock(exit_block); |
| 766 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 767 | |
| 768 | graph->SetEntryBlock(entry_block); |
| 769 | entry_block->AddSuccessor(if_block); |
| 770 | if_block->AddSuccessor(if_true_block); |
| 771 | if_block->AddSuccessor(if_false_block); |
| 772 | if_true_block->AddSuccessor(exit_block); |
| 773 | if_false_block->AddSuccessor(exit_block); |
| 774 | graph->SetExitBlock(exit_block); |
| 775 | |
| 776 | HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]); |
| 777 | HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]); |
| 778 | HLessThan cmp_lt(cst_lhs, cst_rhs); |
| 779 | if_block->AddInstruction(&cmp_lt); |
| 780 | // We insert a temporary to separate the HIf from the HLessThan and force |
| 781 | // the materialization of the condition. |
| 782 | HTemporary force_materialization(0); |
| 783 | if_block->AddInstruction(&force_materialization); |
| 784 | HIf if_lt(&cmp_lt); |
| 785 | if_block->AddInstruction(&if_lt); |
| 786 | |
| 787 | HIntConstant* cst_lt = graph->GetIntConstant(1); |
| 788 | HReturn ret_lt(cst_lt); |
| 789 | if_true_block->AddInstruction(&ret_lt); |
| 790 | HIntConstant* cst_ge = graph->GetIntConstant(0); |
| 791 | HReturn ret_ge(cst_ge); |
| 792 | if_false_block->AddInstruction(&ret_ge); |
| 793 | |
| 794 | TransformToSsa(graph); |
| 795 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 796 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
| 797 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
| 798 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 799 | }; |
| 800 | RunCode(target_isa, graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | TEST_F(CodegenTest, ReturnDivIntLit8) { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 806 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 807 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 808 | Instruction::DIV_INT_LIT8, 3 << 8 | 0, |
| 809 | Instruction::RETURN); |
| 810 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 811 | TestCode(data, true, 1); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 812 | } |
| 813 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 814 | TEST_F(CodegenTest, ReturnDivInt2Addr) { |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 815 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 816 | Instruction::CONST_4 | 4 << 12 | 0, |
| 817 | Instruction::CONST_4 | 2 << 12 | 1 << 8, |
| 818 | Instruction::DIV_INT_2ADDR | 1 << 12, |
| 819 | Instruction::RETURN); |
| 820 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 821 | TestCode(data, true, 2); |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 824 | // Helper method. |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 825 | static void TestComparison(IfCondition condition, |
| 826 | int64_t i, |
| 827 | int64_t j, |
| 828 | Primitive::Type type, |
| 829 | const InstructionSet target_isa) { |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 830 | ArenaPool pool; |
| 831 | ArenaAllocator allocator(&pool); |
| 832 | HGraph* graph = CreateGraph(&allocator); |
| 833 | |
| 834 | HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph); |
| 835 | graph->AddBlock(entry_block); |
| 836 | graph->SetEntryBlock(entry_block); |
| 837 | entry_block->AddInstruction(new (&allocator) HGoto()); |
| 838 | |
| 839 | HBasicBlock* block = new (&allocator) HBasicBlock(graph); |
| 840 | graph->AddBlock(block); |
| 841 | |
| 842 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 843 | graph->AddBlock(exit_block); |
| 844 | graph->SetExitBlock(exit_block); |
| 845 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 846 | |
| 847 | entry_block->AddSuccessor(block); |
| 848 | block->AddSuccessor(exit_block); |
| 849 | |
| 850 | HInstruction* op1; |
| 851 | HInstruction* op2; |
| 852 | if (type == Primitive::kPrimInt) { |
| 853 | op1 = graph->GetIntConstant(i); |
| 854 | op2 = graph->GetIntConstant(j); |
| 855 | } else { |
| 856 | DCHECK_EQ(type, Primitive::kPrimLong); |
| 857 | op1 = graph->GetLongConstant(i); |
| 858 | op2 = graph->GetLongConstant(j); |
| 859 | } |
| 860 | |
| 861 | HInstruction* comparison = nullptr; |
| 862 | bool expected_result = false; |
| 863 | const uint64_t x = i; |
| 864 | const uint64_t y = j; |
| 865 | switch (condition) { |
| 866 | case kCondEQ: |
| 867 | comparison = new (&allocator) HEqual(op1, op2); |
| 868 | expected_result = (i == j); |
| 869 | break; |
| 870 | case kCondNE: |
| 871 | comparison = new (&allocator) HNotEqual(op1, op2); |
| 872 | expected_result = (i != j); |
| 873 | break; |
| 874 | case kCondLT: |
| 875 | comparison = new (&allocator) HLessThan(op1, op2); |
| 876 | expected_result = (i < j); |
| 877 | break; |
| 878 | case kCondLE: |
| 879 | comparison = new (&allocator) HLessThanOrEqual(op1, op2); |
| 880 | expected_result = (i <= j); |
| 881 | break; |
| 882 | case kCondGT: |
| 883 | comparison = new (&allocator) HGreaterThan(op1, op2); |
| 884 | expected_result = (i > j); |
| 885 | break; |
| 886 | case kCondGE: |
| 887 | comparison = new (&allocator) HGreaterThanOrEqual(op1, op2); |
| 888 | expected_result = (i >= j); |
| 889 | break; |
| 890 | case kCondB: |
| 891 | comparison = new (&allocator) HBelow(op1, op2); |
| 892 | expected_result = (x < y); |
| 893 | break; |
| 894 | case kCondBE: |
| 895 | comparison = new (&allocator) HBelowOrEqual(op1, op2); |
| 896 | expected_result = (x <= y); |
| 897 | break; |
| 898 | case kCondA: |
| 899 | comparison = new (&allocator) HAbove(op1, op2); |
| 900 | expected_result = (x > y); |
| 901 | break; |
| 902 | case kCondAE: |
| 903 | comparison = new (&allocator) HAboveOrEqual(op1, op2); |
| 904 | expected_result = (x >= y); |
| 905 | break; |
| 906 | } |
| 907 | block->AddInstruction(comparison); |
| 908 | block->AddInstruction(new (&allocator) HReturn(comparison)); |
| 909 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 910 | TransformToSsa(graph); |
| 911 | RunCode(target_isa, graph, [](HGraph*) {}, true, expected_result); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 912 | } |
| 913 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 914 | TEST_F(CodegenTest, ComparisonsInt) { |
| 915 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 916 | for (int64_t i = -1; i <= 1; i++) { |
| 917 | for (int64_t j = -1; j <= 1; j++) { |
| 918 | TestComparison(kCondEQ, i, j, Primitive::kPrimInt, target_isa); |
| 919 | TestComparison(kCondNE, i, j, Primitive::kPrimInt, target_isa); |
| 920 | TestComparison(kCondLT, i, j, Primitive::kPrimInt, target_isa); |
| 921 | TestComparison(kCondLE, i, j, Primitive::kPrimInt, target_isa); |
| 922 | TestComparison(kCondGT, i, j, Primitive::kPrimInt, target_isa); |
| 923 | TestComparison(kCondGE, i, j, Primitive::kPrimInt, target_isa); |
| 924 | TestComparison(kCondB, i, j, Primitive::kPrimInt, target_isa); |
| 925 | TestComparison(kCondBE, i, j, Primitive::kPrimInt, target_isa); |
| 926 | TestComparison(kCondA, i, j, Primitive::kPrimInt, target_isa); |
| 927 | TestComparison(kCondAE, i, j, Primitive::kPrimInt, target_isa); |
| 928 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 933 | TEST_F(CodegenTest, ComparisonsLong) { |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 934 | // TODO: make MIPS work for long |
| 935 | if (kRuntimeISA == kMips || kRuntimeISA == kMips64) { |
| 936 | return; |
| 937 | } |
| 938 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 939 | for (InstructionSet target_isa : GetTargetISAs()) { |
| 940 | if (target_isa == kMips || target_isa == kMips64) { |
| 941 | continue; |
| 942 | } |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 943 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 944 | for (int64_t i = -1; i <= 1; i++) { |
| 945 | for (int64_t j = -1; j <= 1; j++) { |
| 946 | TestComparison(kCondEQ, i, j, Primitive::kPrimLong, target_isa); |
| 947 | TestComparison(kCondNE, i, j, Primitive::kPrimLong, target_isa); |
| 948 | TestComparison(kCondLT, i, j, Primitive::kPrimLong, target_isa); |
| 949 | TestComparison(kCondLE, i, j, Primitive::kPrimLong, target_isa); |
| 950 | TestComparison(kCondGT, i, j, Primitive::kPrimLong, target_isa); |
| 951 | TestComparison(kCondGE, i, j, Primitive::kPrimLong, target_isa); |
| 952 | TestComparison(kCondB, i, j, Primitive::kPrimLong, target_isa); |
| 953 | TestComparison(kCondBE, i, j, Primitive::kPrimLong, target_isa); |
| 954 | TestComparison(kCondA, i, j, Primitive::kPrimLong, target_isa); |
| 955 | TestComparison(kCondAE, i, j, Primitive::kPrimLong, target_isa); |
| 956 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 961 | } // namespace art |