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

Side by Side Diff: src/hydrogen.cc

Issue 8499010: Optimize switch statements (second pass) Base URL: gh:v8/v8@master
Patch Set: fix merge issues 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 | « no previous file | no next file » | 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 2724 matching lines...) Expand 10 before | Expand all | Expand 10 after
2735 !clause->label()->IsStringLiteral()) || 2735 !clause->label()->IsStringLiteral()) ||
2736 (switch_type == SMI_SWITCH && 2736 (switch_type == SMI_SWITCH &&
2737 !clause->label()->IsSmiLiteral())) { 2737 !clause->label()->IsSmiLiteral())) {
2738 return Bailout("SwitchStatemnt: mixed label types are not supported"); 2738 return Bailout("SwitchStatemnt: mixed label types are not supported");
2739 } 2739 }
2740 } 2740 }
2741 2741
2742 HUnaryControlInstruction* string_check = NULL; 2742 HUnaryControlInstruction* string_check = NULL;
2743 HBasicBlock* not_string_block = NULL; 2743 HBasicBlock* not_string_block = NULL;
2744 2744
2745 int iterations = clause_count;
2746
2745 // Test switch's tag value if all clauses are string literals 2747 // Test switch's tag value if all clauses are string literals
2746 if (switch_type == STRING_SWITCH) { 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");
2758 }
2759 }
2760
2761 // We'll generate two passes of checks
2762 iterations = iterations * 2;
2763
2747 string_check = new(zone()) HIsStringAndBranch(tag_value); 2764 string_check = new(zone()) HIsStringAndBranch(tag_value);
2748 first_test_block = graph()->CreateBasicBlock(); 2765 first_test_block = graph()->CreateBasicBlock();
2749 not_string_block = graph()->CreateBasicBlock(); 2766 not_string_block = graph()->CreateBasicBlock();
2750 2767
2751 string_check->SetSuccessorAt(0, first_test_block); 2768 string_check->SetSuccessorAt(0, first_test_block);
2752 string_check->SetSuccessorAt(1, not_string_block); 2769 string_check->SetSuccessorAt(1, not_string_block);
2753 current_block()->Finish(string_check); 2770 current_block()->Finish(string_check);
2754 2771
2755 set_current_block(first_test_block); 2772 set_current_block(first_test_block);
2756 } 2773 }
2757 2774
2758 // 2. Build all the tests, with dangling true branches 2775 // 2. Build all the tests, with dangling true branches
2759 for (int i = 0; i < clause_count; ++i) { 2776 for (int i = 0; i < iterations; ++i) {
2760 CaseClause* clause = clauses->at(i); 2777 CaseClause* clause = clauses->at(i % clause_count);
2761 if (clause->is_default()) continue; 2778 if (clause->is_default()) continue;
2762 2779
2763 if (switch_type == SMI_SWITCH) { 2780 if (switch_type == SMI_SWITCH) {
2764 clause->RecordTypeFeedback(oracle()); 2781 clause->RecordTypeFeedback(oracle());
2765 } 2782 }
2766 2783
2767 // Generate a compare and branch. 2784 // Generate a compare and branch.
2768 CHECK_ALIVE(VisitForValue(clause->label())); 2785 CHECK_ALIVE(VisitForValue(clause->label()));
2769 HValue* label_value = Pop(); 2786 HValue* label_value = Pop();
2770 2787
(...skipping 11 matching lines...) Expand all
2782 break; 2799 break;
2783 } 2800 }
2784 2801
2785 HCompareIDAndBranch* compare_ = 2802 HCompareIDAndBranch* compare_ =
2786 new(zone()) HCompareIDAndBranch(tag_value, 2803 new(zone()) HCompareIDAndBranch(tag_value,
2787 label_value, 2804 label_value,
2788 Token::EQ_STRICT); 2805 Token::EQ_STRICT);
2789 compare_->SetInputRepresentation(Representation::Integer32()); 2806 compare_->SetInputRepresentation(Representation::Integer32());
2790 compare = compare_; 2807 compare = compare_;
2791 } else { 2808 } else {
2792 compare = new(zone()) HStringCompareAndBranch(context, tag_value, 2809 if (i < clause_count) {
2793 label_value, 2810 compare = new(zone()) HCompareObjectEqAndBranch(tag_value, label_value);
2794 Token::EQ_STRICT); 2811 } else {
2812 compare = new(zone()) HStringCompareAndBranch(context, tag_value,
2813 label_value,
2814 Token::EQ_STRICT);
2815 }
2795 } 2816 }
2796 2817
2797 compare->SetSuccessorAt(0, body_block); 2818 compare->SetSuccessorAt(0, body_block);
2798 compare->SetSuccessorAt(1, next_test_block); 2819 compare->SetSuccessorAt(1, next_test_block);
2799 current_block()->Finish(compare); 2820 current_block()->Finish(compare);
2800 2821
2801 set_current_block(next_test_block); 2822 set_current_block(next_test_block);
2802 } 2823 }
2803 2824
2804 // 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
2805 // exit. This block is NULL if we deoptimized. 2826 // exit. This block is NULL if we deoptimized.
2806 HBasicBlock* last_block = current_block(); 2827 HBasicBlock* last_block = current_block();
2807 2828
2808 if (not_string_block != NULL) { 2829 if (not_string_block != NULL) {
2809 last_block = CreateJoin(last_block, not_string_block, stmt->ExitId()); 2830 last_block = CreateJoin(last_block, not_string_block, stmt->ExitId());
2810 } 2831 }
2811 2832
2812 // 3. Loop over the clauses and the linked list of tests in lockstep, 2833 // 3. Loop over the clauses and the linked list of tests in lockstep,
2813 // translating the clause bodies. 2834 // translating the clause bodies.
2814 HBasicBlock* curr_test_block = first_test_block; 2835 HBasicBlock* curr_test_block = first_test_block;
2815 HBasicBlock* fall_through_block = NULL; 2836 HBasicBlock* fall_through_block = NULL;
2837 HBasicBlock* fall_through_block_left = NULL;
2816 2838
2817 BreakAndContinueInfo break_info(stmt); 2839 BreakAndContinueInfo break_info(stmt);
2818 { BreakAndContinueScope push(&break_info, this); 2840 { BreakAndContinueScope push(&break_info, this);
2819 for (int i = 0; i < clause_count; ++i) { 2841 for (int i = 0; i < iterations; ++i) {
2820 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);
2821 2849
2822 // Identify the block where normal (non-fall-through) control flow 2850 // Identify the block where normal (non-fall-through) control flow
2823 // goes to. 2851 // goes to.
2824 HBasicBlock* normal_block = NULL; 2852 HBasicBlock* normal_block = NULL;
2825 if (clause->is_default()) { 2853 if (clause->is_default()) {
2826 if (last_block != NULL) { 2854 if (last_block != NULL) {
2827 normal_block = last_block; 2855 normal_block = last_block;
2828 last_block = NULL; // Cleared to indicate we've handled it. 2856 last_block = NULL; // Cleared to indicate we've handled it.
2829 } 2857 }
2830 } else if (!curr_test_block->end()->IsDeoptimize()) { 2858 } else if (!curr_test_block->end()->IsDeoptimize()) {
(...skipping 18 matching lines...) Expand all
2849 // (c) Reachable only normally. 2877 // (c) Reachable only normally.
2850 set_current_block(normal_block); 2878 set_current_block(normal_block);
2851 } else { 2879 } else {
2852 // (d) Reachable both ways. 2880 // (d) Reachable both ways.
2853 HBasicBlock* join = CreateJoin(fall_through_block, 2881 HBasicBlock* join = CreateJoin(fall_through_block,
2854 normal_block, 2882 normal_block,
2855 clause->EntryId()); 2883 clause->EntryId());
2856 set_current_block(join); 2884 set_current_block(join);
2857 } 2885 }
2858 2886
2859 CHECK_BAILOUT(VisitStatements(clause->statements())); 2887 CHECK_BAILOUT(VisitStatements(clause->statements()));
fschneider 2011/11/23 15:12:47 I think duplicating all clause bodies increases co
2860 fall_through_block = current_block(); 2888 fall_through_block = current_block();
2861 } 2889 }
2862 } 2890 }
2863 2891
2892 // Join tails of parallel branches
2893 fall_through_block = CreateJoin(fall_through_block, fall_through_block_left,
2894 stmt->ExitId());
2895
2864 // 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
2865 // it's already a join block. 2897 // it's already a join block.
2866 HBasicBlock* break_block = break_info.break_block(); 2898 HBasicBlock* break_block = break_info.break_block();
2867 if (break_block == NULL) { 2899 if (break_block == NULL) {
2868 set_current_block(CreateJoin(fall_through_block, 2900 set_current_block(CreateJoin(fall_through_block,
2869 last_block, 2901 last_block,
2870 stmt->ExitId())); 2902 stmt->ExitId()));
2871 } else { 2903 } else {
2872 if (fall_through_block != NULL) fall_through_block->Goto(break_block); 2904 if (fall_through_block != NULL) fall_through_block->Goto(break_block);
2873 if (last_block != NULL) last_block->Goto(break_block); 2905 if (last_block != NULL) last_block->Goto(break_block);
(...skipping 4278 matching lines...) Expand 10 before | Expand all | Expand 10 after
7152 } 7184 }
7153 } 7185 }
7154 7186
7155 #ifdef DEBUG 7187 #ifdef DEBUG
7156 if (graph_ != NULL) graph_->Verify(false); // No full verify. 7188 if (graph_ != NULL) graph_->Verify(false); // No full verify.
7157 if (allocator_ != NULL) allocator_->Verify(); 7189 if (allocator_ != NULL) allocator_->Verify();
7158 #endif 7190 #endif
7159 } 7191 }
7160 7192
7161 } } // namespace v8::internal 7193 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698