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