blob: ea12df865d7b0d91a43eb6f7162374e538509afb [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;
206 } else {
207 // Eliminate the load. Reuse previously stored value or load instruction.
208 return approx->last_value_;
209 }
210 }
211
212 // Process a store instruction, updating internal table state. If a previous
213 // store to the same object and field makes this store redundant (e.g. because
214 // the stored values are the same), return NULL indicating that this store
215 // instruction is redundant. Otherwise, return {instr}.
216 HValue* store(HStoreNamedField* instr) {
machenbach@chromium.org05150ab2014-01-29 08:13:29 +0000217 if (instr->store_mode() == PREINITIALIZING_STORE) {
218 TRACE((" skipping preinitializing store\n"));
219 return instr;
220 }
221
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000222 int field = FieldOf(instr->access());
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000223 if (field < 0) return KillIfMisaligned(instr);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000224
225 HValue* object = instr->object()->ActualValue();
226 HValue* value = instr->value();
227
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000228 if (instr->has_transition()) {
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +0000229 // A transition introduces a new field and alters the map of the object.
230 // Since the field in the object is new, it cannot alias existing entries.
231 // TODO(titzer): introduce a constant for the new map and remember it.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000232 KillFieldInternal(object, FieldOf(JSObject::kMapOffset), NULL);
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +0000233 } else {
234 // Kill non-equivalent may-alias entries.
235 KillFieldInternal(object, field, value);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000236 }
237 HFieldApproximation* approx = FindOrCreate(object, field);
238
239 if (Equal(approx->last_value_, value)) {
240 // The store is redundant because the field already has this value.
241 return NULL;
242 } else {
243 // The store is not redundant. Update the entry.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000244 approx->last_value_ = value;
245 return instr;
246 }
247 }
248
249 // Kill everything in this table.
250 void Kill() {
251 fields_.Rewind(0);
252 }
253
254 // Kill all entries matching the given offset.
255 void KillOffset(int offset) {
256 int field = FieldOf(offset);
257 if (field >= 0 && field < fields_.length()) {
258 fields_[field] = NULL;
259 }
260 }
261
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000262 // Kill all entries aliasing the given store.
263 void KillStore(HStoreNamedField* s) {
264 int field = FieldOf(s->access());
265 if (field >= 0) {
266 KillFieldInternal(s->object()->ActualValue(), field, s->value());
267 } else {
268 KillIfMisaligned(s);
269 }
270 }
271
272 // Kill multiple entries in the case of a misaligned store.
273 HValue* KillIfMisaligned(HStoreNamedField* instr) {
274 HObjectAccess access = instr->access();
275 if (access.IsInobject()) {
276 int offset = access.offset();
277 if ((offset % kPointerSize) != 0) {
278 // Kill the field containing the first word of the access.
279 HValue* object = instr->object()->ActualValue();
280 int field = offset / kPointerSize;
281 KillFieldInternal(object, field, NULL);
282
283 // Kill the next field in case of overlap.
machenbach@chromium.org935a7792013-11-12 09:05:18 +0000284 int size = access.representation().size();
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000285 int next_field = (offset + size - 1) / kPointerSize;
286 if (next_field != field) KillFieldInternal(object, next_field, NULL);
287 }
288 }
289 return instr;
290 }
291
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000292 // Find an entry for the given object and field pair.
293 HFieldApproximation* Find(HValue* object, int field) {
294 // Search for a field approximation for this object.
295 HFieldApproximation* approx = fields_[field];
296 while (approx != NULL) {
297 if (aliasing_->MustAlias(object, approx->object_)) return approx;
298 approx = approx->next_;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000299 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000300 return NULL;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000301 }
302
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000303 // Find or create an entry for the given object and field pair.
304 HFieldApproximation* FindOrCreate(HValue* object, int field) {
305 EnsureFields(field + 1);
306
307 // Search for a field approximation for this object.
308 HFieldApproximation* approx = fields_[field];
309 int count = 0;
310 while (approx != NULL) {
311 if (aliasing_->MustAlias(object, approx->object_)) return approx;
312 count++;
313 approx = approx->next_;
314 }
315
316 if (count >= kMaxTrackedObjects) {
317 // Pull the last entry off the end and repurpose it for this object.
318 approx = ReuseLastApproximation(field);
319 } else {
320 // Allocate a new entry.
321 approx = new(zone_) HFieldApproximation();
322 }
323
324 // Insert the entry at the head of the list.
325 approx->object_ = object;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000326 approx->last_value_ = NULL;
327 approx->next_ = fields_[field];
328 fields_[field] = approx;
329
330 return approx;
331 }
332
333 // Kill all entries for a given field that _may_ alias the given object
334 // and do _not_ have the given value.
335 void KillFieldInternal(HValue* object, int field, HValue* value) {
336 if (field >= fields_.length()) return; // Nothing to do.
337
338 HFieldApproximation* approx = fields_[field];
339 HFieldApproximation* prev = NULL;
340 while (approx != NULL) {
341 if (aliasing_->MayAlias(object, approx->object_)) {
342 if (!Equal(approx->last_value_, value)) {
343 // Kill an aliasing entry that doesn't agree on the value.
344 if (prev != NULL) {
345 prev->next_ = approx->next_;
346 } else {
347 fields_[field] = approx->next_;
348 }
349 approx = approx->next_;
350 continue;
351 }
352 }
353 prev = approx;
354 approx = approx->next_;
355 }
356 }
357
358 bool Equal(HValue* a, HValue* b) {
359 if (a == b) return true;
yangguo@chromium.orgcc536052013-11-29 11:43:20 +0000360 if (a != NULL && b != NULL && a->CheckFlag(HValue::kUseGVN)) {
361 return a->Equals(b);
362 }
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000363 return false;
364 }
365
366 // Remove the last approximation for a field so that it can be reused.
367 // We reuse the last entry because it was the first inserted and is thus
368 // farthest away from the current instruction.
369 HFieldApproximation* ReuseLastApproximation(int field) {
370 HFieldApproximation* approx = fields_[field];
371 ASSERT(approx != NULL);
372
373 HFieldApproximation* prev = NULL;
374 while (approx->next_ != NULL) {
375 prev = approx;
376 approx = approx->next_;
377 }
378 if (prev != NULL) prev->next_ = NULL;
379 return approx;
380 }
381
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000382 // Compute the field index for the given object access; -1 if not tracked.
383 int FieldOf(HObjectAccess access) {
384 return access.IsInobject() ? FieldOf(access.offset()) : -1;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000385 }
386
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000387 // Compute the field index for the given in-object offset; -1 if not tracked.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000388 int FieldOf(int offset) {
389 if (offset >= kMaxTrackedFields * kPointerSize) return -1;
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000390 // TODO(titzer): track misaligned loads in a separate list?
391 if ((offset % kPointerSize) != 0) return -1; // Ignore misaligned accesses.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000392 return offset / kPointerSize;
393 }
394
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000395 // Ensure internal storage for the given number of fields.
396 void EnsureFields(int num_fields) {
397 if (fields_.length() < num_fields) {
398 fields_.AddBlock(NULL, num_fields - fields_.length(), zone_);
399 }
400 }
401
402 // Print this table to stdout.
403 void Print() {
404 for (int i = 0; i < fields_.length(); i++) {
405 PrintF(" field %d: ", i);
406 for (HFieldApproximation* a = fields_[i]; a != NULL; a = a->next_) {
407 PrintF("[o%d =", a->object_->id());
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000408 if (a->last_value_ != NULL) PrintF(" v%d", a->last_value_->id());
409 PrintF("] ");
410 }
411 PrintF("\n");
412 }
413 }
414
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000415 Zone* zone_;
416 ZoneList<HFieldApproximation*> fields_;
417 HAliasAnalyzer* aliasing_;
418};
419
420
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000421// Support for HFlowEngine: collect store effects within loops.
422class HLoadEliminationEffects : public ZoneObject {
423 public:
424 explicit HLoadEliminationEffects(Zone* zone)
425 : zone_(zone),
426 maps_stored_(false),
427 fields_stored_(false),
428 elements_stored_(false),
429 stores_(5, zone) { }
430
431 inline bool Disabled() {
432 return false; // Effects are _not_ disabled.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000433 }
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000434
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000435 // Process a possibly side-effecting instruction.
436 void Process(HInstruction* instr, Zone* zone) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000437 switch (instr->opcode()) {
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000438 case HValue::kStoreNamedField: {
439 stores_.Add(HStoreNamedField::cast(instr), zone_);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000440 break;
441 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000442 case HValue::kOsrEntry: {
443 // Kill everything. Loads must not be hoisted past the OSR entry.
444 maps_stored_ = true;
445 fields_stored_ = true;
446 elements_stored_ = true;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000447 }
448 default: {
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000449 fields_stored_ |= instr->CheckGVNFlag(kChangesInobjectFields);
450 maps_stored_ |= instr->CheckGVNFlag(kChangesMaps);
451 maps_stored_ |= instr->CheckGVNFlag(kChangesElementsKind);
452 elements_stored_ |= instr->CheckGVNFlag(kChangesElementsKind);
453 elements_stored_ |= instr->CheckGVNFlag(kChangesElementsPointer);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000454 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000455 }
456 }
457
458 // Apply these effects to the given load elimination table.
459 void Apply(HLoadEliminationTable* table) {
460 if (fields_stored_) {
461 table->Kill();
462 return;
463 }
464 if (maps_stored_) {
465 table->KillOffset(JSObject::kMapOffset);
466 }
467 if (elements_stored_) {
468 table->KillOffset(JSObject::kElementsOffset);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000469 }
470
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000471 // Kill non-agreeing fields for each store contained in these effects.
472 for (int i = 0; i < stores_.length(); i++) {
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000473 table->KillStore(stores_[i]);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000474 }
475 }
476
477 // Union these effects with the other effects.
478 void Union(HLoadEliminationEffects* that, Zone* zone) {
479 maps_stored_ |= that->maps_stored_;
480 fields_stored_ |= that->fields_stored_;
481 elements_stored_ |= that->elements_stored_;
482 for (int i = 0; i < that->stores_.length(); i++) {
483 stores_.Add(that->stores_[i], zone);
484 }
485 }
486
487 private:
488 Zone* zone_;
489 bool maps_stored_ : 1;
490 bool fields_stored_ : 1;
491 bool elements_stored_ : 1;
492 ZoneList<HStoreNamedField*> stores_;
493};
494
495
496// The main routine of the analysis phase. Use the HFlowEngine for either a
497// local or a global analysis.
498void HLoadEliminationPhase::Run() {
499 HFlowEngine<HLoadEliminationTable, HLoadEliminationEffects>
500 engine(graph(), zone());
501 HAliasAnalyzer aliasing;
502 HLoadEliminationTable* table =
503 new(zone()) HLoadEliminationTable(zone(), &aliasing);
504
505 if (GLOBAL) {
506 // Perform a global analysis.
507 engine.AnalyzeDominatedBlocks(graph()->blocks()->at(0), table);
508 } else {
509 // Perform only local analysis.
510 for (int i = 0; i < graph()->blocks()->length(); i++) {
511 table->Kill();
512 engine.AnalyzeOneBlock(graph()->blocks()->at(i), table);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000513 }
514 }
515}
516
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000517} } // namespace v8::internal