Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 } | 110 } |
| 111 // Improvements possible: | 111 // Improvements possible: |
| 112 // - eliminate redundant HCheckSmi, HCheckInstanceType instructions | 112 // - eliminate redundant HCheckSmi, HCheckInstanceType instructions |
| 113 // - track which values have been HCheckHeapObject'd | 113 // - track which values have been HCheckHeapObject'd |
| 114 } | 114 } |
| 115 | 115 |
| 116 return this; | 116 return this; |
| 117 } | 117 } |
| 118 | 118 |
| 119 // Global analysis: Copy state to successor block. | 119 // Global analysis: Copy state to successor block. |
| 120 HCheckTable* Copy(HBasicBlock* succ, Zone* zone) { | 120 HCheckTable* Copy(HBasicBlock* succ, HBasicBlock* from_block, Zone* zone) { |
| 121 HCheckTable* copy = new(phase_->zone()) HCheckTable(phase_); | 121 HCheckTable* copy = new(phase_->zone()) HCheckTable(phase_); |
| 122 for (int i = 0; i < size_; i++) { | 122 for (int i = 0; i < size_; i++) { |
| 123 HCheckTableEntry* old_entry = &entries_[i]; | 123 HCheckTableEntry* old_entry = &entries_[i]; |
| 124 HCheckTableEntry* new_entry = ©->entries_[i]; | 124 HCheckTableEntry* new_entry = ©->entries_[i]; |
| 125 // TODO(titzer): keep the check if this block dominates the successor? | 125 // TODO(titzer): keep the check if this block dominates the successor? |
| 126 new_entry->object_ = old_entry->object_; | 126 new_entry->object_ = old_entry->object_; |
| 127 new_entry->check_ = NULL; | 127 new_entry->check_ = NULL; |
| 128 new_entry->maps_ = old_entry->maps_->Copy(phase_->zone()); | 128 new_entry->maps_ = old_entry->maps_->Copy(phase_->zone()); |
| 129 } | 129 } |
| 130 copy->cursor_ = cursor_; | 130 copy->cursor_ = cursor_; |
| 131 copy->size_ = size_; | 131 copy->size_ = size_; |
| 132 | 132 |
| 133 copy->InheritPhiOperands(succ, this, from_block, COPY, zone); | |
| 134 | |
| 133 // Branch-sensitive analysis for certain comparisons may add more facts | 135 // Branch-sensitive analysis for certain comparisons may add more facts |
| 134 // to the state for the successor on the true branch. | 136 // to the state for the successor on the true branch. |
| 137 bool learned = false; | |
| 135 HControlInstruction* end = succ->predecessors()->at(0)->end(); | 138 HControlInstruction* end = succ->predecessors()->at(0)->end(); |
| 136 if (succ->predecessors()->length() == 1 && end->SuccessorAt(0) == succ) { | 139 if (succ->predecessors()->length() == 1 && end->SuccessorAt(0) == succ) { |
| 137 if (end->IsCompareMap()) { | 140 if (end->IsCompareMap()) { |
| 138 // Learn on the true branch of if(CompareMap(x)). | 141 // Learn on the true branch of if(CompareMap(x)). |
| 139 HCompareMap* cmp = HCompareMap::cast(end); | 142 HCompareMap* cmp = HCompareMap::cast(end); |
| 140 HValue* object = cmp->value()->ActualValue(); | 143 HValue* object = cmp->value()->ActualValue(); |
| 141 HCheckTableEntry* entry = copy->Find(object); | 144 HCheckTableEntry* entry = copy->Find(object); |
| 142 if (entry == NULL) { | 145 if (entry == NULL) { |
| 143 copy->Insert(object, cmp->map()); | 146 copy->Insert(object, cmp->map()); |
| 144 } else { | 147 } else { |
| 145 MapSet list = new(phase_->zone()) UniqueSet<Map>(); | 148 MapSet list = new(phase_->zone()) UniqueSet<Map>(); |
| 146 list->Add(cmp->map(), phase_->zone()); | 149 list->Add(cmp->map(), phase_->zone()); |
| 147 entry->maps_ = list; | 150 entry->maps_ = list; |
| 148 } | 151 } |
| 152 learned = true; | |
| 149 } else if (end->IsCompareObjectEqAndBranch()) { | 153 } else if (end->IsCompareObjectEqAndBranch()) { |
| 150 // Learn on the true branch of if(CmpObjectEq(x, y)). | 154 // Learn on the true branch of if(CmpObjectEq(x, y)). |
| 151 HCompareObjectEqAndBranch* cmp = | 155 HCompareObjectEqAndBranch* cmp = |
| 152 HCompareObjectEqAndBranch::cast(end); | 156 HCompareObjectEqAndBranch::cast(end); |
| 153 HValue* left = cmp->left()->ActualValue(); | 157 HValue* left = cmp->left()->ActualValue(); |
| 154 HValue* right = cmp->right()->ActualValue(); | 158 HValue* right = cmp->right()->ActualValue(); |
| 155 HCheckTableEntry* le = copy->Find(left); | 159 HCheckTableEntry* le = copy->Find(left); |
| 156 HCheckTableEntry* re = copy->Find(right); | 160 HCheckTableEntry* re = copy->Find(right); |
| 157 if (le == NULL) { | 161 if (le == NULL) { |
| 158 if (re != NULL) { | 162 if (re != NULL) { |
| 159 copy->Insert(left, NULL, re->maps_->Copy(zone)); | 163 copy->Insert(left, NULL, re->maps_->Copy(zone)); |
| 160 } | 164 } |
| 161 } else if (re == NULL) { | 165 } else if (re == NULL) { |
| 162 copy->Insert(right, NULL, le->maps_->Copy(zone)); | 166 copy->Insert(right, NULL, le->maps_->Copy(zone)); |
| 163 } else { | 167 } else { |
| 164 MapSet intersect = le->maps_->Intersect(re->maps_, zone); | 168 MapSet intersect = le->maps_->Intersect(re->maps_, zone); |
| 165 le->maps_ = intersect; | 169 le->maps_ = intersect; |
| 166 re->maps_ = intersect->Copy(zone); | 170 re->maps_ = intersect->Copy(zone); |
| 167 } | 171 } |
| 172 learned = true; | |
| 168 } | 173 } |
| 169 // Learning on false branches requires storing negative facts. | 174 // Learning on false branches requires storing negative facts. |
| 170 } | 175 } |
| 171 | 176 |
| 177 if (FLAG_trace_check_elimination) { | |
| 178 PrintF("B%d checkmaps-table %s from B%d:\n", | |
| 179 succ->block_id(), | |
| 180 learned ? "learned" : "copied", | |
| 181 from_block->block_id()); | |
| 182 copy->Print(); | |
| 183 } | |
| 184 | |
| 172 return copy; | 185 return copy; |
| 173 } | 186 } |
| 174 | 187 |
| 175 // Global analysis: Merge this state with the other incoming state. | 188 // Global analysis: Merge this state with the other incoming state. |
| 176 HCheckTable* Merge(HBasicBlock* succ, HCheckTable* that, Zone* zone) { | 189 void Merge(HBasicBlock* succ, HCheckTable* that, |
| 177 if (that->size_ == 0) { | 190 HBasicBlock* that_block, Zone* zone) { |
| 178 // If the other state is empty, simply reset. | 191 if (that_block->IsReachable()) { |
| 179 size_ = 0; | 192 if (that->size_ == 0) { |
| 180 cursor_ = 0; | 193 // If the other state is empty, simply reset. |
| 181 return this; | 194 size_ = 0; |
| 182 } | 195 cursor_ = 0; |
| 183 bool compact = false; | |
| 184 for (int i = 0; i < size_; i++) { | |
| 185 HCheckTableEntry* this_entry = &entries_[i]; | |
| 186 HCheckTableEntry* that_entry = that->Find(this_entry->object_); | |
| 187 if (that_entry == NULL) { | |
| 188 this_entry->object_ = NULL; | |
| 189 compact = true; | |
| 190 } else { | 196 } else { |
| 191 this_entry->maps_ = this_entry->maps_->Union( | 197 InheritPhiOperands(succ, that, that_block, MERGE, zone); |
| 192 that_entry->maps_, phase_->zone()); | 198 |
| 193 if (this_entry->check_ != that_entry->check_) this_entry->check_ = NULL; | 199 bool compact = false; |
| 194 ASSERT(this_entry->maps_->size() > 0); | 200 for (int i = 0; i < size_; i++) { |
| 201 HCheckTableEntry* this_entry = &entries_[i]; | |
| 202 HCheckTableEntry* that_entry = that->Find(this_entry->object_); | |
| 203 // Keep those phis in the table that dominate succ block. | |
| 204 if (that_entry == NULL) { | |
| 205 if (!this_entry->object_->IsPhi() || | |
| 206 !this_entry->object_->block()->EqualToOrDominates(succ)) { | |
| 207 this_entry->object_ = NULL; | |
| 208 compact = true; | |
| 209 } | |
| 210 } else { | |
| 211 this_entry->maps_ = | |
| 212 this_entry->maps_->Union(that_entry->maps_, phase_->zone()); | |
| 213 if (this_entry->check_ != that_entry->check_) { | |
| 214 this_entry->check_ = NULL; | |
| 215 } | |
| 216 ASSERT(this_entry->maps_->size() > 0); | |
| 217 } | |
| 218 } | |
| 219 if (compact) Compact(); | |
| 195 } | 220 } |
| 196 } | 221 } |
| 197 if (compact) Compact(); | 222 |
| 198 return this; | 223 if (FLAG_trace_check_elimination) { |
| 224 PrintF("B%d checkmaps-table merged with B%d table:\n", | |
| 225 succ->block_id(), that_block->block_id()); | |
| 226 Print(); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 enum InheritPhiOperandsMode { COPY, MERGE }; | |
| 231 void InheritPhiOperands(HBasicBlock* succ, HCheckTable* that, | |
| 232 HBasicBlock* that_block, InheritPhiOperandsMode mode, | |
| 233 Zone* zone) { | |
| 234 if (succ->predecessors()->length() == 1) return; | |
| 235 | |
| 236 int pred_index = succ->PredecessorIndexOf(that_block); | |
| 237 | |
| 238 for (int i = 0; i < that->size_; i++) { | |
| 239 HCheckTableEntry* that_entry = &that->entries_[i]; | |
| 240 if (that_entry->object_ != NULL) { | |
| 241 HPhi* phi = NULL; | |
| 242 for (int phi_index = 0; | |
| 243 phi_index < succ->phis()->length(); | |
| 244 ++phi_index) { | |
| 245 HPhi* tmp_phi = succ->phis()->at(phi_index); | |
| 246 if (tmp_phi->OperandAt(pred_index) == that_entry->object_) { | |
| 247 phi = tmp_phi; | |
| 248 break; | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 if (phi != NULL) { | |
| 253 // Found a |phi| with operand equal to |that_entry->object_|. | |
| 254 HCheckTableEntry* phi_entry = (mode == MERGE) ? Find(phi) : NULL; | |
| 255 if (phi_entry == NULL) { | |
| 256 // Create an entry for a phi in the table. | |
| 257 Insert(phi, NULL, that_entry->maps_); | |
| 258 } else { | |
| 259 phi_entry->maps_ = | |
| 260 phi_entry->maps_->Union(that_entry->maps_, phase_->zone()); | |
| 261 } | |
| 262 } | |
| 263 } | |
| 264 } | |
| 199 } | 265 } |
| 200 | 266 |
| 201 void ReduceCheckMaps(HCheckMaps* instr) { | 267 void ReduceCheckMaps(HCheckMaps* instr) { |
| 202 HValue* object = instr->value()->ActualValue(); | 268 HValue* object = instr->value()->ActualValue(); |
| 203 HCheckTableEntry* entry = Find(object); | 269 HCheckTableEntry* entry = Find(object); |
| 204 if (entry != NULL) { | 270 if (entry != NULL) { |
| 205 // entry found; | 271 // entry found; |
| 206 MapSet a = entry->maps_; | 272 MapSet a = entry->maps_; |
| 207 MapSet i = instr->map_set().Copy(phase_->zone()); | 273 MapSet i = instr->map_set().Copy(phase_->zone()); |
| 208 if (a->IsSubset(i)) { | 274 if (a->IsSubset(i)) { |
| 209 // The first check is more strict; the second is redundant. | 275 // The first check is more strict; the second is redundant. |
| 210 if (entry->check_ != NULL) { | 276 if (entry->check_ != NULL) { |
| 277 TRACE(("Replacing redundant CheckMaps #%d at B%d with #%d\n", | |
| 278 instr->id(), instr->block()->block_id(), entry->check_->id())); | |
| 211 instr->DeleteAndReplaceWith(entry->check_); | 279 instr->DeleteAndReplaceWith(entry->check_); |
| 212 INC_STAT(redundant_); | 280 INC_STAT(redundant_); |
| 213 } else { | 281 } else { |
| 214 instr->DeleteAndReplaceWith(instr->value()); | 282 TRACE(("Marking redundant CheckMaps #%d at B%d as dead\n", |
| 283 instr->id(), instr->block()->block_id())); | |
| 284 // Mark check as dead but leave it in the graph as a checkpoint for | |
| 285 // subsequent checks. | |
| 286 instr->SetFlag(HValue::kIsDead); | |
| 287 entry->check_ = instr; | |
| 215 INC_STAT(removed_); | 288 INC_STAT(removed_); |
| 216 } | 289 } |
| 217 return; | 290 return; |
| 218 } | 291 } |
| 219 i = i->Intersect(a, phase_->zone()); | 292 MapSet narrowed_maps = i->Intersect(a, phase_->zone()); |
|
titzer
2014/01/23 18:40:55
Would prefer the name "intersection" instead of na
Igor Sheludko
2014/01/29 12:27:13
Done.
| |
| 220 if (i->size() == 0) { | 293 if (narrowed_maps->size() == 0) { |
| 221 // Intersection is empty; probably megamorphic, which is likely to | 294 // Intersection is empty; probably megamorphic, which is likely to |
| 222 // deopt anyway, so just leave things as they are. | 295 // deopt anyway, so just leave things as they are. |
| 223 INC_STAT(empty_); | 296 INC_STAT(empty_); |
| 224 } else { | 297 } else { |
| 225 // TODO(titzer): replace the first check with a more strict check | 298 // Narrow set of maps in the second check and replace the first |
| 299 // check with a more strict one. | |
| 300 entry->maps_ = narrowed_maps; | |
| 301 if (narrowed_maps->size() != i->size()) { | |
| 302 // Narrow set of maps in the second check maps instruction. | |
| 303 HGraph* graph = instr->block()->graph(); | |
| 304 HCheckMaps* new_check_maps = | |
| 305 HCheckMaps::New(graph->zone(), NULL, instr->value(), | |
| 306 narrowed_maps, instr->typecheck()); | |
| 307 new_check_maps->InsertBefore(instr); | |
| 308 TRACE(("CheckMaps #%d for #%d narrowed to #%d:\n", | |
| 309 instr->id(), instr->value()->id(), new_check_maps->id())); | |
| 310 instr->DeleteAndReplaceWith(new_check_maps); | |
|
titzer
2014/01/23 18:40:55
I think you want to replace both entry->check_ (if
Igor Sheludko
2014/01/29 12:27:13
Done.
| |
| 311 entry->check_ = new_check_maps; | |
| 312 } else { | |
| 313 TRACE(("CheckMaps #%d for #%d narrowed:\n", | |
| 314 instr->id(), instr->value()->id())); | |
|
titzer
2014/01/23 18:40:55
In this case, you haven't actually changed anythin
Igor Sheludko
2014/01/29 12:27:13
Done.
| |
| 315 entry->check_ = instr; | |
| 316 } | |
| 317 if (FLAG_trace_check_elimination) { | |
| 318 Print(); | |
| 319 } | |
| 226 INC_STAT(narrowed_); | 320 INC_STAT(narrowed_); |
| 227 } | 321 } |
| 228 } else { | 322 } else { |
| 229 // No entry; insert a new one. | 323 // No entry; insert a new one. |
| 230 Insert(object, instr, instr->map_set().Copy(phase_->zone())); | 324 Insert(object, instr, instr->map_set().Copy(phase_->zone())); |
| 231 } | 325 } |
| 232 } | 326 } |
| 233 | 327 |
| 234 void ReduceCheckValue(HCheckValue* instr) { | 328 void ReduceCheckValue(HCheckValue* instr) { |
| 235 // Canonicalize HCheckValues; they might have their values load-eliminated. | 329 // Canonicalize HCheckValues; they might have their values load-eliminated. |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 552 PRINT_STAT(removed_cho); | 646 PRINT_STAT(removed_cho); |
| 553 PRINT_STAT(narrowed); | 647 PRINT_STAT(narrowed); |
| 554 PRINT_STAT(loads); | 648 PRINT_STAT(loads); |
| 555 PRINT_STAT(empty); | 649 PRINT_STAT(empty); |
| 556 PRINT_STAT(compares_true); | 650 PRINT_STAT(compares_true); |
| 557 PRINT_STAT(compares_false); | 651 PRINT_STAT(compares_false); |
| 558 PRINT_STAT(transitions); | 652 PRINT_STAT(transitions); |
| 559 } | 653 } |
| 560 | 654 |
| 561 } } // namespace v8::internal | 655 } } // namespace v8::internal |
| OLD | NEW |