| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 } | 405 } |
| 406 block_side_effects_[id].Add(side_effects); | 406 block_side_effects_[id].Add(side_effects); |
| 407 | 407 |
| 408 // Loop headers are part of their loop. | 408 // Loop headers are part of their loop. |
| 409 if (block->IsLoopHeader()) { | 409 if (block->IsLoopHeader()) { |
| 410 loop_side_effects_[id].Add(side_effects); | 410 loop_side_effects_[id].Add(side_effects); |
| 411 } | 411 } |
| 412 | 412 |
| 413 // Propagate loop side effects upwards. | 413 // Propagate loop side effects upwards. |
| 414 if (block->HasParentLoopHeader()) { | 414 if (block->HasParentLoopHeader()) { |
| 415 int header_id = block->parent_loop_header()->block_id(); | 415 HBasicBlock* with_parent = block; |
| 416 loop_side_effects_[header_id].Add(block->IsLoopHeader() | 416 if (block->IsLoopHeader()) side_effects = loop_side_effects_[id]; |
| 417 ? loop_side_effects_[id] | 417 do { |
| 418 : side_effects); | 418 HBasicBlock* parent_block = with_parent->parent_loop_header(); |
| 419 loop_side_effects_[parent_block->block_id()].Add(side_effects); |
| 420 with_parent = parent_block; |
| 421 } while (with_parent->HasParentLoopHeader()); |
| 419 } | 422 } |
| 420 } | 423 } |
| 421 } | 424 } |
| 422 } | 425 } |
| 423 | 426 |
| 424 | 427 |
| 425 SmartArrayPointer<char> GetGVNFlagsString(GVNFlagSet flags) { | 428 SmartArrayPointer<char> GetGVNFlagsString(GVNFlagSet flags) { |
| 426 char underlying_buffer[kLastFlag * 128]; | 429 char underlying_buffer[kLastFlag * 128]; |
| 427 Vector<char> buffer(underlying_buffer, sizeof(underlying_buffer)); | 430 Vector<char> buffer(underlying_buffer, sizeof(underlying_buffer)); |
| 428 #if DEBUG | 431 #if DEBUG |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 | 513 |
| 511 void HGlobalValueNumberingPhase::LoopInvariantCodeMotion() { | 514 void HGlobalValueNumberingPhase::LoopInvariantCodeMotion() { |
| 512 TRACE_GVN_1("Using optimistic loop invariant code motion: %s\n", | 515 TRACE_GVN_1("Using optimistic loop invariant code motion: %s\n", |
| 513 graph()->use_optimistic_licm() ? "yes" : "no"); | 516 graph()->use_optimistic_licm() ? "yes" : "no"); |
| 514 for (int i = graph()->blocks()->length() - 1; i >= 0; --i) { | 517 for (int i = graph()->blocks()->length() - 1; i >= 0; --i) { |
| 515 HBasicBlock* block = graph()->blocks()->at(i); | 518 HBasicBlock* block = graph()->blocks()->at(i); |
| 516 if (block->IsLoopHeader()) { | 519 if (block->IsLoopHeader()) { |
| 517 GVNFlagSet side_effects = loop_side_effects_[block->block_id()]; | 520 GVNFlagSet side_effects = loop_side_effects_[block->block_id()]; |
| 518 TRACE_GVN_2("Try loop invariant motion for block B%d %s\n", | 521 TRACE_GVN_2("Try loop invariant motion for block B%d %s\n", |
| 519 block->block_id(), | 522 block->block_id(), |
| 520 *GetGVNFlagsString(side_effects)); | 523 GetGVNFlagsString(side_effects).get()); |
| 521 | 524 |
| 522 GVNFlagSet accumulated_first_time_depends; | 525 GVNFlagSet accumulated_first_time_depends; |
| 523 GVNFlagSet accumulated_first_time_changes; | 526 GVNFlagSet accumulated_first_time_changes; |
| 524 HBasicBlock* last = block->loop_information()->GetLastBackEdge(); | 527 HBasicBlock* last = block->loop_information()->GetLastBackEdge(); |
| 525 for (int j = block->block_id(); j <= last->block_id(); ++j) { | 528 for (int j = block->block_id(); j <= last->block_id(); ++j) { |
| 526 ProcessLoopBlock(graph()->blocks()->at(j), block, side_effects, | 529 ProcessLoopBlock(graph()->blocks()->at(j), block, side_effects, |
| 527 &accumulated_first_time_depends, | 530 &accumulated_first_time_depends, |
| 528 &accumulated_first_time_changes); | 531 &accumulated_first_time_changes); |
| 529 } | 532 } |
| 530 } | 533 } |
| 531 } | 534 } |
| 532 } | 535 } |
| 533 | 536 |
| 534 | 537 |
| 535 void HGlobalValueNumberingPhase::ProcessLoopBlock( | 538 void HGlobalValueNumberingPhase::ProcessLoopBlock( |
| 536 HBasicBlock* block, | 539 HBasicBlock* block, |
| 537 HBasicBlock* loop_header, | 540 HBasicBlock* loop_header, |
| 538 GVNFlagSet loop_kills, | 541 GVNFlagSet loop_kills, |
| 539 GVNFlagSet* first_time_depends, | 542 GVNFlagSet* first_time_depends, |
| 540 GVNFlagSet* first_time_changes) { | 543 GVNFlagSet* first_time_changes) { |
| 541 HBasicBlock* pre_header = loop_header->predecessors()->at(0); | 544 HBasicBlock* pre_header = loop_header->predecessors()->at(0); |
| 542 GVNFlagSet depends_flags = HValue::ConvertChangesToDependsFlags(loop_kills); | 545 GVNFlagSet depends_flags = HValue::ConvertChangesToDependsFlags(loop_kills); |
| 543 TRACE_GVN_2("Loop invariant motion for B%d %s\n", | 546 TRACE_GVN_2("Loop invariant motion for B%d %s\n", |
| 544 block->block_id(), | 547 block->block_id(), |
| 545 *GetGVNFlagsString(depends_flags)); | 548 GetGVNFlagsString(depends_flags).get()); |
| 546 HInstruction* instr = block->first(); | 549 HInstruction* instr = block->first(); |
| 547 while (instr != NULL) { | 550 while (instr != NULL) { |
| 548 HInstruction* next = instr->next(); | 551 HInstruction* next = instr->next(); |
| 549 bool hoisted = false; | 552 bool hoisted = false; |
| 550 if (instr->CheckFlag(HValue::kUseGVN)) { | 553 if (instr->CheckFlag(HValue::kUseGVN)) { |
| 551 TRACE_GVN_4("Checking instruction %d (%s) %s. Loop %s\n", | 554 TRACE_GVN_4("Checking instruction %d (%s) %s. Loop %s\n", |
| 552 instr->id(), | 555 instr->id(), |
| 553 instr->Mnemonic(), | 556 instr->Mnemonic(), |
| 554 *GetGVNFlagsString(instr->gvn_flags()), | 557 GetGVNFlagsString(instr->gvn_flags()).get(), |
| 555 *GetGVNFlagsString(loop_kills)); | 558 GetGVNFlagsString(loop_kills).get()); |
| 556 bool can_hoist = !instr->gvn_flags().ContainsAnyOf(depends_flags); | 559 bool can_hoist = !instr->gvn_flags().ContainsAnyOf(depends_flags); |
| 557 if (can_hoist && !graph()->use_optimistic_licm()) { | 560 if (can_hoist && !graph()->use_optimistic_licm()) { |
| 558 can_hoist = block->IsLoopSuccessorDominator(); | 561 can_hoist = block->IsLoopSuccessorDominator(); |
| 559 } | 562 } |
| 560 | 563 |
| 561 if (can_hoist) { | 564 if (can_hoist) { |
| 562 bool inputs_loop_invariant = true; | 565 bool inputs_loop_invariant = true; |
| 563 for (int i = 0; i < instr->OperandCount(); ++i) { | 566 for (int i = 0; i < instr->OperandCount(); ++i) { |
| 564 if (instr->OperandAt(i)->IsDefinedAfter(pre_header)) { | 567 if (instr->OperandAt(i)->IsDefinedAfter(pre_header)) { |
| 565 inputs_loop_invariant = false; | 568 inputs_loop_invariant = false; |
| 566 } | 569 } |
| 567 } | 570 } |
| 568 | 571 |
| 569 if (inputs_loop_invariant && ShouldMove(instr, loop_header)) { | 572 if (inputs_loop_invariant && ShouldMove(instr, loop_header)) { |
| 570 TRACE_GVN_1("Hoisting loop invariant instruction %d\n", instr->id()); | 573 TRACE_GVN_2("Hoisting loop invariant instruction i%d to block B%d\n", |
| 574 instr->id(), pre_header->block_id()); |
| 571 // Move the instruction out of the loop. | 575 // Move the instruction out of the loop. |
| 572 instr->Unlink(); | 576 instr->Unlink(); |
| 573 instr->InsertBefore(pre_header->end()); | 577 instr->InsertBefore(pre_header->end()); |
| 574 if (instr->HasSideEffects()) removed_side_effects_ = true; | 578 if (instr->HasSideEffects()) removed_side_effects_ = true; |
| 575 hoisted = true; | 579 hoisted = true; |
| 576 } | 580 } |
| 577 } | 581 } |
| 578 } | 582 } |
| 579 if (!hoisted) { | 583 if (!hoisted) { |
| 580 // If an instruction is not hoisted, we have to account for its side | 584 // If an instruction is not hoisted, we have to account for its side |
| 581 // effects when hoisting later HTransitionElementsKind instructions. | 585 // effects when hoisting later HTransitionElementsKind instructions. |
| 582 GVNFlagSet previous_depends = *first_time_depends; | 586 GVNFlagSet previous_depends = *first_time_depends; |
| 583 GVNFlagSet previous_changes = *first_time_changes; | 587 GVNFlagSet previous_changes = *first_time_changes; |
| 584 first_time_depends->Add(instr->DependsOnFlags()); | 588 first_time_depends->Add(instr->DependsOnFlags()); |
| 585 first_time_changes->Add(instr->ChangesFlags()); | 589 first_time_changes->Add(instr->ChangesFlags()); |
| 586 if (!(previous_depends == *first_time_depends)) { | 590 if (!(previous_depends == *first_time_depends)) { |
| 587 TRACE_GVN_1("Updated first-time accumulated %s\n", | 591 TRACE_GVN_1("Updated first-time accumulated %s\n", |
| 588 *GetGVNFlagsString(*first_time_depends)); | 592 GetGVNFlagsString(*first_time_depends).get()); |
| 589 } | 593 } |
| 590 if (!(previous_changes == *first_time_changes)) { | 594 if (!(previous_changes == *first_time_changes)) { |
| 591 TRACE_GVN_1("Updated first-time accumulated %s\n", | 595 TRACE_GVN_1("Updated first-time accumulated %s\n", |
| 592 *GetGVNFlagsString(*first_time_changes)); | 596 GetGVNFlagsString(*first_time_changes).get()); |
| 593 } | 597 } |
| 594 } | 598 } |
| 595 instr = next; | 599 instr = next; |
| 596 } | 600 } |
| 597 } | 601 } |
| 598 | 602 |
| 599 | 603 |
| 600 bool HGlobalValueNumberingPhase::AllowCodeMotion() { | 604 bool HGlobalValueNumberingPhase::AllowCodeMotion() { |
| 601 return info()->IsStub() || info()->opt_count() + 1 < FLAG_max_opt_count; | 605 return info()->IsStub() || info()->opt_count() + 1 < FLAG_max_opt_count; |
| 602 } | 606 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 // Instruction was unlinked during graph traversal. | 798 // Instruction was unlinked during graph traversal. |
| 795 if (!instr->IsLinked()) continue; | 799 if (!instr->IsLinked()) continue; |
| 796 | 800 |
| 797 GVNFlagSet flags = instr->ChangesFlags(); | 801 GVNFlagSet flags = instr->ChangesFlags(); |
| 798 if (!flags.IsEmpty()) { | 802 if (!flags.IsEmpty()) { |
| 799 // Clear all instructions in the map that are affected by side effects. | 803 // Clear all instructions in the map that are affected by side effects. |
| 800 // Store instruction as the dominating one for tracked side effects. | 804 // Store instruction as the dominating one for tracked side effects. |
| 801 map->Kill(flags); | 805 map->Kill(flags); |
| 802 dominators->Store(flags, instr); | 806 dominators->Store(flags, instr); |
| 803 TRACE_GVN_2("Instruction %d %s\n", instr->id(), | 807 TRACE_GVN_2("Instruction %d %s\n", instr->id(), |
| 804 *GetGVNFlagsString(flags)); | 808 GetGVNFlagsString(flags).get()); |
| 805 } | 809 } |
| 806 if (instr->CheckFlag(HValue::kUseGVN)) { | 810 if (instr->CheckFlag(HValue::kUseGVN)) { |
| 807 ASSERT(!instr->HasObservableSideEffects()); | 811 ASSERT(!instr->HasObservableSideEffects()); |
| 808 HValue* other = map->Lookup(instr); | 812 HValue* other = map->Lookup(instr); |
| 809 if (other != NULL) { | 813 if (other != NULL) { |
| 810 ASSERT(instr->Equals(other) && other->Equals(instr)); | 814 ASSERT(instr->Equals(other) && other->Equals(instr)); |
| 811 TRACE_GVN_4("Replacing value %d (%s) with value %d (%s)\n", | 815 TRACE_GVN_4("Replacing value %d (%s) with value %d (%s)\n", |
| 812 instr->id(), | 816 instr->id(), |
| 813 instr->Mnemonic(), | 817 instr->Mnemonic(), |
| 814 other->id(), | 818 other->id(), |
| (...skipping 29 matching lines...) Expand all Loading... |
| 844 dominated); | 848 dominated); |
| 845 successor_map->Kill(side_effects_on_all_paths); | 849 successor_map->Kill(side_effects_on_all_paths); |
| 846 successor_dominators->Kill(side_effects_on_all_paths); | 850 successor_dominators->Kill(side_effects_on_all_paths); |
| 847 } | 851 } |
| 848 } | 852 } |
| 849 current = next; | 853 current = next; |
| 850 } | 854 } |
| 851 } | 855 } |
| 852 | 856 |
| 853 } } // namespace v8::internal | 857 } } // namespace v8::internal |
| OLD | NEW |