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