OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 HBasicBlock* loop_header) { | 727 HBasicBlock* loop_header) { |
728 if (block == NULL || visited->Contains(block->block_id())) return; | 728 if (block == NULL || visited->Contains(block->block_id())) return; |
729 if (block->parent_loop_header() != loop_header) return; | 729 if (block->parent_loop_header() != loop_header) return; |
730 visited->Add(block->block_id()); | 730 visited->Add(block->block_id()); |
731 if (block->IsLoopHeader()) { | 731 if (block->IsLoopHeader()) { |
732 PostorderLoopBlocks(block->loop_information(), visited, order, loop_header); | 732 PostorderLoopBlocks(block->loop_information(), visited, order, loop_header); |
733 for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) { | 733 for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) { |
734 Postorder(it.Current(), visited, order, block); | 734 Postorder(it.Current(), visited, order, block); |
735 } | 735 } |
736 } else { | 736 } else { |
| 737 ASSERT(block->IsFinished()); |
737 for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) { | 738 for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) { |
738 Postorder(it.Current(), visited, order, loop_header); | 739 Postorder(it.Current(), visited, order, loop_header); |
739 } | 740 } |
740 } | 741 } |
741 ASSERT(block->end()->FirstSuccessor() == NULL || | 742 ASSERT(block->end()->FirstSuccessor() == NULL || |
742 order->Contains(block->end()->FirstSuccessor()) || | 743 order->Contains(block->end()->FirstSuccessor()) || |
743 block->end()->FirstSuccessor()->IsLoopHeader()); | 744 block->end()->FirstSuccessor()->IsLoopHeader()); |
744 ASSERT(block->end()->SecondSuccessor() == NULL || | 745 ASSERT(block->end()->SecondSuccessor() == NULL || |
745 order->Contains(block->end()->SecondSuccessor()) || | 746 order->Contains(block->end()->SecondSuccessor()) || |
746 block->end()->SecondSuccessor()->IsLoopHeader()); | 747 block->end()->SecondSuccessor()->IsLoopHeader()); |
(...skipping 1954 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2701 ASSERT(current_block()->HasPredecessor()); | 2702 ASSERT(current_block()->HasPredecessor()); |
2702 // We only optimize switch statements with smi-literal smi comparisons, | 2703 // We only optimize switch statements with smi-literal smi comparisons, |
2703 // with a bounded number of clauses. | 2704 // with a bounded number of clauses. |
2704 const int kCaseClauseLimit = 128; | 2705 const int kCaseClauseLimit = 128; |
2705 ZoneList<CaseClause*>* clauses = stmt->cases(); | 2706 ZoneList<CaseClause*>* clauses = stmt->cases(); |
2706 int clause_count = clauses->length(); | 2707 int clause_count = clauses->length(); |
2707 if (clause_count > kCaseClauseLimit) { | 2708 if (clause_count > kCaseClauseLimit) { |
2708 return Bailout("SwitchStatement: too many clauses"); | 2709 return Bailout("SwitchStatement: too many clauses"); |
2709 } | 2710 } |
2710 | 2711 |
| 2712 HValue* context = environment()->LookupContext(); |
| 2713 |
2711 CHECK_ALIVE(VisitForValue(stmt->tag())); | 2714 CHECK_ALIVE(VisitForValue(stmt->tag())); |
2712 AddSimulate(stmt->EntryId()); | 2715 AddSimulate(stmt->EntryId()); |
2713 HValue* tag_value = Pop(); | 2716 HValue* tag_value = Pop(); |
2714 HBasicBlock* first_test_block = current_block(); | 2717 HBasicBlock* first_test_block = current_block(); |
2715 | 2718 |
2716 // 1. Build all the tests, with dangling true branches. Unconditionally | 2719 SwitchType switch_type = UNKNOWN_SWITCH; |
2717 // deoptimize if we encounter a non-smi comparison. | 2720 |
| 2721 // 1. Extract clause type |
2718 for (int i = 0; i < clause_count; ++i) { | 2722 for (int i = 0; i < clause_count; ++i) { |
2719 CaseClause* clause = clauses->at(i); | 2723 CaseClause* clause = clauses->at(i); |
2720 if (clause->is_default()) continue; | 2724 if (clause->is_default()) continue; |
2721 if (!clause->label()->IsSmiLiteral()) { | 2725 |
2722 return Bailout("SwitchStatement: non-literal switch label"); | 2726 if (switch_type == UNKNOWN_SWITCH) { |
| 2727 if (clause->label()->IsSmiLiteral()) { |
| 2728 switch_type = SMI_SWITCH; |
| 2729 } else if (clause->label()->IsStringLiteral()) { |
| 2730 switch_type = STRING_SWITCH; |
| 2731 } else { |
| 2732 return Bailout("SwitchStatement: non-literal switch label"); |
| 2733 } |
| 2734 } else if ((switch_type == STRING_SWITCH && |
| 2735 !clause->label()->IsStringLiteral()) || |
| 2736 (switch_type == SMI_SWITCH && |
| 2737 !clause->label()->IsSmiLiteral())) { |
| 2738 return Bailout("SwitchStatemnt: mixed label types are not supported"); |
| 2739 } |
| 2740 } |
| 2741 |
| 2742 HUnaryControlInstruction* string_check = NULL; |
| 2743 HBasicBlock* not_string_block = NULL; |
| 2744 |
| 2745 // Test switch's tag value if all clauses are string literals |
| 2746 if (switch_type == STRING_SWITCH) { |
| 2747 string_check = new(zone()) HIsStringAndBranch(tag_value); |
| 2748 first_test_block = graph()->CreateBasicBlock(); |
| 2749 not_string_block = graph()->CreateBasicBlock(); |
| 2750 |
| 2751 string_check->SetSuccessorAt(0, first_test_block); |
| 2752 string_check->SetSuccessorAt(1, not_string_block); |
| 2753 current_block()->Finish(string_check); |
| 2754 |
| 2755 set_current_block(first_test_block); |
| 2756 } |
| 2757 |
| 2758 // 2. Build all the tests, with dangling true branches |
| 2759 for (int i = 0; i < clause_count; ++i) { |
| 2760 CaseClause* clause = clauses->at(i); |
| 2761 if (clause->is_default()) continue; |
| 2762 |
| 2763 if (switch_type == SMI_SWITCH) { |
| 2764 clause->RecordTypeFeedback(oracle()); |
2723 } | 2765 } |
2724 | 2766 |
2725 // Unconditionally deoptimize on the first non-smi compare. | 2767 // Generate a compare and branch. |
2726 clause->RecordTypeFeedback(oracle()); | 2768 CHECK_ALIVE(VisitForValue(clause->label())); |
2727 if (!clause->IsSmiCompare()) { | 2769 HValue* label_value = Pop(); |
2728 // Finish with deoptimize and add uses of enviroment values to | 2770 |
2729 // account for invisible uses. | 2771 HBasicBlock* next_test_block = graph()->CreateBasicBlock(); |
2730 current_block()->FinishExitWithDeoptimization(HDeoptimize::kUseAll); | 2772 HBasicBlock* body_block = graph()->CreateBasicBlock(); |
2731 set_current_block(NULL); | 2773 |
2732 break; | 2774 HControlInstruction* compare; |
| 2775 |
| 2776 if (switch_type == SMI_SWITCH) { |
| 2777 if (!clause->IsSmiCompare()) { |
| 2778 // Finish with deoptimize and add uses of enviroment values to |
| 2779 // account for invisible uses. |
| 2780 current_block()->FinishExitWithDeoptimization(HDeoptimize::kUseAll); |
| 2781 set_current_block(NULL); |
| 2782 break; |
| 2783 } |
| 2784 |
| 2785 HCompareIDAndBranch* compare_ = |
| 2786 new(zone()) HCompareIDAndBranch(tag_value, |
| 2787 label_value, |
| 2788 Token::EQ_STRICT); |
| 2789 compare_->SetInputRepresentation(Representation::Integer32()); |
| 2790 compare = compare_; |
| 2791 } else { |
| 2792 compare = new(zone()) HStringCompareAndBranch(context, tag_value, |
| 2793 label_value, |
| 2794 Token::EQ_STRICT); |
2733 } | 2795 } |
2734 | 2796 |
2735 // Otherwise generate a compare and branch. | |
2736 CHECK_ALIVE(VisitForValue(clause->label())); | |
2737 HValue* label_value = Pop(); | |
2738 HCompareIDAndBranch* compare = | |
2739 new(zone()) HCompareIDAndBranch(tag_value, | |
2740 label_value, | |
2741 Token::EQ_STRICT); | |
2742 compare->SetInputRepresentation(Representation::Integer32()); | |
2743 HBasicBlock* body_block = graph()->CreateBasicBlock(); | |
2744 HBasicBlock* next_test_block = graph()->CreateBasicBlock(); | |
2745 compare->SetSuccessorAt(0, body_block); | 2797 compare->SetSuccessorAt(0, body_block); |
2746 compare->SetSuccessorAt(1, next_test_block); | 2798 compare->SetSuccessorAt(1, next_test_block); |
2747 current_block()->Finish(compare); | 2799 current_block()->Finish(compare); |
| 2800 |
2748 set_current_block(next_test_block); | 2801 set_current_block(next_test_block); |
2749 } | 2802 } |
2750 | 2803 |
2751 // Save the current block to use for the default or to join with the | 2804 // Save the current block to use for the default or to join with the |
2752 // exit. This block is NULL if we deoptimized. | 2805 // exit. This block is NULL if we deoptimized. |
2753 HBasicBlock* last_block = current_block(); | 2806 HBasicBlock* last_block = current_block(); |
2754 | 2807 |
2755 // 2. Loop over the clauses and the linked list of tests in lockstep, | 2808 if (not_string_block != NULL) { |
| 2809 last_block = CreateJoin(last_block, not_string_block, stmt->ExitId()); |
| 2810 } |
| 2811 |
| 2812 // 3. Loop over the clauses and the linked list of tests in lockstep, |
2756 // translating the clause bodies. | 2813 // translating the clause bodies. |
2757 HBasicBlock* curr_test_block = first_test_block; | 2814 HBasicBlock* curr_test_block = first_test_block; |
2758 HBasicBlock* fall_through_block = NULL; | 2815 HBasicBlock* fall_through_block = NULL; |
| 2816 |
2759 BreakAndContinueInfo break_info(stmt); | 2817 BreakAndContinueInfo break_info(stmt); |
2760 { BreakAndContinueScope push(&break_info, this); | 2818 { BreakAndContinueScope push(&break_info, this); |
2761 for (int i = 0; i < clause_count; ++i) { | 2819 for (int i = 0; i < clause_count; ++i) { |
2762 CaseClause* clause = clauses->at(i); | 2820 CaseClause* clause = clauses->at(i); |
2763 | 2821 |
2764 // Identify the block where normal (non-fall-through) control flow | 2822 // Identify the block where normal (non-fall-through) control flow |
2765 // goes to. | 2823 // goes to. |
2766 HBasicBlock* normal_block = NULL; | 2824 HBasicBlock* normal_block = NULL; |
2767 if (clause->is_default()) { | 2825 if (clause->is_default()) { |
2768 if (last_block != NULL) { | 2826 if (last_block != NULL) { |
(...skipping 4325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7094 } | 7152 } |
7095 } | 7153 } |
7096 | 7154 |
7097 #ifdef DEBUG | 7155 #ifdef DEBUG |
7098 if (graph_ != NULL) graph_->Verify(false); // No full verify. | 7156 if (graph_ != NULL) graph_->Verify(false); // No full verify. |
7099 if (allocator_ != NULL) allocator_->Verify(); | 7157 if (allocator_ != NULL) allocator_->Verify(); |
7100 #endif | 7158 #endif |
7101 } | 7159 } |
7102 | 7160 |
7103 } } // namespace v8::internal | 7161 } } // namespace v8::internal |
OLD | NEW |