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

Side by Side Diff: src/hydrogen.cc

Issue 8373029: [hydrogen] optimize switch with string clauses (Closed) Base URL: gh:v8/v8@master
Patch Set: last_block != NULL for string cases 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
« no previous file with comments | « src/hydrogen.h ('k') | src/hydrogen-instructions.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 1942 matching lines...) Expand 10 before | Expand all | Expand 10 after
2689 ASSERT(current_block()->HasPredecessor()); 2690 ASSERT(current_block()->HasPredecessor());
2690 // We only optimize switch statements with smi-literal smi comparisons, 2691 // We only optimize switch statements with smi-literal smi comparisons,
2691 // with a bounded number of clauses. 2692 // with a bounded number of clauses.
2692 const int kCaseClauseLimit = 128; 2693 const int kCaseClauseLimit = 128;
2693 ZoneList<CaseClause*>* clauses = stmt->cases(); 2694 ZoneList<CaseClause*>* clauses = stmt->cases();
2694 int clause_count = clauses->length(); 2695 int clause_count = clauses->length();
2695 if (clause_count > kCaseClauseLimit) { 2696 if (clause_count > kCaseClauseLimit) {
2696 return Bailout("SwitchStatement: too many clauses"); 2697 return Bailout("SwitchStatement: too many clauses");
2697 } 2698 }
2698 2699
2700 HValue* context = environment()->LookupContext();
2701
2699 CHECK_ALIVE(VisitForValue(stmt->tag())); 2702 CHECK_ALIVE(VisitForValue(stmt->tag()));
2700 AddSimulate(stmt->EntryId()); 2703 AddSimulate(stmt->EntryId());
2701 HValue* tag_value = Pop(); 2704 HValue* tag_value = Pop();
2702 HBasicBlock* first_test_block = current_block(); 2705 HBasicBlock* first_test_block = current_block();
2703 2706
2704 // 1. Build all the tests, with dangling true branches. Unconditionally 2707 SwitchType switch_type = UNKNOWN_SWITCH;
2705 // deoptimize if we encounter a non-smi comparison. 2708
2709 // 1. Extract clause type
2706 for (int i = 0; i < clause_count; ++i) { 2710 for (int i = 0; i < clause_count; ++i) {
2707 CaseClause* clause = clauses->at(i); 2711 CaseClause* clause = clauses->at(i);
2708 if (clause->is_default()) continue; 2712 if (clause->is_default()) continue;
2709 if (!clause->label()->IsSmiLiteral()) { 2713
2710 return Bailout("SwitchStatement: non-literal switch label"); 2714 if (switch_type == UNKNOWN_SWITCH) {
2715 if (clause->label()->IsSmiLiteral()) {
2716 switch_type = SMI_SWITCH;
2717 } else if (clause->label()->IsStringLiteral()) {
2718 switch_type = STRING_SWITCH;
2719 } else {
2720 return Bailout("SwitchStatement: non-literal switch label");
2721 }
2722 } else if ((switch_type == STRING_SWITCH &&
2723 !clause->label()->IsStringLiteral()) ||
2724 (switch_type == SMI_SWITCH &&
2725 !clause->label()->IsSmiLiteral())) {
2726 return Bailout("SwitchStatemnt: mixed label types are not supported");
2727 }
2728 }
2729
2730 HUnaryControlInstruction* oddball_check = NULL;
2731 HBasicBlock* oddball_block = NULL;
2732
2733 int iterations = clause_count;
2734
2735 // Test switch's tag value if all clauses are string literals
2736 if (switch_type == STRING_SWITCH) {
2737 // We'll generate two passes of checks
2738 iterations = iterations * 2;
2739
2740 oddball_check = new(zone()) HIsStringAndBranch(tag_value);
fschneider 2011/11/02 15:01:17 oddball_check is really the wrong name, I'd sugges
2741 first_test_block = graph()->CreateBasicBlock();
2742 oddball_block = graph()->CreateBasicBlock();
2743
2744 oddball_check->SetSuccessorAt(0, first_test_block);
2745 oddball_check->SetSuccessorAt(1, oddball_block);
2746 current_block()->Finish(oddball_check);
2747
2748 set_current_block(first_test_block);
2749 }
2750
2751 // 2. Build all the tests, with dangling true branches
2752 for (int i = 0; i < iterations; ++i) {
2753 CaseClause* clause = clauses->at(i % clause_count);
2754 if (clause->is_default()) continue;
2755
2756 clause->RecordTypeFeedback(oracle());
2757
2758 // Generate a compare and branch.
2759 CHECK_ALIVE(VisitForValue(clause->label()));
2760 HValue* label_value = Pop();
2761
2762 HBasicBlock* next_test_block = graph()->CreateBasicBlock();
2763 HBasicBlock* body_block = graph()->CreateBasicBlock();
2764
2765 HControlInstruction* compare;
2766
2767 if (switch_type == SMI_SWITCH) {
2768 if (!clause->IsSmiCompare()) {
2769 // Finish with deoptimize and add uses of enviroment values to
2770 // account for invisible uses.
2771 current_block()->FinishExitWithDeoptimization(HDeoptimize::kUseAll);
2772 set_current_block(NULL);
2773 break;
2774 }
2775
2776 HCompareIDAndBranch* compare_ =
2777 new(zone()) HCompareIDAndBranch(tag_value,
2778 label_value,
2779 Token::EQ_STRICT);
2780 compare_->SetInputRepresentation(Representation::Integer32());
2781 compare = compare_;
2782 } else {
2783 if (i < clause_count) {
2784 compare = new(zone()) HCompareObjectEqAndBranch(tag_value, label_value);
2785 } else {
2786 compare = new(zone()) HCompareGenericAndBranch(context, tag_value,
2787 label_value,
2788 Token::EQ_STRICT);
2789 }
2711 } 2790 }
2712 2791
2713 // Unconditionally deoptimize on the first non-smi compare.
2714 clause->RecordTypeFeedback(oracle());
2715 if (!clause->IsSmiCompare()) {
2716 // Finish with deoptimize and add uses of enviroment values to
2717 // account for invisible uses.
2718 current_block()->FinishExitWithDeoptimization(HDeoptimize::kUseAll);
2719 set_current_block(NULL);
2720 break;
2721 }
2722
2723 // Otherwise generate a compare and branch.
2724 CHECK_ALIVE(VisitForValue(clause->label()));
2725 HValue* label_value = Pop();
2726 HCompareIDAndBranch* compare =
2727 new(zone()) HCompareIDAndBranch(tag_value,
2728 label_value,
2729 Token::EQ_STRICT);
2730 compare->SetInputRepresentation(Representation::Integer32());
2731 HBasicBlock* body_block = graph()->CreateBasicBlock();
2732 HBasicBlock* next_test_block = graph()->CreateBasicBlock();
2733 compare->SetSuccessorAt(0, body_block); 2792 compare->SetSuccessorAt(0, body_block);
2734 compare->SetSuccessorAt(1, next_test_block); 2793 compare->SetSuccessorAt(1, next_test_block);
2735 current_block()->Finish(compare); 2794 current_block()->Finish(compare);
2795
2736 set_current_block(next_test_block); 2796 set_current_block(next_test_block);
2737 } 2797 }
2738 2798
2739 // Save the current block to use for the default or to join with the 2799 // Save the current block to use for the default or to join with the
2740 // exit. This block is NULL if we deoptimized. 2800 // exit. This block is NULL if we deoptimized.
2741 HBasicBlock* last_block = current_block(); 2801 HBasicBlock* last_block = current_block();
2742 2802
2743 // 2. Loop over the clauses and the linked list of tests in lockstep, 2803 if (oddball_block != NULL) {
2804 last_block = CreateJoin(last_block, oddball_block, stmt->ExitId());
2805 }
2806
2807 // 3. Loop over the clauses and the linked list of tests in lockstep,
2744 // translating the clause bodies. 2808 // translating the clause bodies.
2745 HBasicBlock* curr_test_block = first_test_block; 2809 HBasicBlock* curr_test_block = first_test_block;
2746 HBasicBlock* fall_through_block = NULL; 2810 HBasicBlock* fall_through_block = NULL;
2811 HBasicBlock* fall_through_block_left = NULL;
2812
2747 BreakAndContinueInfo break_info(stmt); 2813 BreakAndContinueInfo break_info(stmt);
2748 { BreakAndContinueScope push(&break_info, this); 2814 { BreakAndContinueScope push(&break_info, this);
2749 for (int i = 0; i < clause_count; ++i) { 2815 for (int i = 0; i < iterations; ++i) {
2750 CaseClause* clause = clauses->at(i); 2816 // Create shadow last block for second pass
2817 if (i == clause_count) {
2818 fall_through_block_left = fall_through_block;
2819 fall_through_block = NULL;
2820 }
2821
2822 CaseClause* clause = clauses->at(i % clause_count);
2751 2823
2752 // Identify the block where normal (non-fall-through) control flow 2824 // Identify the block where normal (non-fall-through) control flow
2753 // goes to. 2825 // goes to.
2754 HBasicBlock* normal_block = NULL; 2826 HBasicBlock* normal_block = NULL;
2755 if (clause->is_default()) { 2827 if (clause->is_default()) {
2756 if (last_block != NULL) { 2828 if (last_block != NULL) {
2757 normal_block = last_block; 2829 normal_block = last_block;
2758 last_block = NULL; // Cleared to indicate we've handled it. 2830 last_block = NULL; // Cleared to indicate we've handled it.
2759 } 2831 }
2760 } else if (!curr_test_block->end()->IsDeoptimize()) { 2832 } else if (!curr_test_block->end()->IsDeoptimize()) {
(...skipping 23 matching lines...) Expand all
2784 normal_block, 2856 normal_block,
2785 clause->EntryId()); 2857 clause->EntryId());
2786 set_current_block(join); 2858 set_current_block(join);
2787 } 2859 }
2788 2860
2789 CHECK_BAILOUT(VisitStatements(clause->statements())); 2861 CHECK_BAILOUT(VisitStatements(clause->statements()));
2790 fall_through_block = current_block(); 2862 fall_through_block = current_block();
2791 } 2863 }
2792 } 2864 }
2793 2865
2866 // Join tails of parallel branches
2867 fall_through_block = CreateJoin(fall_through_block, fall_through_block_left,
2868 stmt->ExitId());
2869
2794 // Create an up-to-3-way join. Use the break block if it exists since 2870 // Create an up-to-3-way join. Use the break block if it exists since
2795 // it's already a join block. 2871 // it's already a join block.
2796 HBasicBlock* break_block = break_info.break_block(); 2872 HBasicBlock* break_block = break_info.break_block();
2797 if (break_block == NULL) { 2873 if (break_block == NULL) {
2798 set_current_block(CreateJoin(fall_through_block, 2874 set_current_block(CreateJoin(fall_through_block,
2799 last_block, 2875 last_block,
2800 stmt->ExitId())); 2876 stmt->ExitId()));
2801 } else { 2877 } else {
2802 if (fall_through_block != NULL) fall_through_block->Goto(break_block); 2878 if (fall_through_block != NULL) fall_through_block->Goto(break_block);
2803 if (last_block != NULL) last_block->Goto(break_block); 2879 if (last_block != NULL) last_block->Goto(break_block);
(...skipping 4215 matching lines...) Expand 10 before | Expand all | Expand 10 after
7019 } 7095 }
7020 } 7096 }
7021 7097
7022 #ifdef DEBUG 7098 #ifdef DEBUG
7023 if (graph_ != NULL) graph_->Verify(false); // No full verify. 7099 if (graph_ != NULL) graph_->Verify(false); // No full verify.
7024 if (allocator_ != NULL) allocator_->Verify(); 7100 if (allocator_ != NULL) allocator_->Verify();
7025 #endif 7101 #endif
7026 } 7102 }
7027 7103
7028 } } // namespace v8::internal 7104 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/hydrogen.h ('k') | src/hydrogen-instructions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698