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