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 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 19 | #include "builder.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 20 | #include "code_generator_arm.h" |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 21 | #include "code_generator_arm64.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 22 | #include "code_generator_x86.h" |
| 23 | #include "code_generator_x86_64.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 24 | #include "common_compiler_test.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 25 | #include "dex_file.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 26 | #include "dex_instruction.h" |
| 27 | #include "instruction_set.h" |
| 28 | #include "nodes.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 29 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 30 | #include "prepare_for_register_allocation.h" |
| 31 | #include "register_allocator.h" |
| 32 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 33 | |
| 34 | #include "gtest/gtest.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 38 | class InternalCodeAllocator : public CodeAllocator { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 39 | public: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 40 | InternalCodeAllocator() { } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 41 | |
| 42 | virtual uint8_t* Allocate(size_t size) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 43 | size_ = size; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 44 | memory_.reset(new uint8_t[size]); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 45 | return memory_.get(); |
| 46 | } |
| 47 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 48 | size_t GetSize() const { return size_; } |
| 49 | uint8_t* GetMemory() const { return memory_.get(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 50 | |
| 51 | private: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 52 | size_t size_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 53 | std::unique_ptr<uint8_t[]> memory_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 54 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 55 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 58 | static void Run(const InternalCodeAllocator& allocator, |
| 59 | const CodeGenerator& codegen, |
| 60 | bool has_result, |
| 61 | int32_t expected) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 62 | typedef int32_t (*fptr)(); |
| 63 | CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 64 | fptr f = reinterpret_cast<fptr>(allocator.GetMemory()); |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 65 | if (codegen.GetInstructionSet() == kThumb2) { |
| 66 | // For thumb we need the bottom bit set. |
| 67 | f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1); |
| 68 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 69 | int32_t result = f(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 70 | if (has_result) { |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 71 | ASSERT_EQ(result, expected); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 75 | static void RunCodeBaseline(HGraph* graph, bool has_result, int32_t expected) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 76 | InternalCodeAllocator allocator; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 77 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 78 | x86::CodeGeneratorX86 codegenX86(graph); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 79 | // We avoid doing a stack overflow check that requires the runtime being setup, |
| 80 | // 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] | 81 | codegenX86.CompileBaseline(&allocator, true); |
| 82 | if (kRuntimeISA == kX86) { |
| 83 | Run(allocator, codegenX86, has_result, expected); |
| 84 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 85 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 86 | arm::CodeGeneratorARM codegenARM(graph); |
| 87 | codegenARM.CompileBaseline(&allocator, true); |
| 88 | if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) { |
| 89 | Run(allocator, codegenARM, has_result, expected); |
| 90 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 91 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 92 | x86_64::CodeGeneratorX86_64 codegenX86_64(graph); |
| 93 | codegenX86_64.CompileBaseline(&allocator, true); |
| 94 | if (kRuntimeISA == kX86_64) { |
| 95 | Run(allocator, codegenX86_64, has_result, expected); |
| 96 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 97 | |
| 98 | arm64::CodeGeneratorARM64 codegenARM64(graph); |
| 99 | codegenARM64.CompileBaseline(&allocator, true); |
| 100 | if (kRuntimeISA == kArm64) { |
| 101 | Run(allocator, codegenARM64, has_result, expected); |
| 102 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 105 | static void RunCodeOptimized(CodeGenerator* codegen, |
| 106 | HGraph* graph, |
| 107 | std::function<void(HGraph*)> hook_before_codegen, |
| 108 | bool has_result, |
| 109 | int32_t expected) { |
| 110 | SsaLivenessAnalysis liveness(*graph, codegen); |
| 111 | liveness.Analyze(); |
| 112 | |
| 113 | RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness); |
| 114 | register_allocator.AllocateRegisters(); |
| 115 | hook_before_codegen(graph); |
| 116 | |
| 117 | InternalCodeAllocator allocator; |
| 118 | codegen->CompileOptimized(&allocator); |
| 119 | Run(allocator, *codegen, has_result, expected); |
| 120 | } |
| 121 | |
| 122 | static void RunCodeOptimized(HGraph* graph, |
| 123 | std::function<void(HGraph*)> hook_before_codegen, |
| 124 | bool has_result, |
| 125 | int32_t expected) { |
| 126 | if (kRuntimeISA == kX86) { |
| 127 | x86::CodeGeneratorX86 codegenX86(graph); |
| 128 | RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected); |
| 129 | } else if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) { |
| 130 | arm::CodeGeneratorARM codegenARM(graph); |
| 131 | RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected); |
| 132 | } else if (kRuntimeISA == kX86_64) { |
| 133 | x86_64::CodeGeneratorX86_64 codegenX86_64(graph); |
| 134 | RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) { |
| 139 | ArenaPool pool; |
| 140 | ArenaAllocator arena(&pool); |
| 141 | HGraphBuilder builder(&arena); |
| 142 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 143 | HGraph* graph = builder.BuildGraph(*item); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 144 | ASSERT_NE(graph, nullptr); |
Calin Juravle | 039b6e2 | 2014-10-23 12:32:11 +0100 | [diff] [blame] | 145 | // Remove suspend checks, they cannot be executed in this context. |
Calin Juravle | 48dee04 | 2014-10-22 15:54:12 +0100 | [diff] [blame] | 146 | RemoveSuspendChecks(graph); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 147 | RunCodeBaseline(graph, has_result, expected); |
| 148 | } |
| 149 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 150 | TEST(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 151 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
| 152 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 155 | TEST(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 156 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 157 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 158 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 159 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 160 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 163 | TEST(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 164 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 165 | Instruction::GOTO | 0x100, |
| 166 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 167 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 168 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 169 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 172 | TEST(CodegenTest, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 173 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 174 | Instruction::GOTO | 0x200, |
| 175 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 176 | Instruction::GOTO | 0xFF00); |
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(data1); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 179 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 180 | const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 181 | Instruction::GOTO_16, 3, |
| 182 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 183 | Instruction::GOTO_16, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 184 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 185 | TestCode(data2); |
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 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 188 | Instruction::GOTO_32, 4, 0, |
| 189 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 190 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 191 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 192 | TestCode(data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 195 | TEST(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 196 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 197 | Instruction::RETURN_VOID, |
| 198 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 199 | Instruction::GOTO | 0xFE00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 200 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 201 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 204 | TEST(CodegenTest, CFG5) { |
| 205 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 206 | Instruction::CONST_4 | 0 | 0, |
| 207 | Instruction::IF_EQ, 3, |
| 208 | Instruction::GOTO | 0x100, |
| 209 | Instruction::RETURN_VOID); |
| 210 | |
| 211 | TestCode(data); |
| 212 | } |
| 213 | |
| 214 | TEST(CodegenTest, IntConstant) { |
| 215 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 216 | Instruction::CONST_4 | 0 | 0, |
| 217 | Instruction::RETURN_VOID); |
| 218 | |
| 219 | TestCode(data); |
| 220 | } |
| 221 | |
| 222 | TEST(CodegenTest, Return1) { |
| 223 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 224 | Instruction::CONST_4 | 0 | 0, |
| 225 | Instruction::RETURN | 0); |
| 226 | |
| 227 | TestCode(data, true, 0); |
| 228 | } |
| 229 | |
| 230 | TEST(CodegenTest, Return2) { |
| 231 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 232 | Instruction::CONST_4 | 0 | 0, |
| 233 | Instruction::CONST_4 | 0 | 1 << 8, |
| 234 | Instruction::RETURN | 1 << 8); |
| 235 | |
| 236 | TestCode(data, true, 0); |
| 237 | } |
| 238 | |
| 239 | TEST(CodegenTest, Return3) { |
| 240 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 241 | Instruction::CONST_4 | 0 | 0, |
| 242 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 243 | Instruction::RETURN | 1 << 8); |
| 244 | |
| 245 | TestCode(data, true, 1); |
| 246 | } |
| 247 | |
| 248 | TEST(CodegenTest, ReturnIf1) { |
| 249 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 250 | Instruction::CONST_4 | 0 | 0, |
| 251 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 252 | Instruction::IF_EQ, 3, |
| 253 | Instruction::RETURN | 0 << 8, |
| 254 | Instruction::RETURN | 1 << 8); |
| 255 | |
| 256 | TestCode(data, true, 1); |
| 257 | } |
| 258 | |
| 259 | TEST(CodegenTest, ReturnIf2) { |
| 260 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 261 | Instruction::CONST_4 | 0 | 0, |
| 262 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 263 | Instruction::IF_EQ | 0 << 4 | 1 << 8, 3, |
| 264 | Instruction::RETURN | 0 << 8, |
| 265 | Instruction::RETURN | 1 << 8); |
| 266 | |
| 267 | TestCode(data, true, 0); |
| 268 | } |
| 269 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 270 | // Exercise bit-wise (one's complement) not-int instruction. |
| 271 | #define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
| 272 | TEST(CodegenTest, TEST_NAME) { \ |
| 273 | const int32_t input = INPUT; \ |
| 274 | const uint16_t input_lo = input & 0x0000FFFF; \ |
| 275 | const uint16_t input_hi = input >> 16; \ |
| 276 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \ |
| 277 | Instruction::CONST | 0 << 8, input_lo, input_hi, \ |
| 278 | Instruction::NOT_INT | 1 << 8 | 0 << 12 , \ |
| 279 | Instruction::RETURN | 1 << 8); \ |
| 280 | \ |
| 281 | TestCode(data, true, EXPECTED_OUTPUT); \ |
| 282 | } |
| 283 | |
| 284 | NOT_INT_TEST(ReturnNotIntMinus2, -2, 1) |
| 285 | NOT_INT_TEST(ReturnNotIntMinus1, -1, 0) |
| 286 | NOT_INT_TEST(ReturnNotInt0, 0, -1) |
| 287 | NOT_INT_TEST(ReturnNotInt1, 1, -2) |
| 288 | NOT_INT_TEST(ReturnNotIntINT_MIN, -2147483648, 2147483647) // (2^31) - 1 |
| 289 | NOT_INT_TEST(ReturnNotIntINT_MINPlus1, -2147483647, 2147483646) // (2^31) - 2 |
| 290 | NOT_INT_TEST(ReturnNotIntINT_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1 |
| 291 | NOT_INT_TEST(ReturnNotIntINT_MAX, 2147483647, -2147483648) // -(2^31) |
| 292 | |
| 293 | #undef NOT_INT_TEST |
| 294 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 295 | TEST(CodegenTest, ReturnAdd1) { |
| 296 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 297 | Instruction::CONST_4 | 3 << 12 | 0, |
| 298 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 299 | Instruction::ADD_INT, 1 << 8 | 0, |
| 300 | Instruction::RETURN); |
| 301 | |
| 302 | TestCode(data, true, 7); |
| 303 | } |
| 304 | |
| 305 | TEST(CodegenTest, ReturnAdd2) { |
| 306 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 307 | Instruction::CONST_4 | 3 << 12 | 0, |
| 308 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 309 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 310 | Instruction::RETURN); |
| 311 | |
| 312 | TestCode(data, true, 7); |
| 313 | } |
| 314 | |
| 315 | TEST(CodegenTest, ReturnAdd3) { |
| 316 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 317 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 318 | Instruction::ADD_INT_LIT8, 3 << 8 | 0, |
| 319 | Instruction::RETURN); |
| 320 | |
| 321 | TestCode(data, true, 7); |
| 322 | } |
| 323 | |
| 324 | TEST(CodegenTest, ReturnAdd4) { |
| 325 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 326 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 327 | Instruction::ADD_INT_LIT16, 3, |
| 328 | Instruction::RETURN); |
| 329 | |
| 330 | TestCode(data, true, 7); |
| 331 | } |
| 332 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 333 | TEST(CodegenTest, NonMaterializedCondition) { |
| 334 | ArenaPool pool; |
| 335 | ArenaAllocator allocator(&pool); |
| 336 | |
| 337 | HGraph* graph = new (&allocator) HGraph(&allocator); |
| 338 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 339 | graph->AddBlock(entry); |
| 340 | graph->SetEntryBlock(entry); |
| 341 | entry->AddInstruction(new (&allocator) HGoto()); |
| 342 | |
| 343 | HBasicBlock* first_block = new (&allocator) HBasicBlock(graph); |
| 344 | graph->AddBlock(first_block); |
| 345 | entry->AddSuccessor(first_block); |
| 346 | HIntConstant* constant0 = new (&allocator) HIntConstant(0); |
| 347 | entry->AddInstruction(constant0); |
| 348 | HIntConstant* constant1 = new (&allocator) HIntConstant(1); |
| 349 | entry->AddInstruction(constant1); |
| 350 | HEqual* equal = new (&allocator) HEqual(constant0, constant0); |
| 351 | first_block->AddInstruction(equal); |
| 352 | first_block->AddInstruction(new (&allocator) HIf(equal)); |
| 353 | |
| 354 | HBasicBlock* then = new (&allocator) HBasicBlock(graph); |
| 355 | HBasicBlock* else_ = new (&allocator) HBasicBlock(graph); |
| 356 | HBasicBlock* exit = new (&allocator) HBasicBlock(graph); |
| 357 | |
| 358 | graph->AddBlock(then); |
| 359 | graph->AddBlock(else_); |
| 360 | graph->AddBlock(exit); |
| 361 | first_block->AddSuccessor(then); |
| 362 | first_block->AddSuccessor(else_); |
| 363 | then->AddSuccessor(exit); |
| 364 | else_->AddSuccessor(exit); |
| 365 | |
| 366 | exit->AddInstruction(new (&allocator) HExit()); |
| 367 | then->AddInstruction(new (&allocator) HReturn(constant0)); |
| 368 | else_->AddInstruction(new (&allocator) HReturn(constant1)); |
| 369 | |
| 370 | ASSERT_TRUE(equal->NeedsMaterialization()); |
| 371 | graph->BuildDominatorTree(); |
| 372 | PrepareForRegisterAllocation(graph).Run(); |
| 373 | ASSERT_FALSE(equal->NeedsMaterialization()); |
| 374 | |
| 375 | auto hook_before_codegen = [](HGraph* graph) { |
| 376 | HBasicBlock* block = graph->GetEntryBlock()->GetSuccessors().Get(0); |
| 377 | HParallelMove* move = new (graph->GetArena()) HParallelMove(graph->GetArena()); |
| 378 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 379 | }; |
| 380 | |
| 381 | RunCodeOptimized(graph, hook_before_codegen, true, 0); |
| 382 | } |
| 383 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 384 | #define MUL_TEST(TYPE, TEST_NAME) \ |
| 385 | TEST(CodegenTest, Return ## TEST_NAME) { \ |
| 386 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \ |
| 387 | Instruction::CONST_4 | 3 << 12 | 0, \ |
| 388 | Instruction::CONST_4 | 4 << 12 | 1 << 8, \ |
| 389 | Instruction::MUL_ ## TYPE, 1 << 8 | 0, \ |
| 390 | Instruction::RETURN); \ |
| 391 | \ |
| 392 | TestCode(data, true, 12); \ |
| 393 | } \ |
| 394 | \ |
| 395 | TEST(CodegenTest, Return ## TEST_NAME ## 2addr) { \ |
| 396 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \ |
| 397 | Instruction::CONST_4 | 3 << 12 | 0, \ |
| 398 | Instruction::CONST_4 | 4 << 12 | 1 << 8, \ |
| 399 | Instruction::MUL_ ## TYPE ## _2ADDR | 1 << 12, \ |
| 400 | Instruction::RETURN); \ |
| 401 | \ |
| 402 | TestCode(data, true, 12); \ |
| 403 | } |
| 404 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 405 | #if !defined(__aarch64__) |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 406 | MUL_TEST(INT, MulInt); |
| 407 | MUL_TEST(LONG, MulLong); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 408 | #endif |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 409 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 410 | #if defined(__aarch64__) |
| 411 | TEST(CodegenTest, DISABLED_ReturnMulIntLit8) { |
| 412 | #else |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 413 | TEST(CodegenTest, ReturnMulIntLit8) { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 414 | #endif |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 415 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 416 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 417 | Instruction::MUL_INT_LIT8, 3 << 8 | 0, |
| 418 | Instruction::RETURN); |
| 419 | |
| 420 | TestCode(data, true, 12); |
| 421 | } |
| 422 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 423 | #if defined(__aarch64__) |
| 424 | TEST(CodegenTest, DISABLED_ReturnMulIntLit16) { |
| 425 | #else |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 426 | TEST(CodegenTest, ReturnMulIntLit16) { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 427 | #endif |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 428 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 429 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 430 | Instruction::MUL_INT_LIT16, 3, |
| 431 | Instruction::RETURN); |
| 432 | |
| 433 | TestCode(data, true, 12); |
| 434 | } |
| 435 | |
| 436 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 437 | } // namespace art |