blob: 437c01fd153a6e67e1cc607edb07db2873ddeb9d [file] [log] [blame]
Ben Murdoch014dc512016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/compiler/escape-analysis.h"
6
7#include <limits>
8
9#include "src/base/flags.h"
10#include "src/bootstrapper.h"
11#include "src/compilation-dependencies.h"
12#include "src/compiler/common-operator.h"
13#include "src/compiler/graph-reducer.h"
14#include "src/compiler/js-operator.h"
15#include "src/compiler/node.h"
16#include "src/compiler/node-matchers.h"
17#include "src/compiler/node-properties.h"
18#include "src/compiler/operator-properties.h"
19#include "src/compiler/simplified-operator.h"
20#include "src/objects-inl.h"
21#include "src/type-cache.h"
22
23namespace v8 {
24namespace internal {
25namespace compiler {
26
Ben Murdochbcf72ee2016-08-08 18:44:38 +010027typedef NodeId Alias;
Ben Murdoch014dc512016-03-22 12:00:34 +000028
Ben Murdoch109988c2016-05-18 11:27:45 +010029#ifdef DEBUG
30#define TRACE(...) \
31 do { \
32 if (FLAG_trace_turbo_escape) PrintF(__VA_ARGS__); \
33 } while (false)
34#else
35#define TRACE(...)
36#endif
37
Ben Murdochbcf72ee2016-08-08 18:44:38 +010038// EscapeStatusAnalysis determines for each allocation whether it escapes.
39class EscapeStatusAnalysis : public ZoneObject {
40 public:
41 enum Status {
42 kUnknown = 0u,
43 kTracked = 1u << 0,
44 kEscaped = 1u << 1,
45 kOnStack = 1u << 2,
46 kVisited = 1u << 3,
47 // A node is dangling, if it is a load of some kind, and does not have
48 // an effect successor.
49 kDanglingComputed = 1u << 4,
50 kDangling = 1u << 5,
51 // A node is is an effect branch point, if it has more than 2 non-dangling
52 // effect successors.
53 kBranchPointComputed = 1u << 6,
54 kBranchPoint = 1u << 7,
55 kInQueue = 1u << 8
56 };
57 typedef base::Flags<Status, uint16_t> StatusFlags;
58
59 void RunStatusAnalysis();
60
61 bool IsVirtual(Node* node);
62 bool IsEscaped(Node* node);
63 bool IsAllocation(Node* node);
64
65 bool IsInQueue(NodeId id);
66 void SetInQueue(NodeId id, bool on_stack);
67
68 void DebugPrint();
69
70 EscapeStatusAnalysis(EscapeAnalysis* object_analysis, Graph* graph,
71 Zone* zone);
72 void EnqueueForStatusAnalysis(Node* node);
73 bool SetEscaped(Node* node);
74 bool IsEffectBranchPoint(Node* node);
75 bool IsDanglingEffectNode(Node* node);
76 void ResizeStatusVector();
77 size_t GetStatusVectorSize();
78 bool IsVirtual(NodeId id);
79
80 Graph* graph() const { return graph_; }
81 void AssignAliases();
82 Alias GetAlias(NodeId id) const { return aliases_[id]; }
83 const ZoneVector<Alias>& GetAliasMap() const { return aliases_; }
84 Alias AliasCount() const { return next_free_alias_; }
85 static const Alias kNotReachable;
86 static const Alias kUntrackable;
87
88 bool IsNotReachable(Node* node);
89
90 private:
91 void Process(Node* node);
92 void ProcessAllocate(Node* node);
93 void ProcessFinishRegion(Node* node);
94 void ProcessStoreField(Node* node);
95 void ProcessStoreElement(Node* node);
96 bool CheckUsesForEscape(Node* node, bool phi_escaping = false) {
97 return CheckUsesForEscape(node, node, phi_escaping);
98 }
99 bool CheckUsesForEscape(Node* node, Node* rep, bool phi_escaping = false);
100 void RevisitUses(Node* node);
101 void RevisitInputs(Node* node);
102
103 Alias NextAlias() { return next_free_alias_++; }
104
105 bool HasEntry(Node* node);
106
107 bool IsAllocationPhi(Node* node);
108
109 ZoneVector<Node*> stack_;
110 EscapeAnalysis* object_analysis_;
111 Graph* const graph_;
112 ZoneVector<StatusFlags> status_;
113 Alias next_free_alias_;
114 ZoneVector<Node*> status_stack_;
115 ZoneVector<Alias> aliases_;
116
117 DISALLOW_COPY_AND_ASSIGN(EscapeStatusAnalysis);
118};
119
120DEFINE_OPERATORS_FOR_FLAGS(EscapeStatusAnalysis::StatusFlags)
121
Ben Murdoch109988c2016-05-18 11:27:45 +0100122const Alias EscapeStatusAnalysis::kNotReachable =
123 std::numeric_limits<Alias>::max();
124const Alias EscapeStatusAnalysis::kUntrackable =
125 std::numeric_limits<Alias>::max() - 1;
Ben Murdochf2e39942016-05-18 10:25:55 +0000126
Ben Murdoch014dc512016-03-22 12:00:34 +0000127class VirtualObject : public ZoneObject {
128 public:
Ben Murdoch109988c2016-05-18 11:27:45 +0100129 enum Status {
130 kInitial = 0,
131 kTracked = 1u << 0,
132 kInitialized = 1u << 1,
133 kCopyRequired = 1u << 2,
134 };
135 typedef base::Flags<Status, unsigned char> StatusFlags;
136
137 VirtualObject(NodeId id, VirtualState* owner, Zone* zone)
Ben Murdoch014dc512016-03-22 12:00:34 +0000138 : id_(id),
Ben Murdoch109988c2016-05-18 11:27:45 +0100139 status_(kInitial),
Ben Murdoch014dc512016-03-22 12:00:34 +0000140 fields_(zone),
141 phi_(zone),
Ben Murdoch109988c2016-05-18 11:27:45 +0100142 object_state_(nullptr),
143 owner_(owner) {}
Ben Murdoch014dc512016-03-22 12:00:34 +0000144
Ben Murdoch109988c2016-05-18 11:27:45 +0100145 VirtualObject(VirtualState* owner, const VirtualObject& other)
Ben Murdoch014dc512016-03-22 12:00:34 +0000146 : id_(other.id_),
Ben Murdoch109988c2016-05-18 11:27:45 +0100147 status_(other.status_ & ~kCopyRequired),
Ben Murdoch014dc512016-03-22 12:00:34 +0000148 fields_(other.fields_),
149 phi_(other.phi_),
Ben Murdoch109988c2016-05-18 11:27:45 +0100150 object_state_(other.object_state_),
151 owner_(owner) {}
Ben Murdoch014dc512016-03-22 12:00:34 +0000152
Ben Murdoch109988c2016-05-18 11:27:45 +0100153 VirtualObject(NodeId id, VirtualState* owner, Zone* zone, size_t field_number,
154 bool initialized)
Ben Murdoch014dc512016-03-22 12:00:34 +0000155 : id_(id),
Ben Murdoch109988c2016-05-18 11:27:45 +0100156 status_(kTracked | (initialized ? kInitialized : kInitial)),
Ben Murdoch014dc512016-03-22 12:00:34 +0000157 fields_(zone),
158 phi_(zone),
Ben Murdoch109988c2016-05-18 11:27:45 +0100159 object_state_(nullptr),
160 owner_(owner) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000161 fields_.resize(field_number);
162 phi_.resize(field_number, false);
163 }
164
Ben Murdoch109988c2016-05-18 11:27:45 +0100165 Node* GetField(size_t offset) { return fields_[offset]; }
Ben Murdoch014dc512016-03-22 12:00:34 +0000166
Ben Murdoch109988c2016-05-18 11:27:45 +0100167 bool IsCreatedPhi(size_t offset) { return phi_[offset]; }
Ben Murdoch014dc512016-03-22 12:00:34 +0000168
Ben Murdoch109988c2016-05-18 11:27:45 +0100169 void SetField(size_t offset, Node* node, bool created_phi = false) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000170 fields_[offset] = node;
171 phi_[offset] = created_phi;
Ben Murdoch014dc512016-03-22 12:00:34 +0000172 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100173 bool IsTracked() const { return status_ & kTracked; }
174 bool IsInitialized() const { return status_ & kInitialized; }
175 bool SetInitialized() { return status_ |= kInitialized; }
176 VirtualState* owner() const { return owner_; }
Ben Murdoch014dc512016-03-22 12:00:34 +0000177
178 Node** fields_array() { return &fields_.front(); }
179 size_t field_count() { return fields_.size(); }
180 bool ResizeFields(size_t field_count) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100181 if (field_count > fields_.size()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000182 fields_.resize(field_count);
183 phi_.resize(field_count);
184 return true;
185 }
186 return false;
187 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100188 void ClearAllFields() {
Ben Murdoch83897452016-05-17 11:12:09 +0100189 for (size_t i = 0; i < fields_.size(); ++i) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100190 fields_[i] = nullptr;
Ben Murdochf2e39942016-05-18 10:25:55 +0000191 phi_[i] = false;
Ben Murdoch83897452016-05-17 11:12:09 +0100192 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100193 }
194 bool AllFieldsClear() {
195 for (size_t i = 0; i < fields_.size(); ++i) {
196 if (fields_[i] != nullptr) {
197 return false;
198 }
199 }
200 return true;
Ben Murdoch014dc512016-03-22 12:00:34 +0000201 }
202 bool UpdateFrom(const VirtualObject& other);
Ben Murdoch109988c2016-05-18 11:27:45 +0100203 bool MergeFrom(MergeCache* cache, Node* at, Graph* graph,
204 CommonOperatorBuilder* common);
Ben Murdoch014dc512016-03-22 12:00:34 +0000205 void SetObjectState(Node* node) { object_state_ = node; }
206 Node* GetObjectState() const { return object_state_; }
Ben Murdoch109988c2016-05-18 11:27:45 +0100207 bool IsCopyRequired() const { return status_ & kCopyRequired; }
208 void SetCopyRequired() { status_ |= kCopyRequired; }
209 bool NeedCopyForModification() {
210 if (!IsCopyRequired() || !IsInitialized()) {
211 return false;
212 }
213 return true;
214 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000215
216 NodeId id() const { return id_; }
217 void id(NodeId id) { id_ = id; }
218
219 private:
Ben Murdoch109988c2016-05-18 11:27:45 +0100220 bool MergeFields(size_t i, Node* at, MergeCache* cache, Graph* graph,
221 CommonOperatorBuilder* common);
222
Ben Murdoch014dc512016-03-22 12:00:34 +0000223 NodeId id_;
Ben Murdoch109988c2016-05-18 11:27:45 +0100224 StatusFlags status_;
Ben Murdoch014dc512016-03-22 12:00:34 +0000225 ZoneVector<Node*> fields_;
226 ZoneVector<bool> phi_;
227 Node* object_state_;
Ben Murdoch109988c2016-05-18 11:27:45 +0100228 VirtualState* owner_;
229
230 DISALLOW_COPY_AND_ASSIGN(VirtualObject);
Ben Murdoch014dc512016-03-22 12:00:34 +0000231};
232
Ben Murdoch109988c2016-05-18 11:27:45 +0100233DEFINE_OPERATORS_FOR_FLAGS(VirtualObject::StatusFlags)
Ben Murdoch014dc512016-03-22 12:00:34 +0000234
235bool VirtualObject::UpdateFrom(const VirtualObject& other) {
236 bool changed = status_ != other.status_;
237 status_ = other.status_;
Ben Murdoch109988c2016-05-18 11:27:45 +0100238 phi_ = other.phi_;
Ben Murdoch014dc512016-03-22 12:00:34 +0000239 if (fields_.size() != other.fields_.size()) {
240 fields_ = other.fields_;
241 return true;
242 }
243 for (size_t i = 0; i < fields_.size(); ++i) {
244 if (fields_[i] != other.fields_[i]) {
245 changed = true;
246 fields_[i] = other.fields_[i];
247 }
248 }
249 return changed;
250}
251
Ben Murdoch014dc512016-03-22 12:00:34 +0000252class VirtualState : public ZoneObject {
253 public:
Ben Murdoch109988c2016-05-18 11:27:45 +0100254 VirtualState(Node* owner, Zone* zone, size_t size)
255 : info_(size, nullptr, zone), owner_(owner) {}
256
257 VirtualState(Node* owner, const VirtualState& state)
258 : info_(state.info_.size(), nullptr, state.info_.get_allocator().zone()),
259 owner_(owner) {
260 for (size_t i = 0; i < info_.size(); ++i) {
261 if (state.info_[i]) {
262 info_[i] = state.info_[i];
263 }
264 }
265 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000266
267 VirtualObject* VirtualObjectFromAlias(size_t alias);
Ben Murdoch109988c2016-05-18 11:27:45 +0100268 void SetVirtualObject(Alias alias, VirtualObject* state);
Ben Murdoch014dc512016-03-22 12:00:34 +0000269 bool UpdateFrom(VirtualState* state, Zone* zone);
270 bool MergeFrom(MergeCache* cache, Zone* zone, Graph* graph,
Ben Murdoch109988c2016-05-18 11:27:45 +0100271 CommonOperatorBuilder* common, Node* at);
Ben Murdoch014dc512016-03-22 12:00:34 +0000272 size_t size() const { return info_.size(); }
Ben Murdoch109988c2016-05-18 11:27:45 +0100273 Node* owner() const { return owner_; }
274 VirtualObject* Copy(VirtualObject* obj, Alias alias);
275 void SetCopyRequired() {
276 for (VirtualObject* obj : info_) {
277 if (obj) obj->SetCopyRequired();
278 }
279 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000280
281 private:
282 ZoneVector<VirtualObject*> info_;
Ben Murdoch109988c2016-05-18 11:27:45 +0100283 Node* owner_;
Ben Murdoch014dc512016-03-22 12:00:34 +0000284
Ben Murdoch109988c2016-05-18 11:27:45 +0100285 DISALLOW_COPY_AND_ASSIGN(VirtualState);
286};
Ben Murdochf2e39942016-05-18 10:25:55 +0000287
Ben Murdoch014dc512016-03-22 12:00:34 +0000288class MergeCache : public ZoneObject {
289 public:
290 explicit MergeCache(Zone* zone)
291 : states_(zone), objects_(zone), fields_(zone) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100292 states_.reserve(5);
293 objects_.reserve(5);
294 fields_.reserve(5);
Ben Murdoch014dc512016-03-22 12:00:34 +0000295 }
296 ZoneVector<VirtualState*>& states() { return states_; }
297 ZoneVector<VirtualObject*>& objects() { return objects_; }
298 ZoneVector<Node*>& fields() { return fields_; }
299 void Clear() {
300 states_.clear();
301 objects_.clear();
302 fields_.clear();
303 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100304 size_t LoadVirtualObjectsFromStatesFor(Alias alias);
305 void LoadVirtualObjectsForFieldsFrom(VirtualState* state,
306 const ZoneVector<Alias>& aliases);
Ben Murdoch014dc512016-03-22 12:00:34 +0000307 Node* GetFields(size_t pos);
308
309 private:
310 ZoneVector<VirtualState*> states_;
311 ZoneVector<VirtualObject*> objects_;
312 ZoneVector<Node*> fields_;
Ben Murdoch109988c2016-05-18 11:27:45 +0100313
314 DISALLOW_COPY_AND_ASSIGN(MergeCache);
Ben Murdoch014dc512016-03-22 12:00:34 +0000315};
316
Ben Murdoch109988c2016-05-18 11:27:45 +0100317size_t MergeCache::LoadVirtualObjectsFromStatesFor(Alias alias) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000318 objects_.clear();
319 DCHECK_GT(states_.size(), 0u);
320 size_t min = std::numeric_limits<size_t>::max();
321 for (VirtualState* state : states_) {
322 if (VirtualObject* obj = state->VirtualObjectFromAlias(alias)) {
323 objects_.push_back(obj);
324 min = std::min(obj->field_count(), min);
325 }
326 }
327 return min;
328}
329
Ben Murdoch014dc512016-03-22 12:00:34 +0000330void MergeCache::LoadVirtualObjectsForFieldsFrom(
Ben Murdoch109988c2016-05-18 11:27:45 +0100331 VirtualState* state, const ZoneVector<Alias>& aliases) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000332 objects_.clear();
333 size_t max_alias = state->size();
334 for (Node* field : fields_) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100335 Alias alias = aliases[field->id()];
Ben Murdoch014dc512016-03-22 12:00:34 +0000336 if (alias >= max_alias) continue;
337 if (VirtualObject* obj = state->VirtualObjectFromAlias(alias)) {
338 objects_.push_back(obj);
339 }
340 }
341}
342
Ben Murdoch014dc512016-03-22 12:00:34 +0000343Node* MergeCache::GetFields(size_t pos) {
344 fields_.clear();
Ben Murdoch109988c2016-05-18 11:27:45 +0100345 Node* rep = pos >= objects_.front()->field_count()
346 ? nullptr
347 : objects_.front()->GetField(pos);
Ben Murdoch014dc512016-03-22 12:00:34 +0000348 for (VirtualObject* obj : objects_) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100349 if (pos >= obj->field_count()) continue;
Ben Murdoch014dc512016-03-22 12:00:34 +0000350 Node* field = obj->GetField(pos);
351 if (field) {
352 fields_.push_back(field);
353 }
354 if (field != rep) {
355 rep = nullptr;
356 }
357 }
358 return rep;
359}
360
Ben Murdoch109988c2016-05-18 11:27:45 +0100361VirtualObject* VirtualState::Copy(VirtualObject* obj, Alias alias) {
362 if (obj->owner() == this) return obj;
363 VirtualObject* new_obj =
364 new (info_.get_allocator().zone()) VirtualObject(this, *obj);
365 TRACE("At state %p, alias @%d (#%d), copying virtual object from %p to %p\n",
366 static_cast<void*>(this), alias, obj->id(), static_cast<void*>(obj),
367 static_cast<void*>(new_obj));
368 info_[alias] = new_obj;
369 return new_obj;
Ben Murdoch014dc512016-03-22 12:00:34 +0000370}
371
Ben Murdoch014dc512016-03-22 12:00:34 +0000372VirtualObject* VirtualState::VirtualObjectFromAlias(size_t alias) {
373 return info_[alias];
374}
375
Ben Murdoch109988c2016-05-18 11:27:45 +0100376void VirtualState::SetVirtualObject(Alias alias, VirtualObject* obj) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000377 info_[alias] = obj;
378}
379
Ben Murdoch014dc512016-03-22 12:00:34 +0000380bool VirtualState::UpdateFrom(VirtualState* from, Zone* zone) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100381 if (from == this) return false;
Ben Murdoch014dc512016-03-22 12:00:34 +0000382 bool changed = false;
Ben Murdoch109988c2016-05-18 11:27:45 +0100383 for (Alias alias = 0; alias < size(); ++alias) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000384 VirtualObject* ls = VirtualObjectFromAlias(alias);
385 VirtualObject* rs = from->VirtualObjectFromAlias(alias);
386
Ben Murdoch109988c2016-05-18 11:27:45 +0100387 if (ls == rs || rs == nullptr) continue;
Ben Murdoch014dc512016-03-22 12:00:34 +0000388
389 if (ls == nullptr) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100390 ls = new (zone) VirtualObject(this, *rs);
Ben Murdoch014dc512016-03-22 12:00:34 +0000391 SetVirtualObject(alias, ls);
392 changed = true;
393 continue;
394 }
395
Ben Murdoch109988c2016-05-18 11:27:45 +0100396 TRACE(" Updating fields of @%d\n", alias);
Ben Murdoch014dc512016-03-22 12:00:34 +0000397
398 changed = ls->UpdateFrom(*rs) || changed;
399 }
400 return false;
401}
402
Ben Murdoch014dc512016-03-22 12:00:34 +0000403namespace {
404
405bool IsEquivalentPhi(Node* node1, Node* node2) {
406 if (node1 == node2) return true;
407 if (node1->opcode() != IrOpcode::kPhi || node2->opcode() != IrOpcode::kPhi ||
408 node1->op()->ValueInputCount() != node2->op()->ValueInputCount()) {
409 return false;
410 }
411 for (int i = 0; i < node1->op()->ValueInputCount(); ++i) {
412 Node* input1 = NodeProperties::GetValueInput(node1, i);
413 Node* input2 = NodeProperties::GetValueInput(node2, i);
414 if (!IsEquivalentPhi(input1, input2)) {
415 return false;
416 }
417 }
418 return true;
419}
420
Ben Murdoch014dc512016-03-22 12:00:34 +0000421bool IsEquivalentPhi(Node* phi, ZoneVector<Node*>& inputs) {
422 if (phi->opcode() != IrOpcode::kPhi) return false;
423 if (phi->op()->ValueInputCount() != inputs.size()) {
424 return false;
425 }
426 for (size_t i = 0; i < inputs.size(); ++i) {
427 Node* input = NodeProperties::GetValueInput(phi, static_cast<int>(i));
428 if (!IsEquivalentPhi(input, inputs[i])) {
429 return false;
430 }
431 }
432 return true;
433}
434
435} // namespace
436
Ben Murdoch109988c2016-05-18 11:27:45 +0100437bool VirtualObject::MergeFields(size_t i, Node* at, MergeCache* cache,
438 Graph* graph, CommonOperatorBuilder* common) {
439 bool changed = false;
440 int value_input_count = static_cast<int>(cache->fields().size());
441 Node* rep = GetField(i);
442 if (!rep || !IsCreatedPhi(i)) {
443 Node* control = NodeProperties::GetControlInput(at);
444 cache->fields().push_back(control);
445 Node* phi = graph->NewNode(
446 common->Phi(MachineRepresentation::kTagged, value_input_count),
447 value_input_count + 1, &cache->fields().front());
448 SetField(i, phi, true);
449#ifdef DEBUG
450 if (FLAG_trace_turbo_escape) {
451 PrintF(" Creating Phi #%d as merge of", phi->id());
452 for (int i = 0; i < value_input_count; i++) {
453 PrintF(" #%d (%s)", cache->fields()[i]->id(),
454 cache->fields()[i]->op()->mnemonic());
455 }
456 PrintF("\n");
457 }
458#endif
459 changed = true;
460 } else {
461 DCHECK(rep->opcode() == IrOpcode::kPhi);
462 for (int n = 0; n < value_input_count; ++n) {
463 Node* old = NodeProperties::GetValueInput(rep, n);
464 if (old != cache->fields()[n]) {
465 changed = true;
466 NodeProperties::ReplaceValueInput(rep, cache->fields()[n], n);
467 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000468 }
469 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100470 return changed;
Ben Murdoch014dc512016-03-22 12:00:34 +0000471}
472
Ben Murdoch109988c2016-05-18 11:27:45 +0100473bool VirtualObject::MergeFrom(MergeCache* cache, Node* at, Graph* graph,
474 CommonOperatorBuilder* common) {
475 DCHECK(at->opcode() == IrOpcode::kEffectPhi ||
476 at->opcode() == IrOpcode::kPhi);
477 bool changed = false;
478 for (size_t i = 0; i < field_count(); ++i) {
479 if (Node* field = cache->GetFields(i)) {
480 changed = changed || GetField(i) != field;
481 SetField(i, field);
482 TRACE(" Field %zu agree on rep #%d\n", i, field->id());
483 } else {
484 int arity = at->opcode() == IrOpcode::kEffectPhi
485 ? at->op()->EffectInputCount()
486 : at->op()->ValueInputCount();
487 if (cache->fields().size() == arity) {
488 changed = MergeFields(i, at, cache, graph, common) || changed;
489 } else {
490 if (GetField(i) != nullptr) {
491 TRACE(" Field %zu cleared\n", i);
492 changed = true;
493 }
494 SetField(i, nullptr);
495 }
496 }
497 }
498 return changed;
499}
Ben Murdoch014dc512016-03-22 12:00:34 +0000500
501bool VirtualState::MergeFrom(MergeCache* cache, Zone* zone, Graph* graph,
Ben Murdoch109988c2016-05-18 11:27:45 +0100502 CommonOperatorBuilder* common, Node* at) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000503 DCHECK_GT(cache->states().size(), 0u);
504 bool changed = false;
Ben Murdoch109988c2016-05-18 11:27:45 +0100505 for (Alias alias = 0; alias < size(); ++alias) {
506 cache->objects().clear();
507 VirtualObject* mergeObject = VirtualObjectFromAlias(alias);
508 bool copy_merge_object = false;
509 size_t fields = std::numeric_limits<size_t>::max();
510 for (VirtualState* state : cache->states()) {
511 if (VirtualObject* obj = state->VirtualObjectFromAlias(alias)) {
512 cache->objects().push_back(obj);
513 if (mergeObject == obj) {
514 copy_merge_object = true;
Ben Murdoch83897452016-05-17 11:12:09 +0100515 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100516 fields = std::min(obj->field_count(), fields);
Ben Murdoch83897452016-05-17 11:12:09 +0100517 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100518 }
519 if (cache->objects().size() == cache->states().size()) {
520 if (!mergeObject) {
521 VirtualObject* obj = new (zone)
522 VirtualObject(cache->objects().front()->id(), this, zone, fields,
523 cache->objects().front()->IsInitialized());
524 SetVirtualObject(alias, obj);
525 mergeObject = obj;
526 changed = true;
527 } else if (copy_merge_object) {
528 VirtualObject* obj = new (zone) VirtualObject(this, *mergeObject);
529 SetVirtualObject(alias, obj);
530 mergeObject = obj;
531 changed = true;
532 } else {
533 changed = mergeObject->ResizeFields(fields) || changed;
534 }
535#ifdef DEBUG
536 if (FLAG_trace_turbo_escape) {
537 PrintF(" Alias @%d, merging into %p virtual objects", alias,
538 static_cast<void*>(mergeObject));
539 for (size_t i = 0; i < cache->objects().size(); i++) {
540 PrintF(" %p", static_cast<void*>(cache->objects()[i]));
541 }
542 PrintF("\n");
543 }
544#endif // DEBUG
545 changed = mergeObject->MergeFrom(cache, at, graph, common) || changed;
Ben Murdoch014dc512016-03-22 12:00:34 +0000546 } else {
Ben Murdoch109988c2016-05-18 11:27:45 +0100547 if (mergeObject) {
548 TRACE(" Alias %d, virtual object removed\n", alias);
549 changed = true;
550 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000551 SetVirtualObject(alias, nullptr);
552 }
553 }
554 return changed;
555}
556
Ben Murdoch014dc512016-03-22 12:00:34 +0000557EscapeStatusAnalysis::EscapeStatusAnalysis(EscapeAnalysis* object_analysis,
558 Graph* graph, Zone* zone)
Ben Murdoch109988c2016-05-18 11:27:45 +0100559 : stack_(zone),
560 object_analysis_(object_analysis),
Ben Murdoch014dc512016-03-22 12:00:34 +0000561 graph_(graph),
Ben Murdoch109988c2016-05-18 11:27:45 +0100562 status_(zone),
563 next_free_alias_(0),
564 status_stack_(zone),
565 aliases_(zone) {}
Ben Murdoch014dc512016-03-22 12:00:34 +0000566
Ben Murdoch014dc512016-03-22 12:00:34 +0000567bool EscapeStatusAnalysis::HasEntry(Node* node) {
568 return status_[node->id()] & (kTracked | kEscaped);
569}
570
Ben Murdoch014dc512016-03-22 12:00:34 +0000571bool EscapeStatusAnalysis::IsVirtual(Node* node) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100572 return IsVirtual(node->id());
Ben Murdoch014dc512016-03-22 12:00:34 +0000573}
574
Ben Murdoch109988c2016-05-18 11:27:45 +0100575bool EscapeStatusAnalysis::IsVirtual(NodeId id) {
576 return (status_[id] & kTracked) && !(status_[id] & kEscaped);
577}
Ben Murdoch014dc512016-03-22 12:00:34 +0000578
579bool EscapeStatusAnalysis::IsEscaped(Node* node) {
580 return status_[node->id()] & kEscaped;
581}
582
Ben Murdoch014dc512016-03-22 12:00:34 +0000583bool EscapeStatusAnalysis::IsAllocation(Node* node) {
584 return node->opcode() == IrOpcode::kAllocate ||
585 node->opcode() == IrOpcode::kFinishRegion;
586}
587
Ben Murdoch014dc512016-03-22 12:00:34 +0000588bool EscapeStatusAnalysis::SetEscaped(Node* node) {
589 bool changed = !(status_[node->id()] & kEscaped);
590 status_[node->id()] |= kEscaped | kTracked;
591 return changed;
592}
593
Ben Murdoch109988c2016-05-18 11:27:45 +0100594bool EscapeStatusAnalysis::IsInQueue(NodeId id) {
595 return status_[id] & kInQueue;
Ben Murdoch014dc512016-03-22 12:00:34 +0000596}
597
Ben Murdoch109988c2016-05-18 11:27:45 +0100598void EscapeStatusAnalysis::SetInQueue(NodeId id, bool on_stack) {
599 if (on_stack) {
600 status_[id] |= kInQueue;
601 } else {
602 status_[id] &= ~kInQueue;
Ben Murdoch83897452016-05-17 11:12:09 +0100603 }
604}
605
Ben Murdoch109988c2016-05-18 11:27:45 +0100606void EscapeStatusAnalysis::ResizeStatusVector() {
607 if (status_.size() <= graph()->NodeCount()) {
608 status_.resize(graph()->NodeCount() * 1.1, kUnknown);
609 }
610}
611
612size_t EscapeStatusAnalysis::GetStatusVectorSize() { return status_.size(); }
613
614void EscapeStatusAnalysis::RunStatusAnalysis() {
615 ResizeStatusVector();
616 while (!status_stack_.empty()) {
617 Node* node = status_stack_.back();
618 status_stack_.pop_back();
619 status_[node->id()] &= ~kOnStack;
620 Process(node);
621 status_[node->id()] |= kVisited;
622 }
623}
624
625void EscapeStatusAnalysis::EnqueueForStatusAnalysis(Node* node) {
626 DCHECK_NOT_NULL(node);
627 if (!(status_[node->id()] & kOnStack)) {
628 status_stack_.push_back(node);
629 status_[node->id()] |= kOnStack;
630 }
631}
Ben Murdoch014dc512016-03-22 12:00:34 +0000632
633void EscapeStatusAnalysis::RevisitInputs(Node* node) {
634 for (Edge edge : node->input_edges()) {
635 Node* input = edge.to();
636 if (!(status_[input->id()] & kOnStack)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100637 status_stack_.push_back(input);
Ben Murdoch014dc512016-03-22 12:00:34 +0000638 status_[input->id()] |= kOnStack;
639 }
640 }
641}
642
Ben Murdoch014dc512016-03-22 12:00:34 +0000643void EscapeStatusAnalysis::RevisitUses(Node* node) {
644 for (Edge edge : node->use_edges()) {
645 Node* use = edge.from();
Ben Murdoch109988c2016-05-18 11:27:45 +0100646 if (!(status_[use->id()] & kOnStack) && !IsNotReachable(use)) {
647 status_stack_.push_back(use);
Ben Murdoch014dc512016-03-22 12:00:34 +0000648 status_[use->id()] |= kOnStack;
649 }
650 }
651}
652
Ben Murdoch014dc512016-03-22 12:00:34 +0000653void EscapeStatusAnalysis::Process(Node* node) {
654 switch (node->opcode()) {
655 case IrOpcode::kAllocate:
656 ProcessAllocate(node);
657 break;
658 case IrOpcode::kFinishRegion:
659 ProcessFinishRegion(node);
660 break;
661 case IrOpcode::kStoreField:
662 ProcessStoreField(node);
663 break;
664 case IrOpcode::kStoreElement:
665 ProcessStoreElement(node);
666 break;
667 case IrOpcode::kLoadField:
668 case IrOpcode::kLoadElement: {
669 if (Node* rep = object_analysis_->GetReplacement(node)) {
670 if (IsAllocation(rep) && CheckUsesForEscape(node, rep)) {
671 RevisitInputs(rep);
672 RevisitUses(rep);
673 }
674 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100675 RevisitUses(node);
Ben Murdoch014dc512016-03-22 12:00:34 +0000676 break;
677 }
678 case IrOpcode::kPhi:
679 if (!HasEntry(node)) {
680 status_[node->id()] |= kTracked;
Ben Murdoch109988c2016-05-18 11:27:45 +0100681 RevisitUses(node);
682 }
683 if (!IsAllocationPhi(node) && SetEscaped(node)) {
684 RevisitInputs(node);
685 RevisitUses(node);
Ben Murdoch014dc512016-03-22 12:00:34 +0000686 }
687 CheckUsesForEscape(node);
688 default:
689 break;
690 }
691}
692
Ben Murdoch014dc512016-03-22 12:00:34 +0000693bool EscapeStatusAnalysis::IsAllocationPhi(Node* node) {
694 for (Edge edge : node->input_edges()) {
695 Node* input = edge.to();
696 if (input->opcode() == IrOpcode::kPhi && !IsEscaped(input)) continue;
697 if (IsAllocation(input)) continue;
698 return false;
699 }
700 return true;
701}
702
Ben Murdoch014dc512016-03-22 12:00:34 +0000703void EscapeStatusAnalysis::ProcessStoreField(Node* node) {
704 DCHECK_EQ(node->opcode(), IrOpcode::kStoreField);
705 Node* to = NodeProperties::GetValueInput(node, 0);
706 Node* val = NodeProperties::GetValueInput(node, 1);
707 if ((IsEscaped(to) || !IsAllocation(to)) && SetEscaped(val)) {
708 RevisitUses(val);
709 RevisitInputs(val);
Ben Murdoch109988c2016-05-18 11:27:45 +0100710 TRACE("Setting #%d (%s) to escaped because of store to field of #%d\n",
711 val->id(), val->op()->mnemonic(), to->id());
Ben Murdoch014dc512016-03-22 12:00:34 +0000712 }
713}
714
Ben Murdoch014dc512016-03-22 12:00:34 +0000715void EscapeStatusAnalysis::ProcessStoreElement(Node* node) {
716 DCHECK_EQ(node->opcode(), IrOpcode::kStoreElement);
717 Node* to = NodeProperties::GetValueInput(node, 0);
718 Node* val = NodeProperties::GetValueInput(node, 2);
719 if ((IsEscaped(to) || !IsAllocation(to)) && SetEscaped(val)) {
720 RevisitUses(val);
721 RevisitInputs(val);
Ben Murdoch109988c2016-05-18 11:27:45 +0100722 TRACE("Setting #%d (%s) to escaped because of store to field of #%d\n",
723 val->id(), val->op()->mnemonic(), to->id());
Ben Murdoch014dc512016-03-22 12:00:34 +0000724 }
725}
726
Ben Murdoch014dc512016-03-22 12:00:34 +0000727void EscapeStatusAnalysis::ProcessAllocate(Node* node) {
728 DCHECK_EQ(node->opcode(), IrOpcode::kAllocate);
729 if (!HasEntry(node)) {
730 status_[node->id()] |= kTracked;
Ben Murdoch109988c2016-05-18 11:27:45 +0100731 TRACE("Created status entry for node #%d (%s)\n", node->id(),
732 node->op()->mnemonic());
Ben Murdoch014dc512016-03-22 12:00:34 +0000733 NumberMatcher size(node->InputAt(0));
734 DCHECK(node->InputAt(0)->opcode() != IrOpcode::kInt32Constant &&
735 node->InputAt(0)->opcode() != IrOpcode::kInt64Constant &&
736 node->InputAt(0)->opcode() != IrOpcode::kFloat32Constant &&
737 node->InputAt(0)->opcode() != IrOpcode::kFloat64Constant);
Ben Murdoch109988c2016-05-18 11:27:45 +0100738 RevisitUses(node);
Ben Murdoch014dc512016-03-22 12:00:34 +0000739 if (!size.HasValue() && SetEscaped(node)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100740 TRACE("Setting #%d to escaped because of non-const alloc\n", node->id());
741 // This node is already known to escape, uses do not have to be checked
742 // for escape.
Ben Murdoch014dc512016-03-22 12:00:34 +0000743 return;
744 }
745 }
746 if (CheckUsesForEscape(node, true)) {
747 RevisitUses(node);
748 }
749}
750
Ben Murdoch014dc512016-03-22 12:00:34 +0000751bool EscapeStatusAnalysis::CheckUsesForEscape(Node* uses, Node* rep,
752 bool phi_escaping) {
753 for (Edge edge : uses->use_edges()) {
754 Node* use = edge.from();
Ben Murdoch109988c2016-05-18 11:27:45 +0100755 if (IsNotReachable(use)) continue;
Ben Murdoch014dc512016-03-22 12:00:34 +0000756 if (edge.index() >= use->op()->ValueInputCount() +
757 OperatorProperties::GetContextInputCount(use->op()))
758 continue;
759 switch (use->opcode()) {
760 case IrOpcode::kPhi:
761 if (phi_escaping && SetEscaped(rep)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100762 TRACE(
763 "Setting #%d (%s) to escaped because of use by phi node "
764 "#%d (%s)\n",
765 rep->id(), rep->op()->mnemonic(), use->id(),
766 use->op()->mnemonic());
Ben Murdoch014dc512016-03-22 12:00:34 +0000767 return true;
768 }
769 // Fallthrough.
770 case IrOpcode::kStoreField:
771 case IrOpcode::kLoadField:
772 case IrOpcode::kStoreElement:
773 case IrOpcode::kLoadElement:
774 case IrOpcode::kFrameState:
775 case IrOpcode::kStateValues:
776 case IrOpcode::kReferenceEqual:
777 case IrOpcode::kFinishRegion:
778 if (IsEscaped(use) && SetEscaped(rep)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100779 TRACE(
780 "Setting #%d (%s) to escaped because of use by escaping node "
781 "#%d (%s)\n",
782 rep->id(), rep->op()->mnemonic(), use->id(),
783 use->op()->mnemonic());
Ben Murdoch014dc512016-03-22 12:00:34 +0000784 return true;
785 }
786 break;
787 case IrOpcode::kObjectIsSmi:
788 if (!IsAllocation(rep) && SetEscaped(rep)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100789 TRACE("Setting #%d (%s) to escaped because of use by #%d (%s)\n",
790 rep->id(), rep->op()->mnemonic(), use->id(),
791 use->op()->mnemonic());
792 return true;
793 }
794 break;
795 case IrOpcode::kSelect:
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100796 // TODO(mstarzinger): The following list of operators will eventually be
797 // handled by the EscapeAnalysisReducer (similar to ObjectIsSmi).
798 case IrOpcode::kObjectIsCallable:
799 case IrOpcode::kObjectIsNumber:
800 case IrOpcode::kObjectIsString:
801 case IrOpcode::kObjectIsUndetectable:
Ben Murdoch109988c2016-05-18 11:27:45 +0100802 if (SetEscaped(rep)) {
803 TRACE("Setting #%d (%s) to escaped because of use by #%d (%s)\n",
804 rep->id(), rep->op()->mnemonic(), use->id(),
805 use->op()->mnemonic());
Ben Murdoch014dc512016-03-22 12:00:34 +0000806 return true;
807 }
808 break;
809 default:
810 if (use->op()->EffectInputCount() == 0 &&
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100811 uses->op()->EffectInputCount() > 0 &&
812 !IrOpcode::IsJsOpcode(use->opcode())) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100813 TRACE("Encountered unaccounted use by #%d (%s)\n", use->id(),
814 use->op()->mnemonic());
Ben Murdoch014dc512016-03-22 12:00:34 +0000815 UNREACHABLE();
816 }
817 if (SetEscaped(rep)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100818 TRACE("Setting #%d (%s) to escaped because of use by #%d (%s)\n",
819 rep->id(), rep->op()->mnemonic(), use->id(),
820 use->op()->mnemonic());
Ben Murdoch014dc512016-03-22 12:00:34 +0000821 return true;
822 }
823 }
824 }
825 return false;
826}
827
Ben Murdoch014dc512016-03-22 12:00:34 +0000828void EscapeStatusAnalysis::ProcessFinishRegion(Node* node) {
829 DCHECK_EQ(node->opcode(), IrOpcode::kFinishRegion);
830 if (!HasEntry(node)) {
831 status_[node->id()] |= kTracked;
832 RevisitUses(node);
833 }
834 if (CheckUsesForEscape(node, true)) {
835 RevisitInputs(node);
836 }
837}
838
Ben Murdoch014dc512016-03-22 12:00:34 +0000839void EscapeStatusAnalysis::DebugPrint() {
840 for (NodeId id = 0; id < status_.size(); id++) {
841 if (status_[id] & kTracked) {
842 PrintF("Node #%d is %s\n", id,
843 (status_[id] & kEscaped) ? "escaping" : "virtual");
844 }
845 }
846}
847
Ben Murdoch014dc512016-03-22 12:00:34 +0000848EscapeAnalysis::EscapeAnalysis(Graph* graph, CommonOperatorBuilder* common,
849 Zone* zone)
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100850 : zone_(zone),
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100851 slot_not_analyzed_(graph->NewNode(common->NumberConstant(0x1c0debad))),
Ben Murdoch014dc512016-03-22 12:00:34 +0000852 common_(common),
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100853 status_analysis_(new (zone) EscapeStatusAnalysis(this, graph, zone)),
Ben Murdoch014dc512016-03-22 12:00:34 +0000854 virtual_states_(zone),
855 replacements_(zone),
Ben Murdoch109988c2016-05-18 11:27:45 +0100856 cache_(nullptr) {}
Ben Murdoch014dc512016-03-22 12:00:34 +0000857
858EscapeAnalysis::~EscapeAnalysis() {}
859
Ben Murdoch014dc512016-03-22 12:00:34 +0000860void EscapeAnalysis::Run() {
861 replacements_.resize(graph()->NodeCount());
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100862 status_analysis_->AssignAliases();
863 if (status_analysis_->AliasCount() > 0) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100864 cache_ = new (zone()) MergeCache(zone());
865 replacements_.resize(graph()->NodeCount());
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100866 status_analysis_->ResizeStatusVector();
Ben Murdoch109988c2016-05-18 11:27:45 +0100867 RunObjectAnalysis();
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100868 status_analysis_->RunStatusAnalysis();
Ben Murdoch109988c2016-05-18 11:27:45 +0100869 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000870}
871
Ben Murdoch109988c2016-05-18 11:27:45 +0100872void EscapeStatusAnalysis::AssignAliases() {
873 size_t max_size = 1024;
874 size_t min_size = 32;
875 size_t stack_size =
876 std::min(std::max(graph()->NodeCount() / 5, min_size), max_size);
877 stack_.reserve(stack_size);
878 ResizeStatusVector();
879 stack_.push_back(graph()->end());
Ben Murdoch014dc512016-03-22 12:00:34 +0000880 CHECK_LT(graph()->NodeCount(), kUntrackable);
881 aliases_.resize(graph()->NodeCount(), kNotReachable);
882 aliases_[graph()->end()->id()] = kUntrackable;
Ben Murdoch109988c2016-05-18 11:27:45 +0100883 status_stack_.reserve(8);
884 TRACE("Discovering trackable nodes");
885 while (!stack_.empty()) {
886 Node* node = stack_.back();
887 stack_.pop_back();
Ben Murdoch014dc512016-03-22 12:00:34 +0000888 switch (node->opcode()) {
889 case IrOpcode::kAllocate:
890 if (aliases_[node->id()] >= kUntrackable) {
891 aliases_[node->id()] = NextAlias();
Ben Murdoch109988c2016-05-18 11:27:45 +0100892 TRACE(" @%d:%s#%u", aliases_[node->id()], node->op()->mnemonic(),
893 node->id());
894 EnqueueForStatusAnalysis(node);
Ben Murdoch014dc512016-03-22 12:00:34 +0000895 }
896 break;
897 case IrOpcode::kFinishRegion: {
898 Node* allocate = NodeProperties::GetValueInput(node, 0);
Ben Murdoch109988c2016-05-18 11:27:45 +0100899 DCHECK_NOT_NULL(allocate);
Ben Murdoch014dc512016-03-22 12:00:34 +0000900 if (allocate->opcode() == IrOpcode::kAllocate) {
901 if (aliases_[allocate->id()] >= kUntrackable) {
902 if (aliases_[allocate->id()] == kNotReachable) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100903 stack_.push_back(allocate);
Ben Murdoch014dc512016-03-22 12:00:34 +0000904 }
905 aliases_[allocate->id()] = NextAlias();
Ben Murdoch109988c2016-05-18 11:27:45 +0100906 TRACE(" @%d:%s#%u", aliases_[allocate->id()],
907 allocate->op()->mnemonic(), allocate->id());
908 EnqueueForStatusAnalysis(allocate);
Ben Murdoch014dc512016-03-22 12:00:34 +0000909 }
910 aliases_[node->id()] = aliases_[allocate->id()];
Ben Murdoch109988c2016-05-18 11:27:45 +0100911 TRACE(" @%d:%s#%u", aliases_[node->id()], node->op()->mnemonic(),
912 node->id());
Ben Murdoch014dc512016-03-22 12:00:34 +0000913 }
914 break;
915 }
916 default:
917 DCHECK_EQ(aliases_[node->id()], kUntrackable);
918 break;
919 }
920 for (Edge edge : node->input_edges()) {
921 Node* input = edge.to();
922 if (aliases_[input->id()] == kNotReachable) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100923 stack_.push_back(input);
Ben Murdoch014dc512016-03-22 12:00:34 +0000924 aliases_[input->id()] = kUntrackable;
925 }
926 }
927 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100928 TRACE("\n");
Ben Murdoch014dc512016-03-22 12:00:34 +0000929}
930
Ben Murdoch109988c2016-05-18 11:27:45 +0100931bool EscapeStatusAnalysis::IsNotReachable(Node* node) {
932 if (node->id() >= aliases_.size()) {
933 return false;
934 }
935 return aliases_[node->id()] == kNotReachable;
936}
Ben Murdoch014dc512016-03-22 12:00:34 +0000937
938void EscapeAnalysis::RunObjectAnalysis() {
939 virtual_states_.resize(graph()->NodeCount());
Ben Murdoch109988c2016-05-18 11:27:45 +0100940 ZoneDeque<Node*> queue(zone());
941 queue.push_back(graph()->start());
942 ZoneVector<Node*> danglers(zone());
943 while (!queue.empty()) {
944 Node* node = queue.back();
945 queue.pop_back();
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100946 status_analysis_->SetInQueue(node->id(), false);
Ben Murdoch109988c2016-05-18 11:27:45 +0100947 if (Process(node)) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000948 for (Edge edge : node->use_edges()) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100949 Node* use = edge.from();
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100950 if (status_analysis_->IsNotReachable(use)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100951 continue;
952 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000953 if (NodeProperties::IsEffectEdge(edge)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100954 // Iteration order: depth first, but delay phis.
955 // We need DFS do avoid some duplication of VirtualStates and
956 // VirtualObjects, and we want to delay phis to improve performance.
957 if (use->opcode() == IrOpcode::kEffectPhi) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100958 if (!status_analysis_->IsInQueue(use->id())) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100959 queue.push_front(use);
960 }
961 } else if ((use->opcode() != IrOpcode::kLoadField &&
962 use->opcode() != IrOpcode::kLoadElement) ||
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100963 !status_analysis_->IsDanglingEffectNode(use)) {
964 if (!status_analysis_->IsInQueue(use->id())) {
965 status_analysis_->SetInQueue(use->id(), true);
Ben Murdoch109988c2016-05-18 11:27:45 +0100966 queue.push_back(use);
967 }
968 } else {
969 danglers.push_back(use);
Ben Murdoch014dc512016-03-22 12:00:34 +0000970 }
971 }
972 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100973 // Danglers need to be processed immediately, even if they are
974 // on the stack. Since they do not have effect outputs,
975 // we don't have to track whether they are on the stack.
976 queue.insert(queue.end(), danglers.begin(), danglers.end());
977 danglers.clear();
Ben Murdoch83897452016-05-17 11:12:09 +0100978 }
979 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100980#ifdef DEBUG
Ben Murdochf2e39942016-05-18 10:25:55 +0000981 if (FLAG_trace_turbo_escape) {
982 DebugPrint();
983 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100984#endif
Ben Murdoch83897452016-05-17 11:12:09 +0100985}
Ben Murdoch014dc512016-03-22 12:00:34 +0000986
Ben Murdoch109988c2016-05-18 11:27:45 +0100987bool EscapeStatusAnalysis::IsDanglingEffectNode(Node* node) {
988 if (status_[node->id()] & kDanglingComputed) {
989 return status_[node->id()] & kDangling;
990 }
991 if (node->op()->EffectInputCount() == 0 ||
992 node->op()->EffectOutputCount() == 0 ||
993 (node->op()->EffectInputCount() == 1 &&
994 NodeProperties::GetEffectInput(node)->opcode() == IrOpcode::kStart)) {
Ben Murdochf2e39942016-05-18 10:25:55 +0000995 // The start node is used as sentinel for nodes that are in general
996 // effectful, but of which an analysis has determined that they do not
997 // produce effects in this instance. We don't consider these nodes dangling.
Ben Murdoch109988c2016-05-18 11:27:45 +0100998 status_[node->id()] |= kDanglingComputed;
Ben Murdochf2e39942016-05-18 10:25:55 +0000999 return false;
1000 }
1001 for (Edge edge : node->use_edges()) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001002 Node* use = edge.from();
1003 if (aliases_[use->id()] == kNotReachable) continue;
Ben Murdochf2e39942016-05-18 10:25:55 +00001004 if (NodeProperties::IsEffectEdge(edge)) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001005 status_[node->id()] |= kDanglingComputed;
Ben Murdochf2e39942016-05-18 10:25:55 +00001006 return false;
1007 }
1008 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001009 status_[node->id()] |= kDanglingComputed | kDangling;
Ben Murdochf2e39942016-05-18 10:25:55 +00001010 return true;
1011}
1012
Ben Murdoch109988c2016-05-18 11:27:45 +01001013bool EscapeStatusAnalysis::IsEffectBranchPoint(Node* node) {
1014 if (status_[node->id()] & kBranchPointComputed) {
1015 return status_[node->id()] & kBranchPoint;
1016 }
1017 int count = 0;
1018 for (Edge edge : node->use_edges()) {
1019 Node* use = edge.from();
1020 if (aliases_[use->id()] == kNotReachable) continue;
1021 if (NodeProperties::IsEffectEdge(edge)) {
1022 if ((use->opcode() == IrOpcode::kLoadField ||
1023 use->opcode() == IrOpcode::kLoadElement ||
1024 use->opcode() == IrOpcode::kLoad) &&
1025 IsDanglingEffectNode(use))
1026 continue;
1027 if (++count > 1) {
1028 status_[node->id()] |= kBranchPointComputed | kBranchPoint;
1029 return true;
1030 }
1031 }
1032 }
1033 status_[node->id()] |= kBranchPointComputed;
1034 return false;
1035}
Ben Murdochf2e39942016-05-18 10:25:55 +00001036
Ben Murdoch014dc512016-03-22 12:00:34 +00001037bool EscapeAnalysis::Process(Node* node) {
1038 switch (node->opcode()) {
1039 case IrOpcode::kAllocate:
1040 ProcessAllocation(node);
1041 break;
1042 case IrOpcode::kBeginRegion:
1043 ForwardVirtualState(node);
1044 break;
1045 case IrOpcode::kFinishRegion:
1046 ProcessFinishRegion(node);
1047 break;
1048 case IrOpcode::kStoreField:
1049 ProcessStoreField(node);
1050 break;
1051 case IrOpcode::kLoadField:
1052 ProcessLoadField(node);
1053 break;
1054 case IrOpcode::kStoreElement:
1055 ProcessStoreElement(node);
1056 break;
1057 case IrOpcode::kLoadElement:
1058 ProcessLoadElement(node);
1059 break;
1060 case IrOpcode::kStart:
1061 ProcessStart(node);
1062 break;
1063 case IrOpcode::kEffectPhi:
1064 return ProcessEffectPhi(node);
1065 break;
1066 default:
1067 if (node->op()->EffectInputCount() > 0) {
1068 ForwardVirtualState(node);
1069 }
1070 ProcessAllocationUsers(node);
1071 break;
1072 }
1073 return true;
1074}
1075
Ben Murdoch014dc512016-03-22 12:00:34 +00001076void EscapeAnalysis::ProcessAllocationUsers(Node* node) {
1077 for (Edge edge : node->input_edges()) {
1078 Node* input = edge.to();
Ben Murdoch109988c2016-05-18 11:27:45 +01001079 Node* use = edge.from();
1080 if (edge.index() >= use->op()->ValueInputCount() +
1081 OperatorProperties::GetContextInputCount(use->op()))
Ben Murdoch014dc512016-03-22 12:00:34 +00001082 continue;
1083 switch (node->opcode()) {
1084 case IrOpcode::kStoreField:
1085 case IrOpcode::kLoadField:
1086 case IrOpcode::kStoreElement:
1087 case IrOpcode::kLoadElement:
1088 case IrOpcode::kFrameState:
1089 case IrOpcode::kStateValues:
1090 case IrOpcode::kReferenceEqual:
1091 case IrOpcode::kFinishRegion:
Ben Murdoch109988c2016-05-18 11:27:45 +01001092 case IrOpcode::kObjectIsSmi:
Ben Murdoch014dc512016-03-22 12:00:34 +00001093 break;
1094 default:
1095 VirtualState* state = virtual_states_[node->id()];
Ben Murdoch109988c2016-05-18 11:27:45 +01001096 if (VirtualObject* obj =
1097 GetVirtualObject(state, ResolveReplacement(input))) {
1098 if (!obj->AllFieldsClear()) {
1099 obj = CopyForModificationAt(obj, state, node);
1100 obj->ClearAllFields();
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001101 TRACE("Cleared all fields of @%d:#%d\n",
1102 status_analysis_->GetAlias(obj->id()), obj->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001103 }
1104 }
1105 break;
1106 }
1107 }
1108}
1109
Ben Murdoch109988c2016-05-18 11:27:45 +01001110VirtualState* EscapeAnalysis::CopyForModificationAt(VirtualState* state,
1111 Node* node) {
1112 if (state->owner() != node) {
1113 VirtualState* new_state = new (zone()) VirtualState(node, *state);
1114 virtual_states_[node->id()] = new_state;
1115 TRACE("Copying virtual state %p to new state %p at node %s#%d\n",
1116 static_cast<void*>(state), static_cast<void*>(new_state),
1117 node->op()->mnemonic(), node->id());
1118 return new_state;
Ben Murdoch014dc512016-03-22 12:00:34 +00001119 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001120 return state;
Ben Murdoch014dc512016-03-22 12:00:34 +00001121}
1122
Ben Murdoch109988c2016-05-18 11:27:45 +01001123VirtualObject* EscapeAnalysis::CopyForModificationAt(VirtualObject* obj,
1124 VirtualState* state,
1125 Node* node) {
1126 if (obj->NeedCopyForModification()) {
1127 state = CopyForModificationAt(state, node);
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001128 return state->Copy(obj, status_analysis_->GetAlias(obj->id()));
Ben Murdoch109988c2016-05-18 11:27:45 +01001129 }
1130 return obj;
1131}
Ben Murdoch014dc512016-03-22 12:00:34 +00001132
1133void EscapeAnalysis::ForwardVirtualState(Node* node) {
1134 DCHECK_EQ(node->op()->EffectInputCount(), 1);
Ben Murdoch109988c2016-05-18 11:27:45 +01001135#ifdef DEBUG
Ben Murdoch014dc512016-03-22 12:00:34 +00001136 if (node->opcode() != IrOpcode::kLoadField &&
1137 node->opcode() != IrOpcode::kLoadElement &&
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001138 node->opcode() != IrOpcode::kLoad &&
1139 status_analysis_->IsDanglingEffectNode(node)) {
Ben Murdoch014dc512016-03-22 12:00:34 +00001140 PrintF("Dangeling effect node: #%d (%s)\n", node->id(),
1141 node->op()->mnemonic());
1142 UNREACHABLE();
1143 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001144#endif // DEBUG
Ben Murdoch014dc512016-03-22 12:00:34 +00001145 Node* effect = NodeProperties::GetEffectInput(node);
Ben Murdoch014dc512016-03-22 12:00:34 +00001146 DCHECK_NOT_NULL(virtual_states_[effect->id()]);
Ben Murdoch109988c2016-05-18 11:27:45 +01001147 if (virtual_states_[node->id()]) {
1148 virtual_states_[node->id()]->UpdateFrom(virtual_states_[effect->id()],
1149 zone());
Ben Murdoch014dc512016-03-22 12:00:34 +00001150 } else {
1151 virtual_states_[node->id()] = virtual_states_[effect->id()];
Ben Murdoch109988c2016-05-18 11:27:45 +01001152 TRACE("Forwarding object state %p from %s#%d to %s#%d",
1153 static_cast<void*>(virtual_states_[effect->id()]),
1154 effect->op()->mnemonic(), effect->id(), node->op()->mnemonic(),
1155 node->id());
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001156 if (status_analysis_->IsEffectBranchPoint(effect) ||
Ben Murdochf91f0612016-11-29 16:50:11 +00001157 OperatorProperties::HasFrameStateInput(node->op())) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001158 virtual_states_[node->id()]->SetCopyRequired();
1159 TRACE(", effect input %s#%d is branch point", effect->op()->mnemonic(),
1160 effect->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001161 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001162 TRACE("\n");
Ben Murdoch014dc512016-03-22 12:00:34 +00001163 }
1164}
1165
Ben Murdoch014dc512016-03-22 12:00:34 +00001166void EscapeAnalysis::ProcessStart(Node* node) {
1167 DCHECK_EQ(node->opcode(), IrOpcode::kStart);
Ben Murdoch109988c2016-05-18 11:27:45 +01001168 virtual_states_[node->id()] =
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001169 new (zone()) VirtualState(node, zone(), status_analysis_->AliasCount());
Ben Murdoch014dc512016-03-22 12:00:34 +00001170}
1171
Ben Murdoch014dc512016-03-22 12:00:34 +00001172bool EscapeAnalysis::ProcessEffectPhi(Node* node) {
1173 DCHECK_EQ(node->opcode(), IrOpcode::kEffectPhi);
1174 bool changed = false;
1175
1176 VirtualState* mergeState = virtual_states_[node->id()];
1177 if (!mergeState) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001178 mergeState =
1179 new (zone()) VirtualState(node, zone(), status_analysis_->AliasCount());
Ben Murdoch014dc512016-03-22 12:00:34 +00001180 virtual_states_[node->id()] = mergeState;
1181 changed = true;
Ben Murdoch109988c2016-05-18 11:27:45 +01001182 TRACE("Effect Phi #%d got new virtual state %p.\n", node->id(),
1183 static_cast<void*>(mergeState));
Ben Murdoch014dc512016-03-22 12:00:34 +00001184 }
1185
1186 cache_->Clear();
1187
Ben Murdoch109988c2016-05-18 11:27:45 +01001188 TRACE("At Effect Phi #%d, merging states into %p:", node->id(),
1189 static_cast<void*>(mergeState));
Ben Murdoch014dc512016-03-22 12:00:34 +00001190
1191 for (int i = 0; i < node->op()->EffectInputCount(); ++i) {
1192 Node* input = NodeProperties::GetEffectInput(node, i);
1193 VirtualState* state = virtual_states_[input->id()];
1194 if (state) {
1195 cache_->states().push_back(state);
Ben Murdoch109988c2016-05-18 11:27:45 +01001196 if (state == mergeState) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001197 mergeState = new (zone())
1198 VirtualState(node, zone(), status_analysis_->AliasCount());
Ben Murdoch109988c2016-05-18 11:27:45 +01001199 virtual_states_[node->id()] = mergeState;
1200 changed = true;
1201 }
Ben Murdoch014dc512016-03-22 12:00:34 +00001202 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001203 TRACE(" %p (from %d %s)", static_cast<void*>(state), input->id(),
1204 input->op()->mnemonic());
Ben Murdoch014dc512016-03-22 12:00:34 +00001205 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001206 TRACE("\n");
Ben Murdoch014dc512016-03-22 12:00:34 +00001207
1208 if (cache_->states().size() == 0) {
1209 return changed;
1210 }
1211
Ben Murdoch109988c2016-05-18 11:27:45 +01001212 changed =
1213 mergeState->MergeFrom(cache_, zone(), graph(), common(), node) || changed;
Ben Murdoch014dc512016-03-22 12:00:34 +00001214
Ben Murdoch109988c2016-05-18 11:27:45 +01001215 TRACE("Merge %s the node.\n", changed ? "changed" : "did not change");
Ben Murdoch014dc512016-03-22 12:00:34 +00001216
1217 if (changed) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001218 status_analysis_->ResizeStatusVector();
Ben Murdoch014dc512016-03-22 12:00:34 +00001219 }
1220 return changed;
1221}
1222
Ben Murdoch014dc512016-03-22 12:00:34 +00001223void EscapeAnalysis::ProcessAllocation(Node* node) {
1224 DCHECK_EQ(node->opcode(), IrOpcode::kAllocate);
1225 ForwardVirtualState(node);
Ben Murdoch109988c2016-05-18 11:27:45 +01001226 VirtualState* state = virtual_states_[node->id()];
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001227 Alias alias = status_analysis_->GetAlias(node->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001228
1229 // Check if we have already processed this node.
Ben Murdoch109988c2016-05-18 11:27:45 +01001230 if (state->VirtualObjectFromAlias(alias)) {
Ben Murdoch014dc512016-03-22 12:00:34 +00001231 return;
1232 }
1233
Ben Murdoch109988c2016-05-18 11:27:45 +01001234 if (state->owner()->opcode() == IrOpcode::kEffectPhi) {
1235 state = CopyForModificationAt(state, node);
1236 }
1237
Ben Murdoch014dc512016-03-22 12:00:34 +00001238 NumberMatcher size(node->InputAt(0));
1239 DCHECK(node->InputAt(0)->opcode() != IrOpcode::kInt32Constant &&
1240 node->InputAt(0)->opcode() != IrOpcode::kInt64Constant &&
1241 node->InputAt(0)->opcode() != IrOpcode::kFloat32Constant &&
1242 node->InputAt(0)->opcode() != IrOpcode::kFloat64Constant);
1243 if (size.HasValue()) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001244 VirtualObject* obj = new (zone()) VirtualObject(
1245 node->id(), state, zone(), size.Value() / kPointerSize, false);
1246 state->SetVirtualObject(alias, obj);
Ben Murdoch014dc512016-03-22 12:00:34 +00001247 } else {
Ben Murdoch109988c2016-05-18 11:27:45 +01001248 state->SetVirtualObject(
1249 alias, new (zone()) VirtualObject(node->id(), state, zone()));
Ben Murdoch014dc512016-03-22 12:00:34 +00001250 }
Ben Murdoch014dc512016-03-22 12:00:34 +00001251}
1252
Ben Murdoch014dc512016-03-22 12:00:34 +00001253void EscapeAnalysis::ProcessFinishRegion(Node* node) {
1254 DCHECK_EQ(node->opcode(), IrOpcode::kFinishRegion);
1255 ForwardVirtualState(node);
1256 Node* allocation = NodeProperties::GetValueInput(node, 0);
1257 if (allocation->opcode() == IrOpcode::kAllocate) {
1258 VirtualState* state = virtual_states_[node->id()];
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001259 VirtualObject* obj =
1260 state->VirtualObjectFromAlias(status_analysis_->GetAlias(node->id()));
Ben Murdoch109988c2016-05-18 11:27:45 +01001261 DCHECK_NOT_NULL(obj);
1262 obj->SetInitialized();
Ben Murdoch014dc512016-03-22 12:00:34 +00001263 }
1264}
1265
Ben Murdoch014dc512016-03-22 12:00:34 +00001266Node* EscapeAnalysis::replacement(Node* node) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001267 if (node->id() >= replacements_.size()) return nullptr;
1268 return replacements_[node->id()];
Ben Murdoch014dc512016-03-22 12:00:34 +00001269}
1270
Ben Murdoch014dc512016-03-22 12:00:34 +00001271bool EscapeAnalysis::SetReplacement(Node* node, Node* rep) {
1272 bool changed = replacements_[node->id()] != rep;
1273 replacements_[node->id()] = rep;
1274 return changed;
1275}
1276
Ben Murdoch014dc512016-03-22 12:00:34 +00001277bool EscapeAnalysis::UpdateReplacement(VirtualState* state, Node* node,
1278 Node* rep) {
1279 if (SetReplacement(node, rep)) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001280 if (rep) {
1281 TRACE("Replacement of #%d is #%d (%s)\n", node->id(), rep->id(),
1282 rep->op()->mnemonic());
1283 } else {
1284 TRACE("Replacement of #%d cleared\n", node->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001285 }
1286 return true;
1287 }
1288 return false;
1289}
1290
Ben Murdoch014dc512016-03-22 12:00:34 +00001291Node* EscapeAnalysis::ResolveReplacement(Node* node) {
1292 while (replacement(node)) {
1293 node = replacement(node);
1294 }
1295 return node;
1296}
1297
Ben Murdoch014dc512016-03-22 12:00:34 +00001298Node* EscapeAnalysis::GetReplacement(Node* node) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001299 Node* result = nullptr;
1300 while (replacement(node)) {
1301 node = result = replacement(node);
Ben Murdoch014dc512016-03-22 12:00:34 +00001302 }
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001303 return result;
Ben Murdoch014dc512016-03-22 12:00:34 +00001304}
1305
Ben Murdoch014dc512016-03-22 12:00:34 +00001306bool EscapeAnalysis::IsVirtual(Node* node) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001307 if (node->id() >= status_analysis_->GetStatusVectorSize()) {
Ben Murdoch014dc512016-03-22 12:00:34 +00001308 return false;
1309 }
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001310 return status_analysis_->IsVirtual(node);
Ben Murdoch014dc512016-03-22 12:00:34 +00001311}
1312
Ben Murdoch014dc512016-03-22 12:00:34 +00001313bool EscapeAnalysis::IsEscaped(Node* node) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001314 if (node->id() >= status_analysis_->GetStatusVectorSize()) {
Ben Murdoch014dc512016-03-22 12:00:34 +00001315 return false;
1316 }
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001317 return status_analysis_->IsEscaped(node);
Ben Murdoch014dc512016-03-22 12:00:34 +00001318}
1319
Ben Murdoch014dc512016-03-22 12:00:34 +00001320bool EscapeAnalysis::CompareVirtualObjects(Node* left, Node* right) {
1321 DCHECK(IsVirtual(left) && IsVirtual(right));
1322 left = ResolveReplacement(left);
1323 right = ResolveReplacement(right);
1324 if (IsEquivalentPhi(left, right)) {
1325 return true;
1326 }
1327 return false;
1328}
1329
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001330namespace {
1331
1332int OffsetForFieldAccess(Node* node) {
1333 FieldAccess access = FieldAccessOf(node->op());
1334 DCHECK_EQ(access.offset % kPointerSize, 0);
1335 return access.offset / kPointerSize;
Ben Murdoch014dc512016-03-22 12:00:34 +00001336}
1337
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001338int OffsetForElementAccess(Node* node, int index) {
1339 ElementAccess access = ElementAccessOf(node->op());
1340 DCHECK_GE(ElementSizeLog2Of(access.machine_type.representation()),
1341 kPointerSizeLog2);
1342 DCHECK_EQ(access.header_size % kPointerSize, 0);
1343 return access.header_size / kPointerSize + index;
1344}
1345
1346} // namespace
1347
Ben Murdoch109988c2016-05-18 11:27:45 +01001348void EscapeAnalysis::ProcessLoadFromPhi(int offset, Node* from, Node* load,
Ben Murdoch014dc512016-03-22 12:00:34 +00001349 VirtualState* state) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001350 TRACE("Load #%d from phi #%d", load->id(), from->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001351
1352 cache_->fields().clear();
Ben Murdoch109988c2016-05-18 11:27:45 +01001353 for (int i = 0; i < load->op()->ValueInputCount(); ++i) {
1354 Node* input = NodeProperties::GetValueInput(load, i);
Ben Murdoch014dc512016-03-22 12:00:34 +00001355 cache_->fields().push_back(input);
1356 }
1357
Ben Murdoch109988c2016-05-18 11:27:45 +01001358 cache_->LoadVirtualObjectsForFieldsFrom(state,
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001359 status_analysis_->GetAliasMap());
Ben Murdoch014dc512016-03-22 12:00:34 +00001360 if (cache_->objects().size() == cache_->fields().size()) {
1361 cache_->GetFields(offset);
1362 if (cache_->fields().size() == cache_->objects().size()) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001363 Node* rep = replacement(load);
Ben Murdoch014dc512016-03-22 12:00:34 +00001364 if (!rep || !IsEquivalentPhi(rep, cache_->fields())) {
1365 int value_input_count = static_cast<int>(cache_->fields().size());
1366 cache_->fields().push_back(NodeProperties::GetControlInput(from));
1367 Node* phi = graph()->NewNode(
1368 common()->Phi(MachineRepresentation::kTagged, value_input_count),
1369 value_input_count + 1, &cache_->fields().front());
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001370 status_analysis_->ResizeStatusVector();
Ben Murdoch109988c2016-05-18 11:27:45 +01001371 SetReplacement(load, phi);
1372 TRACE(" got phi created.\n");
1373 } else {
1374 TRACE(" has already phi #%d.\n", rep->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001375 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001376 } else {
1377 TRACE(" has incomplete field info.\n");
Ben Murdoch014dc512016-03-22 12:00:34 +00001378 }
Ben Murdoch109988c2016-05-18 11:27:45 +01001379 } else {
1380 TRACE(" has incomplete virtual object info.\n");
Ben Murdoch014dc512016-03-22 12:00:34 +00001381 }
1382}
1383
Ben Murdoch014dc512016-03-22 12:00:34 +00001384void EscapeAnalysis::ProcessLoadField(Node* node) {
1385 DCHECK_EQ(node->opcode(), IrOpcode::kLoadField);
1386 ForwardVirtualState(node);
Ben Murdoch109988c2016-05-18 11:27:45 +01001387 Node* from = ResolveReplacement(NodeProperties::GetValueInput(node, 0));
Ben Murdoch014dc512016-03-22 12:00:34 +00001388 VirtualState* state = virtual_states_[node->id()];
Ben Murdoch109988c2016-05-18 11:27:45 +01001389 if (VirtualObject* object = GetVirtualObject(state, from)) {
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001390 if (!object->IsTracked()) return;
1391 int offset = OffsetForFieldAccess(node);
1392 if (static_cast<size_t>(offset) >= object->field_count()) return;
Ben Murdoch014dc512016-03-22 12:00:34 +00001393 Node* value = object->GetField(offset);
1394 if (value) {
1395 value = ResolveReplacement(value);
1396 }
1397 // Record that the load has this alias.
1398 UpdateReplacement(state, node, value);
Ben Murdoch109988c2016-05-18 11:27:45 +01001399 } else if (from->opcode() == IrOpcode::kPhi &&
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001400 FieldAccessOf(node->op()).offset % kPointerSize == 0) {
1401 int offset = OffsetForFieldAccess(node);
Ben Murdoch109988c2016-05-18 11:27:45 +01001402 // Only binary phis are supported for now.
1403 ProcessLoadFromPhi(offset, from, node, state);
Ben Murdoch014dc512016-03-22 12:00:34 +00001404 } else {
Ben Murdoch109988c2016-05-18 11:27:45 +01001405 UpdateReplacement(state, node, nullptr);
Ben Murdoch014dc512016-03-22 12:00:34 +00001406 }
1407}
1408
Ben Murdoch014dc512016-03-22 12:00:34 +00001409void EscapeAnalysis::ProcessLoadElement(Node* node) {
1410 DCHECK_EQ(node->opcode(), IrOpcode::kLoadElement);
1411 ForwardVirtualState(node);
Ben Murdoch109988c2016-05-18 11:27:45 +01001412 Node* from = ResolveReplacement(NodeProperties::GetValueInput(node, 0));
Ben Murdoch014dc512016-03-22 12:00:34 +00001413 VirtualState* state = virtual_states_[node->id()];
1414 Node* index_node = node->InputAt(1);
1415 NumberMatcher index(index_node);
1416 DCHECK(index_node->opcode() != IrOpcode::kInt32Constant &&
1417 index_node->opcode() != IrOpcode::kInt64Constant &&
1418 index_node->opcode() != IrOpcode::kFloat32Constant &&
1419 index_node->opcode() != IrOpcode::kFloat64Constant);
Ben Murdoch014dc512016-03-22 12:00:34 +00001420 if (index.HasValue()) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001421 if (VirtualObject* object = GetVirtualObject(state, from)) {
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001422 if (!object->IsTracked()) return;
1423 int offset = OffsetForElementAccess(node, index.Value());
1424 if (static_cast<size_t>(offset) >= object->field_count()) return;
Ben Murdoch014dc512016-03-22 12:00:34 +00001425 Node* value = object->GetField(offset);
1426 if (value) {
1427 value = ResolveReplacement(value);
1428 }
1429 // Record that the load has this alias.
1430 UpdateReplacement(state, node, value);
1431 } else if (from->opcode() == IrOpcode::kPhi) {
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001432 int offset = OffsetForElementAccess(node, index.Value());
Ben Murdoch014dc512016-03-22 12:00:34 +00001433 ProcessLoadFromPhi(offset, from, node, state);
Ben Murdoch109988c2016-05-18 11:27:45 +01001434 } else {
1435 UpdateReplacement(state, node, nullptr);
Ben Murdoch014dc512016-03-22 12:00:34 +00001436 }
1437 } else {
1438 // We have a load from a non-const index, cannot eliminate object.
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001439 if (status_analysis_->SetEscaped(from)) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001440 TRACE(
1441 "Setting #%d (%s) to escaped because load element #%d from non-const "
1442 "index #%d (%s)\n",
1443 from->id(), from->op()->mnemonic(), node->id(), index_node->id(),
1444 index_node->op()->mnemonic());
Ben Murdoch014dc512016-03-22 12:00:34 +00001445 }
1446 }
1447}
1448
Ben Murdoch014dc512016-03-22 12:00:34 +00001449void EscapeAnalysis::ProcessStoreField(Node* node) {
1450 DCHECK_EQ(node->opcode(), IrOpcode::kStoreField);
1451 ForwardVirtualState(node);
Ben Murdoch109988c2016-05-18 11:27:45 +01001452 Node* to = ResolveReplacement(NodeProperties::GetValueInput(node, 0));
Ben Murdoch014dc512016-03-22 12:00:34 +00001453 VirtualState* state = virtual_states_[node->id()];
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001454 if (VirtualObject* object = GetVirtualObject(state, to)) {
1455 if (!object->IsTracked()) return;
1456 int offset = OffsetForFieldAccess(node);
1457 if (static_cast<size_t>(offset) >= object->field_count()) return;
Ben Murdoch109988c2016-05-18 11:27:45 +01001458 Node* val = ResolveReplacement(NodeProperties::GetValueInput(node, 1));
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001459 // TODO(mstarzinger): The following is a workaround to not track the code
1460 // entry field in virtual JSFunction objects. We only ever store the inner
1461 // pointer into the compile lazy stub in this field and the deoptimizer has
1462 // this assumption hard-coded in {TranslatedState::MaterializeAt} as well.
1463 if (val->opcode() == IrOpcode::kInt32Constant ||
1464 val->opcode() == IrOpcode::kInt64Constant) {
1465 DCHECK_EQ(JSFunction::kCodeEntryOffset, FieldAccessOf(node->op()).offset);
1466 val = slot_not_analyzed_;
1467 }
1468 if (object->GetField(offset) != val) {
1469 object = CopyForModificationAt(object, state, node);
1470 object->SetField(offset, val);
Ben Murdoch014dc512016-03-22 12:00:34 +00001471 }
1472 }
1473}
1474
Ben Murdoch014dc512016-03-22 12:00:34 +00001475void EscapeAnalysis::ProcessStoreElement(Node* node) {
1476 DCHECK_EQ(node->opcode(), IrOpcode::kStoreElement);
1477 ForwardVirtualState(node);
Ben Murdoch109988c2016-05-18 11:27:45 +01001478 Node* to = ResolveReplacement(NodeProperties::GetValueInput(node, 0));
Ben Murdoch014dc512016-03-22 12:00:34 +00001479 Node* index_node = node->InputAt(1);
1480 NumberMatcher index(index_node);
1481 DCHECK(index_node->opcode() != IrOpcode::kInt32Constant &&
1482 index_node->opcode() != IrOpcode::kInt64Constant &&
1483 index_node->opcode() != IrOpcode::kFloat32Constant &&
1484 index_node->opcode() != IrOpcode::kFloat64Constant);
Ben Murdoch109988c2016-05-18 11:27:45 +01001485 VirtualState* state = virtual_states_[node->id()];
Ben Murdoch014dc512016-03-22 12:00:34 +00001486 if (index.HasValue()) {
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001487 if (VirtualObject* object = GetVirtualObject(state, to)) {
1488 if (!object->IsTracked()) return;
1489 int offset = OffsetForElementAccess(node, index.Value());
1490 if (static_cast<size_t>(offset) >= object->field_count()) return;
Ben Murdoch109988c2016-05-18 11:27:45 +01001491 Node* val = ResolveReplacement(NodeProperties::GetValueInput(node, 2));
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001492 if (object->GetField(offset) != val) {
1493 object = CopyForModificationAt(object, state, node);
1494 object->SetField(offset, val);
Ben Murdoch014dc512016-03-22 12:00:34 +00001495 }
1496 }
1497 } else {
1498 // We have a store to a non-const index, cannot eliminate object.
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001499 if (status_analysis_->SetEscaped(to)) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001500 TRACE(
1501 "Setting #%d (%s) to escaped because store element #%d to non-const "
1502 "index #%d (%s)\n",
1503 to->id(), to->op()->mnemonic(), node->id(), index_node->id(),
1504 index_node->op()->mnemonic());
1505 }
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001506 if (VirtualObject* object = GetVirtualObject(state, to)) {
1507 if (!object->IsTracked()) return;
1508 if (!object->AllFieldsClear()) {
1509 object = CopyForModificationAt(object, state, node);
1510 object->ClearAllFields();
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001511 TRACE("Cleared all fields of @%d:#%d\n",
Ben Murdoch13e2dad2016-09-16 13:49:30 +01001512 status_analysis_->GetAlias(object->id()), object->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001513 }
1514 }
1515 }
1516}
1517
Ben Murdoch014dc512016-03-22 12:00:34 +00001518Node* EscapeAnalysis::GetOrCreateObjectState(Node* effect, Node* node) {
1519 if ((node->opcode() == IrOpcode::kFinishRegion ||
1520 node->opcode() == IrOpcode::kAllocate) &&
1521 IsVirtual(node)) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001522 if (VirtualObject* vobj = GetVirtualObject(virtual_states_[effect->id()],
1523 ResolveReplacement(node))) {
Ben Murdoch014dc512016-03-22 12:00:34 +00001524 if (Node* object_state = vobj->GetObjectState()) {
1525 return object_state;
1526 } else {
1527 cache_->fields().clear();
1528 for (size_t i = 0; i < vobj->field_count(); ++i) {
1529 if (Node* field = vobj->GetField(i)) {
1530 cache_->fields().push_back(field);
1531 }
1532 }
1533 int input_count = static_cast<int>(cache_->fields().size());
1534 Node* new_object_state =
1535 graph()->NewNode(common()->ObjectState(input_count, vobj->id()),
1536 input_count, &cache_->fields().front());
1537 vobj->SetObjectState(new_object_state);
Ben Murdoch109988c2016-05-18 11:27:45 +01001538 TRACE(
1539 "Creating object state #%d for vobj %p (from node #%d) at effect "
1540 "#%d\n",
1541 new_object_state->id(), static_cast<void*>(vobj), node->id(),
1542 effect->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001543 // Now fix uses of other objects.
1544 for (size_t i = 0; i < vobj->field_count(); ++i) {
1545 if (Node* field = vobj->GetField(i)) {
1546 if (Node* field_object_state =
1547 GetOrCreateObjectState(effect, field)) {
1548 NodeProperties::ReplaceValueInput(
1549 new_object_state, field_object_state, static_cast<int>(i));
1550 }
1551 }
1552 }
1553 return new_object_state;
1554 }
1555 }
1556 }
1557 return nullptr;
1558}
1559
Ben Murdoch014dc512016-03-22 12:00:34 +00001560void EscapeAnalysis::DebugPrintState(VirtualState* state) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001561 PrintF("Dumping virtual state %p\n", static_cast<void*>(state));
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001562 for (Alias alias = 0; alias < status_analysis_->AliasCount(); ++alias) {
Ben Murdoch014dc512016-03-22 12:00:34 +00001563 if (VirtualObject* object = state->VirtualObjectFromAlias(alias)) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001564 PrintF(" Alias @%d: Object #%d with %zu fields\n", alias, object->id(),
1565 object->field_count());
1566 for (size_t i = 0; i < object->field_count(); ++i) {
1567 if (Node* f = object->GetField(i)) {
1568 PrintF(" Field %zu = #%d (%s)\n", i, f->id(), f->op()->mnemonic());
1569 }
1570 }
Ben Murdoch014dc512016-03-22 12:00:34 +00001571 }
1572 }
1573}
1574
Ben Murdoch014dc512016-03-22 12:00:34 +00001575void EscapeAnalysis::DebugPrint() {
1576 ZoneVector<VirtualState*> object_states(zone());
1577 for (NodeId id = 0; id < virtual_states_.size(); id++) {
1578 if (VirtualState* states = virtual_states_[id]) {
1579 if (std::find(object_states.begin(), object_states.end(), states) ==
1580 object_states.end()) {
1581 object_states.push_back(states);
1582 }
1583 }
1584 }
1585 for (size_t n = 0; n < object_states.size(); n++) {
1586 DebugPrintState(object_states[n]);
1587 }
1588}
1589
Ben Murdoch014dc512016-03-22 12:00:34 +00001590VirtualObject* EscapeAnalysis::GetVirtualObject(VirtualState* state,
1591 Node* node) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001592 if (node->id() >= status_analysis_->GetAliasMap().size()) return nullptr;
1593 Alias alias = status_analysis_->GetAlias(node->id());
Ben Murdoch014dc512016-03-22 12:00:34 +00001594 if (alias >= state->size()) return nullptr;
1595 return state->VirtualObjectFromAlias(alias);
1596}
1597
Ben Murdoch109988c2016-05-18 11:27:45 +01001598bool EscapeAnalysis::ExistsVirtualAllocate() {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001599 for (size_t id = 0; id < status_analysis_->GetAliasMap().size(); ++id) {
1600 Alias alias = status_analysis_->GetAlias(static_cast<NodeId>(id));
Ben Murdoch109988c2016-05-18 11:27:45 +01001601 if (alias < EscapeStatusAnalysis::kUntrackable) {
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001602 if (status_analysis_->IsVirtual(static_cast<int>(id))) {
Ben Murdoch109988c2016-05-18 11:27:45 +01001603 return true;
1604 }
1605 }
1606 }
1607 return false;
1608}
1609
Ben Murdochbcf72ee2016-08-08 18:44:38 +01001610Graph* EscapeAnalysis::graph() const { return status_analysis_->graph(); }
1611
Ben Murdoch014dc512016-03-22 12:00:34 +00001612} // namespace compiler
1613} // namespace internal
1614} // namespace v8