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