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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "hydrogen-check-elimination.h" | 28 #include "hydrogen-check-elimination.h" |
| 29 #include "hydrogen-alias-analysis.h" | 29 #include "hydrogen-alias-analysis.h" |
| 30 #include "hydrogen-flow-engine.h" | |
| 31 | |
| 32 #define GLOBAL 1 | |
| 33 | |
| 34 // Only collect stats in debug mode. | |
| 35 #if DEBUG | |
| 36 #define INC_STAT(x) phase_->x++ | |
| 37 #else | |
| 38 #define INC_STAT(x) | |
| 39 #endif | |
| 40 | |
| 41 // For code de-uglification. | |
| 42 #define TRACE(x) if (FLAG_trace_check_elimination) PrintF x | |
| 30 | 43 |
| 31 namespace v8 { | 44 namespace v8 { |
| 32 namespace internal { | 45 namespace internal { |
| 33 | 46 |
| 34 static const int kMaxTrackedObjects = 10; | 47 static const int kMaxTrackedObjects = 10; |
|
mvstanton
2013/10/31 11:16:13
Could this be public static inside the HCheckTable
| |
| 35 typedef UniqueSet<Map>* MapSet; | 48 typedef UniqueSet<Map>* MapSet; |
| 36 | 49 |
| 50 struct HCheckTableEntry { | |
| 51 HValue* object_; // The object being approximated. NULL => invalid entry. | |
| 52 HValue* check_; // The last check instruction. | |
| 53 MapSet maps_; // The set of known maps for the object. | |
| 54 }; | |
| 55 | |
| 56 | |
| 37 // The main datastructure used during check elimination, which stores a | 57 // The main datastructure used during check elimination, which stores a |
| 38 // set of known maps for each object. | 58 // set of known maps for each object. |
| 39 class HCheckTable { | 59 class HCheckTable : public ZoneObject { |
| 40 public: | 60 public: |
| 41 explicit HCheckTable(Zone* zone) : zone_(zone) { | 61 explicit HCheckTable(HCheckEliminationPhase* phase) |
| 42 Kill(); | 62 : phase_(phase), |
| 43 redundant_ = 0; | 63 cursor_(0), |
| 44 narrowed_ = 0; | 64 size_(0) { |
| 45 empty_ = 0; | 65 } |
| 46 removed_ = 0; | 66 |
| 47 compares_true_ = 0; | 67 // The main processing of instructions. |
| 48 compares_false_ = 0; | 68 HCheckTable* Process(HInstruction* instr, Zone* zone) { |
| 49 transitions_ = 0; | 69 switch (instr->opcode()) { |
| 50 loads_ = 0; | 70 case HValue::kCheckMaps: { |
| 71 ReduceCheckMaps(HCheckMaps::cast(instr)); | |
| 72 break; | |
| 73 } | |
| 74 case HValue::kCheckValue: { | |
| 75 ReduceCheckValue(HCheckValue::cast(instr)); | |
| 76 break; | |
| 77 } | |
| 78 case HValue::kLoadNamedField: { | |
| 79 ReduceLoadNamedField(HLoadNamedField::cast(instr)); | |
| 80 break; | |
| 81 } | |
| 82 case HValue::kStoreNamedField: { | |
| 83 ReduceStoreNamedField(HStoreNamedField::cast(instr)); | |
| 84 break; | |
| 85 } | |
| 86 case HValue::kCompareMap: { | |
| 87 ReduceCompareMap(HCompareMap::cast(instr)); | |
| 88 break; | |
| 89 } | |
| 90 case HValue::kTransitionElementsKind: { | |
| 91 ReduceTransitionElementsKind( | |
| 92 HTransitionElementsKind::cast(instr)); | |
| 93 break; | |
| 94 } | |
| 95 case HValue::kCheckMapValue: { | |
| 96 ReduceCheckMapValue(HCheckMapValue::cast(instr)); | |
| 97 break; | |
| 98 } | |
| 99 default: { | |
| 100 // If the instruction changes maps uncontrollably, kill the whole town. | |
|
mvstanton
2013/10/31 11:16:13
LOL! But a more sober comment for checking in, ple
| |
| 101 if (instr->CheckGVNFlag(kChangesMaps) || | |
| 102 instr->CheckGVNFlag(kChangesOsrEntries)) { | |
| 103 Kill(); | |
| 104 } | |
| 105 } | |
| 106 // Improvements possible: | |
| 107 // - eliminate HCheckSmi and HCheckHeapObject | |
| 108 } | |
| 109 | |
| 110 return this; | |
| 111 } | |
| 112 | |
| 113 // Global analysis: Copy state to successor block. | |
| 114 HCheckTable* Copy(HBasicBlock* succ, Zone* zone) { | |
| 115 HCheckTable* copy = new(phase_->zone()) HCheckTable(phase_); | |
| 116 for (int i = 0; i < size_; i++) { | |
| 117 HCheckTableEntry* old_entry = &entries_[i]; | |
| 118 HCheckTableEntry* new_entry = ©->entries_[i]; | |
| 119 // TODO(titzer): keep the check if this block dominates the successor? | |
| 120 new_entry->object_ = old_entry->object_; | |
|
mvstanton
2013/10/31 11:16:13
Can you explain here why you don't copy the checks
| |
| 121 new_entry->check_ = NULL; | |
| 122 new_entry->maps_ = old_entry->maps_->Copy(phase_->zone()); | |
| 123 } | |
| 124 return copy; | |
| 125 } | |
| 126 | |
| 127 // Global analysis: Merge this state with the other incoming state. | |
| 128 HCheckTable* Merge(HBasicBlock* succ, HCheckTable* that, Zone* zone) { | |
| 129 if (that->size_ == 0) { | |
| 130 // If the other state is empty, simply reset. | |
| 131 size_ = 0; | |
| 132 cursor_ = 0; | |
| 133 return this; | |
| 134 } | |
| 135 bool compact = false; | |
| 136 for (int i = 0; i < size_; i++) { | |
| 137 HCheckTableEntry* this_entry = &entries_[i]; | |
| 138 HCheckTableEntry* that_entry = that->Find(this_entry->object_); | |
| 139 if (that_entry == NULL) { | |
| 140 this_entry->object_ = NULL; | |
| 141 compact = true; | |
| 142 } else { | |
| 143 this_entry->maps_ = this_entry->maps_->Union( | |
| 144 that_entry->maps_, phase_->zone()); | |
| 145 if (this_entry->check_ != that_entry->check_) this_entry->check_ = NULL; | |
| 146 ASSERT(this_entry->maps_->size() > 0); | |
| 147 } | |
| 148 } | |
| 149 if (compact) Compact(); | |
| 150 return this; | |
| 51 } | 151 } |
| 52 | 152 |
| 53 void ReduceCheckMaps(HCheckMaps* instr) { | 153 void ReduceCheckMaps(HCheckMaps* instr) { |
| 54 HValue* object = instr->value()->ActualValue(); | 154 HValue* object = instr->value()->ActualValue(); |
| 55 int index = Find(object); | 155 HCheckTableEntry* entry = Find(object); |
| 56 if (index >= 0) { | 156 if (entry != NULL) { |
| 57 // entry found; | 157 // entry found; |
| 58 MapSet a = known_maps_[index]; | 158 MapSet a = entry->maps_; |
| 59 MapSet i = instr->map_set().Copy(zone_); | 159 MapSet i = instr->map_set().Copy(phase_->zone()); |
| 60 if (a->IsSubset(i)) { | 160 if (a->IsSubset(i)) { |
| 61 // The first check is more strict; the second is redundant. | 161 // The first check is more strict; the second is redundant. |
| 62 if (checks_[index] != NULL) { | 162 if (entry->check_ != NULL) { |
| 63 instr->DeleteAndReplaceWith(checks_[index]); | 163 instr->DeleteAndReplaceWith(entry->check_); |
| 64 redundant_++; | 164 INC_STAT(redundant_); |
| 65 } else { | 165 } else { |
| 66 instr->DeleteAndReplaceWith(instr->value()); | 166 instr->DeleteAndReplaceWith(instr->value()); |
| 67 removed_++; | 167 INC_STAT(removed_); |
| 68 } | 168 } |
| 69 return; | 169 return; |
| 70 } | 170 } |
| 71 i = i->Intersect(a, zone_); | 171 i = i->Intersect(a, phase_->zone()); |
| 72 if (i->size() == 0) { | 172 if (i->size() == 0) { |
| 73 // Intersection is empty; probably megamorphic, which is likely to | 173 // Intersection is empty; probably megamorphic, which is likely to |
| 74 // deopt anyway, so just leave things as they are. | 174 // deopt anyway, so just leave things as they are. |
| 75 empty_++; | 175 INC_STAT(empty_); |
| 76 } else { | 176 } else { |
| 77 // TODO(titzer): replace the first check with a more strict check. | 177 // TODO(titzer): replace the first check with a more strict check |
| 78 narrowed_++; | 178 INC_STAT(narrowed_); |
| 79 } | 179 } |
| 80 } else { | 180 } else { |
| 81 // No entry; insert a new one. | 181 // No entry; insert a new one. |
| 82 Insert(object, instr, instr->map_set().Copy(zone_)); | 182 Insert(object, instr, instr->map_set().Copy(phase_->zone())); |
| 83 } | 183 } |
| 84 } | 184 } |
| 85 | 185 |
| 86 void ReduceCheckValue(HCheckValue* instr) { | 186 void ReduceCheckValue(HCheckValue* instr) { |
| 87 // Canonicalize HCheckValues; they might have their values load-eliminated. | 187 // Canonicalize HCheckValues; they might have their values load-eliminated. |
| 88 HValue* value = instr->Canonicalize(); | 188 HValue* value = instr->Canonicalize(); |
| 89 if (value == NULL) { | 189 if (value == NULL) { |
| 90 instr->DeleteAndReplaceWith(instr->value()); | 190 instr->DeleteAndReplaceWith(instr->value()); |
| 91 removed_++; | 191 INC_STAT(removed_); |
| 92 } else if (value != instr) { | 192 } else if (value != instr) { |
| 93 instr->DeleteAndReplaceWith(value); | 193 instr->DeleteAndReplaceWith(value); |
| 94 redundant_++; | 194 INC_STAT(redundant_); |
| 95 } | 195 } |
| 96 } | 196 } |
| 97 | 197 |
| 98 void ReduceLoadNamedField(HLoadNamedField* instr) { | 198 void ReduceLoadNamedField(HLoadNamedField* instr) { |
| 99 // Reduce a load of the map field when it is known to be a constant. | 199 // Reduce a load of the map field when it is known to be a constant. |
| 100 if (!IsMapAccess(instr->access())) return; | 200 if (!IsMapAccess(instr->access())) return; |
| 101 | 201 |
| 102 HValue* object = instr->object()->ActualValue(); | 202 HValue* object = instr->object()->ActualValue(); |
| 103 MapSet maps = FindMaps(object); | 203 MapSet maps = FindMaps(object); |
| 104 if (maps == NULL || maps->size() != 1) return; // Not a constant. | 204 if (maps == NULL || maps->size() != 1) return; // Not a constant. |
| 105 | 205 |
| 106 Unique<Map> map = maps->at(0); | 206 Unique<Map> map = maps->at(0); |
| 107 HConstant* constant = HConstant::CreateAndInsertBefore( | 207 HConstant* constant = HConstant::CreateAndInsertBefore( |
| 108 instr->block()->graph()->zone(), map, true, instr); | 208 instr->block()->graph()->zone(), map, true, instr); |
| 109 instr->DeleteAndReplaceWith(constant); | 209 instr->DeleteAndReplaceWith(constant); |
| 110 loads_++; | 210 INC_STAT(loads_); |
| 111 } | 211 } |
| 112 | 212 |
| 113 void ReduceCheckMapValue(HCheckMapValue* instr) { | 213 void ReduceCheckMapValue(HCheckMapValue* instr) { |
| 114 if (!instr->map()->IsConstant()) return; // Nothing to learn. | 214 if (!instr->map()->IsConstant()) return; // Nothing to learn. |
| 115 | 215 |
| 116 HValue* object = instr->value()->ActualValue(); | 216 HValue* object = instr->value()->ActualValue(); |
| 117 // Match a HCheckMapValue(object, HConstant(map)) | 217 // Match a HCheckMapValue(object, HConstant(map)) |
| 118 Unique<Map> map = MapConstant(instr->map()); | 218 Unique<Map> map = MapConstant(instr->map()); |
| 119 MapSet maps = FindMaps(object); | 219 MapSet maps = FindMaps(object); |
| 120 if (maps != NULL) { | 220 if (maps != NULL) { |
| 121 if (maps->Contains(map)) { | 221 if (maps->Contains(map)) { |
| 122 if (maps->size() == 1) { | 222 if (maps->size() == 1) { |
| 123 // Object is known to have exactly this map. | 223 // Object is known to have exactly this map. |
| 124 instr->DeleteAndReplaceWith(NULL); | 224 instr->DeleteAndReplaceWith(NULL); |
| 125 removed_++; | 225 INC_STAT(removed_); |
| 126 } else { | 226 } else { |
| 127 // Only one map survives the check. | 227 // Only one map survives the check. |
| 128 maps->Clear(); | 228 maps->Clear(); |
| 129 maps->Add(map, zone_); | 229 maps->Add(map, phase_->zone()); |
| 130 } | 230 } |
| 131 } | 231 } |
| 132 } else { | 232 } else { |
| 133 // No prior information. | 233 // No prior information. |
| 134 Insert(object, map); | 234 Insert(object, map); |
| 135 } | 235 } |
| 136 } | 236 } |
| 137 | 237 |
| 138 void ReduceStoreNamedField(HStoreNamedField* instr) { | 238 void ReduceStoreNamedField(HStoreNamedField* instr) { |
| 139 HValue* object = instr->object()->ActualValue(); | 239 HValue* object = instr->object()->ActualValue(); |
| 140 if (instr->has_transition()) { | 240 if (instr->has_transition()) { |
| 141 // This store transitions the object to a new map. | 241 // This store transitions the object to a new map. |
| 142 Kill(object); | 242 Kill(object); |
| 143 Insert(object, MapConstant(instr->transition())); | 243 Insert(object, MapConstant(instr->transition())); |
| 144 } else if (IsMapAccess(instr->access())) { | 244 } else if (IsMapAccess(instr->access())) { |
| 145 // This is a store directly to the map field of the object. | 245 // This is a store directly to the map field of the object. |
| 146 Kill(object); | 246 Kill(object); |
| 147 if (!instr->value()->IsConstant()) return; | 247 if (!instr->value()->IsConstant()) return; |
| 148 Insert(object, MapConstant(instr->value())); | 248 Insert(object, MapConstant(instr->value())); |
| 149 } else if (instr->CheckGVNFlag(kChangesMaps)) { | 249 } else if (instr->CheckGVNFlag(kChangesMaps)) { |
| 150 // This store indirectly changes the map of the object. | 250 // This store indirectly changes the map of the object. |
| 151 Kill(instr->object()); | 251 Kill(instr->object()); |
|
mvstanton
2013/10/31 11:16:13
I don't like the combination of action (Kill()) an
| |
| 152 UNREACHABLE(); | 252 UNREACHABLE(); |
| 153 } | 253 } |
| 154 } | 254 } |
| 155 | 255 |
| 156 void ReduceCompareMap(HCompareMap* instr) { | 256 void ReduceCompareMap(HCompareMap* instr) { |
| 157 MapSet maps = FindMaps(instr->value()->ActualValue()); | 257 MapSet maps = FindMaps(instr->value()->ActualValue()); |
| 158 if (maps == NULL) return; | 258 if (maps == NULL) return; |
| 159 if (maps->Contains(instr->map())) { | 259 if (maps->Contains(instr->map())) { |
| 160 // TODO(titzer): replace with goto true branch | 260 // TODO(titzer): replace with goto true branch |
| 161 if (maps->size() == 1) compares_true_++; | 261 if (maps->size() == 1) INC_STAT(compares_true_); |
|
mvstanton
2013/10/31 11:16:13
It looks like this function just serves to update
| |
| 162 } else { | 262 } else { |
| 163 // TODO(titzer): replace with goto false branch | 263 // TODO(titzer): replace with goto false branch |
| 164 compares_false_++; | 264 INC_STAT(compares_false_); |
| 165 } | 265 } |
| 166 } | 266 } |
| 167 | 267 |
| 168 void ReduceTransitionElementsKind(HTransitionElementsKind* instr) { | 268 void ReduceTransitionElementsKind(HTransitionElementsKind* instr) { |
| 169 MapSet maps = FindMaps(instr->object()->ActualValue()); | 269 MapSet maps = FindMaps(instr->object()->ActualValue()); |
| 170 // Can only learn more about an object that already has a known set of maps. | 270 // Can only learn more about an object that already has a known set of maps. |
| 171 if (maps == NULL) return; | 271 if (maps == NULL) return; |
| 172 if (maps->Contains(instr->original_map())) { | 272 if (maps->Contains(instr->original_map())) { |
| 173 // If the object has the original map, it will be transitioned. | 273 // If the object has the original map, it will be transitioned. |
| 174 maps->Remove(instr->original_map()); | 274 maps->Remove(instr->original_map()); |
| 175 maps->Add(instr->transitioned_map(), zone_); | 275 maps->Add(instr->transitioned_map(), phase_->zone()); |
| 176 } else { | 276 } else { |
| 177 // Object does not have the given map, thus the transition is redundant. | 277 // Object does not have the given map, thus the transition is redundant. |
| 178 instr->DeleteAndReplaceWith(instr->object()); | 278 instr->DeleteAndReplaceWith(instr->object()); |
| 179 transitions_++; | 279 INC_STAT(transitions_); |
| 180 } | 280 } |
| 181 } | 281 } |
| 182 | 282 |
| 183 // Kill everything in the table. | 283 // Kill everything in the table. |
| 184 void Kill() { | 284 void Kill() { |
| 185 memset(objects_, 0, sizeof(objects_)); | 285 size_ = 0; |
| 286 cursor_ = 0; | |
| 186 } | 287 } |
| 187 | 288 |
| 188 // Kill everything in the table that may alias {object}. | 289 // Kill everything in the table that may alias {object}. |
| 189 void Kill(HValue* object) { | 290 void Kill(HValue* object) { |
| 190 for (int i = 0; i < kMaxTrackedObjects; i++) { | 291 bool compact = false; |
| 191 if (objects_[i] == NULL) continue; | 292 for (int i = 0; i < size_; i++) { |
| 192 if (aliasing_.MayAlias(objects_[i], object)) objects_[i] = NULL; | 293 HCheckTableEntry* entry = &entries_[i]; |
| 294 ASSERT(entry->object_ != NULL); | |
| 295 if (phase_->aliasing_->MayAlias(entry->object_, object)) { | |
| 296 entry->object_ = NULL; | |
| 297 compact = true; | |
| 298 } | |
| 193 } | 299 } |
| 194 ASSERT(Find(object) < 0); | 300 if (compact) Compact(); |
| 301 ASSERT(Find(object) == NULL); | |
| 302 } | |
| 303 | |
| 304 void Compact() { | |
| 305 // First, compact the array in place. | |
| 306 int max = size_, dest = 0, old_cursor = cursor_; | |
| 307 for (int i = 0; i < max; i++) { | |
| 308 if (entries_[i].object_ != NULL) { | |
| 309 if (dest != i) entries_[dest] = entries_[i]; | |
| 310 dest++; | |
| 311 } else { | |
| 312 if (i < old_cursor) cursor_--; | |
| 313 size_--; | |
| 314 } | |
| 315 } | |
| 316 ASSERT(size_ == dest); | |
| 317 ASSERT(cursor_ <= size_); | |
| 318 | |
| 319 // Preserve the age of the entries by moving the older entries to the end. | |
| 320 if (cursor_ == size_) return; // Cursor already points at end. | |
| 321 if (cursor_ != 0) { | |
| 322 // | L = oldest | R = newest | | | |
| 323 // ^ cursor ^ size ^ MAX | |
| 324 HCheckTableEntry tmp_entries[kMaxTrackedObjects]; | |
| 325 int L = cursor_; | |
| 326 int R = size_ - cursor_; | |
| 327 | |
| 328 OS::MemMove(&tmp_entries[0], &entries_[0], L * sizeof(HCheckTableEntry)); | |
| 329 OS::MemMove(&entries_[0], &entries_[L], R * sizeof(HCheckTableEntry)); | |
| 330 OS::MemMove(&entries_[R], &tmp_entries[0], L * sizeof(HCheckTableEntry)); | |
| 331 } | |
| 332 | |
| 333 cursor_ = size_; // Move cursor to end. | |
|
mvstanton
2013/10/31 11:16:13
Do performance concerns justify the complexity? Ho
| |
| 195 } | 334 } |
| 196 | 335 |
| 197 void Print() { | 336 void Print() { |
| 198 for (int i = 0; i < kMaxTrackedObjects; i++) { | 337 for (int i = 0; i < size_; i++) { |
| 199 if (objects_[i] == NULL) continue; | 338 HCheckTableEntry* entry = &entries_[i]; |
| 200 PrintF(" checkmaps-table @%d: object #%d ", i, objects_[i]->id()); | 339 ASSERT(entry->object_ != NULL); |
| 201 if (checks_[i] != NULL) { | 340 PrintF(" checkmaps-table @%d: object #%d ", i, entry->object_->id()); |
| 202 PrintF("check #%d ", checks_[i]->id()); | 341 if (entry->check_ != NULL) { |
| 342 PrintF("check #%d ", entry->check_->id()); | |
| 203 } | 343 } |
| 204 MapSet list = known_maps_[i]; | 344 MapSet list = entry->maps_; |
| 205 PrintF("%d maps { ", list->size()); | 345 PrintF("%d maps { ", list->size()); |
| 206 for (int j = 0; j < list->size(); j++) { | 346 for (int j = 0; j < list->size(); j++) { |
| 207 if (j > 0) PrintF(", "); | 347 if (j > 0) PrintF(", "); |
| 208 PrintF("%" V8PRIxPTR, list->at(j).Hashcode()); | 348 PrintF("%" V8PRIxPTR, list->at(j).Hashcode()); |
| 209 } | 349 } |
| 210 PrintF(" }\n"); | 350 PrintF(" }\n"); |
| 211 } | 351 } |
| 212 } | 352 } |
| 213 | 353 |
| 214 void PrintStats() { | |
| 215 if (redundant_ > 0) PrintF(" redundant = %2d\n", redundant_); | |
| 216 if (removed_ > 0) PrintF(" removed = %2d\n", removed_); | |
| 217 if (narrowed_ > 0) PrintF(" narrowed = %2d\n", narrowed_); | |
| 218 if (loads_ > 0) PrintF(" loads = %2d\n", loads_); | |
| 219 if (empty_ > 0) PrintF(" empty = %2d\n", empty_); | |
| 220 if (compares_true_ > 0) PrintF(" cmp_true = %2d\n", compares_true_); | |
| 221 if (compares_false_ > 0) PrintF(" cmp_false = %2d\n", compares_false_); | |
| 222 if (transitions_ > 0) PrintF(" transitions = %2d\n", transitions_); | |
| 223 } | |
| 224 | |
| 225 private: | 354 private: |
| 226 int Find(HValue* object) { | 355 HCheckTableEntry* Find(HValue* object) { |
| 227 for (int i = 0; i < kMaxTrackedObjects; i++) { | 356 for (int i = size_ - 1; i >= 0; i--) { |
| 228 if (objects_[i] == NULL) continue; | 357 // Search from most-recently-inserted to least-recently-inserted. |
| 229 if (aliasing_.MustAlias(objects_[i], object)) return i; | 358 HCheckTableEntry* entry = &entries_[i]; |
| 359 ASSERT(entry->object_ != NULL); | |
| 360 if (phase_->aliasing_->MustAlias(entry->object_, object)) return entry; | |
| 230 } | 361 } |
| 231 return -1; | 362 return NULL; |
| 232 } | 363 } |
| 233 | 364 |
| 234 MapSet FindMaps(HValue* object) { | 365 MapSet FindMaps(HValue* object) { |
| 235 int index = Find(object); | 366 HCheckTableEntry* entry = Find(object); |
| 236 return index < 0 ? NULL : known_maps_[index]; | 367 return entry == NULL ? NULL : entry->maps_; |
| 237 } | 368 } |
| 238 | 369 |
| 239 void Insert(HValue* object, Unique<Map> map) { | 370 void Insert(HValue* object, Unique<Map> map) { |
| 240 MapSet list = new(zone_) UniqueSet<Map>(); | 371 MapSet list = new(phase_->zone()) UniqueSet<Map>(); |
| 241 list->Add(map, zone_); | 372 list->Add(map, phase_->zone()); |
| 242 Insert(object, NULL, list); | 373 Insert(object, NULL, list); |
| 243 } | 374 } |
| 244 | 375 |
| 245 void Insert(HValue* object, HCheckMaps* check, MapSet maps) { | 376 void Insert(HValue* object, HCheckMaps* check, MapSet maps) { |
| 246 for (int i = 0; i < kMaxTrackedObjects; i++) { | 377 HCheckTableEntry* entry = &entries_[cursor_++]; |
| 247 // TODO(titzer): drop old entries instead of disallowing new ones. | 378 entry->object_ = object; |
| 248 if (objects_[i] == NULL) { | 379 entry->check_ = check; |
| 249 objects_[i] = object; | 380 entry->maps_ = maps; |
| 250 checks_[i] = check; | 381 // If the table becomes full, wrap around and overwrite older entries. |
| 251 known_maps_[i] = maps; | 382 if (cursor_ == kMaxTrackedObjects) cursor_ = 0; |
| 252 return; | 383 if (size_ < kMaxTrackedObjects) size_++; |
| 253 } | |
| 254 } | |
| 255 } | 384 } |
| 256 | 385 |
| 257 bool IsMapAccess(HObjectAccess access) { | 386 bool IsMapAccess(HObjectAccess access) { |
| 258 return access.IsInobject() && access.offset() == JSObject::kMapOffset; | 387 return access.IsInobject() && access.offset() == JSObject::kMapOffset; |
| 259 } | 388 } |
| 260 | 389 |
| 261 Unique<Map> MapConstant(HValue* value) { | 390 Unique<Map> MapConstant(HValue* value) { |
| 262 return Unique<Map>::cast(HConstant::cast(value)->GetUnique()); | 391 return Unique<Map>::cast(HConstant::cast(value)->GetUnique()); |
| 263 } | 392 } |
| 264 | 393 |
| 265 Zone* zone_; | 394 friend class HCheckMapsEffects; |
| 266 HValue* objects_[kMaxTrackedObjects]; | 395 |
| 267 HValue* checks_[kMaxTrackedObjects]; | 396 HCheckEliminationPhase* phase_; |
| 268 MapSet known_maps_[kMaxTrackedObjects]; | 397 HCheckTableEntry entries_[kMaxTrackedObjects]; |
| 269 HAliasAnalyzer aliasing_; | 398 int16_t cursor_; // Must be <= kMaxTrackedObjects |
|
mvstanton
2013/10/31 11:16:13
STATIC_ASSERT on sizeof cursor_ and size_ that the
mvstanton
2013/11/28 07:14:47
Ben asked: "where should I put this?"
I think rig
| |
| 270 int redundant_; | 399 int16_t size_; // Must be <= kMaxTrackedObjects |
| 271 int removed_; | |
| 272 int narrowed_; | |
| 273 int loads_; | |
| 274 int empty_; | |
| 275 int compares_true_; | |
| 276 int compares_false_; | |
| 277 int transitions_; | |
| 278 }; | 400 }; |
| 279 | 401 |
| 280 | 402 |
| 403 // Collects instructions that can cause effects that invalidate information | |
| 404 // needed for check elimination. | |
| 405 class HCheckMapsEffects : public ZoneObject { | |
| 406 public: | |
| 407 explicit HCheckMapsEffects(Zone* zone) | |
| 408 : maps_stored_(false), | |
| 409 stores_(5, zone) { } | |
| 410 | |
| 411 inline bool Disabled() { | |
| 412 return false; // Effects are _not_ disabled. | |
| 413 } | |
| 414 | |
| 415 // Process a possibly side-effecting instruction. | |
| 416 void Process(HInstruction* instr, Zone* zone) { | |
| 417 switch (instr->opcode()) { | |
| 418 case HValue::kStoreNamedField: { | |
| 419 stores_.Add(HStoreNamedField::cast(instr), zone); | |
| 420 break; | |
| 421 } | |
| 422 case HValue::kOsrEntry: { | |
| 423 // Kill everything. Loads must not be hoisted past the OSR entry. | |
| 424 maps_stored_ = true; | |
| 425 } | |
| 426 default: { | |
| 427 maps_stored_ |= (instr->CheckGVNFlag(kChangesMaps) | | |
| 428 instr->CheckGVNFlag(kChangesElementsKind)); | |
| 429 } | |
| 430 } | |
| 431 } | |
| 432 | |
| 433 // Apply these effects to the given check elimination table. | |
| 434 void Apply(HCheckTable* table) { | |
| 435 if (maps_stored_) { | |
| 436 // Uncontrollable map modifications; kill everything. | |
| 437 table->Kill(); | |
| 438 return; | |
| 439 } | |
| 440 | |
| 441 // Kill maps for each store contained in these effects. | |
| 442 for (int i = 0; i < stores_.length(); i++) { | |
| 443 HStoreNamedField* s = stores_[i]; | |
| 444 if (table->IsMapAccess(s->access()) || s->has_transition()) { | |
| 445 table->Kill(s->object()->ActualValue()); | |
| 446 } | |
| 447 } | |
| 448 } | |
| 449 | |
| 450 // Union these effects with the other effects. | |
| 451 void Union(HCheckMapsEffects* that, Zone* zone) { | |
| 452 maps_stored_ |= that->maps_stored_; | |
| 453 for (int i = 0; i < that->stores_.length(); i++) { | |
| 454 stores_.Add(that->stores_[i], zone); | |
| 455 } | |
| 456 } | |
| 457 | |
| 458 private: | |
| 459 bool maps_stored_ : 1; | |
| 460 ZoneList<HStoreNamedField*> stores_; | |
| 461 }; | |
| 462 | |
| 463 | |
| 464 // The main routine of the analysis phase. Use the HFlowEngine for either a | |
| 465 // local or a global analysis. | |
| 281 void HCheckEliminationPhase::Run() { | 466 void HCheckEliminationPhase::Run() { |
| 282 for (int i = 0; i < graph()->blocks()->length(); i++) { | 467 HFlowEngine<HCheckTable, HCheckMapsEffects> engine(graph(), zone()); |
| 283 EliminateLocalChecks(graph()->blocks()->at(i)); | 468 HCheckTable* table = new(zone()) HCheckTable(this); |
| 469 | |
| 470 if (GLOBAL) { | |
| 471 // Perform a global analysis. | |
| 472 engine.AnalyzeDominatedBlocks(graph()->blocks()->at(0), table); | |
| 473 } else { | |
| 474 // Perform only local analysis. | |
| 475 for (int i = 0; i < graph()->blocks()->length(); i++) { | |
| 476 table->Kill(); | |
| 477 engine.AnalyzeOneBlock(graph()->blocks()->at(i), table); | |
| 478 } | |
| 284 } | 479 } |
| 480 | |
| 481 if (FLAG_trace_check_elimination) PrintStats(); | |
| 285 } | 482 } |
| 286 | 483 |
| 287 | 484 |
| 288 // For code de-uglification. | 485 // Are we eliminated yet? |
| 289 #define TRACE(x) if (FLAG_trace_check_elimination) PrintF x | 486 void HCheckEliminationPhase::PrintStats() { |
| 290 | 487 #if DEBUG |
|
mvstanton
2013/10/31 11:16:13
Same here with a defined flag instead of DEBUG. I
| |
| 291 | 488 if (redundant_ > 0) PrintF(" redundant = %2d\n", redundant_); |
| 292 // Eliminate checks local to a block. | 489 if (removed_ > 0) PrintF(" removed = %2d\n", removed_); |
| 293 void HCheckEliminationPhase::EliminateLocalChecks(HBasicBlock* block) { | 490 if (narrowed_ > 0) PrintF(" narrowed = %2d\n", narrowed_); |
| 294 HCheckTable table(zone()); | 491 if (loads_ > 0) PrintF(" loads = %2d\n", loads_); |
| 295 TRACE(("-- check-elim B%d ------------------------------------------------\n", | 492 if (empty_ > 0) PrintF(" empty = %2d\n", empty_); |
| 296 block->block_id())); | 493 if (compares_true_ > 0) PrintF(" cmp_true = %2d\n", compares_true_); |
| 297 | 494 if (compares_false_ > 0) PrintF(" cmp_false = %2d\n", compares_false_); |
| 298 for (HInstructionIterator it(block); !it.Done(); it.Advance()) { | 495 if (transitions_ > 0) PrintF(" transitions = %2d\n", transitions_); |
| 299 bool changed = false; | 496 #endif |
| 300 HInstruction* instr = it.Current(); | |
| 301 | |
| 302 switch (instr->opcode()) { | |
| 303 case HValue::kCheckMaps: { | |
| 304 table.ReduceCheckMaps(HCheckMaps::cast(instr)); | |
| 305 changed = true; | |
| 306 break; | |
| 307 } | |
| 308 case HValue::kCheckValue: { | |
| 309 table.ReduceCheckValue(HCheckValue::cast(instr)); | |
| 310 changed = true; | |
| 311 break; | |
| 312 } | |
| 313 case HValue::kLoadNamedField: { | |
| 314 table.ReduceLoadNamedField(HLoadNamedField::cast(instr)); | |
| 315 changed = true; | |
| 316 break; | |
| 317 } | |
| 318 case HValue::kStoreNamedField: { | |
| 319 table.ReduceStoreNamedField(HStoreNamedField::cast(instr)); | |
| 320 changed = true; | |
| 321 break; | |
| 322 } | |
| 323 case HValue::kCompareMap: { | |
| 324 table.ReduceCompareMap(HCompareMap::cast(instr)); | |
| 325 changed = true; | |
| 326 break; | |
| 327 } | |
| 328 case HValue::kTransitionElementsKind: { | |
| 329 table.ReduceTransitionElementsKind( | |
| 330 HTransitionElementsKind::cast(instr)); | |
| 331 changed = true; | |
| 332 break; | |
| 333 } | |
| 334 case HValue::kCheckMapValue: { | |
| 335 table.ReduceCheckMapValue(HCheckMapValue::cast(instr)); | |
| 336 changed = true; | |
| 337 break; | |
| 338 } | |
| 339 default: { | |
| 340 // If the instruction changes maps uncontrollably, kill the whole town. | |
| 341 if (instr->CheckGVNFlag(kChangesMaps)) { | |
| 342 table.Kill(); | |
| 343 changed = true; | |
| 344 } | |
| 345 } | |
| 346 // Improvements possible: | |
| 347 // - eliminate HCheckSmi and HCheckHeapObject | |
| 348 } | |
| 349 | |
| 350 if (changed && FLAG_trace_check_elimination) table.Print(); | |
| 351 } | |
| 352 | |
| 353 if (FLAG_trace_check_elimination) table.PrintStats(); | |
| 354 } | 497 } |
| 355 | 498 |
| 356 | |
| 357 } } // namespace v8::internal | 499 } } // namespace v8::internal |
| OLD | NEW |