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