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