blob: 634d75d8620f5ec866048f9794b8e2ff87a5a0a2 [file] [log] [blame]
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28#include "hydrogen-alias-analysis.h"
29#include "hydrogen-load-elimination.h"
30#include "hydrogen-instructions.h"
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +000031#include "hydrogen-flow-engine.h"
machenbach@chromium.org528ce022013-09-23 14:09:36 +000032
33namespace v8 {
34namespace internal {
35
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +000036#define GLOBAL true
37#define TRACE(x) if (FLAG_trace_load_elimination) PrintF x
38
machenbach@chromium.org528ce022013-09-23 14:09:36 +000039static const int kMaxTrackedFields = 16;
40static const int kMaxTrackedObjects = 5;
41
42// An element in the field approximation list.
43class HFieldApproximation : public ZoneObject {
44 public: // Just a data blob.
45 HValue* object_;
machenbach@chromium.org528ce022013-09-23 14:09:36 +000046 HValue* last_value_;
47 HFieldApproximation* next_;
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +000048
49 // Recursively copy the entire linked list of field approximations.
50 HFieldApproximation* Copy(Zone* zone) {
51 if (this == NULL) return NULL;
52 HFieldApproximation* copy = new(zone) HFieldApproximation();
53 copy->object_ = this->object_;
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +000054 copy->last_value_ = this->last_value_;
55 copy->next_ = this->next_->Copy(zone);
56 return copy;
57 }
machenbach@chromium.org528ce022013-09-23 14:09:36 +000058};
59
60
61// The main datastructure used during load/store elimination. Each in-object
62// field is tracked separately. For each field, store a list of known field
63// values for known objects.
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +000064class HLoadEliminationTable : public ZoneObject {
machenbach@chromium.org528ce022013-09-23 14:09:36 +000065 public:
66 HLoadEliminationTable(Zone* zone, HAliasAnalyzer* aliasing)
67 : zone_(zone), fields_(kMaxTrackedFields, zone), aliasing_(aliasing) { }
68
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +000069 // The main processing of instructions.
70 HLoadEliminationTable* Process(HInstruction* instr, Zone* zone) {
71 switch (instr->opcode()) {
72 case HValue::kLoadNamedField: {
73 HLoadNamedField* l = HLoadNamedField::cast(instr);
74 TRACE((" process L%d field %d (o%d)\n",
75 instr->id(),
76 FieldOf(l->access()),
77 l->object()->ActualValue()->id()));
78 HValue* result = load(l);
79 if (result != instr) {
80 // The load can be replaced with a previous load or a value.
81 TRACE((" replace L%d -> v%d\n", instr->id(), result->id()));
82 instr->DeleteAndReplaceWith(result);
83 }
84 break;
85 }
86 case HValue::kStoreNamedField: {
87 HStoreNamedField* s = HStoreNamedField::cast(instr);
88 TRACE((" process S%d field %d (o%d) = v%d\n",
89 instr->id(),
90 FieldOf(s->access()),
91 s->object()->ActualValue()->id(),
92 s->value()->id()));
93 HValue* result = store(s);
94 if (result == NULL) {
95 // The store is redundant. Remove it.
96 TRACE((" remove S%d\n", instr->id()));
97 instr->DeleteAndReplaceWith(NULL);
98 }
99 break;
100 }
101 default: {
102 if (instr->CheckGVNFlag(kChangesInobjectFields)) {
103 TRACE((" kill-all i%d\n", instr->id()));
104 Kill();
105 break;
106 }
107 if (instr->CheckGVNFlag(kChangesMaps)) {
108 TRACE((" kill-maps i%d\n", instr->id()));
109 KillOffset(JSObject::kMapOffset);
110 }
111 if (instr->CheckGVNFlag(kChangesElementsKind)) {
112 TRACE((" kill-elements-kind i%d\n", instr->id()));
113 KillOffset(JSObject::kMapOffset);
114 KillOffset(JSObject::kElementsOffset);
115 }
116 if (instr->CheckGVNFlag(kChangesElementsPointer)) {
117 TRACE((" kill-elements i%d\n", instr->id()));
118 KillOffset(JSObject::kElementsOffset);
119 }
120 if (instr->CheckGVNFlag(kChangesOsrEntries)) {
121 TRACE((" kill-osr i%d\n", instr->id()));
122 Kill();
123 }
124 }
125 // Improvements possible:
126 // - learn from HCheckMaps for field 0
127 // - remove unobservable stores (write-after-write)
128 // - track cells
129 // - track globals
130 // - track roots
131 }
132 return this;
133 }
134
machenbach@chromium.org05150ab2014-01-29 08:13:29 +0000135 // Support for global analysis with HFlowEngine: Copy state to successor
136 // block.
137 HLoadEliminationTable* Copy(HBasicBlock* succ, HBasicBlock* from_block,
138 Zone* zone) {
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000139 HLoadEliminationTable* copy =
140 new(zone) HLoadEliminationTable(zone, aliasing_);
141 copy->EnsureFields(fields_.length());
142 for (int i = 0; i < fields_.length(); i++) {
143 copy->fields_[i] = fields_[i]->Copy(zone);
144 }
145 if (FLAG_trace_load_elimination) {
146 TRACE((" copy-to B%d\n", succ->block_id()));
147 copy->Print();
148 }
149 return copy;
150 }
151
152 // Support for global analysis with HFlowEngine: Merge this state with
153 // the other incoming state.
machenbach@chromium.org05150ab2014-01-29 08:13:29 +0000154 HLoadEliminationTable* Merge(HBasicBlock* succ, HLoadEliminationTable* that,
155 HBasicBlock* that_block, Zone* zone) {
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000156 if (that->fields_.length() < fields_.length()) {
157 // Drop fields not in the other table.
158 fields_.Rewind(that->fields_.length());
159 }
160 for (int i = 0; i < fields_.length(); i++) {
161 // Merge the field approximations for like fields.
162 HFieldApproximation* approx = fields_[i];
163 HFieldApproximation* prev = NULL;
164 while (approx != NULL) {
165 // TODO(titzer): Merging is O(N * M); sort?
166 HFieldApproximation* other = that->Find(approx->object_, i);
167 if (other == NULL || !Equal(approx->last_value_, other->last_value_)) {
168 // Kill an entry that doesn't agree with the other value.
169 if (prev != NULL) {
170 prev->next_ = approx->next_;
171 } else {
172 fields_[i] = approx->next_;
173 }
174 approx = approx->next_;
175 continue;
176 }
177 prev = approx;
178 approx = approx->next_;
179 }
180 }
machenbach@chromium.org05150ab2014-01-29 08:13:29 +0000181 if (FLAG_trace_load_elimination) {
182 TRACE((" merge-to B%d\n", succ->block_id()));
183 Print();
184 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000185 return this;
186 }
187
188 friend class HLoadEliminationEffects; // Calls Kill() and others.
189 friend class HLoadEliminationPhase;
190
191 private:
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000192 // Process a load instruction, updating internal table state. If a previous
193 // load or store for this object and field exists, return the new value with
194 // which the load should be replaced. Otherwise, return {instr}.
195 HValue* load(HLoadNamedField* instr) {
196 int field = FieldOf(instr->access());
197 if (field < 0) return instr;
198
199 HValue* object = instr->object()->ActualValue();
200 HFieldApproximation* approx = FindOrCreate(object, field);
201
202 if (approx->last_value_ == NULL) {
203 // Load is not redundant. Fill out a new entry.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000204 approx->last_value_ = instr;
205 return instr;
machenbach@chromium.org57a54ac2014-01-31 14:01:53 +0000206 } else if (approx->last_value_->block()->EqualToOrDominates(
207 instr->block())) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000208 // Eliminate the load. Reuse previously stored value or load instruction.
209 return approx->last_value_;
machenbach@chromium.org57a54ac2014-01-31 14:01:53 +0000210 } else {
211 return instr;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000212 }
213 }
214
215 // Process a store instruction, updating internal table state. If a previous
216 // store to the same object and field makes this store redundant (e.g. because
217 // the stored values are the same), return NULL indicating that this store
218 // instruction is redundant. Otherwise, return {instr}.
219 HValue* store(HStoreNamedField* instr) {
machenbach@chromium.org05150ab2014-01-29 08:13:29 +0000220 if (instr->store_mode() == PREINITIALIZING_STORE) {
221 TRACE((" skipping preinitializing store\n"));
222 return instr;
223 }
224
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000225 int field = FieldOf(instr->access());
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000226 if (field < 0) return KillIfMisaligned(instr);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000227
228 HValue* object = instr->object()->ActualValue();
229 HValue* value = instr->value();
230
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000231 if (instr->has_transition()) {
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +0000232 // A transition introduces a new field and alters the map of the object.
233 // Since the field in the object is new, it cannot alias existing entries.
234 // TODO(titzer): introduce a constant for the new map and remember it.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000235 KillFieldInternal(object, FieldOf(JSObject::kMapOffset), NULL);
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +0000236 } else {
237 // Kill non-equivalent may-alias entries.
238 KillFieldInternal(object, field, value);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000239 }
240 HFieldApproximation* approx = FindOrCreate(object, field);
241
242 if (Equal(approx->last_value_, value)) {
243 // The store is redundant because the field already has this value.
244 return NULL;
245 } else {
246 // The store is not redundant. Update the entry.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000247 approx->last_value_ = value;
248 return instr;
249 }
250 }
251
252 // Kill everything in this table.
253 void Kill() {
254 fields_.Rewind(0);
255 }
256
257 // Kill all entries matching the given offset.
258 void KillOffset(int offset) {
259 int field = FieldOf(offset);
260 if (field >= 0 && field < fields_.length()) {
261 fields_[field] = NULL;
262 }
263 }
264
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000265 // Kill all entries aliasing the given store.
266 void KillStore(HStoreNamedField* s) {
267 int field = FieldOf(s->access());
268 if (field >= 0) {
269 KillFieldInternal(s->object()->ActualValue(), field, s->value());
270 } else {
271 KillIfMisaligned(s);
272 }
273 }
274
275 // Kill multiple entries in the case of a misaligned store.
276 HValue* KillIfMisaligned(HStoreNamedField* instr) {
277 HObjectAccess access = instr->access();
278 if (access.IsInobject()) {
279 int offset = access.offset();
280 if ((offset % kPointerSize) != 0) {
281 // Kill the field containing the first word of the access.
282 HValue* object = instr->object()->ActualValue();
283 int field = offset / kPointerSize;
284 KillFieldInternal(object, field, NULL);
285
286 // Kill the next field in case of overlap.
machenbach@chromium.org935a7792013-11-12 09:05:18 +0000287 int size = access.representation().size();
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000288 int next_field = (offset + size - 1) / kPointerSize;
289 if (next_field != field) KillFieldInternal(object, next_field, NULL);
290 }
291 }
292 return instr;
293 }
294
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000295 // Find an entry for the given object and field pair.
296 HFieldApproximation* Find(HValue* object, int field) {
297 // Search for a field approximation for this object.
298 HFieldApproximation* approx = fields_[field];
299 while (approx != NULL) {
300 if (aliasing_->MustAlias(object, approx->object_)) return approx;
301 approx = approx->next_;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000302 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000303 return NULL;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000304 }
305
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000306 // Find or create an entry for the given object and field pair.
307 HFieldApproximation* FindOrCreate(HValue* object, int field) {
308 EnsureFields(field + 1);
309
310 // Search for a field approximation for this object.
311 HFieldApproximation* approx = fields_[field];
312 int count = 0;
313 while (approx != NULL) {
314 if (aliasing_->MustAlias(object, approx->object_)) return approx;
315 count++;
316 approx = approx->next_;
317 }
318
319 if (count >= kMaxTrackedObjects) {
320 // Pull the last entry off the end and repurpose it for this object.
321 approx = ReuseLastApproximation(field);
322 } else {
323 // Allocate a new entry.
324 approx = new(zone_) HFieldApproximation();
325 }
326
327 // Insert the entry at the head of the list.
328 approx->object_ = object;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000329 approx->last_value_ = NULL;
330 approx->next_ = fields_[field];
331 fields_[field] = approx;
332
333 return approx;
334 }
335
336 // Kill all entries for a given field that _may_ alias the given object
337 // and do _not_ have the given value.
338 void KillFieldInternal(HValue* object, int field, HValue* value) {
339 if (field >= fields_.length()) return; // Nothing to do.
340
341 HFieldApproximation* approx = fields_[field];
342 HFieldApproximation* prev = NULL;
343 while (approx != NULL) {
344 if (aliasing_->MayAlias(object, approx->object_)) {
345 if (!Equal(approx->last_value_, value)) {
346 // Kill an aliasing entry that doesn't agree on the value.
347 if (prev != NULL) {
348 prev->next_ = approx->next_;
349 } else {
350 fields_[field] = approx->next_;
351 }
352 approx = approx->next_;
353 continue;
354 }
355 }
356 prev = approx;
357 approx = approx->next_;
358 }
359 }
360
361 bool Equal(HValue* a, HValue* b) {
362 if (a == b) return true;
yangguo@chromium.orgcc536052013-11-29 11:43:20 +0000363 if (a != NULL && b != NULL && a->CheckFlag(HValue::kUseGVN)) {
364 return a->Equals(b);
365 }
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000366 return false;
367 }
368
369 // Remove the last approximation for a field so that it can be reused.
370 // We reuse the last entry because it was the first inserted and is thus
371 // farthest away from the current instruction.
372 HFieldApproximation* ReuseLastApproximation(int field) {
373 HFieldApproximation* approx = fields_[field];
374 ASSERT(approx != NULL);
375
376 HFieldApproximation* prev = NULL;
377 while (approx->next_ != NULL) {
378 prev = approx;
379 approx = approx->next_;
380 }
381 if (prev != NULL) prev->next_ = NULL;
382 return approx;
383 }
384
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000385 // Compute the field index for the given object access; -1 if not tracked.
386 int FieldOf(HObjectAccess access) {
387 return access.IsInobject() ? FieldOf(access.offset()) : -1;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000388 }
389
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000390 // Compute the field index for the given in-object offset; -1 if not tracked.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000391 int FieldOf(int offset) {
392 if (offset >= kMaxTrackedFields * kPointerSize) return -1;
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000393 // TODO(titzer): track misaligned loads in a separate list?
394 if ((offset % kPointerSize) != 0) return -1; // Ignore misaligned accesses.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000395 return offset / kPointerSize;
396 }
397
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000398 // Ensure internal storage for the given number of fields.
399 void EnsureFields(int num_fields) {
400 if (fields_.length() < num_fields) {
401 fields_.AddBlock(NULL, num_fields - fields_.length(), zone_);
402 }
403 }
404
405 // Print this table to stdout.
406 void Print() {
407 for (int i = 0; i < fields_.length(); i++) {
408 PrintF(" field %d: ", i);
409 for (HFieldApproximation* a = fields_[i]; a != NULL; a = a->next_) {
410 PrintF("[o%d =", a->object_->id());
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000411 if (a->last_value_ != NULL) PrintF(" v%d", a->last_value_->id());
412 PrintF("] ");
413 }
414 PrintF("\n");
415 }
416 }
417
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000418 Zone* zone_;
419 ZoneList<HFieldApproximation*> fields_;
420 HAliasAnalyzer* aliasing_;
421};
422
423
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000424// Support for HFlowEngine: collect store effects within loops.
425class HLoadEliminationEffects : public ZoneObject {
426 public:
427 explicit HLoadEliminationEffects(Zone* zone)
428 : zone_(zone),
429 maps_stored_(false),
430 fields_stored_(false),
431 elements_stored_(false),
432 stores_(5, zone) { }
433
434 inline bool Disabled() {
435 return false; // Effects are _not_ disabled.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000436 }
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000437
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000438 // Process a possibly side-effecting instruction.
439 void Process(HInstruction* instr, Zone* zone) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000440 switch (instr->opcode()) {
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000441 case HValue::kStoreNamedField: {
442 stores_.Add(HStoreNamedField::cast(instr), zone_);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000443 break;
444 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000445 case HValue::kOsrEntry: {
446 // Kill everything. Loads must not be hoisted past the OSR entry.
447 maps_stored_ = true;
448 fields_stored_ = true;
449 elements_stored_ = true;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000450 }
451 default: {
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000452 fields_stored_ |= instr->CheckGVNFlag(kChangesInobjectFields);
453 maps_stored_ |= instr->CheckGVNFlag(kChangesMaps);
454 maps_stored_ |= instr->CheckGVNFlag(kChangesElementsKind);
455 elements_stored_ |= instr->CheckGVNFlag(kChangesElementsKind);
456 elements_stored_ |= instr->CheckGVNFlag(kChangesElementsPointer);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000457 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000458 }
459 }
460
461 // Apply these effects to the given load elimination table.
462 void Apply(HLoadEliminationTable* table) {
463 if (fields_stored_) {
464 table->Kill();
465 return;
466 }
467 if (maps_stored_) {
468 table->KillOffset(JSObject::kMapOffset);
469 }
470 if (elements_stored_) {
471 table->KillOffset(JSObject::kElementsOffset);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000472 }
473
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000474 // Kill non-agreeing fields for each store contained in these effects.
475 for (int i = 0; i < stores_.length(); i++) {
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000476 table->KillStore(stores_[i]);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000477 }
478 }
479
480 // Union these effects with the other effects.
481 void Union(HLoadEliminationEffects* that, Zone* zone) {
482 maps_stored_ |= that->maps_stored_;
483 fields_stored_ |= that->fields_stored_;
484 elements_stored_ |= that->elements_stored_;
485 for (int i = 0; i < that->stores_.length(); i++) {
486 stores_.Add(that->stores_[i], zone);
487 }
488 }
489
490 private:
491 Zone* zone_;
492 bool maps_stored_ : 1;
493 bool fields_stored_ : 1;
494 bool elements_stored_ : 1;
495 ZoneList<HStoreNamedField*> stores_;
496};
497
498
499// The main routine of the analysis phase. Use the HFlowEngine for either a
500// local or a global analysis.
501void HLoadEliminationPhase::Run() {
502 HFlowEngine<HLoadEliminationTable, HLoadEliminationEffects>
503 engine(graph(), zone());
504 HAliasAnalyzer aliasing;
505 HLoadEliminationTable* table =
506 new(zone()) HLoadEliminationTable(zone(), &aliasing);
507
508 if (GLOBAL) {
509 // Perform a global analysis.
510 engine.AnalyzeDominatedBlocks(graph()->blocks()->at(0), table);
511 } else {
512 // Perform only local analysis.
513 for (int i = 0; i < graph()->blocks()->length(); i++) {
514 table->Kill();
515 engine.AnalyzeOneBlock(graph()->blocks()->at(i), table);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000516 }
517 }
518}
519
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000520} } // namespace v8::internal