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