blob: f2e993b18dea2305884acd01274f85edab887260 [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
135 // Support for global analysis with HFlowEngine: Copy state to sucessor block.
136 HLoadEliminationTable* Copy(HBasicBlock* succ, Zone* zone) {
137 HLoadEliminationTable* copy =
138 new(zone) HLoadEliminationTable(zone, aliasing_);
139 copy->EnsureFields(fields_.length());
140 for (int i = 0; i < fields_.length(); i++) {
141 copy->fields_[i] = fields_[i]->Copy(zone);
142 }
143 if (FLAG_trace_load_elimination) {
144 TRACE((" copy-to B%d\n", succ->block_id()));
145 copy->Print();
146 }
147 return copy;
148 }
149
150 // Support for global analysis with HFlowEngine: Merge this state with
151 // the other incoming state.
152 HLoadEliminationTable* Merge(HBasicBlock* succ,
153 HLoadEliminationTable* that, Zone* zone) {
154 if (that->fields_.length() < fields_.length()) {
155 // Drop fields not in the other table.
156 fields_.Rewind(that->fields_.length());
157 }
158 for (int i = 0; i < fields_.length(); i++) {
159 // Merge the field approximations for like fields.
160 HFieldApproximation* approx = fields_[i];
161 HFieldApproximation* prev = NULL;
162 while (approx != NULL) {
163 // TODO(titzer): Merging is O(N * M); sort?
164 HFieldApproximation* other = that->Find(approx->object_, i);
165 if (other == NULL || !Equal(approx->last_value_, other->last_value_)) {
166 // Kill an entry that doesn't agree with the other value.
167 if (prev != NULL) {
168 prev->next_ = approx->next_;
169 } else {
170 fields_[i] = approx->next_;
171 }
172 approx = approx->next_;
173 continue;
174 }
175 prev = approx;
176 approx = approx->next_;
177 }
178 }
179 return this;
180 }
181
182 friend class HLoadEliminationEffects; // Calls Kill() and others.
183 friend class HLoadEliminationPhase;
184
185 private:
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000186 // Process a load instruction, updating internal table state. If a previous
187 // load or store for this object and field exists, return the new value with
188 // which the load should be replaced. Otherwise, return {instr}.
189 HValue* load(HLoadNamedField* instr) {
190 int field = FieldOf(instr->access());
191 if (field < 0) return instr;
192
193 HValue* object = instr->object()->ActualValue();
194 HFieldApproximation* approx = FindOrCreate(object, field);
195
196 if (approx->last_value_ == NULL) {
197 // Load is not redundant. Fill out a new entry.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000198 approx->last_value_ = instr;
199 return instr;
200 } else {
201 // Eliminate the load. Reuse previously stored value or load instruction.
202 return approx->last_value_;
203 }
204 }
205
206 // Process a store instruction, updating internal table state. If a previous
207 // store to the same object and field makes this store redundant (e.g. because
208 // the stored values are the same), return NULL indicating that this store
209 // instruction is redundant. Otherwise, return {instr}.
210 HValue* store(HStoreNamedField* instr) {
211 int field = FieldOf(instr->access());
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000212 if (field < 0) return KillIfMisaligned(instr);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000213
214 HValue* object = instr->object()->ActualValue();
215 HValue* value = instr->value();
216
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000217 if (instr->has_transition()) {
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +0000218 // A transition introduces a new field and alters the map of the object.
219 // Since the field in the object is new, it cannot alias existing entries.
220 // TODO(titzer): introduce a constant for the new map and remember it.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000221 KillFieldInternal(object, FieldOf(JSObject::kMapOffset), NULL);
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +0000222 } else {
223 // Kill non-equivalent may-alias entries.
224 KillFieldInternal(object, field, value);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000225 }
226 HFieldApproximation* approx = FindOrCreate(object, field);
227
228 if (Equal(approx->last_value_, value)) {
229 // The store is redundant because the field already has this value.
230 return NULL;
231 } else {
232 // The store is not redundant. Update the entry.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000233 approx->last_value_ = value;
234 return instr;
235 }
236 }
237
238 // Kill everything in this table.
239 void Kill() {
240 fields_.Rewind(0);
241 }
242
243 // Kill all entries matching the given offset.
244 void KillOffset(int offset) {
245 int field = FieldOf(offset);
246 if (field >= 0 && field < fields_.length()) {
247 fields_[field] = NULL;
248 }
249 }
250
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000251 // Kill all entries aliasing the given store.
252 void KillStore(HStoreNamedField* s) {
253 int field = FieldOf(s->access());
254 if (field >= 0) {
255 KillFieldInternal(s->object()->ActualValue(), field, s->value());
256 } else {
257 KillIfMisaligned(s);
258 }
259 }
260
261 // Kill multiple entries in the case of a misaligned store.
262 HValue* KillIfMisaligned(HStoreNamedField* instr) {
263 HObjectAccess access = instr->access();
264 if (access.IsInobject()) {
265 int offset = access.offset();
266 if ((offset % kPointerSize) != 0) {
267 // Kill the field containing the first word of the access.
268 HValue* object = instr->object()->ActualValue();
269 int field = offset / kPointerSize;
270 KillFieldInternal(object, field, NULL);
271
272 // Kill the next field in case of overlap.
machenbach@chromium.org935a7792013-11-12 09:05:18 +0000273 int size = access.representation().size();
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000274 int next_field = (offset + size - 1) / kPointerSize;
275 if (next_field != field) KillFieldInternal(object, next_field, NULL);
276 }
277 }
278 return instr;
279 }
280
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000281 // Find an entry for the given object and field pair.
282 HFieldApproximation* Find(HValue* object, int field) {
283 // Search for a field approximation for this object.
284 HFieldApproximation* approx = fields_[field];
285 while (approx != NULL) {
286 if (aliasing_->MustAlias(object, approx->object_)) return approx;
287 approx = approx->next_;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000288 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000289 return NULL;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000290 }
291
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000292 // Find or create an entry for the given object and field pair.
293 HFieldApproximation* FindOrCreate(HValue* object, int field) {
294 EnsureFields(field + 1);
295
296 // Search for a field approximation for this object.
297 HFieldApproximation* approx = fields_[field];
298 int count = 0;
299 while (approx != NULL) {
300 if (aliasing_->MustAlias(object, approx->object_)) return approx;
301 count++;
302 approx = approx->next_;
303 }
304
305 if (count >= kMaxTrackedObjects) {
306 // Pull the last entry off the end and repurpose it for this object.
307 approx = ReuseLastApproximation(field);
308 } else {
309 // Allocate a new entry.
310 approx = new(zone_) HFieldApproximation();
311 }
312
313 // Insert the entry at the head of the list.
314 approx->object_ = object;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000315 approx->last_value_ = NULL;
316 approx->next_ = fields_[field];
317 fields_[field] = approx;
318
319 return approx;
320 }
321
322 // Kill all entries for a given field that _may_ alias the given object
323 // and do _not_ have the given value.
324 void KillFieldInternal(HValue* object, int field, HValue* value) {
325 if (field >= fields_.length()) return; // Nothing to do.
326
327 HFieldApproximation* approx = fields_[field];
328 HFieldApproximation* prev = NULL;
329 while (approx != NULL) {
330 if (aliasing_->MayAlias(object, approx->object_)) {
331 if (!Equal(approx->last_value_, value)) {
332 // Kill an aliasing entry that doesn't agree on the value.
333 if (prev != NULL) {
334 prev->next_ = approx->next_;
335 } else {
336 fields_[field] = approx->next_;
337 }
338 approx = approx->next_;
339 continue;
340 }
341 }
342 prev = approx;
343 approx = approx->next_;
344 }
345 }
346
347 bool Equal(HValue* a, HValue* b) {
348 if (a == b) return true;
yangguo@chromium.orgcc536052013-11-29 11:43:20 +0000349 if (a != NULL && b != NULL && a->CheckFlag(HValue::kUseGVN)) {
350 return a->Equals(b);
351 }
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000352 return false;
353 }
354
355 // Remove the last approximation for a field so that it can be reused.
356 // We reuse the last entry because it was the first inserted and is thus
357 // farthest away from the current instruction.
358 HFieldApproximation* ReuseLastApproximation(int field) {
359 HFieldApproximation* approx = fields_[field];
360 ASSERT(approx != NULL);
361
362 HFieldApproximation* prev = NULL;
363 while (approx->next_ != NULL) {
364 prev = approx;
365 approx = approx->next_;
366 }
367 if (prev != NULL) prev->next_ = NULL;
368 return approx;
369 }
370
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000371 // Compute the field index for the given object access; -1 if not tracked.
372 int FieldOf(HObjectAccess access) {
373 return access.IsInobject() ? FieldOf(access.offset()) : -1;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000374 }
375
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000376 // Compute the field index for the given in-object offset; -1 if not tracked.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000377 int FieldOf(int offset) {
378 if (offset >= kMaxTrackedFields * kPointerSize) return -1;
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000379 // TODO(titzer): track misaligned loads in a separate list?
380 if ((offset % kPointerSize) != 0) return -1; // Ignore misaligned accesses.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000381 return offset / kPointerSize;
382 }
383
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000384 // Ensure internal storage for the given number of fields.
385 void EnsureFields(int num_fields) {
386 if (fields_.length() < num_fields) {
387 fields_.AddBlock(NULL, num_fields - fields_.length(), zone_);
388 }
389 }
390
391 // Print this table to stdout.
392 void Print() {
393 for (int i = 0; i < fields_.length(); i++) {
394 PrintF(" field %d: ", i);
395 for (HFieldApproximation* a = fields_[i]; a != NULL; a = a->next_) {
396 PrintF("[o%d =", a->object_->id());
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000397 if (a->last_value_ != NULL) PrintF(" v%d", a->last_value_->id());
398 PrintF("] ");
399 }
400 PrintF("\n");
401 }
402 }
403
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000404 Zone* zone_;
405 ZoneList<HFieldApproximation*> fields_;
406 HAliasAnalyzer* aliasing_;
407};
408
409
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000410// Support for HFlowEngine: collect store effects within loops.
411class HLoadEliminationEffects : public ZoneObject {
412 public:
413 explicit HLoadEliminationEffects(Zone* zone)
414 : zone_(zone),
415 maps_stored_(false),
416 fields_stored_(false),
417 elements_stored_(false),
418 stores_(5, zone) { }
419
420 inline bool Disabled() {
421 return false; // Effects are _not_ disabled.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000422 }
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000423
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000424 // Process a possibly side-effecting instruction.
425 void Process(HInstruction* instr, Zone* zone) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000426 switch (instr->opcode()) {
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000427 case HValue::kStoreNamedField: {
428 stores_.Add(HStoreNamedField::cast(instr), zone_);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000429 break;
430 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000431 case HValue::kOsrEntry: {
432 // Kill everything. Loads must not be hoisted past the OSR entry.
433 maps_stored_ = true;
434 fields_stored_ = true;
435 elements_stored_ = true;
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000436 }
437 default: {
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000438 fields_stored_ |= instr->CheckGVNFlag(kChangesInobjectFields);
439 maps_stored_ |= instr->CheckGVNFlag(kChangesMaps);
440 maps_stored_ |= instr->CheckGVNFlag(kChangesElementsKind);
441 elements_stored_ |= instr->CheckGVNFlag(kChangesElementsKind);
442 elements_stored_ |= instr->CheckGVNFlag(kChangesElementsPointer);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000443 }
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000444 }
445 }
446
447 // Apply these effects to the given load elimination table.
448 void Apply(HLoadEliminationTable* table) {
449 if (fields_stored_) {
450 table->Kill();
451 return;
452 }
453 if (maps_stored_) {
454 table->KillOffset(JSObject::kMapOffset);
455 }
456 if (elements_stored_) {
457 table->KillOffset(JSObject::kElementsOffset);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000458 }
459
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000460 // Kill non-agreeing fields for each store contained in these effects.
461 for (int i = 0; i < stores_.length(); i++) {
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000462 table->KillStore(stores_[i]);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000463 }
464 }
465
466 // Union these effects with the other effects.
467 void Union(HLoadEliminationEffects* that, Zone* zone) {
468 maps_stored_ |= that->maps_stored_;
469 fields_stored_ |= that->fields_stored_;
470 elements_stored_ |= that->elements_stored_;
471 for (int i = 0; i < that->stores_.length(); i++) {
472 stores_.Add(that->stores_[i], zone);
473 }
474 }
475
476 private:
477 Zone* zone_;
478 bool maps_stored_ : 1;
479 bool fields_stored_ : 1;
480 bool elements_stored_ : 1;
481 ZoneList<HStoreNamedField*> stores_;
482};
483
484
485// The main routine of the analysis phase. Use the HFlowEngine for either a
486// local or a global analysis.
487void HLoadEliminationPhase::Run() {
488 HFlowEngine<HLoadEliminationTable, HLoadEliminationEffects>
489 engine(graph(), zone());
490 HAliasAnalyzer aliasing;
491 HLoadEliminationTable* table =
492 new(zone()) HLoadEliminationTable(zone(), &aliasing);
493
494 if (GLOBAL) {
495 // Perform a global analysis.
496 engine.AnalyzeDominatedBlocks(graph()->blocks()->at(0), table);
497 } else {
498 // Perform only local analysis.
499 for (int i = 0; i < graph()->blocks()->length(); i++) {
500 table->Kill();
501 engine.AnalyzeOneBlock(graph()->blocks()->at(i), table);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000502 }
503 }
504}
505
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000506} } // namespace v8::internal