Add Intrinsic Getters and use them to fix performance on the getters of TypedArray...
[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 ArrayPush:
383     case ArrayPop:
384     case Call:
385     case TailCallInlinedCaller:
386     case Construct:
387     case CallVarargs:
388     case CallForwardVarargs:
389     case TailCallVarargsInlinedCaller:
390     case TailCallForwardVarargsInlinedCaller:
391     case ConstructVarargs:
392     case ConstructForwardVarargs:
393     case ToPrimitive:
394     case In:
395     case ValueAdd:
396         read(World);
397         write(Heap);
398         return;
399
400     case TailCall:
401     case TailCallVarargs:
402     case TailCallForwardVarargs:
403         read(World);
404         write(SideState);
405         return;
406         
407     case GetGetter:
408         read(GetterSetter_getter);
409         def(HeapLocation(GetterLoc, GetterSetter_getter, node->child1()), LazyNode(node));
410         return;
411         
412     case GetSetter:
413         read(GetterSetter_setter);
414         def(HeapLocation(SetterLoc, GetterSetter_setter, node->child1()), LazyNode(node));
415         return;
416         
417     case GetCallee:
418         read(AbstractHeap(Stack, JSStack::Callee));
419         def(HeapLocation(StackLoc, AbstractHeap(Stack, JSStack::Callee)), LazyNode(node));
420         return;
421         
422     case GetArgumentCount:
423         read(AbstractHeap(Stack, JSStack::ArgumentCount));
424         def(HeapLocation(StackPayloadLoc, AbstractHeap(Stack, JSStack::ArgumentCount)), LazyNode(node));
425         return;
426         
427     case GetLocal:
428         read(AbstractHeap(Stack, node->local()));
429         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->local())), LazyNode(node));
430         return;
431         
432     case SetLocal:
433         write(AbstractHeap(Stack, node->local()));
434         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->local())), LazyNode(node->child1().node()));
435         return;
436         
437     case GetStack: {
438         AbstractHeap heap(Stack, node->stackAccessData()->local);
439         read(heap);
440         def(HeapLocation(StackLoc, heap), LazyNode(node));
441         return;
442     }
443         
444     case PutStack: {
445         AbstractHeap heap(Stack, node->stackAccessData()->local);
446         write(heap);
447         def(HeapLocation(StackLoc, heap), LazyNode(node->child1().node()));
448         return;
449     }
450         
451     case LoadVarargs: {
452         read(World);
453         write(Heap);
454         LoadVarargsData* data = node->loadVarargsData();
455         write(AbstractHeap(Stack, data->count.offset()));
456         for (unsigned i = data->limit; i--;)
457             write(AbstractHeap(Stack, data->start.offset() + static_cast<int>(i)));
458         return;
459     }
460         
461     case ForwardVarargs: {
462         // We could be way more precise here.
463         read(Stack);
464         
465         LoadVarargsData* data = node->loadVarargsData();
466         write(AbstractHeap(Stack, data->count.offset()));
467         for (unsigned i = data->limit; i--;)
468             write(AbstractHeap(Stack, data->start.offset() + static_cast<int>(i)));
469         return;
470     }
471         
472     case GetLocalUnlinked:
473         read(AbstractHeap(Stack, node->unlinkedLocal()));
474         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->unlinkedLocal())), LazyNode(node));
475         return;
476         
477     case GetByVal: {
478         ArrayMode mode = node->arrayMode();
479         switch (mode.type()) {
480         case Array::SelectUsingPredictions:
481         case Array::Unprofiled:
482         case Array::SelectUsingArguments:
483             // Assume the worst since we don't have profiling yet.
484             read(World);
485             write(Heap);
486             return;
487             
488         case Array::ForceExit:
489             write(SideState);
490             return;
491             
492         case Array::Generic:
493             read(World);
494             write(Heap);
495             return;
496             
497         case Array::String:
498             if (mode.isOutOfBounds()) {
499                 read(World);
500                 write(Heap);
501                 return;
502             }
503             // This appears to read nothing because it's only reading immutable data.
504             def(PureValue(node, mode.asWord()));
505             return;
506             
507         case Array::DirectArguments:
508             read(DirectArgumentsProperties);
509             def(HeapLocation(IndexedPropertyLoc, DirectArgumentsProperties, node->child1(), node->child2()), LazyNode(node));
510             return;
511             
512         case Array::ScopedArguments:
513             read(ScopeProperties);
514             def(HeapLocation(IndexedPropertyLoc, ScopeProperties, node->child1(), node->child2()), LazyNode(node));
515             return;
516             
517         case Array::Int32:
518             if (mode.isInBounds()) {
519                 read(Butterfly_publicLength);
520                 read(IndexedInt32Properties);
521                 def(HeapLocation(IndexedPropertyLoc, IndexedInt32Properties, node->child1(), node->child2()), LazyNode(node));
522                 return;
523             }
524             read(World);
525             write(Heap);
526             return;
527             
528         case Array::Double:
529             if (mode.isInBounds()) {
530                 read(Butterfly_publicLength);
531                 read(IndexedDoubleProperties);
532                 def(HeapLocation(IndexedPropertyLoc, IndexedDoubleProperties, node->child1(), node->child2()), LazyNode(node));
533                 return;
534             }
535             read(World);
536             write(Heap);
537             return;
538             
539         case Array::Contiguous:
540             if (mode.isInBounds()) {
541                 read(Butterfly_publicLength);
542                 read(IndexedContiguousProperties);
543                 def(HeapLocation(IndexedPropertyLoc, IndexedContiguousProperties, node->child1(), node->child2()), LazyNode(node));
544                 return;
545             }
546             read(World);
547             write(Heap);
548             return;
549
550         case Array::Undecided:
551             def(PureValue(node));
552             return;
553             
554         case Array::ArrayStorage:
555         case Array::SlowPutArrayStorage:
556             if (mode.isInBounds()) {
557                 read(Butterfly_vectorLength);
558                 read(IndexedArrayStorageProperties);
559                 return;
560             }
561             read(World);
562             write(Heap);
563             return;
564             
565         case Array::Int8Array:
566         case Array::Int16Array:
567         case Array::Int32Array:
568         case Array::Uint8Array:
569         case Array::Uint8ClampedArray:
570         case Array::Uint16Array:
571         case Array::Uint32Array:
572         case Array::Float32Array:
573         case Array::Float64Array:
574             read(TypedArrayProperties);
575             read(MiscFields);
576             def(HeapLocation(IndexedPropertyLoc, TypedArrayProperties, node->child1(), node->child2()), LazyNode(node));
577             return;
578         // We should not get an AnyTypedArray in a GetByVal as AnyTypedArray is only created from intrinsics, which
579         // are only added from Inline Caching a GetById.
580         case Array::AnyTypedArray:
581             DFG_CRASH(graph, node, "impossible array mode for get");
582             return;
583         }
584         RELEASE_ASSERT_NOT_REACHED();
585         return;
586     }
587         
588     case GetMyArgumentByVal: {
589         read(Stack);
590         // FIXME: It would be trivial to have a def here.
591         // https://bugs.webkit.org/show_bug.cgi?id=143077
592         return;
593     }
594
595     case PutByValDirect:
596     case PutByVal:
597     case PutByValAlias: {
598         ArrayMode mode = node->arrayMode();
599         Node* base = graph.varArgChild(node, 0).node();
600         Node* index = graph.varArgChild(node, 1).node();
601         Node* value = graph.varArgChild(node, 2).node();
602         switch (mode.modeForPut().type()) {
603         case Array::SelectUsingPredictions:
604         case Array::SelectUsingArguments:
605         case Array::Unprofiled:
606         case Array::Undecided:
607             // Assume the worst since we don't have profiling yet.
608             read(World);
609             write(Heap);
610             return;
611             
612         case Array::ForceExit:
613             write(SideState);
614             return;
615             
616         case Array::Generic:
617             read(World);
618             write(Heap);
619             return;
620             
621         case Array::Int32:
622             if (node->arrayMode().isOutOfBounds()) {
623                 read(World);
624                 write(Heap);
625                 return;
626             }
627             read(Butterfly_publicLength);
628             read(Butterfly_vectorLength);
629             read(IndexedInt32Properties);
630             write(IndexedInt32Properties);
631             if (node->arrayMode().mayStoreToHole())
632                 write(Butterfly_publicLength);
633             def(HeapLocation(IndexedPropertyLoc, IndexedInt32Properties, base, index), LazyNode(value));
634             return;
635             
636         case Array::Double:
637             if (node->arrayMode().isOutOfBounds()) {
638                 read(World);
639                 write(Heap);
640                 return;
641             }
642             read(Butterfly_publicLength);
643             read(Butterfly_vectorLength);
644             read(IndexedDoubleProperties);
645             write(IndexedDoubleProperties);
646             if (node->arrayMode().mayStoreToHole())
647                 write(Butterfly_publicLength);
648             def(HeapLocation(IndexedPropertyLoc, IndexedDoubleProperties, base, index), LazyNode(value));
649             return;
650             
651         case Array::Contiguous:
652             if (node->arrayMode().isOutOfBounds()) {
653                 read(World);
654                 write(Heap);
655                 return;
656             }
657             read(Butterfly_publicLength);
658             read(Butterfly_vectorLength);
659             read(IndexedContiguousProperties);
660             write(IndexedContiguousProperties);
661             if (node->arrayMode().mayStoreToHole())
662                 write(Butterfly_publicLength);
663             def(HeapLocation(IndexedPropertyLoc, IndexedContiguousProperties, base, index), LazyNode(value));
664             return;
665             
666         case Array::ArrayStorage:
667         case Array::SlowPutArrayStorage:
668             // Give up on life for now.
669             read(World);
670             write(Heap);
671             return;
672
673         case Array::Int8Array:
674         case Array::Int16Array:
675         case Array::Int32Array:
676         case Array::Uint8Array:
677         case Array::Uint8ClampedArray:
678         case Array::Uint16Array:
679         case Array::Uint32Array:
680         case Array::Float32Array:
681         case Array::Float64Array:
682             read(MiscFields);
683             write(TypedArrayProperties);
684             // FIXME: We can't def() anything here because these operations truncate their inputs.
685             // https://bugs.webkit.org/show_bug.cgi?id=134737
686             return;
687         case Array::AnyTypedArray:
688         case Array::String:
689         case Array::DirectArguments:
690         case Array::ScopedArguments:
691             DFG_CRASH(graph, node, "impossible array mode for put");
692             return;
693         }
694         RELEASE_ASSERT_NOT_REACHED();
695         return;
696     }
697         
698     case CheckStructure:
699         read(JSCell_structureID);
700         return;
701
702     case CheckArray:
703         read(JSCell_indexingType);
704         read(JSCell_typeInfoType);
705         read(JSCell_structureID);
706         return;
707
708     case CheckHasInstance:
709         read(JSCell_typeInfoFlags);
710         def(HeapLocation(CheckHasInstanceLoc, JSCell_typeInfoFlags, node->child1()), LazyNode(node));
711         return;
712
713     case InstanceOf:
714         read(JSCell_structureID);
715         def(HeapLocation(InstanceOfLoc, JSCell_structureID, node->child1(), node->child2()), LazyNode(node));
716         return;
717
718     case PutStructure:
719         write(JSCell_structureID);
720         write(JSCell_typeInfoType);
721         write(JSCell_typeInfoFlags);
722         write(JSCell_indexingType);
723         return;
724         
725     case AllocatePropertyStorage:
726         write(JSObject_butterfly);
727         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
728         return;
729         
730     case ReallocatePropertyStorage:
731         read(JSObject_butterfly);
732         write(JSObject_butterfly);
733         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
734         return;
735         
736     case GetButterfly:
737         read(JSObject_butterfly);
738         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
739         return;
740
741     case GetButterflyReadOnly:
742         // This rule is separate to prevent CSE of GetButterfly with GetButterflyReadOnly. But in reality,
743         // this works because we don't introduce GetButterflyReadOnly until the bitter end of compilation.
744         read(JSObject_butterfly);
745         def(HeapLocation(ButterflyReadOnlyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
746         return;
747         
748     case Arrayify:
749     case ArrayifyToStructure:
750         read(JSCell_structureID);
751         read(JSCell_indexingType);
752         read(JSObject_butterfly);
753         write(JSCell_structureID);
754         write(JSCell_indexingType);
755         write(JSObject_butterfly);
756         write(Watchpoint_fire);
757         return;
758         
759     case GetIndexedPropertyStorage:
760         if (node->arrayMode().type() == Array::String) {
761             def(PureValue(node, node->arrayMode().asWord()));
762             return;
763         }
764         read(MiscFields);
765         def(HeapLocation(IndexedPropertyStorageLoc, MiscFields, node->child1()), LazyNode(node));
766         return;
767         
768     case GetTypedArrayByteOffset:
769         read(MiscFields);
770         def(HeapLocation(TypedArrayByteOffsetLoc, MiscFields, node->child1()), LazyNode(node));
771         return;
772         
773     case GetByOffset:
774     case GetGetterSetterByOffset: {
775         unsigned identifierNumber = node->storageAccessData().identifierNumber;
776         AbstractHeap heap(NamedProperties, identifierNumber);
777         read(heap);
778         def(HeapLocation(NamedPropertyLoc, heap, node->child2()), LazyNode(node));
779         return;
780     }
781         
782     case MultiGetByOffset: {
783         read(JSCell_structureID);
784         read(JSObject_butterfly);
785         AbstractHeap heap(NamedProperties, node->multiGetByOffsetData().identifierNumber);
786         read(heap);
787         def(HeapLocation(NamedPropertyLoc, heap, node->child1()), LazyNode(node));
788         return;
789     }
790         
791     case MultiPutByOffset: {
792         read(JSCell_structureID);
793         read(JSObject_butterfly);
794         AbstractHeap heap(NamedProperties, node->multiPutByOffsetData().identifierNumber);
795         write(heap);
796         if (node->multiPutByOffsetData().writesStructures())
797             write(JSCell_structureID);
798         if (node->multiPutByOffsetData().reallocatesStorage())
799             write(JSObject_butterfly);
800         def(HeapLocation(NamedPropertyLoc, heap, node->child1()), LazyNode(node->child2().node()));
801         return;
802     }
803         
804     case PutByOffset: {
805         unsigned identifierNumber = node->storageAccessData().identifierNumber;
806         AbstractHeap heap(NamedProperties, identifierNumber);
807         write(heap);
808         def(HeapLocation(NamedPropertyLoc, heap, node->child2()), LazyNode(node->child3().node()));
809         return;
810     }
811         
812     case GetArrayLength: {
813         ArrayMode mode = node->arrayMode();
814         switch (mode.type()) {
815         case Array::Int32:
816         case Array::Double:
817         case Array::Contiguous:
818         case Array::ArrayStorage:
819         case Array::SlowPutArrayStorage:
820             read(Butterfly_publicLength);
821             def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node->child1()), LazyNode(node));
822             return;
823             
824         case Array::String:
825             def(PureValue(node, mode.asWord()));
826             return;
827
828         case Array::DirectArguments:
829         case Array::ScopedArguments:
830             read(MiscFields);
831             def(HeapLocation(ArrayLengthLoc, MiscFields, node->child1()), LazyNode(node));
832             return;
833
834         default:
835             ASSERT(mode.isSomeTypedArrayView());
836             read(MiscFields);
837             def(HeapLocation(ArrayLengthLoc, MiscFields, node->child1()), LazyNode(node));
838             return;
839         }
840     }
841         
842     case GetClosureVar:
843         read(AbstractHeap(ScopeProperties, node->scopeOffset().offset()));
844         def(HeapLocation(ClosureVariableLoc, AbstractHeap(ScopeProperties, node->scopeOffset().offset()), node->child1()), LazyNode(node));
845         return;
846         
847     case PutClosureVar:
848         write(AbstractHeap(ScopeProperties, node->scopeOffset().offset()));
849         def(HeapLocation(ClosureVariableLoc, AbstractHeap(ScopeProperties, node->scopeOffset().offset()), node->child1()), LazyNode(node->child2().node()));
850         return;
851         
852     case GetFromArguments: {
853         AbstractHeap heap(DirectArgumentsProperties, node->capturedArgumentsOffset().offset());
854         read(heap);
855         def(HeapLocation(DirectArgumentsLoc, heap, node->child1()), LazyNode(node));
856         return;
857     }
858         
859     case PutToArguments: {
860         AbstractHeap heap(DirectArgumentsProperties, node->capturedArgumentsOffset().offset());
861         write(heap);
862         def(HeapLocation(DirectArgumentsLoc, heap, node->child1()), LazyNode(node->child2().node()));
863         return;
864     }
865         
866     case GetGlobalVar:
867     case GetGlobalLexicalVariable:
868         read(AbstractHeap(Absolute, node->variablePointer()));
869         def(HeapLocation(GlobalVariableLoc, AbstractHeap(Absolute, node->variablePointer())), LazyNode(node));
870         return;
871         
872     case PutGlobalVariable:
873         write(AbstractHeap(Absolute, node->variablePointer()));
874         def(HeapLocation(GlobalVariableLoc, AbstractHeap(Absolute, node->variablePointer())), LazyNode(node->child2().node()));
875         return;
876
877     case NewArrayWithSize:
878     case NewTypedArray:
879         read(HeapObjectCount);
880         write(HeapObjectCount);
881         return;
882
883     case NewArray: {
884         read(HeapObjectCount);
885         write(HeapObjectCount);
886
887         unsigned numElements = node->numChildren();
888
889         def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node),
890             LazyNode(graph.freeze(jsNumber(numElements))));
891
892         if (!numElements)
893             return;
894
895         AbstractHeap heap;
896         switch (node->indexingType()) {
897         case ALL_DOUBLE_INDEXING_TYPES:
898             heap = IndexedDoubleProperties;
899             break;
900
901         case ALL_INT32_INDEXING_TYPES:
902             heap = IndexedInt32Properties;
903             break;
904
905         case ALL_CONTIGUOUS_INDEXING_TYPES:
906             heap = IndexedContiguousProperties;
907             break;
908
909         default:
910             return;
911         }
912
913         if (numElements < graph.m_uint32ValuesInUse.size()) {
914             for (unsigned operandIdx = 0; operandIdx < numElements; ++operandIdx) {
915                 Edge use = graph.m_varArgChildren[node->firstChild() + operandIdx];
916                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(operandIdx)))),
917                     LazyNode(use.node()));
918             }
919         } else {
920             for (uint32_t operandIdx : graph.m_uint32ValuesInUse) {
921                 if (operandIdx >= numElements)
922                     continue;
923                 Edge use = graph.m_varArgChildren[node->firstChild() + operandIdx];
924                 // operandIdx comes from graph.m_uint32ValuesInUse and thus is guaranteed to be already frozen
925                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(operandIdx)))),
926                     LazyNode(use.node()));
927             }
928         }
929         return;
930     }
931
932     case NewArrayBuffer: {
933         read(HeapObjectCount);
934         write(HeapObjectCount);
935
936         unsigned numElements = node->numConstants();
937         def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node),
938             LazyNode(graph.freeze(jsNumber(numElements))));
939
940         AbstractHeap heap;
941         NodeType op = JSConstant;
942         switch (node->indexingType()) {
943         case ALL_DOUBLE_INDEXING_TYPES:
944             heap = IndexedDoubleProperties;
945             op = DoubleConstant;
946             break;
947
948         case ALL_INT32_INDEXING_TYPES:
949             heap = IndexedInt32Properties;
950             break;
951
952         case ALL_CONTIGUOUS_INDEXING_TYPES:
953             heap = IndexedContiguousProperties;
954             break;
955
956         default:
957             return;
958         }
959
960         JSValue* data = graph.m_codeBlock->constantBuffer(node->startConstant());
961         if (numElements < graph.m_uint32ValuesInUse.size()) {
962             for (unsigned index = 0; index < numElements; ++index) {
963                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(index)))),
964                     LazyNode(graph.freeze(data[index]), op));
965             }
966         } else {
967             Vector<uint32_t> possibleIndices;
968             for (uint32_t index : graph.m_uint32ValuesInUse) {
969                 if (index >= numElements)
970                     continue;
971                 possibleIndices.append(index);
972             }
973             for (uint32_t index : possibleIndices) {
974                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(index)))),
975                     LazyNode(graph.freeze(data[index]), op));
976             }
977         }
978         return;
979     }
980
981     case NewObject:
982     case NewRegexp:
983     case NewStringObject:
984     case PhantomNewObject:
985     case MaterializeNewObject:
986     case PhantomNewFunction:
987     case PhantomCreateActivation:
988     case MaterializeCreateActivation:
989         read(HeapObjectCount);
990         write(HeapObjectCount);
991         return;
992     
993     case NewArrowFunction:
994     case NewFunction:
995         if (node->castOperand<FunctionExecutable*>()->singletonFunction()->isStillValid())
996             write(Watchpoint_fire);
997         read(HeapObjectCount);
998         write(HeapObjectCount);
999         return;
1000
1001     case RegExpExec:
1002     case RegExpTest:
1003         read(RegExpState);
1004         write(RegExpState);
1005         return;
1006
1007     case StringCharAt:
1008         if (node->arrayMode().isOutOfBounds()) {
1009             read(World);
1010             write(Heap);
1011             return;
1012         }
1013         def(PureValue(node));
1014         return;
1015         
1016     case CompareEq:
1017     case CompareLess:
1018     case CompareLessEq:
1019     case CompareGreater:
1020     case CompareGreaterEq:
1021         if (!node->isBinaryUseKind(UntypedUse)) {
1022             def(PureValue(node));
1023             return;
1024         }
1025         read(World);
1026         write(Heap);
1027         return;
1028         
1029     case ToString:
1030     case CallStringConstructor:
1031         switch (node->child1().useKind()) {
1032         case StringObjectUse:
1033         case StringOrStringObjectUse:
1034             // These don't def a pure value, unfortunately. I'll avoid load-eliminating these for
1035             // now.
1036             return;
1037             
1038         case CellUse:
1039         case UntypedUse:
1040             read(World);
1041             write(Heap);
1042             return;
1043             
1044         default:
1045             RELEASE_ASSERT_NOT_REACHED();
1046             return;
1047         }
1048         
1049     case ThrowReferenceError:
1050         write(SideState);
1051         return;
1052         
1053     case CountExecution:
1054     case CheckWatchdogTimer:
1055         read(InternalState);
1056         write(InternalState);
1057         return;
1058         
1059     case LastNodeType:
1060         RELEASE_ASSERT_NOT_REACHED();
1061         return;
1062     }
1063     
1064     DFG_CRASH(graph, node, toCString("Unrecognized node type: ", Graph::opName(node->op())).data());
1065 }
1066
1067 class NoOpClobberize {
1068 public:
1069     NoOpClobberize() { }
1070     template<typename... T>
1071     void operator()(T...) const { }
1072 };
1073
1074 class CheckClobberize {
1075 public:
1076     CheckClobberize()
1077         : m_result(false)
1078     {
1079     }
1080     
1081     template<typename... T>
1082     void operator()(T...) const { m_result = true; }
1083     
1084     bool result() const { return m_result; }
1085     
1086 private:
1087     mutable bool m_result;
1088 };
1089
1090 bool doesWrites(Graph&, Node*);
1091
1092 class AbstractHeapOverlaps {
1093 public:
1094     AbstractHeapOverlaps(AbstractHeap heap)
1095         : m_heap(heap)
1096         , m_result(false)
1097     {
1098     }
1099     
1100     void operator()(AbstractHeap otherHeap) const
1101     {
1102         if (m_result)
1103             return;
1104         m_result = m_heap.overlaps(otherHeap);
1105     }
1106     
1107     bool result() const { return m_result; }
1108
1109 private:
1110     AbstractHeap m_heap;
1111     mutable bool m_result;
1112 };
1113
1114 bool accessesOverlap(Graph&, Node*, AbstractHeap);
1115 bool writesOverlap(Graph&, Node*, AbstractHeap);
1116
1117 bool clobbersHeap(Graph&, Node*);
1118
1119 // We would have used bind() for these, but because of the overlaoding that we are doing,
1120 // it's quite a bit of clearer to just write this out the traditional way.
1121
1122 template<typename T>
1123 class ReadMethodClobberize {
1124 public:
1125     ReadMethodClobberize(T& value)
1126         : m_value(value)
1127     {
1128     }
1129     
1130     void operator()(AbstractHeap heap) const
1131     {
1132         m_value.read(heap);
1133     }
1134 private:
1135     T& m_value;
1136 };
1137
1138 template<typename T>
1139 class WriteMethodClobberize {
1140 public:
1141     WriteMethodClobberize(T& value)
1142         : m_value(value)
1143     {
1144     }
1145     
1146     void operator()(AbstractHeap heap) const
1147     {
1148         m_value.write(heap);
1149     }
1150 private:
1151     T& m_value;
1152 };
1153
1154 template<typename T>
1155 class DefMethodClobberize {
1156 public:
1157     DefMethodClobberize(T& value)
1158         : m_value(value)
1159     {
1160     }
1161     
1162     void operator()(PureValue value) const
1163     {
1164         m_value.def(value);
1165     }
1166     
1167     void operator()(HeapLocation location, LazyNode node) const
1168     {
1169         m_value.def(location, node);
1170     }
1171
1172 private:
1173     T& m_value;
1174 };
1175
1176 template<typename Adaptor>
1177 void clobberize(Graph& graph, Node* node, Adaptor& adaptor)
1178 {
1179     ReadMethodClobberize<Adaptor> read(adaptor);
1180     WriteMethodClobberize<Adaptor> write(adaptor);
1181     DefMethodClobberize<Adaptor> def(adaptor);
1182     clobberize(graph, node, read, write, def);
1183 }
1184
1185 } } // namespace JSC::DFG
1186
1187 #endif // ENABLE(DFG_JIT)
1188
1189 #endif // DFGClobberize_h
1190