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