r238510 broke scopes of size zero
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGObjectAllocationSinkingPhase.cpp
1 /*
2  * Copyright (C) 2015-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "DFGObjectAllocationSinkingPhase.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "DFGBlockMapInlines.h"
32 #include "DFGClobbersExitState.h"
33 #include "DFGCombinedLiveness.h"
34 #include "DFGGraph.h"
35 #include "DFGInsertionSet.h"
36 #include "DFGLazyNode.h"
37 #include "DFGLivenessAnalysisPhase.h"
38 #include "DFGOSRAvailabilityAnalysisPhase.h"
39 #include "DFGPhase.h"
40 #include "DFGPromotedHeapLocation.h"
41 #include "DFGSSACalculator.h"
42 #include "DFGValidate.h"
43 #include "JSCInlines.h"
44 #include <wtf/StdList.h>
45
46 namespace JSC { namespace DFG {
47
48 namespace {
49
50 namespace DFGObjectAllocationSinkingPhaseInternal {
51 static const bool verbose = false;
52 }
53
54 // In order to sink object cycles, we use a points-to analysis coupled
55 // with an escape analysis. This analysis is actually similar to an
56 // abstract interpreter focused on local allocations and ignoring
57 // everything else.
58 //
59 // We represent the local heap using two mappings:
60 //
61 // - A set of the local allocations present in the function, where
62 //   each of those have a further mapping from
63 //   PromotedLocationDescriptor to local allocations they must point
64 //   to.
65 //
66 // - A "pointer" mapping from nodes to local allocations, if they must
67 //   be equal to said local allocation and are currently live. This
68 //   can be because the node is the actual node that created the
69 //   allocation, or any other node that must currently point to it -
70 //   we don't make a difference.
71 //
72 // The following graph is a motivation for why we separate allocations
73 // from pointers:
74 //
75 // Block #0
76 //  0: NewObject({})
77 //  1: NewObject({})
78 //  -: PutByOffset(@0, @1, x)
79 //  -: PutStructure(@0, {x:0})
80 //  2: GetByOffset(@0, x)
81 //  -: Jump(#1)
82 //
83 // Block #1
84 //  -: Return(@2)
85 //
86 // Here, we need to remember in block #1 that @2 points to a local
87 // allocation with appropriate fields and structures information
88 // (because we should be able to place a materialization on top of
89 // block #1 here), even though @1 is dead. We *could* just keep @1
90 // artificially alive here, but there is no real reason to do it:
91 // after all, by the end of block #0, @1 and @2 should be completely
92 // interchangeable, and there is no reason for us to artificially make
93 // @1 more important.
94 //
95 // An important point to consider to understand this separation is
96 // that we should think of the local heap as follow: we have a
97 // bunch of nodes that are pointers to "allocations" that live
98 // someplace on the heap, and those allocations can have pointers in
99 // between themselves as well. We shouldn't care about whatever
100 // names we give to the allocations ; what matters when
101 // comparing/merging two heaps is the isomorphism/comparison between
102 // the allocation graphs as seen by the nodes.
103 //
104 // For instance, in the following graph:
105 //
106 // Block #0
107 //  0: NewObject({})
108 //  -: Branch(#1, #2)
109 //
110 // Block #1
111 //  1: NewObject({})
112 //  -: PutByOffset(@0, @1, x)
113 //  -: PutStructure(@0, {x:0})
114 //  -: Jump(#3)
115 //
116 // Block #2
117 //  2: NewObject({})
118 //  -: PutByOffset(@2, undefined, x)
119 //  -: PutStructure(@2, {x:0})
120 //  -: PutByOffset(@0, @2, x)
121 //  -: PutStructure(@0, {x:0})
122 //  -: Jump(#3)
123 //
124 // Block #3
125 //  -: Return(@0)
126 //
127 // we should think of the heaps at tail of blocks #1 and #2 as being
128 // exactly the same, even though one has @0.x pointing to @1 and the
129 // other has @0.x pointing to @2, because in essence this should not
130 // be different from the graph where we hoisted @1 and @2 into a
131 // single allocation in block #0. We currently will not handle this
132 // case, because we merge allocations based on the node they are
133 // coming from, but this is only a technicality for the sake of
134 // simplicity that shouldn't hide the deeper idea outlined here.
135
136 class Allocation {
137 public:
138     // We use Escaped as a special allocation kind because when we
139     // decide to sink an allocation, we still need to keep track of it
140     // once it is escaped if it still has pointers to it in order to
141     // replace any use of those pointers by the corresponding
142     // materialization
143     enum class Kind { Escaped, Object, Activation, Function, GeneratorFunction, AsyncFunction, AsyncGeneratorFunction, RegExpObject };
144
145     using Fields = HashMap<PromotedLocationDescriptor, Node*>;
146
147     explicit Allocation(Node* identifier = nullptr, Kind kind = Kind::Escaped)
148         : m_identifier(identifier)
149         , m_kind(kind)
150     {
151     }
152
153
154     const Fields& fields() const
155     {
156         return m_fields;
157     }
158
159     Fields& fields()
160     {
161         return m_fields;
162     }
163
164     Node* get(PromotedLocationDescriptor descriptor)
165     {
166         return m_fields.get(descriptor);
167     }
168
169     Allocation& set(PromotedLocationDescriptor descriptor, Node* value)
170     {
171         // Pointing to anything else than an unescaped local
172         // allocation is represented by simply not having the
173         // field
174         if (value)
175             m_fields.set(descriptor, value);
176         else
177             m_fields.remove(descriptor);
178         return *this;
179     }
180
181     void remove(PromotedLocationDescriptor descriptor)
182     {
183         set(descriptor, nullptr);
184     }
185
186     bool hasStructures() const
187     {
188         switch (kind()) {
189         case Kind::Object:
190             return true;
191
192         default:
193             return false;
194         }
195     }
196
197     Allocation& setStructures(const RegisteredStructureSet& structures)
198     {
199         ASSERT(hasStructures() && !structures.isEmpty());
200         m_structures = structures;
201         return *this;
202     }
203
204     Allocation& mergeStructures(const RegisteredStructureSet& structures)
205     {
206         ASSERT(hasStructures() || structures.isEmpty());
207         m_structures.merge(structures);
208         return *this;
209     }
210
211     Allocation& filterStructures(const RegisteredStructureSet& structures)
212     {
213         ASSERT(hasStructures());
214         m_structures.filter(structures);
215         RELEASE_ASSERT(!m_structures.isEmpty());
216         return *this;
217     }
218
219     const RegisteredStructureSet& structures() const
220     {
221         return m_structures;
222     }
223
224     Node* identifier() const { return m_identifier; }
225
226     Kind kind() const { return m_kind; }
227
228     bool isEscapedAllocation() const
229     {
230         return kind() == Kind::Escaped;
231     }
232
233     bool isObjectAllocation() const
234     {
235         return m_kind == Kind::Object;
236     }
237
238     bool isActivationAllocation() const
239     {
240         return m_kind == Kind::Activation;
241     }
242
243     bool isFunctionAllocation() const
244     {
245         return m_kind == Kind::Function || m_kind == Kind::GeneratorFunction || m_kind == Kind::AsyncFunction;
246     }
247
248     bool isRegExpObjectAllocation() const
249     {
250         return m_kind == Kind::RegExpObject;
251     }
252
253     bool operator==(const Allocation& other) const
254     {
255         return m_identifier == other.m_identifier
256             && m_kind == other.m_kind
257             && m_fields == other.m_fields
258             && m_structures == other.m_structures;
259     }
260
261     bool operator!=(const Allocation& other) const
262     {
263         return !(*this == other);
264     }
265
266     void dump(PrintStream& out) const
267     {
268         dumpInContext(out, nullptr);
269     }
270
271     void dumpInContext(PrintStream& out, DumpContext* context) const
272     {
273         switch (m_kind) {
274         case Kind::Escaped:
275             out.print("Escaped");
276             break;
277
278         case Kind::Object:
279             out.print("Object");
280             break;
281
282         case Kind::Function:
283             out.print("Function");
284             break;
285
286         case Kind::GeneratorFunction:
287             out.print("GeneratorFunction");
288             break;
289
290         case Kind::AsyncFunction:
291             out.print("AsyncFunction");
292             break;
293
294         case Kind::AsyncGeneratorFunction:
295             out.print("AsyncGeneratorFunction");
296             break;
297
298         case Kind::Activation:
299             out.print("Activation");
300             break;
301
302         case Kind::RegExpObject:
303             out.print("RegExpObject");
304             break;
305         }
306         out.print("Allocation(");
307         if (!m_structures.isEmpty())
308             out.print(inContext(m_structures.toStructureSet(), context));
309         if (!m_fields.isEmpty()) {
310             if (!m_structures.isEmpty())
311                 out.print(", ");
312             out.print(mapDump(m_fields, " => #", ", "));
313         }
314         out.print(")");
315     }
316
317 private:
318     Node* m_identifier; // This is the actual node that created the allocation
319     Kind m_kind;
320     Fields m_fields;
321     RegisteredStructureSet m_structures;
322 };
323
324 class LocalHeap {
325 public:
326     Allocation& newAllocation(Node* node, Allocation::Kind kind)
327     {
328         ASSERT(!m_pointers.contains(node) && !isAllocation(node));
329         m_pointers.add(node, node);
330         return m_allocations.set(node, Allocation(node, kind)).iterator->value;
331     }
332
333     bool isAllocation(Node* identifier) const
334     {
335         return m_allocations.contains(identifier);
336     }
337
338     // Note that this is fundamentally different from
339     // onlyLocalAllocation() below. getAllocation() takes as argument
340     // a node-as-identifier, that is, an allocation node. This
341     // allocation node doesn't have to be alive; it may only be
342     // pointed to by other nodes or allocation fields.
343     // For instance, in the following graph:
344     //
345     // Block #0
346     //  0: NewObject({})
347     //  1: NewObject({})
348     //  -: PutByOffset(@0, @1, x)
349     //  -: PutStructure(@0, {x:0})
350     //  2: GetByOffset(@0, x)
351     //  -: Jump(#1)
352     //
353     // Block #1
354     //  -: Return(@2)
355     //
356     // At head of block #1, the only reachable allocation is #@1,
357     // which can be reached through node @2. Thus, getAllocation(#@1)
358     // contains the appropriate metadata for this allocation, but
359     // onlyLocalAllocation(@1) is null, as @1 is no longer a pointer
360     // to #@1 (since it is dead). Conversely, onlyLocalAllocation(@2)
361     // is the same as getAllocation(#@1), while getAllocation(#@2)
362     // does not make sense since @2 is not an allocation node.
363     //
364     // This is meant to be used when the node is already known to be
365     // an identifier (i.e. an allocation) - probably because it was
366     // found as value of a field or pointer in the current heap, or
367     // was the result of a call to follow(). In any other cases (such
368     // as when doing anything while traversing the graph), the
369     // appropriate function to call is probably onlyLocalAllocation.
370     Allocation& getAllocation(Node* identifier)
371     {
372         auto iter = m_allocations.find(identifier);
373         ASSERT(iter != m_allocations.end());
374         return iter->value;
375     }
376
377     void newPointer(Node* node, Node* identifier)
378     {
379         ASSERT(!m_allocations.contains(node) && !m_pointers.contains(node));
380         ASSERT(isAllocation(identifier));
381         m_pointers.add(node, identifier);
382     }
383
384     // follow solves the points-to problem. Given a live node, which
385     // may be either an allocation itself or a heap read (e.g. a
386     // GetByOffset node), it returns the corresponding allocation
387     // node, if there is one. If the argument node is neither an
388     // allocation or a heap read, or may point to different nodes,
389     // nullptr will be returned. Note that a node that points to
390     // different nodes can never point to an unescaped local
391     // allocation.
392     Node* follow(Node* node) const
393     {
394         auto iter = m_pointers.find(node);
395         ASSERT(iter == m_pointers.end() || m_allocations.contains(iter->value));
396         return iter == m_pointers.end() ? nullptr : iter->value;
397     }
398
399     Node* follow(PromotedHeapLocation location) const
400     {
401         const Allocation& base = m_allocations.find(location.base())->value;
402         auto iter = base.fields().find(location.descriptor());
403
404         if (iter == base.fields().end())
405             return nullptr;
406
407         return iter->value;
408     }
409
410     // onlyLocalAllocation find the corresponding allocation metadata
411     // for any live node. onlyLocalAllocation(node) is essentially
412     // getAllocation(follow(node)), with appropriate null handling.
413     Allocation* onlyLocalAllocation(Node* node)
414     {
415         Node* identifier = follow(node);
416         if (!identifier)
417             return nullptr;
418
419         return &getAllocation(identifier);
420     }
421
422     Allocation* onlyLocalAllocation(PromotedHeapLocation location)
423     {
424         Node* identifier = follow(location);
425         if (!identifier)
426             return nullptr;
427
428         return &getAllocation(identifier);
429     }
430
431     // This allows us to store the escapees only when necessary. If
432     // set, the current escapees can be retrieved at any time using
433     // takeEscapees(), which will clear the cached set of escapees;
434     // otherwise the heap won't remember escaping allocations.
435     void setWantEscapees()
436     {
437         m_wantEscapees = true;
438     }
439
440     HashMap<Node*, Allocation> takeEscapees()
441     {
442         return WTFMove(m_escapees);
443     }
444
445     void escape(Node* node)
446     {
447         Node* identifier = follow(node);
448         if (!identifier)
449             return;
450
451         escapeAllocation(identifier);
452     }
453
454     void merge(const LocalHeap& other)
455     {
456         assertIsValid();
457         other.assertIsValid();
458         ASSERT(!m_wantEscapees);
459
460         if (!reached()) {
461             ASSERT(other.reached());
462             *this = other;
463             return;
464         }
465
466         NodeSet toEscape;
467
468         for (auto& allocationEntry : other.m_allocations)
469             m_allocations.add(allocationEntry.key, allocationEntry.value);
470         for (auto& allocationEntry : m_allocations) {
471             auto allocationIter = other.m_allocations.find(allocationEntry.key);
472
473             // If we have it and they don't, it died for them but we
474             // are keeping it alive from another field somewhere.
475             // There is nothing to do - we will be escaped
476             // automatically when we handle that other field.
477             // This will also happen for allocation that we have and
478             // they don't, and all of those will get pruned.
479             if (allocationIter == other.m_allocations.end())
480                 continue;
481
482             if (allocationEntry.value.kind() != allocationIter->value.kind()) {
483                 toEscape.addVoid(allocationEntry.key);
484                 for (const auto& fieldEntry : allocationIter->value.fields())
485                     toEscape.addVoid(fieldEntry.value);
486             } else {
487                 mergePointerSets(allocationEntry.value.fields(), allocationIter->value.fields(), toEscape);
488                 allocationEntry.value.mergeStructures(allocationIter->value.structures());
489             }
490         }
491
492         mergePointerSets(m_pointers, other.m_pointers, toEscape);
493
494         for (Node* identifier : toEscape)
495             escapeAllocation(identifier);
496
497         if (!ASSERT_DISABLED) {
498             for (const auto& entry : m_allocations)
499                 ASSERT_UNUSED(entry, entry.value.isEscapedAllocation() || other.m_allocations.contains(entry.key));
500         }
501
502         // If there is no remaining pointer to an allocation, we can
503         // remove it. This should only happen for escaped allocations,
504         // because we only merge liveness-pruned heaps in the first
505         // place.
506         prune();
507
508         assertIsValid();
509     }
510
511     void pruneByLiveness(const NodeSet& live)
512     {
513         m_pointers.removeIf(
514             [&] (const auto& entry) {
515                 return !live.contains(entry.key);
516             });
517         prune();
518     }
519
520     void assertIsValid() const
521     {
522         if (ASSERT_DISABLED)
523             return;
524
525         // Pointers should point to an actual allocation
526         for (const auto& entry : m_pointers) {
527             ASSERT_UNUSED(entry, entry.value);
528             ASSERT(m_allocations.contains(entry.value));
529         }
530
531         for (const auto& allocationEntry : m_allocations) {
532             // Fields should point to an actual allocation
533             for (const auto& fieldEntry : allocationEntry.value.fields()) {
534                 ASSERT_UNUSED(fieldEntry, fieldEntry.value);
535                 ASSERT(m_allocations.contains(fieldEntry.value));
536             }
537         }
538     }
539
540     bool operator==(const LocalHeap& other) const
541     {
542         assertIsValid();
543         other.assertIsValid();
544         return m_allocations == other.m_allocations
545             && m_pointers == other.m_pointers;
546     }
547
548     bool operator!=(const LocalHeap& other) const
549     {
550         return !(*this == other);
551     }
552
553     const HashMap<Node*, Allocation>& allocations() const
554     {
555         return m_allocations;
556     }
557
558     const HashMap<Node*, Node*>& pointers() const
559     {
560         return m_pointers;
561     }
562
563     void dump(PrintStream& out) const
564     {
565         out.print("  Allocations:\n");
566         for (const auto& entry : m_allocations)
567             out.print("    #", entry.key, ": ", entry.value, "\n");
568         out.print("  Pointers:\n");
569         for (const auto& entry : m_pointers)
570             out.print("    ", entry.key, " => #", entry.value, "\n");
571     }
572
573     bool reached() const
574     {
575         return m_reached;
576     }
577
578     void setReached()
579     {
580         m_reached = true;
581     }
582
583 private:
584     // When we merge two heaps, we escape all fields of allocations,
585     // unless they point to the same thing in both heaps.
586     // The reason for this is that it allows us not to do extra work
587     // for diamond graphs where we would otherwise have to check
588     // whether we have a single definition or not, which would be
589     // cumbersome.
590     //
591     // Note that we should try to unify nodes even when they are not
592     // from the same allocation; for instance we should be able to
593     // completely eliminate all allocations from the following graph:
594     //
595     // Block #0
596     //  0: NewObject({})
597     //  -: Branch(#1, #2)
598     //
599     // Block #1
600     //  1: NewObject({})
601     //  -: PutByOffset(@1, "left", val)
602     //  -: PutStructure(@1, {val:0})
603     //  -: PutByOffset(@0, @1, x)
604     //  -: PutStructure(@0, {x:0})
605     //  -: Jump(#3)
606     //
607     // Block #2
608     //  2: NewObject({})
609     //  -: PutByOffset(@2, "right", val)
610     //  -: PutStructure(@2, {val:0})
611     //  -: PutByOffset(@0, @2, x)
612     //  -: PutStructure(@0, {x:0})
613     //  -: Jump(#3)
614     //
615     // Block #3:
616     //  3: GetByOffset(@0, x)
617     //  4: GetByOffset(@3, val)
618     //  -: Return(@4)
619     template<typename Key>
620     static void mergePointerSets(HashMap<Key, Node*>& my, const HashMap<Key, Node*>& their, NodeSet& toEscape)
621     {
622         auto escape = [&] (Node* identifier) {
623             toEscape.addVoid(identifier);
624         };
625
626         for (const auto& entry : their) {
627             if (!my.contains(entry.key))
628                 escape(entry.value);
629         }
630         my.removeIf([&] (const auto& entry) {
631             auto iter = their.find(entry.key);
632             if (iter == their.end()) {
633                 escape(entry.value);
634                 return true;
635             }
636             if (iter->value != entry.value) {
637                 escape(entry.value);
638                 escape(iter->value);
639                 return true;
640             }
641             return false;
642         });
643     }
644
645     void escapeAllocation(Node* identifier)
646     {
647         Allocation& allocation = getAllocation(identifier);
648         if (allocation.isEscapedAllocation())
649             return;
650
651         Allocation unescaped = WTFMove(allocation);
652         allocation = Allocation(unescaped.identifier(), Allocation::Kind::Escaped);
653
654         for (const auto& entry : unescaped.fields())
655             escapeAllocation(entry.value);
656
657         if (m_wantEscapees)
658             m_escapees.add(unescaped.identifier(), WTFMove(unescaped));
659     }
660
661     void prune()
662     {
663         NodeSet reachable;
664         for (const auto& entry : m_pointers)
665             reachable.addVoid(entry.value);
666
667         // Repeatedly mark as reachable allocations in fields of other
668         // reachable allocations
669         {
670             Vector<Node*> worklist;
671             worklist.appendRange(reachable.begin(), reachable.end());
672
673             while (!worklist.isEmpty()) {
674                 Node* identifier = worklist.takeLast();
675                 Allocation& allocation = m_allocations.find(identifier)->value;
676                 for (const auto& entry : allocation.fields()) {
677                     if (reachable.add(entry.value).isNewEntry)
678                         worklist.append(entry.value);
679                 }
680             }
681         }
682
683         // Remove unreachable allocations
684         m_allocations.removeIf(
685             [&] (const auto& entry) {
686                 return !reachable.contains(entry.key);
687             });
688     }
689
690     bool m_reached = false;
691     HashMap<Node*, Node*> m_pointers;
692     HashMap<Node*, Allocation> m_allocations;
693
694     bool m_wantEscapees = false;
695     HashMap<Node*, Allocation> m_escapees;
696 };
697
698 class ObjectAllocationSinkingPhase : public Phase {
699 public:
700     ObjectAllocationSinkingPhase(Graph& graph)
701         : Phase(graph, "object allocation elimination")
702         , m_pointerSSA(graph)
703         , m_allocationSSA(graph)
704         , m_insertionSet(graph)
705     {
706     }
707
708     bool run()
709     {
710         ASSERT(m_graph.m_form == SSA);
711         ASSERT(m_graph.m_fixpointState == FixpointNotConverged);
712
713         if (!performSinking())
714             return false;
715
716         if (DFGObjectAllocationSinkingPhaseInternal::verbose) {
717             dataLog("Graph after elimination:\n");
718             m_graph.dump();
719         }
720
721         return true;
722     }
723
724 private:
725     bool performSinking()
726     {
727         m_graph.computeRefCounts();
728         m_graph.initializeNodeOwners();
729         m_graph.ensureSSADominators();
730         performLivenessAnalysis(m_graph);
731         performOSRAvailabilityAnalysis(m_graph);
732         m_combinedLiveness = CombinedLiveness(m_graph);
733
734         CString graphBeforeSinking;
735         if (Options::verboseValidationFailure() && Options::validateGraphAtEachPhase()) {
736             StringPrintStream out;
737             m_graph.dump(out);
738             graphBeforeSinking = out.toCString();
739         }
740
741         if (DFGObjectAllocationSinkingPhaseInternal::verbose) {
742             dataLog("Graph before elimination:\n");
743             m_graph.dump();
744         }
745
746         performAnalysis();
747
748         if (!determineSinkCandidates())
749             return false;
750
751         if (DFGObjectAllocationSinkingPhaseInternal::verbose) {
752             for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
753                 dataLog("Heap at head of ", *block, ": \n", m_heapAtHead[block]);
754                 dataLog("Heap at tail of ", *block, ": \n", m_heapAtTail[block]);
755             }
756         }
757
758         promoteLocalHeap();
759         removeICStatusFilters();
760
761         if (Options::validateGraphAtEachPhase())
762             DFG::validate(m_graph, DumpGraph, graphBeforeSinking);
763         return true;
764     }
765
766     void performAnalysis()
767     {
768         m_heapAtHead = BlockMap<LocalHeap>(m_graph);
769         m_heapAtTail = BlockMap<LocalHeap>(m_graph);
770
771         bool changed;
772         do {
773             if (DFGObjectAllocationSinkingPhaseInternal::verbose)
774                 dataLog("Doing iteration of escape analysis.\n");
775             changed = false;
776
777             for (BasicBlock* block : m_graph.blocksInPreOrder()) {
778                 m_heapAtHead[block].setReached();
779                 m_heap = m_heapAtHead[block];
780
781                 for (Node* node : *block) {
782                     handleNode(
783                         node,
784                         [] (PromotedHeapLocation, LazyNode) { },
785                         [&] (PromotedHeapLocation) -> Node* {
786                             return nullptr;
787                         });
788                 }
789
790                 if (m_heap == m_heapAtTail[block])
791                     continue;
792
793                 m_heapAtTail[block] = m_heap;
794                 changed = true;
795
796                 m_heap.assertIsValid();
797
798                 // We keep only pointers that are live, and only
799                 // allocations that are either live, pointed to by a
800                 // live pointer, or (recursively) stored in a field of
801                 // a live allocation.
802                 //
803                 // This means we can accidentaly leak non-dominating
804                 // nodes into the successor. However, due to the
805                 // non-dominance property, we are guaranteed that the
806                 // successor has at least one predecessor that is not
807                 // dominated either: this means any reference to a
808                 // non-dominating allocation in the successor will
809                 // trigger an escape and get pruned during the merge.
810                 m_heap.pruneByLiveness(m_combinedLiveness.liveAtTail[block]);
811
812                 for (BasicBlock* successorBlock : block->successors())
813                     m_heapAtHead[successorBlock].merge(m_heap);
814             }
815         } while (changed);
816     }
817
818     template<typename WriteFunctor, typename ResolveFunctor>
819     void handleNode(
820         Node* node,
821         const WriteFunctor& heapWrite,
822         const ResolveFunctor& heapResolve)
823     {
824         m_heap.assertIsValid();
825         ASSERT(m_heap.takeEscapees().isEmpty());
826
827         Allocation* target = nullptr;
828         HashMap<PromotedLocationDescriptor, LazyNode> writes;
829         PromotedLocationDescriptor exactRead;
830
831         switch (node->op()) {
832         case NewObject:
833             target = &m_heap.newAllocation(node, Allocation::Kind::Object);
834             target->setStructures(node->structure());
835             writes.add(
836                 StructurePLoc, LazyNode(m_graph.freeze(node->structure().get())));
837             break;
838
839         case NewFunction:
840         case NewGeneratorFunction:
841         case NewAsyncGeneratorFunction:
842         case NewAsyncFunction: {
843             if (isStillValid(node->castOperand<FunctionExecutable*>()->singletonFunction())) {
844                 m_heap.escape(node->child1().node());
845                 break;
846             }
847
848             if (node->op() == NewGeneratorFunction)
849                 target = &m_heap.newAllocation(node, Allocation::Kind::GeneratorFunction);
850             else if (node->op() == NewAsyncFunction)
851                 target = &m_heap.newAllocation(node, Allocation::Kind::AsyncFunction);
852             else if (node->op() == NewAsyncGeneratorFunction)
853                 target = &m_heap.newAllocation(node, Allocation::Kind::AsyncGeneratorFunction);
854             else
855                 target = &m_heap.newAllocation(node, Allocation::Kind::Function);
856
857             writes.add(FunctionExecutablePLoc, LazyNode(node->cellOperand()));
858             writes.add(FunctionActivationPLoc, LazyNode(node->child1().node()));
859             break;
860         }
861
862         case NewRegexp: {
863             target = &m_heap.newAllocation(node, Allocation::Kind::RegExpObject);
864
865             writes.add(RegExpObjectRegExpPLoc, LazyNode(node->cellOperand()));
866             writes.add(RegExpObjectLastIndexPLoc, LazyNode(node->child1().node()));
867             break;
868         }
869
870         case CreateActivation: {
871             if (isStillValid(node->castOperand<SymbolTable*>()->singletonScope())) {
872                 m_heap.escape(node->child1().node());
873                 break;
874             }
875             target = &m_heap.newAllocation(node, Allocation::Kind::Activation);
876             writes.add(ActivationSymbolTablePLoc, LazyNode(node->cellOperand()));
877             writes.add(ActivationScopePLoc, LazyNode(node->child1().node()));
878             {
879                 SymbolTable* symbolTable = node->castOperand<SymbolTable*>();
880                 LazyNode initialValue(m_graph.freeze(node->initializationValueForActivation()));
881                 for (unsigned offset = 0; offset < symbolTable->scopeSize(); ++offset) {
882                     writes.add(
883                         PromotedLocationDescriptor(ClosureVarPLoc, offset),
884                         initialValue);
885                 }
886             }
887             break;
888         }
889
890         case PutStructure:
891             target = m_heap.onlyLocalAllocation(node->child1().node());
892             if (target && target->isObjectAllocation()) {
893                 writes.add(StructurePLoc, LazyNode(m_graph.freeze(JSValue(node->transition()->next.get()))));
894                 target->setStructures(node->transition()->next);
895             } else
896                 m_heap.escape(node->child1().node());
897             break;
898
899         case CheckStructureOrEmpty:
900         case CheckStructure: {
901             Allocation* allocation = m_heap.onlyLocalAllocation(node->child1().node());
902             if (allocation && allocation->isObjectAllocation()) {
903                 RegisteredStructureSet filteredStructures = allocation->structures();
904                 filteredStructures.filter(node->structureSet());
905                 if (filteredStructures.isEmpty()) {
906                     // FIXME: Write a test for this:
907                     // https://bugs.webkit.org/show_bug.cgi?id=174322
908                     m_heap.escape(node->child1().node());
909                     break;
910                 }
911                 allocation->setStructures(filteredStructures);
912                 if (Node* value = heapResolve(PromotedHeapLocation(allocation->identifier(), StructurePLoc)))
913                     node->convertToCheckStructureImmediate(value);
914             } else
915                 m_heap.escape(node->child1().node());
916             break;
917         }
918
919         case GetByOffset:
920         case GetGetterSetterByOffset:
921             target = m_heap.onlyLocalAllocation(node->child2().node());
922             if (target && target->isObjectAllocation()) {
923                 unsigned identifierNumber = node->storageAccessData().identifierNumber;
924                 exactRead = PromotedLocationDescriptor(NamedPropertyPLoc, identifierNumber);
925             } else {
926                 m_heap.escape(node->child1().node());
927                 m_heap.escape(node->child2().node());
928             }
929             break;
930
931         case MultiGetByOffset: {
932             Allocation* allocation = m_heap.onlyLocalAllocation(node->child1().node());
933             if (allocation && allocation->isObjectAllocation()) {
934                 MultiGetByOffsetData& data = node->multiGetByOffsetData();
935                 RegisteredStructureSet validStructures;
936                 bool hasInvalidStructures = false;
937                 for (const auto& multiGetByOffsetCase : data.cases) {
938                     if (!allocation->structures().overlaps(multiGetByOffsetCase.set()))
939                         continue;
940
941                     switch (multiGetByOffsetCase.method().kind()) {
942                     case GetByOffsetMethod::LoadFromPrototype: // We need to escape those
943                     case GetByOffsetMethod::Constant: // We don't really have a way of expressing this
944                         hasInvalidStructures = true;
945                         break;
946
947                     case GetByOffsetMethod::Load: // We're good
948                         validStructures.merge(multiGetByOffsetCase.set());
949                         break;
950
951                     default:
952                         RELEASE_ASSERT_NOT_REACHED();
953                     }
954                 }
955                 if (hasInvalidStructures || validStructures.isEmpty()) {
956                     m_heap.escape(node->child1().node());
957                     break;
958                 }
959                 unsigned identifierNumber = data.identifierNumber;
960                 PromotedHeapLocation location(NamedPropertyPLoc, allocation->identifier(), identifierNumber);
961                 if (Node* value = heapResolve(location)) {
962                     if (allocation->structures().isSubsetOf(validStructures))
963                         node->replaceWithWithoutChecks(value);
964                     else {
965                         Node* structure = heapResolve(PromotedHeapLocation(allocation->identifier(), StructurePLoc));
966                         ASSERT(structure);
967                         allocation->filterStructures(validStructures);
968                         node->convertToCheckStructure(m_graph.addStructureSet(allocation->structures()));
969                         node->convertToCheckStructureImmediate(structure);
970                         node->setReplacement(value);
971                     }
972                 } else if (!allocation->structures().isSubsetOf(validStructures)) {
973                     // Even though we don't need the result here, we still need
974                     // to make the call to tell our caller that we could need
975                     // the StructurePLoc.
976                     // The reason for this is that when we decide not to sink a
977                     // node, we will still lower any read to its fields before
978                     // it escapes (which are usually reads across a function
979                     // call that DFGClobberize can't handle) - but we only do
980                     // this for PromotedHeapLocations that we have seen read
981                     // during the analysis!
982                     heapResolve(PromotedHeapLocation(allocation->identifier(), StructurePLoc));
983                     allocation->filterStructures(validStructures);
984                 }
985                 Node* identifier = allocation->get(location.descriptor());
986                 if (identifier)
987                     m_heap.newPointer(node, identifier);
988             } else
989                 m_heap.escape(node->child1().node());
990             break;
991         }
992
993         case PutByOffset:
994             target = m_heap.onlyLocalAllocation(node->child2().node());
995             if (target && target->isObjectAllocation()) {
996                 unsigned identifierNumber = node->storageAccessData().identifierNumber;
997                 writes.add(
998                     PromotedLocationDescriptor(NamedPropertyPLoc, identifierNumber),
999                     LazyNode(node->child3().node()));
1000             } else {
1001                 m_heap.escape(node->child1().node());
1002                 m_heap.escape(node->child2().node());
1003                 m_heap.escape(node->child3().node());
1004             }
1005             break;
1006
1007         case GetClosureVar:
1008             target = m_heap.onlyLocalAllocation(node->child1().node());
1009             if (target && target->isActivationAllocation()) {
1010                 exactRead =
1011                     PromotedLocationDescriptor(ClosureVarPLoc, node->scopeOffset().offset());
1012             } else
1013                 m_heap.escape(node->child1().node());
1014             break;
1015
1016         case PutClosureVar:
1017             target = m_heap.onlyLocalAllocation(node->child1().node());
1018             if (target && target->isActivationAllocation()) {
1019                 writes.add(
1020                     PromotedLocationDescriptor(ClosureVarPLoc, node->scopeOffset().offset()),
1021                     LazyNode(node->child2().node()));
1022             } else {
1023                 m_heap.escape(node->child1().node());
1024                 m_heap.escape(node->child2().node());
1025             }
1026             break;
1027
1028         case SkipScope:
1029             target = m_heap.onlyLocalAllocation(node->child1().node());
1030             if (target && target->isActivationAllocation())
1031                 exactRead = ActivationScopePLoc;
1032             else
1033                 m_heap.escape(node->child1().node());
1034             break;
1035
1036         case GetExecutable:
1037             target = m_heap.onlyLocalAllocation(node->child1().node());
1038             if (target && target->isFunctionAllocation())
1039                 exactRead = FunctionExecutablePLoc;
1040             else
1041                 m_heap.escape(node->child1().node());
1042             break;
1043
1044         case GetScope:
1045             target = m_heap.onlyLocalAllocation(node->child1().node());
1046             if (target && target->isFunctionAllocation())
1047                 exactRead = FunctionActivationPLoc;
1048             else
1049                 m_heap.escape(node->child1().node());
1050             break;
1051
1052         case GetRegExpObjectLastIndex:
1053             target = m_heap.onlyLocalAllocation(node->child1().node());
1054             if (target && target->isRegExpObjectAllocation())
1055                 exactRead = RegExpObjectLastIndexPLoc;
1056             else
1057                 m_heap.escape(node->child1().node());
1058             break;
1059
1060         case SetRegExpObjectLastIndex:
1061             target = m_heap.onlyLocalAllocation(node->child1().node());
1062             if (target && target->isRegExpObjectAllocation()) {
1063                 writes.add(
1064                     PromotedLocationDescriptor(RegExpObjectLastIndexPLoc),
1065                     LazyNode(node->child2().node()));
1066             } else {
1067                 m_heap.escape(node->child1().node());
1068                 m_heap.escape(node->child2().node());
1069             }
1070             break;
1071
1072         case Check:
1073         case CheckVarargs:
1074             m_graph.doToChildren(
1075                 node,
1076                 [&] (Edge edge) {
1077                     if (edge.willNotHaveCheck())
1078                         return;
1079
1080                     if (alreadyChecked(edge.useKind(), SpecObject))
1081                         return;
1082
1083                     m_heap.escape(edge.node());
1084                 });
1085             break;
1086
1087         case MovHint:
1088         case PutHint:
1089             // Handled by OSR availability analysis
1090             break;
1091             
1092         case FilterCallLinkStatus:
1093         case FilterGetByIdStatus:
1094         case FilterPutByIdStatus:
1095         case FilterInByIdStatus:
1096             break;
1097
1098         default:
1099             m_graph.doToChildren(
1100                 node,
1101                 [&] (Edge edge) {
1102                     m_heap.escape(edge.node());
1103                 });
1104             break;
1105         }
1106
1107         if (exactRead) {
1108             ASSERT(target);
1109             ASSERT(writes.isEmpty());
1110             if (Node* value = heapResolve(PromotedHeapLocation(target->identifier(), exactRead))) {
1111                 ASSERT(!value->replacement());
1112                 node->replaceWith(m_graph, value);
1113             }
1114             Node* identifier = target->get(exactRead);
1115             if (identifier)
1116                 m_heap.newPointer(node, identifier);
1117         }
1118
1119         for (auto entry : writes) {
1120             ASSERT(target);
1121             if (entry.value.isNode())
1122                 target->set(entry.key, m_heap.follow(entry.value.asNode()));
1123             else
1124                 target->remove(entry.key);
1125             heapWrite(PromotedHeapLocation(target->identifier(), entry.key), entry.value);
1126         }
1127
1128         m_heap.assertIsValid();
1129     }
1130
1131     bool determineSinkCandidates()
1132     {
1133         m_sinkCandidates.clear();
1134         m_materializationToEscapee.clear();
1135         m_materializationSiteToMaterializations.clear();
1136         m_materializationSiteToRecoveries.clear();
1137         m_materializationSiteToHints.clear();
1138
1139         // Logically we wish to consider every allocation and sink
1140         // it. However, it is probably not profitable to sink an
1141         // allocation that will always escape. So, we only sink an
1142         // allocation if one of the following is true:
1143         //
1144         // 1) There exists a basic block with only backwards outgoing
1145         //    edges (or no outgoing edges) in which the node wasn't
1146         //    materialized. This is meant to catch
1147         //    effectively-infinite loops in which we don't need to
1148         //    have allocated the object.
1149         //
1150         // 2) There exists a basic block at the tail of which the node
1151         //    is dead and not materialized.
1152         //
1153         // 3) The sum of execution counts of the materializations is
1154         //    less than the sum of execution counts of the original
1155         //    node.
1156         //
1157         // We currently implement only rule #2.
1158         // FIXME: Implement the two other rules.
1159         // https://bugs.webkit.org/show_bug.cgi?id=137073 (rule #1)
1160         // https://bugs.webkit.org/show_bug.cgi?id=137074 (rule #3)
1161         //
1162         // However, these rules allow for a sunk object to be put into
1163         // a non-sunk one, which we don't support. We could solve this
1164         // by supporting PutHints on local allocations, making these
1165         // objects only partially correct, and we would need to adapt
1166         // the OSR availability analysis and OSR exit to handle
1167         // this. This would be totally doable, but would create a
1168         // super rare, and thus bug-prone, code path.
1169         // So, instead, we need to implement one of the following
1170         // closure rules:
1171         //
1172         // 1) If we put a sink candidate into a local allocation that
1173         //    is not a sink candidate, change our minds and don't
1174         //    actually sink the sink candidate.
1175         //
1176         // 2) If we put a sink candidate into a local allocation, that
1177         //    allocation becomes a sink candidate as well.
1178         //
1179         // We currently choose to implement closure rule #2.
1180         HashMap<Node*, Vector<Node*>> dependencies;
1181         bool hasUnescapedReads = false;
1182         for (BasicBlock* block : m_graph.blocksInPreOrder()) {
1183             m_heap = m_heapAtHead[block];
1184
1185             for (Node* node : *block) {
1186                 handleNode(
1187                     node,
1188                     [&] (PromotedHeapLocation location, LazyNode value) {
1189                         if (!value.isNode())
1190                             return;
1191
1192                         Allocation* allocation = m_heap.onlyLocalAllocation(value.asNode());
1193                         if (allocation && !allocation->isEscapedAllocation())
1194                             dependencies.add(allocation->identifier(), Vector<Node*>()).iterator->value.append(location.base());
1195                     },
1196                     [&] (PromotedHeapLocation) -> Node* {
1197                         hasUnescapedReads = true;
1198                         return nullptr;
1199                     });
1200             }
1201
1202             // The sink candidates are initially the unescaped
1203             // allocations dying at tail of blocks
1204             NodeSet allocations;
1205             for (const auto& entry : m_heap.allocations()) {
1206                 if (!entry.value.isEscapedAllocation())
1207                     allocations.addVoid(entry.key);
1208             }
1209
1210             m_heap.pruneByLiveness(m_combinedLiveness.liveAtTail[block]);
1211
1212             for (Node* identifier : allocations) {
1213                 if (!m_heap.isAllocation(identifier))
1214                     m_sinkCandidates.addVoid(identifier);
1215             }
1216         }
1217
1218         // Ensure that the set of sink candidates is closed for put operations
1219         Vector<Node*> worklist;
1220         worklist.appendRange(m_sinkCandidates.begin(), m_sinkCandidates.end());
1221
1222         while (!worklist.isEmpty()) {
1223             for (Node* identifier : dependencies.get(worklist.takeLast())) {
1224                 if (m_sinkCandidates.add(identifier).isNewEntry)
1225                     worklist.append(identifier);
1226             }
1227         }
1228
1229         if (m_sinkCandidates.isEmpty())
1230             return hasUnescapedReads;
1231
1232         if (DFGObjectAllocationSinkingPhaseInternal::verbose)
1233             dataLog("Candidates: ", listDump(m_sinkCandidates), "\n");
1234
1235         // Create the materialization nodes
1236         for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
1237             m_heap = m_heapAtHead[block];
1238             m_heap.setWantEscapees();
1239
1240             for (Node* node : *block) {
1241                 handleNode(
1242                     node,
1243                     [] (PromotedHeapLocation, LazyNode) { },
1244                     [] (PromotedHeapLocation) -> Node* {
1245                         return nullptr;
1246                     });
1247                 auto escapees = m_heap.takeEscapees();
1248                 if (!escapees.isEmpty())
1249                     placeMaterializations(escapees, node);
1250             }
1251
1252             m_heap.pruneByLiveness(m_combinedLiveness.liveAtTail[block]);
1253
1254             {
1255                 HashMap<Node*, Allocation> escapingOnEdge;
1256                 for (const auto& entry : m_heap.allocations()) {
1257                     if (entry.value.isEscapedAllocation())
1258                         continue;
1259
1260                     bool mustEscape = false;
1261                     for (BasicBlock* successorBlock : block->successors()) {
1262                         if (!m_heapAtHead[successorBlock].isAllocation(entry.key)
1263                             || m_heapAtHead[successorBlock].getAllocation(entry.key).isEscapedAllocation())
1264                             mustEscape = true;
1265                     }
1266
1267                     if (mustEscape)
1268                         escapingOnEdge.add(entry.key, entry.value);
1269                 }
1270                 placeMaterializations(WTFMove(escapingOnEdge), block->terminal());
1271             }
1272         }
1273
1274         return hasUnescapedReads || !m_sinkCandidates.isEmpty();
1275     }
1276
1277     void placeMaterializations(HashMap<Node*, Allocation> escapees, Node* where)
1278     {
1279         // We don't create materializations if the escapee is not a
1280         // sink candidate
1281         escapees.removeIf(
1282             [&] (const auto& entry) {
1283                 return !m_sinkCandidates.contains(entry.key);
1284             });
1285         if (escapees.isEmpty())
1286             return;
1287
1288         // First collect the hints that will be needed when the node
1289         // we materialize is still stored into other unescaped sink candidates.
1290         // The way to interpret this vector is:
1291         //
1292         // PromotedHeapLocation(NotEscapedAllocation, field) = identifierAllocation
1293         //
1294         // e.g:
1295         // PromotedHeapLocation(@PhantomNewFunction, FunctionActivationPLoc) = IdentifierOf(@MaterializeCreateActivation)
1296         // or:
1297         // PromotedHeapLocation(@PhantomCreateActivation, ClosureVarPLoc(x)) = IdentifierOf(@NewFunction)
1298         //
1299         // When the rhs of the `=` is to be materialized at this `where` point in the program
1300         // and IdentifierOf(Materialization) is the original sunken allocation of the materialization.
1301         //
1302         // The reason we need to collect all the `identifiers` here is that
1303         // we may materialize multiple versions of the allocation along control
1304         // flow edges. We will PutHint these values along those edges. However,
1305         // we also need to PutHint them when we join and have a Phi of the allocations.
1306         Vector<std::pair<PromotedHeapLocation, Node*>> hints;
1307         for (const auto& entry : m_heap.allocations()) {
1308             if (escapees.contains(entry.key))
1309                 continue;
1310
1311             for (const auto& field : entry.value.fields()) {
1312                 ASSERT(m_sinkCandidates.contains(entry.key) || !escapees.contains(field.value));
1313                 auto iter = escapees.find(field.value);
1314                 if (iter != escapees.end()) {
1315                     ASSERT(m_sinkCandidates.contains(field.value));
1316                     hints.append(std::make_pair(PromotedHeapLocation(entry.key, field.key), field.value));
1317                 }
1318             }
1319         }
1320
1321         // Now we need to order the materialization. Any order is
1322         // valid (as long as we materialize a node first if it is
1323         // needed for the materialization of another node, e.g. a
1324         // function's activation must be materialized before the
1325         // function itself), but we want to try minimizing the number
1326         // of times we have to place Puts to close cycles after a
1327         // materialization. In other words, we are trying to find the
1328         // minimum number of materializations to remove from the
1329         // materialization graph to make it a DAG, known as the
1330         // (vertex) feedback set problem. Unfortunately, this is a
1331         // NP-hard problem, which we don't want to solve exactly.
1332         //
1333         // Instead, we use a simple greedy procedure, that procedes as
1334         // follow:
1335         //  - While there is at least one node with no outgoing edge
1336         //    amongst the remaining materializations, materialize it
1337         //    first
1338         //
1339         //  - Similarily, while there is at least one node with no
1340         //    incoming edge amongst the remaining materializations,
1341         //    materialize it last.
1342         //
1343         //  - When both previous conditions are false, we have an
1344         //    actual cycle, and we need to pick a node to
1345         //    materialize. We try greedily to remove the "pressure" on
1346         //    the remaining nodes by choosing the node with maximum
1347         //    |incoming edges| * |outgoing edges| as a measure of how
1348         //    "central" to the graph it is. We materialize it first,
1349         //    so that all the recoveries will be Puts of things into
1350         //    it (rather than Puts of the materialization into other
1351         //    objects), which means we will have a single
1352         //    StoreBarrier.
1353
1354
1355         // Compute dependencies between materializations
1356         HashMap<Node*, NodeSet> dependencies;
1357         HashMap<Node*, NodeSet> reverseDependencies;
1358         HashMap<Node*, NodeSet> forMaterialization;
1359         for (const auto& entry : escapees) {
1360             auto& myDependencies = dependencies.add(entry.key, NodeSet()).iterator->value;
1361             auto& myDependenciesForMaterialization = forMaterialization.add(entry.key, NodeSet()).iterator->value;
1362             reverseDependencies.add(entry.key, NodeSet());
1363             for (const auto& field : entry.value.fields()) {
1364                 if (escapees.contains(field.value) && field.value != entry.key) {
1365                     myDependencies.addVoid(field.value);
1366                     reverseDependencies.add(field.value, NodeSet()).iterator->value.addVoid(entry.key);
1367                     if (field.key.neededForMaterialization())
1368                         myDependenciesForMaterialization.addVoid(field.value);
1369                 }
1370             }
1371         }
1372
1373         // Helper function to update the materialized set and the
1374         // dependencies
1375         NodeSet materialized;
1376         auto materialize = [&] (Node* identifier) {
1377             materialized.addVoid(identifier);
1378             for (Node* dep : dependencies.get(identifier))
1379                 reverseDependencies.find(dep)->value.remove(identifier);
1380             for (Node* rdep : reverseDependencies.get(identifier)) {
1381                 dependencies.find(rdep)->value.remove(identifier);
1382                 forMaterialization.find(rdep)->value.remove(identifier);
1383             }
1384             dependencies.remove(identifier);
1385             reverseDependencies.remove(identifier);
1386             forMaterialization.remove(identifier);
1387         };
1388
1389         // Nodes without remaining unmaterialized fields will be
1390         // materialized first - amongst the remaining unmaterialized
1391         // nodes
1392         StdList<Allocation> toMaterialize;
1393         auto firstPos = toMaterialize.begin();
1394         auto materializeFirst = [&] (Allocation&& allocation) {
1395             materialize(allocation.identifier());
1396             // We need to insert *after* the current position
1397             if (firstPos != toMaterialize.end())
1398                 ++firstPos;
1399             firstPos = toMaterialize.insert(firstPos, WTFMove(allocation));
1400         };
1401
1402         // Nodes that no other unmaterialized node points to will be
1403         // materialized last - amongst the remaining unmaterialized
1404         // nodes
1405         auto lastPos = toMaterialize.end();
1406         auto materializeLast = [&] (Allocation&& allocation) {
1407             materialize(allocation.identifier());
1408             lastPos = toMaterialize.insert(lastPos, WTFMove(allocation));
1409         };
1410
1411         // These are the promoted locations that contains some of the
1412         // allocations we are currently escaping. If they are a location on
1413         // some other allocation we are currently materializing, we will need
1414         // to "recover" their value with a real put once the corresponding
1415         // allocation is materialized; if they are a location on some other
1416         // not-yet-materialized allocation, we will need a PutHint.
1417         Vector<PromotedHeapLocation> toRecover;
1418
1419         // This loop does the actual cycle breaking
1420         while (!escapees.isEmpty()) {
1421             materialized.clear();
1422
1423             // Materialize nodes that won't require recoveries if we can
1424             for (auto& entry : escapees) {
1425                 if (!forMaterialization.find(entry.key)->value.isEmpty())
1426                     continue;
1427
1428                 if (dependencies.find(entry.key)->value.isEmpty()) {
1429                     materializeFirst(WTFMove(entry.value));
1430                     continue;
1431                 }
1432
1433                 if (reverseDependencies.find(entry.key)->value.isEmpty()) {
1434                     materializeLast(WTFMove(entry.value));
1435                     continue;
1436                 }
1437             }
1438
1439             // We reach this only if there is an actual cycle that needs
1440             // breaking. Because we do not want to solve a NP-hard problem
1441             // here, we just heuristically pick a node and materialize it
1442             // first.
1443             if (materialized.isEmpty()) {
1444                 uint64_t maxEvaluation = 0;
1445                 Allocation* bestAllocation = nullptr;
1446                 for (auto& entry : escapees) {
1447                     if (!forMaterialization.find(entry.key)->value.isEmpty())
1448                         continue;
1449
1450                     uint64_t evaluation =
1451                         static_cast<uint64_t>(dependencies.get(entry.key).size()) * reverseDependencies.get(entry.key).size();
1452                     if (evaluation > maxEvaluation) {
1453                         maxEvaluation = evaluation;
1454                         bestAllocation = &entry.value;
1455                     }
1456                 }
1457                 RELEASE_ASSERT(maxEvaluation > 0);
1458
1459                 materializeFirst(WTFMove(*bestAllocation));
1460             }
1461             RELEASE_ASSERT(!materialized.isEmpty());
1462
1463             for (Node* identifier : materialized)
1464                 escapees.remove(identifier);
1465         }
1466
1467         materialized.clear();
1468
1469         NodeSet escaped;
1470         for (const Allocation& allocation : toMaterialize)
1471             escaped.addVoid(allocation.identifier());
1472         for (const Allocation& allocation : toMaterialize) {
1473             for (const auto& field : allocation.fields()) {
1474                 if (escaped.contains(field.value) && !materialized.contains(field.value))
1475                     toRecover.append(PromotedHeapLocation(allocation.identifier(), field.key));
1476             }
1477             materialized.addVoid(allocation.identifier());
1478         }
1479
1480         Vector<Node*>& materializations = m_materializationSiteToMaterializations.add(
1481             where, Vector<Node*>()).iterator->value;
1482
1483         for (const Allocation& allocation : toMaterialize) {
1484             Node* materialization = createMaterialization(allocation, where);
1485             materializations.append(materialization);
1486             m_materializationToEscapee.add(materialization, allocation.identifier());
1487         }
1488
1489         if (!toRecover.isEmpty()) {
1490             m_materializationSiteToRecoveries.add(
1491                 where, Vector<PromotedHeapLocation>()).iterator->value.appendVector(toRecover);
1492         }
1493
1494         // The hints need to be after the "real" recoveries so that we
1495         // don't hint not-yet-complete objects
1496         m_materializationSiteToHints.add(
1497             where, Vector<std::pair<PromotedHeapLocation, Node*>>()).iterator->value.appendVector(hints);
1498     }
1499
1500     Node* createMaterialization(const Allocation& allocation, Node* where)
1501     {
1502         // FIXME: This is the only place where we actually use the
1503         // fact that an allocation's identifier is indeed the node
1504         // that created the allocation.
1505         switch (allocation.kind()) {
1506         case Allocation::Kind::Object: {
1507             ObjectMaterializationData* data = m_graph.m_objectMaterializationData.add();
1508
1509             return m_graph.addNode(
1510                 allocation.identifier()->prediction(), Node::VarArg, MaterializeNewObject,
1511                 where->origin.withSemantic(allocation.identifier()->origin.semantic),
1512                 OpInfo(m_graph.addStructureSet(allocation.structures())), OpInfo(data), 0, 0);
1513         }
1514
1515         case Allocation::Kind::AsyncGeneratorFunction:
1516         case Allocation::Kind::AsyncFunction:
1517         case Allocation::Kind::GeneratorFunction:
1518         case Allocation::Kind::Function: {
1519             FrozenValue* executable = allocation.identifier()->cellOperand();
1520             
1521             NodeType nodeType;
1522             switch (allocation.kind()) {
1523             case Allocation::Kind::GeneratorFunction:
1524                 nodeType = NewGeneratorFunction;
1525                 break;
1526             case Allocation::Kind::AsyncGeneratorFunction:
1527                 nodeType = NewAsyncGeneratorFunction;
1528                 break;
1529             case Allocation::Kind::AsyncFunction:
1530                 nodeType = NewAsyncFunction;
1531                 break;
1532             default:
1533                 nodeType = NewFunction;
1534             }
1535
1536             return m_graph.addNode(
1537                 allocation.identifier()->prediction(), nodeType,
1538                 where->origin.withSemantic(
1539                     allocation.identifier()->origin.semantic),
1540                 OpInfo(executable));
1541         }
1542
1543         case Allocation::Kind::Activation: {
1544             ObjectMaterializationData* data = m_graph.m_objectMaterializationData.add();
1545             FrozenValue* symbolTable = allocation.identifier()->cellOperand();
1546
1547             return m_graph.addNode(
1548                 allocation.identifier()->prediction(), Node::VarArg, MaterializeCreateActivation,
1549                 where->origin.withSemantic(
1550                     allocation.identifier()->origin.semantic),
1551                 OpInfo(symbolTable), OpInfo(data), 0, 0);
1552         }
1553
1554         case Allocation::Kind::RegExpObject: {
1555             FrozenValue* regExp = allocation.identifier()->cellOperand();
1556             return m_graph.addNode(
1557                 allocation.identifier()->prediction(), NewRegexp,
1558                 where->origin.withSemantic(
1559                     allocation.identifier()->origin.semantic),
1560                 OpInfo(regExp));
1561         }
1562
1563         default:
1564             DFG_CRASH(m_graph, allocation.identifier(), "Bad allocation kind");
1565         }
1566     }
1567
1568     void promoteLocalHeap()
1569     {
1570         // Collect the set of heap locations that we will be operating
1571         // over.
1572         HashSet<PromotedHeapLocation> locations;
1573         for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
1574             m_heap = m_heapAtHead[block];
1575
1576             for (Node* node : *block) {
1577                 handleNode(
1578                     node,
1579                     [&] (PromotedHeapLocation location, LazyNode) {
1580                         // If the location is not on a sink candidate,
1581                         // we only sink it if it is read
1582                         if (m_sinkCandidates.contains(location.base()))
1583                             locations.addVoid(location);
1584                     },
1585                     [&] (PromotedHeapLocation location) -> Node* {
1586                         locations.addVoid(location);
1587                         return nullptr;
1588                     });
1589             }
1590         }
1591
1592         // Figure out which locations belong to which allocations.
1593         m_locationsForAllocation.clear();
1594         for (PromotedHeapLocation location : locations) {
1595             auto result = m_locationsForAllocation.add(
1596                 location.base(),
1597                 Vector<PromotedHeapLocation>());
1598             ASSERT(!result.iterator->value.contains(location));
1599             result.iterator->value.append(location);
1600         }
1601
1602         m_pointerSSA.reset();
1603         m_allocationSSA.reset();
1604
1605         // Collect the set of "variables" that we will be sinking.
1606         m_locationToVariable.clear();
1607         m_nodeToVariable.clear();
1608         Vector<Node*> indexToNode;
1609         Vector<PromotedHeapLocation> indexToLocation;
1610
1611         for (Node* index : m_sinkCandidates) {
1612             SSACalculator::Variable* variable = m_allocationSSA.newVariable();
1613             m_nodeToVariable.add(index, variable);
1614             ASSERT(indexToNode.size() == variable->index());
1615             indexToNode.append(index);
1616         }
1617
1618         for (PromotedHeapLocation location : locations) {
1619             SSACalculator::Variable* variable = m_pointerSSA.newVariable();
1620             m_locationToVariable.add(location, variable);
1621             ASSERT(indexToLocation.size() == variable->index());
1622             indexToLocation.append(location);
1623         }
1624
1625         // We insert all required constants at top of block 0 so that
1626         // they are inserted only once and we don't clutter the graph
1627         // with useless constants everywhere
1628         HashMap<FrozenValue*, Node*> lazyMapping;
1629         if (!m_bottom)
1630             m_bottom = m_insertionSet.insertConstant(0, m_graph.block(0)->at(0)->origin, jsNumber(1927));
1631
1632         Vector<HashSet<PromotedHeapLocation>> hintsForPhi(m_sinkCandidates.size());
1633
1634         for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
1635             m_heap = m_heapAtHead[block];
1636
1637             for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) {
1638                 Node* node = block->at(nodeIndex);
1639
1640                 // Some named properties can be added conditionally,
1641                 // and that would necessitate bottoms
1642                 for (PromotedHeapLocation location : m_locationsForAllocation.get(node)) {
1643                     if (location.kind() != NamedPropertyPLoc)
1644                         continue;
1645
1646                     SSACalculator::Variable* variable = m_locationToVariable.get(location);
1647                     m_pointerSSA.newDef(variable, block, m_bottom);
1648                 }
1649
1650                 for (Node* materialization : m_materializationSiteToMaterializations.get(node)) {
1651                     Node* escapee = m_materializationToEscapee.get(materialization);
1652                     m_allocationSSA.newDef(m_nodeToVariable.get(escapee), block, materialization);
1653                 }
1654
1655                 for (std::pair<PromotedHeapLocation, Node*> pair : m_materializationSiteToHints.get(node)) {
1656                     PromotedHeapLocation location = pair.first;
1657                     Node* identifier = pair.second;
1658                     // We're materializing `identifier` at this point, and the unmaterialized
1659                     // version is inside `location`. We track which SSA variable this belongs
1660                     // to in case we also need a PutHint for the Phi.
1661                     if (UNLIKELY(validationEnabled())) {
1662                         RELEASE_ASSERT(m_sinkCandidates.contains(location.base()));
1663                         RELEASE_ASSERT(m_sinkCandidates.contains(identifier));
1664
1665                         bool found = false;
1666                         for (Node* materialization : m_materializationSiteToMaterializations.get(node)) {
1667                             // We're materializing `identifier` here. This asserts that this is indeed the case.
1668                             if (m_materializationToEscapee.get(materialization) == identifier) {
1669                                 found = true;
1670                                 break;
1671                             }
1672                         }
1673                         RELEASE_ASSERT(found);
1674                     }
1675
1676                     SSACalculator::Variable* variable = m_nodeToVariable.get(identifier);
1677                     hintsForPhi[variable->index()].addVoid(location);
1678                 }
1679
1680                 if (m_sinkCandidates.contains(node))
1681                     m_allocationSSA.newDef(m_nodeToVariable.get(node), block, node);
1682
1683                 handleNode(
1684                     node,
1685                     [&] (PromotedHeapLocation location, LazyNode value) {
1686                         if (!locations.contains(location))
1687                             return;
1688
1689                         Node* nodeValue;
1690                         if (value.isNode())
1691                             nodeValue = value.asNode();
1692                         else {
1693                             auto iter = lazyMapping.find(value.asValue());
1694                             if (iter != lazyMapping.end())
1695                                 nodeValue = iter->value;
1696                             else {
1697                                 nodeValue = value.ensureIsNode(
1698                                     m_insertionSet, m_graph.block(0), 0);
1699                                 lazyMapping.add(value.asValue(), nodeValue);
1700                             }
1701                         }
1702
1703                         SSACalculator::Variable* variable = m_locationToVariable.get(location);
1704                         m_pointerSSA.newDef(variable, block, nodeValue);
1705                     },
1706                     [] (PromotedHeapLocation) -> Node* {
1707                         return nullptr;
1708                     });
1709             }
1710         }
1711         m_insertionSet.execute(m_graph.block(0));
1712
1713         // Run the SSA calculators to create Phis
1714         m_pointerSSA.computePhis(
1715             [&] (SSACalculator::Variable* variable, BasicBlock* block) -> Node* {
1716                 PromotedHeapLocation location = indexToLocation[variable->index()];
1717
1718                 // Don't create Phi nodes for fields of dead allocations
1719                 if (!m_heapAtHead[block].isAllocation(location.base()))
1720                     return nullptr;
1721
1722                 // Don't create Phi nodes once we are escaped
1723                 if (m_heapAtHead[block].getAllocation(location.base()).isEscapedAllocation())
1724                     return nullptr;
1725
1726                 // If we point to a single allocation, we will
1727                 // directly use its materialization
1728                 if (m_heapAtHead[block].follow(location))
1729                     return nullptr;
1730
1731                 Node* phiNode = m_graph.addNode(SpecHeapTop, Phi, block->at(0)->origin.withInvalidExit());
1732                 phiNode->mergeFlags(NodeResultJS);
1733                 return phiNode;
1734             });
1735
1736         m_allocationSSA.computePhis(
1737             [&] (SSACalculator::Variable* variable, BasicBlock* block) -> Node* {
1738                 Node* identifier = indexToNode[variable->index()];
1739
1740                 // Don't create Phi nodes for dead allocations
1741                 if (!m_heapAtHead[block].isAllocation(identifier))
1742                     return nullptr;
1743
1744                 // Don't create Phi nodes until we are escaped
1745                 if (!m_heapAtHead[block].getAllocation(identifier).isEscapedAllocation())
1746                     return nullptr;
1747
1748                 Node* phiNode = m_graph.addNode(SpecHeapTop, Phi, block->at(0)->origin.withInvalidExit());
1749                 phiNode->mergeFlags(NodeResultJS);
1750                 return phiNode;
1751             });
1752
1753         // Place Phis in the right places, replace all uses of any load with the appropriate
1754         // value, and create the materialization nodes.
1755         LocalOSRAvailabilityCalculator availabilityCalculator(m_graph);
1756         m_graph.clearReplacements();
1757         for (BasicBlock* block : m_graph.blocksInPreOrder()) {
1758             m_heap = m_heapAtHead[block];
1759             availabilityCalculator.beginBlock(block);
1760
1761             // These mapping tables are intended to be lazy. If
1762             // something is omitted from the table, it means that
1763             // there haven't been any local stores to the promoted
1764             // heap location (or any local materialization).
1765             m_localMapping.clear();
1766             m_escapeeToMaterialization.clear();
1767
1768             // Insert the Phi functions that we had previously
1769             // created.
1770             for (SSACalculator::Def* phiDef : m_pointerSSA.phisForBlock(block)) {
1771                 SSACalculator::Variable* variable = phiDef->variable();
1772                 m_insertionSet.insert(0, phiDef->value());
1773
1774                 PromotedHeapLocation location = indexToLocation[variable->index()];
1775                 m_localMapping.set(location, phiDef->value());
1776
1777                 if (m_sinkCandidates.contains(location.base())) {
1778                     m_insertionSet.insert(
1779                         0,
1780                         location.createHint(
1781                             m_graph, block->at(0)->origin.withInvalidExit(), phiDef->value()));
1782                 }
1783             }
1784
1785             for (SSACalculator::Def* phiDef : m_allocationSSA.phisForBlock(block)) {
1786                 SSACalculator::Variable* variable = phiDef->variable();
1787                 m_insertionSet.insert(0, phiDef->value());
1788
1789                 Node* identifier = indexToNode[variable->index()];
1790                 m_escapeeToMaterialization.add(identifier, phiDef->value());
1791                 bool canExit = false;
1792                 insertOSRHintsForUpdate(
1793                     0, block->at(0)->origin, canExit,
1794                     availabilityCalculator.m_availability, identifier, phiDef->value());
1795
1796                 for (PromotedHeapLocation location : hintsForPhi[variable->index()]) {
1797                     m_insertionSet.insert(0,
1798                         location.createHint(m_graph, block->at(0)->origin.withInvalidExit(), phiDef->value()));
1799                     m_localMapping.set(location, phiDef->value());
1800                 }
1801             }
1802
1803             if (DFGObjectAllocationSinkingPhaseInternal::verbose) {
1804                 dataLog("Local mapping at ", pointerDump(block), ": ", mapDump(m_localMapping), "\n");
1805                 dataLog("Local materializations at ", pointerDump(block), ": ", mapDump(m_escapeeToMaterialization), "\n");
1806             }
1807
1808             for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) {
1809                 Node* node = block->at(nodeIndex);
1810                 bool canExit = true;
1811                 bool nextCanExit = node->origin.exitOK;
1812                 for (PromotedHeapLocation location : m_locationsForAllocation.get(node)) {
1813                     if (location.kind() != NamedPropertyPLoc)
1814                         continue;
1815
1816                     m_localMapping.set(location, m_bottom);
1817
1818                     if (m_sinkCandidates.contains(node)) {
1819                         if (DFGObjectAllocationSinkingPhaseInternal::verbose)
1820                             dataLog("For sink candidate ", node, " found location ", location, "\n");
1821                         m_insertionSet.insert(
1822                             nodeIndex + 1,
1823                             location.createHint(
1824                                 m_graph, node->origin.takeValidExit(nextCanExit), m_bottom));
1825                     }
1826                 }
1827
1828                 for (Node* materialization : m_materializationSiteToMaterializations.get(node)) {
1829                     materialization->origin.exitOK &= canExit;
1830                     Node* escapee = m_materializationToEscapee.get(materialization);
1831                     populateMaterialization(block, materialization, escapee);
1832                     m_escapeeToMaterialization.set(escapee, materialization);
1833                     m_insertionSet.insert(nodeIndex, materialization);
1834                     if (DFGObjectAllocationSinkingPhaseInternal::verbose)
1835                         dataLog("Materializing ", escapee, " => ", materialization, " at ", node, "\n");
1836                 }
1837
1838                 for (PromotedHeapLocation location : m_materializationSiteToRecoveries.get(node))
1839                     m_insertionSet.insert(nodeIndex, createRecovery(block, location, node, canExit));
1840                 for (std::pair<PromotedHeapLocation, Node*> pair : m_materializationSiteToHints.get(node))
1841                     m_insertionSet.insert(nodeIndex, createRecovery(block, pair.first, node, canExit));
1842
1843                 // We need to put the OSR hints after the recoveries,
1844                 // because we only want the hints once the object is
1845                 // complete
1846                 for (Node* materialization : m_materializationSiteToMaterializations.get(node)) {
1847                     Node* escapee = m_materializationToEscapee.get(materialization);
1848                     insertOSRHintsForUpdate(
1849                         nodeIndex, node->origin, canExit,
1850                         availabilityCalculator.m_availability, escapee, materialization);
1851                 }
1852
1853                 if (node->origin.exitOK && !canExit) {
1854                     // We indicate that the exit state is fine now. It is OK because we updated the
1855                     // state above. We need to indicate this manually because the validation doesn't
1856                     // have enough information to infer that the exit state is fine.
1857                     m_insertionSet.insertNode(nodeIndex, SpecNone, ExitOK, node->origin);
1858                 }
1859
1860                 if (m_sinkCandidates.contains(node))
1861                     m_escapeeToMaterialization.set(node, node);
1862
1863                 availabilityCalculator.executeNode(node);
1864
1865                 bool desiredNextExitOK = node->origin.exitOK && !clobbersExitState(m_graph, node);
1866
1867                 bool doLower = false;
1868                 handleNode(
1869                     node,
1870                     [&] (PromotedHeapLocation location, LazyNode value) {
1871                         if (!locations.contains(location))
1872                             return;
1873
1874                         Node* nodeValue;
1875                         if (value.isNode())
1876                             nodeValue = value.asNode();
1877                         else
1878                             nodeValue = lazyMapping.get(value.asValue());
1879
1880                         nodeValue = resolve(block, nodeValue);
1881
1882                         m_localMapping.set(location, nodeValue);
1883
1884                         if (!m_sinkCandidates.contains(location.base()))
1885                             return;
1886
1887                         doLower = true;
1888
1889                         if (DFGObjectAllocationSinkingPhaseInternal::verbose)
1890                             dataLog("Creating hint with value ", nodeValue, " before ", node, "\n");
1891                         m_insertionSet.insert(
1892                             nodeIndex + 1,
1893                             location.createHint(
1894                                 m_graph, node->origin.takeValidExit(nextCanExit), nodeValue));
1895                     },
1896                     [&] (PromotedHeapLocation location) -> Node* {
1897                         return resolve(block, location);
1898                     });
1899
1900                 if (!nextCanExit && desiredNextExitOK) {
1901                     // We indicate that the exit state is fine now. We need to do this because we
1902                     // emitted hints that appear to invalidate the exit state.
1903                     m_insertionSet.insertNode(nodeIndex + 1, SpecNone, ExitOK, node->origin);
1904                 }
1905
1906                 if (m_sinkCandidates.contains(node) || doLower) {
1907                     switch (node->op()) {
1908                     case NewObject:
1909                         node->convertToPhantomNewObject();
1910                         break;
1911
1912                     case NewFunction:
1913                         node->convertToPhantomNewFunction();
1914                         break;
1915
1916                     case NewGeneratorFunction:
1917                         node->convertToPhantomNewGeneratorFunction();
1918                         break;
1919
1920                     case NewAsyncGeneratorFunction:
1921                         node->convertToPhantomNewAsyncGeneratorFunction();
1922                         break;
1923
1924                     case NewAsyncFunction:
1925                         node->convertToPhantomNewAsyncFunction();
1926                         break;
1927
1928                     case CreateActivation:
1929                         node->convertToPhantomCreateActivation();
1930                         break;
1931
1932                     case NewRegexp:
1933                         node->convertToPhantomNewRegexp();
1934                         break;
1935
1936                     default:
1937                         node->remove(m_graph);
1938                         break;
1939                     }
1940                 }
1941
1942                 m_graph.doToChildren(
1943                     node,
1944                     [&] (Edge& edge) {
1945                         edge.setNode(resolve(block, edge.node()));
1946                     });
1947             }
1948
1949             // Gotta drop some Upsilons.
1950             NodeAndIndex terminal = block->findTerminal();
1951             size_t upsilonInsertionPoint = terminal.index;
1952             NodeOrigin upsilonOrigin = terminal.node->origin;
1953             for (BasicBlock* successorBlock : block->successors()) {
1954                 for (SSACalculator::Def* phiDef : m_pointerSSA.phisForBlock(successorBlock)) {
1955                     Node* phiNode = phiDef->value();
1956                     SSACalculator::Variable* variable = phiDef->variable();
1957                     PromotedHeapLocation location = indexToLocation[variable->index()];
1958                     Node* incoming = resolve(block, location);
1959
1960                     m_insertionSet.insertNode(
1961                         upsilonInsertionPoint, SpecNone, Upsilon, upsilonOrigin,
1962                         OpInfo(phiNode), incoming->defaultEdge());
1963                 }
1964
1965                 for (SSACalculator::Def* phiDef : m_allocationSSA.phisForBlock(successorBlock)) {
1966                     Node* phiNode = phiDef->value();
1967                     SSACalculator::Variable* variable = phiDef->variable();
1968                     Node* incoming = getMaterialization(block, indexToNode[variable->index()]);
1969
1970                     m_insertionSet.insertNode(
1971                         upsilonInsertionPoint, SpecNone, Upsilon, upsilonOrigin,
1972                         OpInfo(phiNode), incoming->defaultEdge());
1973                 }
1974             }
1975
1976             m_insertionSet.execute(block);
1977         }
1978     }
1979
1980     NEVER_INLINE Node* resolve(BasicBlock* block, PromotedHeapLocation location)
1981     {
1982         // If we are currently pointing to a single local allocation,
1983         // simply return the associated materialization.
1984         if (Node* identifier = m_heap.follow(location))
1985             return getMaterialization(block, identifier);
1986
1987         if (Node* result = m_localMapping.get(location))
1988             return result;
1989
1990         // This implies that there is no local mapping. Find a non-local mapping.
1991         SSACalculator::Def* def = m_pointerSSA.nonLocalReachingDef(
1992             block, m_locationToVariable.get(location));
1993         ASSERT(def);
1994         ASSERT(def->value());
1995
1996         Node* result = def->value();
1997         if (result->replacement())
1998             result = result->replacement();
1999         ASSERT(!result->replacement());
2000
2001         m_localMapping.add(location, result);
2002         return result;
2003     }
2004
2005     NEVER_INLINE Node* resolve(BasicBlock* block, Node* node)
2006     {
2007         // If we are currently pointing to a single local allocation,
2008         // simply return the associated materialization.
2009         if (Node* identifier = m_heap.follow(node))
2010             return getMaterialization(block, identifier);
2011
2012         if (node->replacement())
2013             node = node->replacement();
2014         ASSERT(!node->replacement());
2015
2016         return node;
2017     }
2018
2019     NEVER_INLINE Node* getMaterialization(BasicBlock* block, Node* identifier)
2020     {
2021         ASSERT(m_heap.isAllocation(identifier));
2022         if (!m_sinkCandidates.contains(identifier))
2023             return identifier;
2024
2025         if (Node* materialization = m_escapeeToMaterialization.get(identifier))
2026             return materialization;
2027
2028         SSACalculator::Def* def = m_allocationSSA.nonLocalReachingDef(
2029             block, m_nodeToVariable.get(identifier));
2030         ASSERT(def && def->value());
2031         m_escapeeToMaterialization.add(identifier, def->value());
2032         ASSERT(!def->value()->replacement());
2033         return def->value();
2034     }
2035
2036     void insertOSRHintsForUpdate(unsigned nodeIndex, NodeOrigin origin, bool& canExit, AvailabilityMap& availability, Node* escapee, Node* materialization)
2037     {
2038         if (DFGObjectAllocationSinkingPhaseInternal::verbose) {
2039             dataLog("Inserting OSR hints at ", origin, ":\n");
2040             dataLog("    Escapee: ", escapee, "\n");
2041             dataLog("    Materialization: ", materialization, "\n");
2042             dataLog("    Availability: ", availability, "\n");
2043         }
2044         
2045         // We need to follow() the value in the heap.
2046         // Consider the following graph:
2047         //
2048         // Block #0
2049         //   0: NewObject({})
2050         //   1: NewObject({})
2051         //   -: PutByOffset(@0, @1, x:0)
2052         //   -: PutStructure(@0, {x:0})
2053         //   2: GetByOffset(@0, x:0)
2054         //   -: MovHint(@2, loc1)
2055         //   -: Branch(#1, #2)
2056         //
2057         // Block #1
2058         //   3: Call(f, @1)
2059         //   4: Return(@0)
2060         //
2061         // Block #2
2062         //   -: Return(undefined)
2063         //
2064         // We need to materialize @1 at @3, and when doing so we need
2065         // to insert a MovHint for the materialization into loc1 as
2066         // well.
2067         // In order to do this, we say that we need to insert an
2068         // update hint for any availability whose node resolve()s to
2069         // the materialization.
2070         for (auto entry : availability.m_heap) {
2071             if (!entry.value.hasNode())
2072                 continue;
2073             if (m_heap.follow(entry.value.node()) != escapee)
2074                 continue;
2075
2076             m_insertionSet.insert(
2077                 nodeIndex,
2078                 entry.key.createHint(m_graph, origin.takeValidExit(canExit), materialization));
2079         }
2080
2081         for (unsigned i = availability.m_locals.size(); i--;) {
2082             if (!availability.m_locals[i].hasNode())
2083                 continue;
2084             if (m_heap.follow(availability.m_locals[i].node()) != escapee)
2085                 continue;
2086
2087             int operand = availability.m_locals.operandForIndex(i);
2088             m_insertionSet.insertNode(
2089                 nodeIndex, SpecNone, MovHint, origin.takeValidExit(canExit), OpInfo(operand),
2090                 materialization->defaultEdge());
2091         }
2092     }
2093
2094     void populateMaterialization(BasicBlock* block, Node* node, Node* escapee)
2095     {
2096         Allocation& allocation = m_heap.getAllocation(escapee);
2097         switch (node->op()) {
2098         case MaterializeNewObject: {
2099             ObjectMaterializationData& data = node->objectMaterializationData();
2100             unsigned firstChild = m_graph.m_varArgChildren.size();
2101
2102             Vector<PromotedHeapLocation> locations = m_locationsForAllocation.get(escapee);
2103
2104             PromotedHeapLocation structure(StructurePLoc, allocation.identifier());
2105             ASSERT(locations.contains(structure));
2106
2107             m_graph.m_varArgChildren.append(Edge(resolve(block, structure), KnownCellUse));
2108
2109             for (PromotedHeapLocation location : locations) {
2110                 switch (location.kind()) {
2111                 case StructurePLoc:
2112                     ASSERT(location == structure);
2113                     break;
2114
2115                 case NamedPropertyPLoc: {
2116                     ASSERT(location.base() == allocation.identifier());
2117                     data.m_properties.append(location.descriptor());
2118                     Node* value = resolve(block, location);
2119                     if (m_sinkCandidates.contains(value))
2120                         m_graph.m_varArgChildren.append(m_bottom);
2121                     else
2122                         m_graph.m_varArgChildren.append(value);
2123                     break;
2124                 }
2125
2126                 default:
2127                     DFG_CRASH(m_graph, node, "Bad location kind");
2128                 }
2129             }
2130
2131             node->children = AdjacencyList(
2132                 AdjacencyList::Variable,
2133                 firstChild, m_graph.m_varArgChildren.size() - firstChild);
2134             break;
2135         }
2136
2137         case MaterializeCreateActivation: {
2138             ObjectMaterializationData& data = node->objectMaterializationData();
2139
2140             unsigned firstChild = m_graph.m_varArgChildren.size();
2141
2142             Vector<PromotedHeapLocation> locations = m_locationsForAllocation.get(escapee);
2143
2144             PromotedHeapLocation symbolTable(ActivationSymbolTablePLoc, allocation.identifier());
2145             ASSERT(locations.contains(symbolTable));
2146             ASSERT(node->cellOperand() == resolve(block, symbolTable)->constant());
2147             m_graph.m_varArgChildren.append(Edge(resolve(block, symbolTable), KnownCellUse));
2148
2149             PromotedHeapLocation scope(ActivationScopePLoc, allocation.identifier());
2150             ASSERT(locations.contains(scope));
2151             m_graph.m_varArgChildren.append(Edge(resolve(block, scope), KnownCellUse));
2152
2153             for (PromotedHeapLocation location : locations) {
2154                 switch (location.kind()) {
2155                 case ActivationScopePLoc: {
2156                     ASSERT(location == scope);
2157                     break;
2158                 }
2159
2160                 case ActivationSymbolTablePLoc: {
2161                     ASSERT(location == symbolTable);
2162                     break;
2163                 }
2164
2165                 case ClosureVarPLoc: {
2166                     ASSERT(location.base() == allocation.identifier());
2167                     data.m_properties.append(location.descriptor());
2168                     Node* value = resolve(block, location);
2169                     if (m_sinkCandidates.contains(value))
2170                         m_graph.m_varArgChildren.append(m_bottom);
2171                     else
2172                         m_graph.m_varArgChildren.append(value);
2173                     break;
2174                 }
2175
2176                 default:
2177                     DFG_CRASH(m_graph, node, "Bad location kind");
2178                 }
2179             }
2180
2181             node->children = AdjacencyList(
2182                 AdjacencyList::Variable,
2183                 firstChild, m_graph.m_varArgChildren.size() - firstChild);
2184             break;
2185         }
2186         
2187         case NewFunction:
2188         case NewGeneratorFunction:
2189         case NewAsyncGeneratorFunction:
2190         case NewAsyncFunction: {
2191             Vector<PromotedHeapLocation> locations = m_locationsForAllocation.get(escapee);
2192             ASSERT(locations.size() == 2);
2193                 
2194             PromotedHeapLocation executable(FunctionExecutablePLoc, allocation.identifier());
2195             ASSERT_UNUSED(executable, locations.contains(executable));
2196                 
2197             PromotedHeapLocation activation(FunctionActivationPLoc, allocation.identifier());
2198             ASSERT(locations.contains(activation));
2199
2200             node->child1() = Edge(resolve(block, activation), KnownCellUse);
2201             break;
2202         }
2203
2204         case NewRegexp: {
2205             Vector<PromotedHeapLocation> locations = m_locationsForAllocation.get(escapee);
2206             ASSERT(locations.size() == 2);
2207
2208             PromotedHeapLocation regExp(RegExpObjectRegExpPLoc, allocation.identifier());
2209             ASSERT_UNUSED(regExp, locations.contains(regExp));
2210
2211             PromotedHeapLocation lastIndex(RegExpObjectLastIndexPLoc, allocation.identifier());
2212             ASSERT(locations.contains(lastIndex));
2213             Node* value = resolve(block, lastIndex);
2214             if (m_sinkCandidates.contains(value))
2215                 node->child1() = Edge(m_bottom);
2216             else
2217                 node->child1() = Edge(value);
2218             break;
2219         }
2220
2221         default:
2222             DFG_CRASH(m_graph, node, "Bad materialize op");
2223         }
2224     }
2225
2226     Node* createRecovery(BasicBlock* block, PromotedHeapLocation location, Node* where, bool& canExit)
2227     {
2228         if (DFGObjectAllocationSinkingPhaseInternal::verbose)
2229             dataLog("Recovering ", location, " at ", where, "\n");
2230         ASSERT(location.base()->isPhantomAllocation());
2231         Node* base = getMaterialization(block, location.base());
2232         Node* value = resolve(block, location);
2233
2234         NodeOrigin origin = where->origin.withSemantic(base->origin.semantic);
2235
2236         if (DFGObjectAllocationSinkingPhaseInternal::verbose)
2237             dataLog("Base is ", base, " and value is ", value, "\n");
2238
2239         if (base->isPhantomAllocation()) {
2240             return PromotedHeapLocation(base, location.descriptor()).createHint(
2241                 m_graph, origin.takeValidExit(canExit), value);
2242         }
2243
2244         switch (location.kind()) {
2245         case NamedPropertyPLoc: {
2246             Allocation& allocation = m_heap.getAllocation(location.base());
2247
2248             Vector<RegisteredStructure> structures;
2249             structures.appendRange(allocation.structures().begin(), allocation.structures().end());
2250             unsigned identifierNumber = location.info();
2251             UniquedStringImpl* uid = m_graph.identifiers()[identifierNumber];
2252
2253             std::sort(
2254                 structures.begin(),
2255                 structures.end(),
2256                 [uid] (RegisteredStructure a, RegisteredStructure b) -> bool {
2257                     return a->getConcurrently(uid) < b->getConcurrently(uid);
2258                 });
2259
2260             RELEASE_ASSERT(structures.size());
2261             PropertyOffset firstOffset = structures[0]->getConcurrently(uid);
2262
2263             if (firstOffset == structures.last()->getConcurrently(uid)) {
2264                 Node* storage = base;
2265                 // FIXME: When we decide to sink objects with a
2266                 // property storage, we should handle non-inline offsets.
2267                 RELEASE_ASSERT(isInlineOffset(firstOffset));
2268
2269                 StorageAccessData* data = m_graph.m_storageAccessData.add();
2270                 data->offset = firstOffset;
2271                 data->identifierNumber = identifierNumber;
2272
2273                 return m_graph.addNode(
2274                     PutByOffset,
2275                     origin.takeValidExit(canExit),
2276                     OpInfo(data),
2277                     Edge(storage, KnownCellUse),
2278                     Edge(base, KnownCellUse),
2279                     value->defaultEdge());
2280             }
2281
2282             MultiPutByOffsetData* data = m_graph.m_multiPutByOffsetData.add();
2283             data->identifierNumber = identifierNumber;
2284
2285             {
2286                 PropertyOffset currentOffset = firstOffset;
2287                 StructureSet currentSet;
2288                 for (RegisteredStructure structure : structures) {
2289                     PropertyOffset offset = structure->getConcurrently(uid);
2290                     if (offset != currentOffset) {
2291                         // Because our analysis treats MultiPutByOffset like an escape, we only have to
2292                         // deal with storing results that would have been previously stored by PutByOffset
2293                         // nodes. Those nodes were guarded by the appropriate type checks. This means that
2294                         // at this point, we can simply trust that the incoming value has the right type
2295                         // for whatever structure we are using.
2296                         data->variants.append(
2297                             PutByIdVariant::replace(currentSet, currentOffset, InferredType::Top));
2298                         currentOffset = offset;
2299                         currentSet.clear();
2300                     }
2301                     currentSet.add(structure.get());
2302                 }
2303                 data->variants.append(
2304                     PutByIdVariant::replace(currentSet, currentOffset, InferredType::Top));
2305             }
2306
2307             return m_graph.addNode(
2308                 MultiPutByOffset,
2309                 origin.takeValidExit(canExit),
2310                 OpInfo(data),
2311                 Edge(base, KnownCellUse),
2312                 value->defaultEdge());
2313         }
2314
2315         case ClosureVarPLoc: {
2316             return m_graph.addNode(
2317                 PutClosureVar,
2318                 origin.takeValidExit(canExit),
2319                 OpInfo(location.info()),
2320                 Edge(base, KnownCellUse),
2321                 value->defaultEdge());
2322         }
2323
2324         case RegExpObjectLastIndexPLoc: {
2325             return m_graph.addNode(
2326                 SetRegExpObjectLastIndex,
2327                 origin.takeValidExit(canExit),
2328                 OpInfo(true),
2329                 Edge(base, KnownCellUse),
2330                 value->defaultEdge());
2331         }
2332
2333         default:
2334             DFG_CRASH(m_graph, base, "Bad location kind");
2335             break;
2336         }
2337
2338         RELEASE_ASSERT_NOT_REACHED();
2339     }
2340     
2341     void removeICStatusFilters()
2342     {
2343         for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
2344             for (Node* node : *block) {
2345                 switch (node->op()) {
2346                 case FilterCallLinkStatus:
2347                 case FilterGetByIdStatus:
2348                 case FilterPutByIdStatus:
2349                 case FilterInByIdStatus:
2350                     if (node->child1()->isPhantomAllocation())
2351                         node->removeWithoutChecks();
2352                     break;
2353                 default:
2354                     break;
2355                 }
2356             }
2357         }
2358     }
2359
2360     // This is a great way of asking value->isStillValid() without having to worry about getting
2361     // different answers. It turns out that this analysis works OK regardless of what this
2362     // returns but breaks badly if this changes its mind for any particular InferredValue. This
2363     // method protects us from that.
2364     bool isStillValid(InferredValue* value)
2365     {
2366         return m_validInferredValues.add(value, value->isStillValid()).iterator->value;
2367     }
2368
2369     SSACalculator m_pointerSSA;
2370     SSACalculator m_allocationSSA;
2371     NodeSet m_sinkCandidates;
2372     HashMap<PromotedHeapLocation, SSACalculator::Variable*> m_locationToVariable;
2373     HashMap<Node*, SSACalculator::Variable*> m_nodeToVariable;
2374     HashMap<PromotedHeapLocation, Node*> m_localMapping;
2375     HashMap<Node*, Node*> m_escapeeToMaterialization;
2376     InsertionSet m_insertionSet;
2377     CombinedLiveness m_combinedLiveness;
2378
2379     HashMap<InferredValue*, bool> m_validInferredValues;
2380
2381     HashMap<Node*, Node*> m_materializationToEscapee;
2382     HashMap<Node*, Vector<Node*>> m_materializationSiteToMaterializations;
2383     HashMap<Node*, Vector<PromotedHeapLocation>> m_materializationSiteToRecoveries;
2384     HashMap<Node*, Vector<std::pair<PromotedHeapLocation, Node*>>> m_materializationSiteToHints;
2385
2386     HashMap<Node*, Vector<PromotedHeapLocation>> m_locationsForAllocation;
2387
2388     BlockMap<LocalHeap> m_heapAtHead;
2389     BlockMap<LocalHeap> m_heapAtTail;
2390     LocalHeap m_heap;
2391
2392     Node* m_bottom = nullptr;
2393 };
2394
2395 }
2396
2397 bool performObjectAllocationSinking(Graph& graph)
2398 {
2399     return runPhase<ObjectAllocationSinkingPhase>(graph);
2400 }
2401
2402 } } // namespace JSC::DFG
2403
2404 #endif // ENABLE(DFG_JIT)