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