[ES6] Add DFG/FTL support for accessor put operations
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGClobberize.h
1 /*
2  * Copyright (C) 2013-2015 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 #ifndef DFGClobberize_h
27 #define DFGClobberize_h
28
29 #if ENABLE(DFG_JIT)
30
31 #include "DFGAbstractHeap.h"
32 #include "DFGEdgeUsesStructure.h"
33 #include "DFGGraph.h"
34 #include "DFGHeapLocation.h"
35 #include "DFGLazyNode.h"
36 #include "DFGPureValue.h"
37
38 namespace JSC { namespace DFG {
39
40 template<typename ReadFunctor, typename WriteFunctor, typename DefFunctor>
41 void clobberize(Graph& graph, Node* node, const ReadFunctor& read, const WriteFunctor& write, const DefFunctor& def)
42 {
43     // Some notes:
44     //
45     // - The canonical way of clobbering the world is to read world and write
46     //   heap. This is because World subsumes Heap and Stack, and Stack can be
47     //   read by anyone but only written to by explicit stack writing operations.
48     //   Of course, claiming to also write World is not wrong; it'll just
49     //   pessimise some important optimizations.
50     //
51     // - We cannot hoist, or sink, anything that has effects. This means that the
52     //   easiest way of indicating that something cannot be hoisted is to claim
53     //   that it side-effects some miscellaneous thing.
54     //
55     // - We cannot hoist forward-exiting nodes without some additional effort. I
56     //   believe that what it comes down to is that forward-exiting generally have
57     //   their NodeExitsForward cleared upon hoist, except for forward-exiting
58     //   nodes that take bogus state as their input. Those are substantially
59     //   harder. We disable it for now. In the future we could enable it by having
60     //   versions of those nodes that backward-exit instead, but I'm not convinced
61     //   of the soundness.
62     //
63     // - Some nodes lie, and claim that they do not read the JSCell_structureID,
64     //   JSCell_typeInfoFlags, etc. These are nodes that use the structure in a way
65     //   that does not depend on things that change under structure transitions.
66     //
67     // - It's implicitly understood that OSR exits read the world. This is why we
68     //   generally don't move or eliminate stores. Every node can exit, so the
69     //   read set does not reflect things that would be read if we exited.
70     //   Instead, the read set reflects what the node will have to read if it
71     //   *doesn't* exit.
72     //
73     // - Broadly, we don't say that we're reading something if that something is
74     //   immutable.
75     //
76     // - We try to make this work even prior to type inference, just so that we
77     //   can use it for IR dumps. No promises on whether the answers are sound
78     //   prior to type inference - though they probably could be if we did some
79     //   small hacking.
80     //
81     // - If you do read(Stack) or read(World), then make sure that readTop() in
82     //   PreciseLocalClobberize is correct.
83     
84     // While read() and write() are fairly self-explanatory - they track what sorts of things the
85     // node may read or write - the def() functor is more tricky. It tells you the heap locations
86     // (not just abstract heaps) that are defined by a node. A heap location comprises an abstract
87     // heap, some nodes, and a LocationKind. Briefly, a location defined by a node is a location
88     // whose value can be deduced from looking at the node itself. The locations returned must obey
89     // the following properties:
90     //
91     // - If someone wants to CSE a load from the heap, then a HeapLocation object should be
92     //   sufficient to find a single matching node.
93     //
94     // - The abstract heap is the only abstract heap that could be clobbered to invalidate any such
95     //   CSE attempt. I.e. if clobberize() reports that on every path between some node and a node
96     //   that defines a HeapLocation that it wanted, there were no writes to any abstract heap that
97     //   overlap the location's heap, then we have a sound match. Effectively, the semantics of
98     //   write() and def() are intertwined such that for them to be sound they must agree on what
99     //   is CSEable.
100     //
101     // read(), write(), and def() for heap locations is enough to do GCSE on effectful things. To
102     // keep things simple, this code will also def() pure things. def() must be overloaded to also
103     // accept PureValue. This way, a client of clobberize() can implement GCSE entirely using the
104     // information that clobberize() passes to write() and def(). Other clients of clobberize() can
105     // just ignore def() by using a NoOpClobberize functor.
106
107     if (edgesUseStructure(graph, node))
108         read(JSCell_structureID);
109     
110     switch (node->op()) {
111     case JSConstant:
112     case DoubleConstant:
113     case Int52Constant:
114         def(PureValue(node, node->constant()));
115         return;
116         
117     case Identity:
118     case Phantom:
119     case Check:
120     case ExtractOSREntryLocal:
121     case CheckStructureImmediate:
122         return;
123         
124     case BitAnd:
125     case BitOr:
126     case BitXor:
127     case BitLShift:
128     case BitRShift:
129     case BitURShift:
130     case ArithIMul:
131     case ArithAbs:
132     case ArithClz32:
133     case ArithMin:
134     case ArithMax:
135     case ArithPow:
136     case ArithSqrt:
137     case ArithFRound:
138     case ArithSin:
139     case ArithCos:
140     case ArithLog:
141     case GetScope:
142     case LoadArrowFunctionThis:
143     case SkipScope:
144     case StringCharCodeAt:
145     case StringFromCharCode:
146     case CompareStrictEq:
147     case IsUndefined:
148     case IsBoolean:
149     case IsNumber:
150     case IsString:
151     case IsObject:
152     case LogicalNot:
153     case CheckInBounds:
154     case DoubleRep:
155     case ValueRep:
156     case Int52Rep:
157     case BooleanToNumber:
158     case FiatInt52:
159     case MakeRope:
160     case StrCat:
161     case ValueToInt32:
162     case GetExecutable:
163     case BottomValue:
164     case TypeOf:
165         def(PureValue(node));
166         return;
167         
168     case HasGenericProperty:
169     case HasStructureProperty:
170     case GetEnumerableLength:
171     case GetPropertyEnumerator: {
172         read(Heap);
173         write(SideState);
174         return;
175     }
176
177     case GetDirectPname: {
178         // This reads and writes heap because it can end up calling a generic getByVal 
179         // if the Structure changed, which could in turn end up calling a getter.
180         read(World);
181         write(Heap);
182         return;
183     }
184
185     case ToIndexString:
186     case GetEnumeratorStructurePname:
187     case GetEnumeratorGenericPname: {
188         def(PureValue(node));
189         return;
190     }
191
192     case HasIndexedProperty: {
193         read(JSObject_butterfly);
194         ArrayMode mode = node->arrayMode();
195         switch (mode.type()) {
196         case Array::Int32: {
197             if (mode.isInBounds()) {
198                 read(Butterfly_publicLength);
199                 read(IndexedInt32Properties);
200                 def(HeapLocation(HasIndexedPropertyLoc, IndexedInt32Properties, node->child1(), node->child2()), LazyNode(node));
201                 return;
202             }
203             read(Heap);
204             return;
205         }
206             
207         case Array::Double: {
208             if (mode.isInBounds()) {
209                 read(Butterfly_publicLength);
210                 read(IndexedDoubleProperties);
211                 def(HeapLocation(HasIndexedPropertyLoc, IndexedDoubleProperties, node->child1(), node->child2()), LazyNode(node));
212                 return;
213             }
214             read(Heap);
215             return;
216         }
217             
218         case Array::Contiguous: {
219             if (mode.isInBounds()) {
220                 read(Butterfly_publicLength);
221                 read(IndexedContiguousProperties);
222                 def(HeapLocation(HasIndexedPropertyLoc, IndexedContiguousProperties, node->child1(), node->child2()), LazyNode(node));
223                 return;
224             }
225             read(Heap);
226             return;
227         }
228
229         case Array::ArrayStorage: {
230             if (mode.isInBounds()) {
231                 read(Butterfly_vectorLength);
232                 read(IndexedArrayStorageProperties);
233                 return;
234             }
235             read(Heap);
236             return;
237         }
238
239         default: {
240             read(World);
241             write(Heap);
242             return;
243         }
244         }
245         RELEASE_ASSERT_NOT_REACHED();
246         return;
247     }
248
249     case ArithAdd:
250     case ArithSub:
251     case ArithNegate:
252     case ArithMul:
253     case ArithDiv:
254     case ArithMod:
255     case DoubleAsInt32:
256     case UInt32ToNumber:
257         def(PureValue(node, node->arithMode()));
258         return;
259
260     case ArithRound:
261         def(PureValue(node, static_cast<uintptr_t>(node->arithRoundingMode())));
262         return;
263
264     case CheckCell:
265         def(PureValue(CheckCell, AdjacencyList(AdjacencyList::Fixed, node->child1()), node->cellOperand()));
266         return;
267
268     case CheckNotEmpty:
269         def(PureValue(CheckNotEmpty, AdjacencyList(AdjacencyList::Fixed, node->child1())));
270         return;
271
272     case CheckIdent:
273         def(PureValue(CheckIdent, AdjacencyList(AdjacencyList::Fixed, node->child1()), node->uidOperand()));
274         return;
275
276     case ConstantStoragePointer:
277         def(PureValue(node, node->storagePointer()));
278         return;
279          
280     case MovHint:
281     case ZombieHint:
282     case ExitOK:
283     case KillStack:
284     case Upsilon:
285     case Phi:
286     case PhantomLocal:
287     case SetArgument:
288     case Jump:
289     case Branch:
290     case Switch:
291     case Throw:
292     case ForceOSRExit:
293     case CheckBadCell:
294     case Return:
295     case Unreachable:
296     case CheckTierUpInLoop:
297     case CheckTierUpAtReturn:
298     case CheckTierUpAndOSREnter:
299     case CheckTierUpWithNestedTriggerAndOSREnter:
300     case LoopHint:
301     case Breakpoint:
302     case ProfileWillCall:
303     case ProfileDidCall:
304     case ProfileType:
305     case ProfileControlFlow:
306     case StoreBarrier:
307     case PutHint:
308         write(SideState);
309         return;
310         
311     case InvalidationPoint:
312         write(SideState);
313         def(HeapLocation(InvalidationPointLoc, Watchpoint_fire), LazyNode(node));
314         return;
315
316     case Flush:
317         read(AbstractHeap(Stack, node->local()));
318         write(SideState);
319         return;
320
321     case NotifyWrite:
322         write(Watchpoint_fire);
323         write(SideState);
324         return;
325
326     case CreateActivation: {
327         SymbolTable* table = node->castOperand<SymbolTable*>();
328         if (table->singletonScope()->isStillValid())
329             write(Watchpoint_fire);
330         read(HeapObjectCount);
331         write(HeapObjectCount);
332         return;
333     }
334         
335     case CreateDirectArguments:
336     case CreateScopedArguments:
337     case CreateClonedArguments:
338         read(Stack);
339         read(HeapObjectCount);
340         write(HeapObjectCount);
341         return;
342
343     case PhantomDirectArguments:
344     case PhantomClonedArguments:
345         // DFG backend requires that the locals that this reads are flushed. FTL backend can handle those
346         // locals being promoted.
347         if (!isFTL(graph.m_plan.mode))
348             read(Stack);
349         
350         // Even though it's phantom, it still has the property that one can't be replaced with another.
351         read(HeapObjectCount);
352         write(HeapObjectCount);
353         return;
354
355     case ToThis:
356     case CreateThis:
357         read(MiscFields);
358         read(HeapObjectCount);
359         write(HeapObjectCount);
360         return;
361
362     case VarInjectionWatchpoint:
363         read(MiscFields);
364         def(HeapLocation(VarInjectionWatchpointLoc, MiscFields), LazyNode(node));
365         return;
366
367     case IsObjectOrNull:
368         read(MiscFields);
369         def(HeapLocation(IsObjectOrNullLoc, MiscFields, node->child1()), LazyNode(node));
370         return;
371         
372     case IsFunction:
373         read(MiscFields);
374         def(HeapLocation(IsFunctionLoc, MiscFields, node->child1()), LazyNode(node));
375         return;
376         
377     case GetById:
378     case GetByIdFlush:
379     case PutById:
380     case PutByIdFlush:
381     case PutByIdDirect:
382     case PutGetterById:
383     case PutSetterById:
384     case PutGetterSetterById:
385     case PutGetterByVal:
386     case PutSetterByVal:
387     case ArrayPush:
388     case ArrayPop:
389     case Call:
390     case TailCallInlinedCaller:
391     case Construct:
392     case CallVarargs:
393     case CallForwardVarargs:
394     case TailCallVarargsInlinedCaller:
395     case TailCallForwardVarargsInlinedCaller:
396     case ConstructVarargs:
397     case ConstructForwardVarargs:
398     case ToPrimitive:
399     case In:
400     case ValueAdd:
401         read(World);
402         write(Heap);
403         return;
404
405     case TailCall:
406     case TailCallVarargs:
407     case TailCallForwardVarargs:
408         read(World);
409         write(SideState);
410         return;
411         
412     case GetGetter:
413         read(GetterSetter_getter);
414         def(HeapLocation(GetterLoc, GetterSetter_getter, node->child1()), LazyNode(node));
415         return;
416         
417     case GetSetter:
418         read(GetterSetter_setter);
419         def(HeapLocation(SetterLoc, GetterSetter_setter, node->child1()), LazyNode(node));
420         return;
421         
422     case GetCallee:
423         read(AbstractHeap(Stack, JSStack::Callee));
424         def(HeapLocation(StackLoc, AbstractHeap(Stack, JSStack::Callee)), LazyNode(node));
425         return;
426         
427     case GetArgumentCount:
428         read(AbstractHeap(Stack, JSStack::ArgumentCount));
429         def(HeapLocation(StackPayloadLoc, AbstractHeap(Stack, JSStack::ArgumentCount)), LazyNode(node));
430         return;
431         
432     case GetLocal:
433         read(AbstractHeap(Stack, node->local()));
434         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->local())), LazyNode(node));
435         return;
436         
437     case SetLocal:
438         write(AbstractHeap(Stack, node->local()));
439         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->local())), LazyNode(node->child1().node()));
440         return;
441         
442     case GetStack: {
443         AbstractHeap heap(Stack, node->stackAccessData()->local);
444         read(heap);
445         def(HeapLocation(StackLoc, heap), LazyNode(node));
446         return;
447     }
448         
449     case PutStack: {
450         AbstractHeap heap(Stack, node->stackAccessData()->local);
451         write(heap);
452         def(HeapLocation(StackLoc, heap), LazyNode(node->child1().node()));
453         return;
454     }
455         
456     case LoadVarargs: {
457         read(World);
458         write(Heap);
459         LoadVarargsData* data = node->loadVarargsData();
460         write(AbstractHeap(Stack, data->count.offset()));
461         for (unsigned i = data->limit; i--;)
462             write(AbstractHeap(Stack, data->start.offset() + static_cast<int>(i)));
463         return;
464     }
465         
466     case ForwardVarargs: {
467         // We could be way more precise here.
468         read(Stack);
469         
470         LoadVarargsData* data = node->loadVarargsData();
471         write(AbstractHeap(Stack, data->count.offset()));
472         for (unsigned i = data->limit; i--;)
473             write(AbstractHeap(Stack, data->start.offset() + static_cast<int>(i)));
474         return;
475     }
476         
477     case GetLocalUnlinked:
478         read(AbstractHeap(Stack, node->unlinkedLocal()));
479         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->unlinkedLocal())), LazyNode(node));
480         return;
481         
482     case GetByVal: {
483         ArrayMode mode = node->arrayMode();
484         switch (mode.type()) {
485         case Array::SelectUsingPredictions:
486         case Array::Unprofiled:
487         case Array::SelectUsingArguments:
488             // Assume the worst since we don't have profiling yet.
489             read(World);
490             write(Heap);
491             return;
492             
493         case Array::ForceExit:
494             write(SideState);
495             return;
496             
497         case Array::Generic:
498             read(World);
499             write(Heap);
500             return;
501             
502         case Array::String:
503             if (mode.isOutOfBounds()) {
504                 read(World);
505                 write(Heap);
506                 return;
507             }
508             // This appears to read nothing because it's only reading immutable data.
509             def(PureValue(node, mode.asWord()));
510             return;
511             
512         case Array::DirectArguments:
513             read(DirectArgumentsProperties);
514             def(HeapLocation(IndexedPropertyLoc, DirectArgumentsProperties, node->child1(), node->child2()), LazyNode(node));
515             return;
516             
517         case Array::ScopedArguments:
518             read(ScopeProperties);
519             def(HeapLocation(IndexedPropertyLoc, ScopeProperties, node->child1(), node->child2()), LazyNode(node));
520             return;
521             
522         case Array::Int32:
523             if (mode.isInBounds()) {
524                 read(Butterfly_publicLength);
525                 read(IndexedInt32Properties);
526                 def(HeapLocation(IndexedPropertyLoc, IndexedInt32Properties, node->child1(), node->child2()), LazyNode(node));
527                 return;
528             }
529             read(World);
530             write(Heap);
531             return;
532             
533         case Array::Double:
534             if (mode.isInBounds()) {
535                 read(Butterfly_publicLength);
536                 read(IndexedDoubleProperties);
537                 def(HeapLocation(IndexedPropertyLoc, IndexedDoubleProperties, node->child1(), node->child2()), LazyNode(node));
538                 return;
539             }
540             read(World);
541             write(Heap);
542             return;
543             
544         case Array::Contiguous:
545             if (mode.isInBounds()) {
546                 read(Butterfly_publicLength);
547                 read(IndexedContiguousProperties);
548                 def(HeapLocation(IndexedPropertyLoc, IndexedContiguousProperties, node->child1(), node->child2()), LazyNode(node));
549                 return;
550             }
551             read(World);
552             write(Heap);
553             return;
554
555         case Array::Undecided:
556             def(PureValue(node));
557             return;
558             
559         case Array::ArrayStorage:
560         case Array::SlowPutArrayStorage:
561             if (mode.isInBounds()) {
562                 read(Butterfly_vectorLength);
563                 read(IndexedArrayStorageProperties);
564                 return;
565             }
566             read(World);
567             write(Heap);
568             return;
569             
570         case Array::Int8Array:
571         case Array::Int16Array:
572         case Array::Int32Array:
573         case Array::Uint8Array:
574         case Array::Uint8ClampedArray:
575         case Array::Uint16Array:
576         case Array::Uint32Array:
577         case Array::Float32Array:
578         case Array::Float64Array:
579             read(TypedArrayProperties);
580             read(MiscFields);
581             def(HeapLocation(IndexedPropertyLoc, TypedArrayProperties, node->child1(), node->child2()), LazyNode(node));
582             return;
583         // We should not get an AnyTypedArray in a GetByVal as AnyTypedArray is only created from intrinsics, which
584         // are only added from Inline Caching a GetById.
585         case Array::AnyTypedArray:
586             DFG_CRASH(graph, node, "impossible array mode for get");
587             return;
588         }
589         RELEASE_ASSERT_NOT_REACHED();
590         return;
591     }
592         
593     case GetMyArgumentByVal: {
594         read(Stack);
595         // FIXME: It would be trivial to have a def here.
596         // https://bugs.webkit.org/show_bug.cgi?id=143077
597         return;
598     }
599
600     case PutByValDirect:
601     case PutByVal:
602     case PutByValAlias: {
603         ArrayMode mode = node->arrayMode();
604         Node* base = graph.varArgChild(node, 0).node();
605         Node* index = graph.varArgChild(node, 1).node();
606         Node* value = graph.varArgChild(node, 2).node();
607         switch (mode.modeForPut().type()) {
608         case Array::SelectUsingPredictions:
609         case Array::SelectUsingArguments:
610         case Array::Unprofiled:
611         case Array::Undecided:
612             // Assume the worst since we don't have profiling yet.
613             read(World);
614             write(Heap);
615             return;
616             
617         case Array::ForceExit:
618             write(SideState);
619             return;
620             
621         case Array::Generic:
622             read(World);
623             write(Heap);
624             return;
625             
626         case Array::Int32:
627             if (node->arrayMode().isOutOfBounds()) {
628                 read(World);
629                 write(Heap);
630                 return;
631             }
632             read(Butterfly_publicLength);
633             read(Butterfly_vectorLength);
634             read(IndexedInt32Properties);
635             write(IndexedInt32Properties);
636             if (node->arrayMode().mayStoreToHole())
637                 write(Butterfly_publicLength);
638             def(HeapLocation(IndexedPropertyLoc, IndexedInt32Properties, base, index), LazyNode(value));
639             return;
640             
641         case Array::Double:
642             if (node->arrayMode().isOutOfBounds()) {
643                 read(World);
644                 write(Heap);
645                 return;
646             }
647             read(Butterfly_publicLength);
648             read(Butterfly_vectorLength);
649             read(IndexedDoubleProperties);
650             write(IndexedDoubleProperties);
651             if (node->arrayMode().mayStoreToHole())
652                 write(Butterfly_publicLength);
653             def(HeapLocation(IndexedPropertyLoc, IndexedDoubleProperties, base, index), LazyNode(value));
654             return;
655             
656         case Array::Contiguous:
657             if (node->arrayMode().isOutOfBounds()) {
658                 read(World);
659                 write(Heap);
660                 return;
661             }
662             read(Butterfly_publicLength);
663             read(Butterfly_vectorLength);
664             read(IndexedContiguousProperties);
665             write(IndexedContiguousProperties);
666             if (node->arrayMode().mayStoreToHole())
667                 write(Butterfly_publicLength);
668             def(HeapLocation(IndexedPropertyLoc, IndexedContiguousProperties, base, index), LazyNode(value));
669             return;
670             
671         case Array::ArrayStorage:
672         case Array::SlowPutArrayStorage:
673             // Give up on life for now.
674             read(World);
675             write(Heap);
676             return;
677
678         case Array::Int8Array:
679         case Array::Int16Array:
680         case Array::Int32Array:
681         case Array::Uint8Array:
682         case Array::Uint8ClampedArray:
683         case Array::Uint16Array:
684         case Array::Uint32Array:
685         case Array::Float32Array:
686         case Array::Float64Array:
687             read(MiscFields);
688             write(TypedArrayProperties);
689             // FIXME: We can't def() anything here because these operations truncate their inputs.
690             // https://bugs.webkit.org/show_bug.cgi?id=134737
691             return;
692         case Array::AnyTypedArray:
693         case Array::String:
694         case Array::DirectArguments:
695         case Array::ScopedArguments:
696             DFG_CRASH(graph, node, "impossible array mode for put");
697             return;
698         }
699         RELEASE_ASSERT_NOT_REACHED();
700         return;
701     }
702         
703     case CheckStructure:
704         read(JSCell_structureID);
705         return;
706
707     case CheckArray:
708         read(JSCell_indexingType);
709         read(JSCell_typeInfoType);
710         read(JSCell_structureID);
711         return;
712
713     case CheckHasInstance:
714         read(JSCell_typeInfoFlags);
715         def(HeapLocation(CheckHasInstanceLoc, JSCell_typeInfoFlags, node->child1()), LazyNode(node));
716         return;
717
718     case InstanceOf:
719         read(JSCell_structureID);
720         def(HeapLocation(InstanceOfLoc, JSCell_structureID, node->child1(), node->child2()), LazyNode(node));
721         return;
722
723     case PutStructure:
724         write(JSCell_structureID);
725         write(JSCell_typeInfoType);
726         write(JSCell_typeInfoFlags);
727         write(JSCell_indexingType);
728         return;
729         
730     case AllocatePropertyStorage:
731         write(JSObject_butterfly);
732         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
733         return;
734         
735     case ReallocatePropertyStorage:
736         read(JSObject_butterfly);
737         write(JSObject_butterfly);
738         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
739         return;
740         
741     case GetButterfly:
742         read(JSObject_butterfly);
743         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
744         return;
745
746     case GetButterflyReadOnly:
747         // This rule is separate to prevent CSE of GetButterfly with GetButterflyReadOnly. But in reality,
748         // this works because we don't introduce GetButterflyReadOnly until the bitter end of compilation.
749         read(JSObject_butterfly);
750         def(HeapLocation(ButterflyReadOnlyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
751         return;
752         
753     case Arrayify:
754     case ArrayifyToStructure:
755         read(JSCell_structureID);
756         read(JSCell_indexingType);
757         read(JSObject_butterfly);
758         write(JSCell_structureID);
759         write(JSCell_indexingType);
760         write(JSObject_butterfly);
761         write(Watchpoint_fire);
762         return;
763         
764     case GetIndexedPropertyStorage:
765         if (node->arrayMode().type() == Array::String) {
766             def(PureValue(node, node->arrayMode().asWord()));
767             return;
768         }
769         read(MiscFields);
770         def(HeapLocation(IndexedPropertyStorageLoc, MiscFields, node->child1()), LazyNode(node));
771         return;
772         
773     case GetTypedArrayByteOffset:
774         read(MiscFields);
775         def(HeapLocation(TypedArrayByteOffsetLoc, MiscFields, node->child1()), LazyNode(node));
776         return;
777         
778     case GetByOffset:
779     case GetGetterSetterByOffset: {
780         unsigned identifierNumber = node->storageAccessData().identifierNumber;
781         AbstractHeap heap(NamedProperties, identifierNumber);
782         read(heap);
783         def(HeapLocation(NamedPropertyLoc, heap, node->child2()), LazyNode(node));
784         return;
785     }
786         
787     case MultiGetByOffset: {
788         read(JSCell_structureID);
789         read(JSObject_butterfly);
790         AbstractHeap heap(NamedProperties, node->multiGetByOffsetData().identifierNumber);
791         read(heap);
792         def(HeapLocation(NamedPropertyLoc, heap, node->child1()), LazyNode(node));
793         return;
794     }
795         
796     case MultiPutByOffset: {
797         read(JSCell_structureID);
798         read(JSObject_butterfly);
799         AbstractHeap heap(NamedProperties, node->multiPutByOffsetData().identifierNumber);
800         write(heap);
801         if (node->multiPutByOffsetData().writesStructures())
802             write(JSCell_structureID);
803         if (node->multiPutByOffsetData().reallocatesStorage())
804             write(JSObject_butterfly);
805         def(HeapLocation(NamedPropertyLoc, heap, node->child1()), LazyNode(node->child2().node()));
806         return;
807     }
808         
809     case PutByOffset: {
810         unsigned identifierNumber = node->storageAccessData().identifierNumber;
811         AbstractHeap heap(NamedProperties, identifierNumber);
812         write(heap);
813         def(HeapLocation(NamedPropertyLoc, heap, node->child2()), LazyNode(node->child3().node()));
814         return;
815     }
816         
817     case GetArrayLength: {
818         ArrayMode mode = node->arrayMode();
819         switch (mode.type()) {
820         case Array::Int32:
821         case Array::Double:
822         case Array::Contiguous:
823         case Array::ArrayStorage:
824         case Array::SlowPutArrayStorage:
825             read(Butterfly_publicLength);
826             def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node->child1()), LazyNode(node));
827             return;
828             
829         case Array::String:
830             def(PureValue(node, mode.asWord()));
831             return;
832
833         case Array::DirectArguments:
834         case Array::ScopedArguments:
835             read(MiscFields);
836             def(HeapLocation(ArrayLengthLoc, MiscFields, node->child1()), LazyNode(node));
837             return;
838
839         default:
840             ASSERT(mode.isSomeTypedArrayView());
841             read(MiscFields);
842             def(HeapLocation(ArrayLengthLoc, MiscFields, node->child1()), LazyNode(node));
843             return;
844         }
845     }
846         
847     case GetClosureVar:
848         read(AbstractHeap(ScopeProperties, node->scopeOffset().offset()));
849         def(HeapLocation(ClosureVariableLoc, AbstractHeap(ScopeProperties, node->scopeOffset().offset()), node->child1()), LazyNode(node));
850         return;
851         
852     case PutClosureVar:
853         write(AbstractHeap(ScopeProperties, node->scopeOffset().offset()));
854         def(HeapLocation(ClosureVariableLoc, AbstractHeap(ScopeProperties, node->scopeOffset().offset()), node->child1()), LazyNode(node->child2().node()));
855         return;
856         
857     case GetFromArguments: {
858         AbstractHeap heap(DirectArgumentsProperties, node->capturedArgumentsOffset().offset());
859         read(heap);
860         def(HeapLocation(DirectArgumentsLoc, heap, node->child1()), LazyNode(node));
861         return;
862     }
863         
864     case PutToArguments: {
865         AbstractHeap heap(DirectArgumentsProperties, node->capturedArgumentsOffset().offset());
866         write(heap);
867         def(HeapLocation(DirectArgumentsLoc, heap, node->child1()), LazyNode(node->child2().node()));
868         return;
869     }
870         
871     case GetGlobalVar:
872     case GetGlobalLexicalVariable:
873         read(AbstractHeap(Absolute, node->variablePointer()));
874         def(HeapLocation(GlobalVariableLoc, AbstractHeap(Absolute, node->variablePointer())), LazyNode(node));
875         return;
876         
877     case PutGlobalVariable:
878         write(AbstractHeap(Absolute, node->variablePointer()));
879         def(HeapLocation(GlobalVariableLoc, AbstractHeap(Absolute, node->variablePointer())), LazyNode(node->child2().node()));
880         return;
881
882     case NewArrayWithSize:
883     case NewTypedArray:
884         read(HeapObjectCount);
885         write(HeapObjectCount);
886         return;
887
888     case NewArray: {
889         read(HeapObjectCount);
890         write(HeapObjectCount);
891
892         unsigned numElements = node->numChildren();
893
894         def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node),
895             LazyNode(graph.freeze(jsNumber(numElements))));
896
897         if (!numElements)
898             return;
899
900         AbstractHeap heap;
901         switch (node->indexingType()) {
902         case ALL_DOUBLE_INDEXING_TYPES:
903             heap = IndexedDoubleProperties;
904             break;
905
906         case ALL_INT32_INDEXING_TYPES:
907             heap = IndexedInt32Properties;
908             break;
909
910         case ALL_CONTIGUOUS_INDEXING_TYPES:
911             heap = IndexedContiguousProperties;
912             break;
913
914         default:
915             return;
916         }
917
918         if (numElements < graph.m_uint32ValuesInUse.size()) {
919             for (unsigned operandIdx = 0; operandIdx < numElements; ++operandIdx) {
920                 Edge use = graph.m_varArgChildren[node->firstChild() + operandIdx];
921                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(operandIdx)))),
922                     LazyNode(use.node()));
923             }
924         } else {
925             for (uint32_t operandIdx : graph.m_uint32ValuesInUse) {
926                 if (operandIdx >= numElements)
927                     continue;
928                 Edge use = graph.m_varArgChildren[node->firstChild() + operandIdx];
929                 // operandIdx comes from graph.m_uint32ValuesInUse and thus is guaranteed to be already frozen
930                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(operandIdx)))),
931                     LazyNode(use.node()));
932             }
933         }
934         return;
935     }
936
937     case NewArrayBuffer: {
938         read(HeapObjectCount);
939         write(HeapObjectCount);
940
941         unsigned numElements = node->numConstants();
942         def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node),
943             LazyNode(graph.freeze(jsNumber(numElements))));
944
945         AbstractHeap heap;
946         NodeType op = JSConstant;
947         switch (node->indexingType()) {
948         case ALL_DOUBLE_INDEXING_TYPES:
949             heap = IndexedDoubleProperties;
950             op = DoubleConstant;
951             break;
952
953         case ALL_INT32_INDEXING_TYPES:
954             heap = IndexedInt32Properties;
955             break;
956
957         case ALL_CONTIGUOUS_INDEXING_TYPES:
958             heap = IndexedContiguousProperties;
959             break;
960
961         default:
962             return;
963         }
964
965         JSValue* data = graph.m_codeBlock->constantBuffer(node->startConstant());
966         if (numElements < graph.m_uint32ValuesInUse.size()) {
967             for (unsigned index = 0; index < numElements; ++index) {
968                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(index)))),
969                     LazyNode(graph.freeze(data[index]), op));
970             }
971         } else {
972             Vector<uint32_t> possibleIndices;
973             for (uint32_t index : graph.m_uint32ValuesInUse) {
974                 if (index >= numElements)
975                     continue;
976                 possibleIndices.append(index);
977             }
978             for (uint32_t index : possibleIndices) {
979                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(index)))),
980                     LazyNode(graph.freeze(data[index]), op));
981             }
982         }
983         return;
984     }
985
986     case NewObject:
987     case NewRegexp:
988     case NewStringObject:
989     case PhantomNewObject:
990     case MaterializeNewObject:
991     case PhantomNewFunction:
992     case PhantomCreateActivation:
993     case MaterializeCreateActivation:
994         read(HeapObjectCount);
995         write(HeapObjectCount);
996         return;
997     
998     case NewArrowFunction:
999     case NewFunction:
1000         if (node->castOperand<FunctionExecutable*>()->singletonFunction()->isStillValid())
1001             write(Watchpoint_fire);
1002         read(HeapObjectCount);
1003         write(HeapObjectCount);
1004         return;
1005
1006     case RegExpExec:
1007     case RegExpTest:
1008         read(RegExpState);
1009         write(RegExpState);
1010         return;
1011
1012     case StringCharAt:
1013         if (node->arrayMode().isOutOfBounds()) {
1014             read(World);
1015             write(Heap);
1016             return;
1017         }
1018         def(PureValue(node));
1019         return;
1020         
1021     case CompareEq:
1022     case CompareLess:
1023     case CompareLessEq:
1024     case CompareGreater:
1025     case CompareGreaterEq:
1026         if (!node->isBinaryUseKind(UntypedUse)) {
1027             def(PureValue(node));
1028             return;
1029         }
1030         read(World);
1031         write(Heap);
1032         return;
1033         
1034     case ToString:
1035     case CallStringConstructor:
1036         switch (node->child1().useKind()) {
1037         case StringObjectUse:
1038         case StringOrStringObjectUse:
1039             // These don't def a pure value, unfortunately. I'll avoid load-eliminating these for
1040             // now.
1041             return;
1042             
1043         case CellUse:
1044         case UntypedUse:
1045             read(World);
1046             write(Heap);
1047             return;
1048             
1049         default:
1050             RELEASE_ASSERT_NOT_REACHED();
1051             return;
1052         }
1053         
1054     case ThrowReferenceError:
1055         write(SideState);
1056         return;
1057         
1058     case CountExecution:
1059     case CheckWatchdogTimer:
1060         read(InternalState);
1061         write(InternalState);
1062         return;
1063         
1064     case LastNodeType:
1065         RELEASE_ASSERT_NOT_REACHED();
1066         return;
1067     }
1068     
1069     DFG_CRASH(graph, node, toCString("Unrecognized node type: ", Graph::opName(node->op())).data());
1070 }
1071
1072 class NoOpClobberize {
1073 public:
1074     NoOpClobberize() { }
1075     template<typename... T>
1076     void operator()(T...) const { }
1077 };
1078
1079 class CheckClobberize {
1080 public:
1081     CheckClobberize()
1082         : m_result(false)
1083     {
1084     }
1085     
1086     template<typename... T>
1087     void operator()(T...) const { m_result = true; }
1088     
1089     bool result() const { return m_result; }
1090     
1091 private:
1092     mutable bool m_result;
1093 };
1094
1095 bool doesWrites(Graph&, Node*);
1096
1097 class AbstractHeapOverlaps {
1098 public:
1099     AbstractHeapOverlaps(AbstractHeap heap)
1100         : m_heap(heap)
1101         , m_result(false)
1102     {
1103     }
1104     
1105     void operator()(AbstractHeap otherHeap) const
1106     {
1107         if (m_result)
1108             return;
1109         m_result = m_heap.overlaps(otherHeap);
1110     }
1111     
1112     bool result() const { return m_result; }
1113
1114 private:
1115     AbstractHeap m_heap;
1116     mutable bool m_result;
1117 };
1118
1119 bool accessesOverlap(Graph&, Node*, AbstractHeap);
1120 bool writesOverlap(Graph&, Node*, AbstractHeap);
1121
1122 bool clobbersHeap(Graph&, Node*);
1123
1124 // We would have used bind() for these, but because of the overlaoding that we are doing,
1125 // it's quite a bit of clearer to just write this out the traditional way.
1126
1127 template<typename T>
1128 class ReadMethodClobberize {
1129 public:
1130     ReadMethodClobberize(T& value)
1131         : m_value(value)
1132     {
1133     }
1134     
1135     void operator()(AbstractHeap heap) const
1136     {
1137         m_value.read(heap);
1138     }
1139 private:
1140     T& m_value;
1141 };
1142
1143 template<typename T>
1144 class WriteMethodClobberize {
1145 public:
1146     WriteMethodClobberize(T& value)
1147         : m_value(value)
1148     {
1149     }
1150     
1151     void operator()(AbstractHeap heap) const
1152     {
1153         m_value.write(heap);
1154     }
1155 private:
1156     T& m_value;
1157 };
1158
1159 template<typename T>
1160 class DefMethodClobberize {
1161 public:
1162     DefMethodClobberize(T& value)
1163         : m_value(value)
1164     {
1165     }
1166     
1167     void operator()(PureValue value) const
1168     {
1169         m_value.def(value);
1170     }
1171     
1172     void operator()(HeapLocation location, LazyNode node) const
1173     {
1174         m_value.def(location, node);
1175     }
1176
1177 private:
1178     T& m_value;
1179 };
1180
1181 template<typename Adaptor>
1182 void clobberize(Graph& graph, Node* node, Adaptor& adaptor)
1183 {
1184     ReadMethodClobberize<Adaptor> read(adaptor);
1185     WriteMethodClobberize<Adaptor> write(adaptor);
1186     DefMethodClobberize<Adaptor> def(adaptor);
1187     clobberize(graph, node, read, write, def);
1188 }
1189
1190 } } // namespace JSC::DFG
1191
1192 #endif // ENABLE(DFG_JIT)
1193
1194 #endif // DFGClobberize_h
1195