OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 2740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2751 HRangeAnalysis rangeAnalysis(graph()); | 2751 HRangeAnalysis rangeAnalysis(graph()); |
2752 rangeAnalysis.Analyze(); | 2752 rangeAnalysis.Analyze(); |
2753 } | 2753 } |
2754 graph()->ComputeMinusZeroChecks(); | 2754 graph()->ComputeMinusZeroChecks(); |
2755 | 2755 |
2756 // Eliminate redundant stack checks on backwards branches. | 2756 // Eliminate redundant stack checks on backwards branches. |
2757 HStackCheckEliminator sce(graph()); | 2757 HStackCheckEliminator sce(graph()); |
2758 sce.Process(); | 2758 sce.Process(); |
2759 | 2759 |
2760 graph()->EliminateRedundantBoundsChecks(); | 2760 graph()->EliminateRedundantBoundsChecks(); |
| 2761 graph()->DehoistSimpleArrayIndexComputations(); |
2761 | 2762 |
2762 return graph(); | 2763 return graph(); |
2763 } | 2764 } |
2764 | 2765 |
2765 | 2766 |
2766 // We try to "factor up" HBoundsCheck instructions towards the root of the | 2767 // We try to "factor up" HBoundsCheck instructions towards the root of the |
2767 // dominator tree. | 2768 // dominator tree. |
2768 // For now we handle checks where the index is like "exp + int32value". | 2769 // For now we handle checks where the index is like "exp + int32value". |
2769 // If in the dominator tree we check "exp + v1" and later (dominated) | 2770 // If in the dominator tree we check "exp + v1" and later (dominated) |
2770 // "exp + v2", if v2 <= v1 we can safely remove the second check, and if | 2771 // "exp + v2", if v2 <= v1 we can safely remove the second check, and if |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3009 void HGraph::EliminateRedundantBoundsChecks(HBasicBlock* bb, | 3010 void HGraph::EliminateRedundantBoundsChecks(HBasicBlock* bb, |
3010 BoundsCheckTable* table) { | 3011 BoundsCheckTable* table) { |
3011 BoundsCheckBbData* bb_data_list = NULL; | 3012 BoundsCheckBbData* bb_data_list = NULL; |
3012 | 3013 |
3013 for (HInstruction* i = bb->first(); i != NULL; i = i->next()) { | 3014 for (HInstruction* i = bb->first(); i != NULL; i = i->next()) { |
3014 if (!i->IsBoundsCheck()) continue; | 3015 if (!i->IsBoundsCheck()) continue; |
3015 | 3016 |
3016 HBoundsCheck* check = HBoundsCheck::cast(i); | 3017 HBoundsCheck* check = HBoundsCheck::cast(i); |
3017 check->ReplaceAllUsesWith(check->index()); | 3018 check->ReplaceAllUsesWith(check->index()); |
3018 | 3019 |
3019 isolate()->counters()->array_bounds_checks_seen()->Increment(); | |
3020 if (!FLAG_array_bounds_checks_elimination) continue; | 3020 if (!FLAG_array_bounds_checks_elimination) continue; |
3021 | 3021 |
3022 int32_t offset; | 3022 int32_t offset; |
3023 BoundsCheckKey* key = | 3023 BoundsCheckKey* key = |
3024 BoundsCheckKey::Create(bb->zone(), check, &offset); | 3024 BoundsCheckKey::Create(bb->zone(), check, &offset); |
3025 BoundsCheckBbData** data_p = table->LookupOrInsert(key); | 3025 BoundsCheckBbData** data_p = table->LookupOrInsert(key); |
3026 BoundsCheckBbData* data = *data_p; | 3026 BoundsCheckBbData* data = *data_p; |
3027 if (data == NULL) { | 3027 if (data == NULL) { |
3028 bb_data_list = new(zone()) BoundsCheckBbData(key, | 3028 bb_data_list = new(zone()) BoundsCheckBbData(key, |
3029 offset, | 3029 offset, |
3030 offset, | 3030 offset, |
3031 bb, | 3031 bb, |
3032 check, | 3032 check, |
3033 bb_data_list, | 3033 bb_data_list, |
3034 NULL); | 3034 NULL); |
3035 *data_p = bb_data_list; | 3035 *data_p = bb_data_list; |
3036 } else if (data->OffsetIsCovered(offset)) { | 3036 } else if (data->OffsetIsCovered(offset)) { |
3037 check->DeleteAndReplaceWith(NULL); | 3037 check->DeleteAndReplaceWith(NULL); |
3038 isolate()->counters()->array_bounds_checks_removed()->Increment(); | |
3039 } else if (data->BasicBlock() == bb) { | 3038 } else if (data->BasicBlock() == bb) { |
3040 data->CoverCheck(check, offset); | 3039 data->CoverCheck(check, offset); |
3041 isolate()->counters()->array_bounds_checks_removed()->Increment(); | |
3042 } else { | 3040 } else { |
3043 int32_t new_lower_offset = offset < data->LowerOffset() | 3041 int32_t new_lower_offset = offset < data->LowerOffset() |
3044 ? offset | 3042 ? offset |
3045 : data->LowerOffset(); | 3043 : data->LowerOffset(); |
3046 int32_t new_upper_offset = offset > data->UpperOffset() | 3044 int32_t new_upper_offset = offset > data->UpperOffset() |
3047 ? offset | 3045 ? offset |
3048 : data->UpperOffset(); | 3046 : data->UpperOffset(); |
3049 bb_data_list = new(bb->zone()) BoundsCheckBbData(key, | 3047 bb_data_list = new(bb->zone()) BoundsCheckBbData(key, |
3050 new_lower_offset, | 3048 new_lower_offset, |
3051 new_upper_offset, | 3049 new_upper_offset, |
(...skipping 23 matching lines...) Expand all Loading... |
3075 | 3073 |
3076 | 3074 |
3077 void HGraph::EliminateRedundantBoundsChecks() { | 3075 void HGraph::EliminateRedundantBoundsChecks() { |
3078 HPhase phase("H_Eliminate bounds checks", this); | 3076 HPhase phase("H_Eliminate bounds checks", this); |
3079 AssertNoAllocation no_gc; | 3077 AssertNoAllocation no_gc; |
3080 BoundsCheckTable checks_table; | 3078 BoundsCheckTable checks_table; |
3081 EliminateRedundantBoundsChecks(entry_block(), &checks_table); | 3079 EliminateRedundantBoundsChecks(entry_block(), &checks_table); |
3082 } | 3080 } |
3083 | 3081 |
3084 | 3082 |
| 3083 static void DehoistArrayIndex(ArrayInstructionInterface* array_operation) { |
| 3084 HValue* index = array_operation->GetKey(); |
| 3085 |
| 3086 HConstant* constant; |
| 3087 HValue* subexpression; |
| 3088 int32_t sign; |
| 3089 if (index->IsAdd()) { |
| 3090 sign = 1; |
| 3091 HAdd* add = HAdd::cast(index); |
| 3092 if (add->left()->IsConstant()) { |
| 3093 subexpression = add->right(); |
| 3094 constant = HConstant::cast(add->left()); |
| 3095 } else if (add->right()->IsConstant()) { |
| 3096 subexpression = add->left(); |
| 3097 constant = HConstant::cast(add->right()); |
| 3098 } else { |
| 3099 return; |
| 3100 } |
| 3101 } else if (index->IsSub()) { |
| 3102 sign = -1; |
| 3103 HSub* sub = HSub::cast(index); |
| 3104 if (sub->left()->IsConstant()) { |
| 3105 subexpression = sub->right(); |
| 3106 constant = HConstant::cast(sub->left()); |
| 3107 } else if (sub->right()->IsConstant()) { |
| 3108 subexpression = sub->left(); |
| 3109 constant = HConstant::cast(sub->right()); |
| 3110 } return; |
| 3111 } else { |
| 3112 return; |
| 3113 } |
| 3114 |
| 3115 if (!constant->HasInteger32Value()) return; |
| 3116 int32_t value = constant->Integer32Value() * sign; |
| 3117 // We limit offset values to 30 bits because we want to avoid the risk of |
| 3118 // overflows when the offset is added to the object header size. |
| 3119 if (value >= 1 << 30 || value < 0) return; |
| 3120 array_operation->SetKey(subexpression); |
| 3121 if (index->HasNoUses()) { |
| 3122 index->DeleteAndReplaceWith(NULL); |
| 3123 } |
| 3124 ASSERT(value >= 0); |
| 3125 array_operation->SetIndexOffset(static_cast<uint32_t>(value)); |
| 3126 array_operation->SetDehoisted(true); |
| 3127 } |
| 3128 |
| 3129 |
| 3130 void HGraph::DehoistSimpleArrayIndexComputations() { |
| 3131 if (!FLAG_array_index_dehoisting) return; |
| 3132 |
| 3133 HPhase phase("H_Dehoist index computations", this); |
| 3134 for (int i = 0; i < blocks()->length(); ++i) { |
| 3135 for (HInstruction* instr = blocks()->at(i)->first(); |
| 3136 instr != NULL; |
| 3137 instr = instr->next()) { |
| 3138 ArrayInstructionInterface* array_instruction = NULL; |
| 3139 if (instr->IsLoadKeyedFastElement()) { |
| 3140 HLoadKeyedFastElement* op = HLoadKeyedFastElement::cast(instr); |
| 3141 array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3142 } else if (instr->IsLoadKeyedFastDoubleElement()) { |
| 3143 HLoadKeyedFastDoubleElement* op = |
| 3144 HLoadKeyedFastDoubleElement::cast(instr); |
| 3145 array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3146 } else if (instr->IsLoadKeyedSpecializedArrayElement()) { |
| 3147 HLoadKeyedSpecializedArrayElement* op = |
| 3148 HLoadKeyedSpecializedArrayElement::cast(instr); |
| 3149 array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3150 } else if (instr->IsStoreKeyedFastElement()) { |
| 3151 HStoreKeyedFastElement* op = HStoreKeyedFastElement::cast(instr); |
| 3152 array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3153 } else if (instr->IsStoreKeyedFastDoubleElement()) { |
| 3154 HStoreKeyedFastDoubleElement* op = |
| 3155 HStoreKeyedFastDoubleElement::cast(instr); |
| 3156 array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3157 } else if (instr->IsStoreKeyedSpecializedArrayElement()) { |
| 3158 HStoreKeyedSpecializedArrayElement* op = |
| 3159 HStoreKeyedSpecializedArrayElement::cast(instr); |
| 3160 array_instruction = static_cast<ArrayInstructionInterface*>(op); |
| 3161 } else { |
| 3162 continue; |
| 3163 } |
| 3164 DehoistArrayIndex(array_instruction); |
| 3165 } |
| 3166 } |
| 3167 } |
| 3168 |
| 3169 |
3085 HInstruction* HGraphBuilder::AddInstruction(HInstruction* instr) { | 3170 HInstruction* HGraphBuilder::AddInstruction(HInstruction* instr) { |
3086 ASSERT(current_block() != NULL); | 3171 ASSERT(current_block() != NULL); |
3087 current_block()->AddInstruction(instr); | 3172 current_block()->AddInstruction(instr); |
3088 return instr; | 3173 return instr; |
3089 } | 3174 } |
3090 | 3175 |
3091 | 3176 |
3092 void HGraphBuilder::AddSimulate(int ast_id) { | 3177 void HGraphBuilder::AddSimulate(int ast_id) { |
3093 ASSERT(current_block() != NULL); | 3178 ASSERT(current_block() != NULL); |
3094 current_block()->AddSimulate(ast_id); | 3179 current_block()->AddSimulate(ast_id); |
(...skipping 5765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8860 } | 8945 } |
8861 } | 8946 } |
8862 | 8947 |
8863 #ifdef DEBUG | 8948 #ifdef DEBUG |
8864 if (graph_ != NULL) graph_->Verify(false); // No full verify. | 8949 if (graph_ != NULL) graph_->Verify(false); // No full verify. |
8865 if (allocator_ != NULL) allocator_->Verify(); | 8950 if (allocator_ != NULL) allocator_->Verify(); |
8866 #endif | 8951 #endif |
8867 } | 8952 } |
8868 | 8953 |
8869 } } // namespace v8::internal | 8954 } } // namespace v8::internal |
OLD | NEW |