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