codegen_test.cc revision 5319defdf502fc4569316473846b83180ec08035
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 17#include <functional> 18 19#include "builder.h" 20#include "code_generator_arm.h" 21#include "code_generator_arm64.h" 22#include "code_generator_x86.h" 23#include "code_generator_x86_64.h" 24#include "common_compiler_test.h" 25#include "dex_file.h" 26#include "dex_instruction.h" 27#include "instruction_set.h" 28#include "nodes.h" 29#include "optimizing_unit_test.h" 30#include "prepare_for_register_allocation.h" 31#include "register_allocator.h" 32#include "ssa_liveness_analysis.h" 33 34#include "gtest/gtest.h" 35 36namespace art { 37 38class InternalCodeAllocator : public CodeAllocator { 39 public: 40 InternalCodeAllocator() { } 41 42 virtual uint8_t* Allocate(size_t size) { 43 size_ = size; 44 memory_.reset(new uint8_t[size]); 45 return memory_.get(); 46 } 47 48 size_t GetSize() const { return size_; } 49 uint8_t* GetMemory() const { return memory_.get(); } 50 51 private: 52 size_t size_; 53 std::unique_ptr<uint8_t[]> memory_; 54 55 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); 56}; 57 58static void Run(const InternalCodeAllocator& allocator, 59 const CodeGenerator& codegen, 60 bool has_result, 61 int32_t expected) { 62 typedef int32_t (*fptr)(); 63 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); 64 fptr f = reinterpret_cast<fptr>(allocator.GetMemory()); 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 } 69 int32_t result = f(); 70 if (has_result) { 71 ASSERT_EQ(result, expected); 72 } 73} 74 75static void RunCodeBaseline(HGraph* graph, bool has_result, int32_t expected) { 76 InternalCodeAllocator allocator; 77 78 x86::CodeGeneratorX86 codegenX86(graph); 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. 81 codegenX86.CompileBaseline(&allocator, true); 82 if (kRuntimeISA == kX86) { 83 Run(allocator, codegenX86, has_result, expected); 84 } 85 86 arm::CodeGeneratorARM codegenARM(graph); 87 codegenARM.CompileBaseline(&allocator, true); 88 if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) { 89 Run(allocator, codegenARM, has_result, expected); 90 } 91 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 } 97 98 arm64::CodeGeneratorARM64 codegenARM64(graph); 99 codegenARM64.CompileBaseline(&allocator, true); 100 if (kRuntimeISA == kArm64) { 101 Run(allocator, codegenARM64, has_result, expected); 102 } 103} 104 105static 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 122static 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 138static 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); 144 ASSERT_NE(graph, nullptr); 145 // Remove suspend checks, they cannot be executed in this context. 146 RemoveSuspendChecks(graph); 147 RunCodeBaseline(graph, has_result, expected); 148} 149 150TEST(CodegenTest, ReturnVoid) { 151 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); 152 TestCode(data); 153} 154 155TEST(CodegenTest, CFG1) { 156 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( 157 Instruction::GOTO | 0x100, 158 Instruction::RETURN_VOID); 159 160 TestCode(data); 161} 162 163TEST(CodegenTest, CFG2) { 164 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( 165 Instruction::GOTO | 0x100, 166 Instruction::GOTO | 0x100, 167 Instruction::RETURN_VOID); 168 169 TestCode(data); 170} 171 172TEST(CodegenTest, CFG3) { 173 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( 174 Instruction::GOTO | 0x200, 175 Instruction::RETURN_VOID, 176 Instruction::GOTO | 0xFF00); 177 178 TestCode(data1); 179 180 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( 181 Instruction::GOTO_16, 3, 182 Instruction::RETURN_VOID, 183 Instruction::GOTO_16, 0xFFFF); 184 185 TestCode(data2); 186 187 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( 188 Instruction::GOTO_32, 4, 0, 189 Instruction::RETURN_VOID, 190 Instruction::GOTO_32, 0xFFFF, 0xFFFF); 191 192 TestCode(data3); 193} 194 195TEST(CodegenTest, CFG4) { 196 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( 197 Instruction::RETURN_VOID, 198 Instruction::GOTO | 0x100, 199 Instruction::GOTO | 0xFE00); 200 201 TestCode(data); 202} 203 204TEST(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 214TEST(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 222TEST(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 230TEST(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 239TEST(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 248TEST(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 259TEST(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 270// Exercise bit-wise (one's complement) not-int instruction. 271#define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ 272TEST(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 284NOT_INT_TEST(ReturnNotIntMinus2, -2, 1) 285NOT_INT_TEST(ReturnNotIntMinus1, -1, 0) 286NOT_INT_TEST(ReturnNotInt0, 0, -1) 287NOT_INT_TEST(ReturnNotInt1, 1, -2) 288NOT_INT_TEST(ReturnNotIntINT_MIN, -2147483648, 2147483647) // (2^31) - 1 289NOT_INT_TEST(ReturnNotIntINT_MINPlus1, -2147483647, 2147483646) // (2^31) - 2 290NOT_INT_TEST(ReturnNotIntINT_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1 291NOT_INT_TEST(ReturnNotIntINT_MAX, 2147483647, -2147483648) // -(2^31) 292 293#undef NOT_INT_TEST 294 295TEST(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 305TEST(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 315TEST(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 324TEST(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 333TEST(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 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 405#if !defined(__aarch64__) 406MUL_TEST(INT, MulInt); 407MUL_TEST(LONG, MulLong); 408#endif 409 410#if defined(__aarch64__) 411TEST(CodegenTest, DISABLED_ReturnMulIntLit8) { 412#else 413TEST(CodegenTest, ReturnMulIntLit8) { 414#endif 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 423#if defined(__aarch64__) 424TEST(CodegenTest, DISABLED_ReturnMulIntLit16) { 425#else 426TEST(CodegenTest, ReturnMulIntLit16) { 427#endif 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 437} // namespace art 438