Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/hydrogen.cc

Issue 8373029: [hydrogen] optimize switch with string clauses (Closed) Base URL: gh:v8/v8@master
Patch Set: remove fishy assigns Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 int iterations = clause_count;
2746
2747 // Test switch's tag value if all clauses are string literals
2748 if (switch_type == STRING_SWITCH) {
2749 // Bailout if we seen string(non-symbol) comparisons here
2750 for (int i = 0; i < clause_count; ++i) {
2751 CaseClause* clause = clauses->at(i);
2752 if (clause->is_default()) continue;
2753
2754 clause->RecordTypeFeedback(oracle());
2755
2756 if (clause->IsStringCompare()) {
2757 return Bailout("SwitchStatement: no symbol comparisons expected");
fschneider 2011/11/07 15:34:00 I don't understand the reason for this bailout. I
indutny 2011/11/07 17:06:08 In many cases switch is used for parsing (like lex
2758 }
2723 } 2759 }
2724 2760
2725 // Unconditionally deoptimize on the first non-smi compare. 2761 // We'll generate two passes of checks
2726 clause->RecordTypeFeedback(oracle()); 2762 iterations = iterations * 2;
2727 if (!clause->IsSmiCompare()) { 2763
2728 // Finish with deoptimize and add uses of enviroment values to 2764 string_check = new(zone()) HIsStringAndBranch(tag_value);
2729 // account for invisible uses. 2765 first_test_block = graph()->CreateBasicBlock();
2730 current_block()->FinishExitWithDeoptimization(HDeoptimize::kUseAll); 2766 not_string_block = graph()->CreateBasicBlock();
2731 set_current_block(NULL); 2767
2732 break; 2768 string_check->SetSuccessorAt(0, first_test_block);
2769 string_check->SetSuccessorAt(1, not_string_block);
2770 current_block()->Finish(string_check);
2771
2772 set_current_block(first_test_block);
2773 }
2774
2775 // 2. Build all the tests, with dangling true branches
2776 for (int i = 0; i < iterations; ++i) {
2777 CaseClause* clause = clauses->at(i % clause_count);
2778 if (clause->is_default()) continue;
2779
2780 if (switch_type == SMI_SWITCH) {
2781 clause->RecordTypeFeedback(oracle());
2733 } 2782 }
2734 2783
2735 // Otherwise generate a compare and branch. 2784 // Generate a compare and branch.
2736 CHECK_ALIVE(VisitForValue(clause->label())); 2785 CHECK_ALIVE(VisitForValue(clause->label()));
2737 HValue* label_value = Pop(); 2786 HValue* label_value = Pop();
2738 HCompareIDAndBranch* compare = 2787
2739 new(zone()) HCompareIDAndBranch(tag_value, 2788 HBasicBlock* next_test_block = graph()->CreateBasicBlock();
2740 label_value,
2741 Token::EQ_STRICT);
2742 compare->SetInputRepresentation(Representation::Integer32());
2743 HBasicBlock* body_block = graph()->CreateBasicBlock(); 2789 HBasicBlock* body_block = graph()->CreateBasicBlock();
2744 HBasicBlock* next_test_block = graph()->CreateBasicBlock(); 2790
2791 HControlInstruction* compare;
2792
2793 if (switch_type == SMI_SWITCH) {
2794 if (!clause->IsSmiCompare()) {
2795 // Finish with deoptimize and add uses of enviroment values to
2796 // account for invisible uses.
2797 current_block()->FinishExitWithDeoptimization(HDeoptimize::kUseAll);
2798 set_current_block(NULL);
2799 break;
2800 }
2801
2802 HCompareIDAndBranch* compare_ =
2803 new(zone()) HCompareIDAndBranch(tag_value,
2804 label_value,
2805 Token::EQ_STRICT);
2806 compare_->SetInputRepresentation(Representation::Integer32());
2807 compare = compare_;
2808 } else {
2809 if (i < clause_count) {
2810 compare = new(zone()) HCompareObjectEqAndBranch(tag_value, label_value);
2811 } else {
2812 compare = new(zone()) HStringCompareAndBranch(context, tag_value,
2813 label_value,
2814 Token::EQ_STRICT);
2815 }
2816 }
2817
2745 compare->SetSuccessorAt(0, body_block); 2818 compare->SetSuccessorAt(0, body_block);
2746 compare->SetSuccessorAt(1, next_test_block); 2819 compare->SetSuccessorAt(1, next_test_block);
2747 current_block()->Finish(compare); 2820 current_block()->Finish(compare);
2821
2748 set_current_block(next_test_block); 2822 set_current_block(next_test_block);
2749 } 2823 }
2750 2824
2751 // Save the current block to use for the default or to join with the 2825 // Save the current block to use for the default or to join with the
2752 // exit. This block is NULL if we deoptimized. 2826 // exit. This block is NULL if we deoptimized.
2753 HBasicBlock* last_block = current_block(); 2827 HBasicBlock* last_block = current_block();
2754 2828
2755 // 2. Loop over the clauses and the linked list of tests in lockstep, 2829 if (not_string_block != NULL) {
2830 last_block = CreateJoin(last_block, not_string_block, stmt->ExitId());
2831 }
2832
2833 // 3. Loop over the clauses and the linked list of tests in lockstep,
2756 // translating the clause bodies. 2834 // translating the clause bodies.
2757 HBasicBlock* curr_test_block = first_test_block; 2835 HBasicBlock* curr_test_block = first_test_block;
2758 HBasicBlock* fall_through_block = NULL; 2836 HBasicBlock* fall_through_block = NULL;
2837 HBasicBlock* fall_through_block_left = NULL;
2838
2759 BreakAndContinueInfo break_info(stmt); 2839 BreakAndContinueInfo break_info(stmt);
2760 { BreakAndContinueScope push(&break_info, this); 2840 { BreakAndContinueScope push(&break_info, this);
2761 for (int i = 0; i < clause_count; ++i) { 2841 for (int i = 0; i < iterations; ++i) {
2762 CaseClause* clause = clauses->at(i); 2842 // Create shadow last block for second pass
2843 if (i == clause_count) {
2844 fall_through_block_left = fall_through_block;
2845 fall_through_block = NULL;
2846 }
2847
2848 CaseClause* clause = clauses->at(i % clause_count);
2763 2849
2764 // Identify the block where normal (non-fall-through) control flow 2850 // Identify the block where normal (non-fall-through) control flow
2765 // goes to. 2851 // goes to.
2766 HBasicBlock* normal_block = NULL; 2852 HBasicBlock* normal_block = NULL;
2767 if (clause->is_default()) { 2853 if (clause->is_default()) {
2768 if (last_block != NULL) { 2854 if (last_block != NULL) {
2769 normal_block = last_block; 2855 normal_block = last_block;
2770 last_block = NULL; // Cleared to indicate we've handled it. 2856 last_block = NULL; // Cleared to indicate we've handled it.
2771 } 2857 }
2772 } else if (!curr_test_block->end()->IsDeoptimize()) { 2858 } else if (!curr_test_block->end()->IsDeoptimize()) {
(...skipping 23 matching lines...) Expand all
2796 normal_block, 2882 normal_block,
2797 clause->EntryId()); 2883 clause->EntryId());
2798 set_current_block(join); 2884 set_current_block(join);
2799 } 2885 }
2800 2886
2801 CHECK_BAILOUT(VisitStatements(clause->statements())); 2887 CHECK_BAILOUT(VisitStatements(clause->statements()));
2802 fall_through_block = current_block(); 2888 fall_through_block = current_block();
2803 } 2889 }
2804 } 2890 }
2805 2891
2892 // Join tails of parallel branches
2893 fall_through_block = CreateJoin(fall_through_block, fall_through_block_left,
2894 stmt->ExitId());
2895
2806 // Create an up-to-3-way join. Use the break block if it exists since 2896 // Create an up-to-3-way join. Use the break block if it exists since
2807 // it's already a join block. 2897 // it's already a join block.
2808 HBasicBlock* break_block = break_info.break_block(); 2898 HBasicBlock* break_block = break_info.break_block();
2809 if (break_block == NULL) { 2899 if (break_block == NULL) {
2810 set_current_block(CreateJoin(fall_through_block, 2900 set_current_block(CreateJoin(fall_through_block,
2811 last_block, 2901 last_block,
2812 stmt->ExitId())); 2902 stmt->ExitId()));
2813 } else { 2903 } else {
2814 if (fall_through_block != NULL) fall_through_block->Goto(break_block); 2904 if (fall_through_block != NULL) fall_through_block->Goto(break_block);
2815 if (last_block != NULL) last_block->Goto(break_block); 2905 if (last_block != NULL) last_block->Goto(break_block);
(...skipping 4217 matching lines...) Expand 10 before | Expand all | Expand 10 after
7033 } 7123 }
7034 } 7124 }
7035 7125
7036 #ifdef DEBUG 7126 #ifdef DEBUG
7037 if (graph_ != NULL) graph_->Verify(false); // No full verify. 7127 if (graph_ != NULL) graph_->Verify(false); // No full verify.
7038 if (allocator_ != NULL) allocator_->Verify(); 7128 if (allocator_ != NULL) allocator_->Verify();
7039 #endif 7129 #endif
7040 } 7130 }
7041 7131
7042 } } // namespace v8::internal 7132 } } // namespace v8::internal
OLDNEW
« src/arm/lithium-arm.cc ('K') | « src/hydrogen.h ('k') | src/hydrogen-instructions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698