Unreviewed, rollout r198808. The patch causes crashes on 32-bit and appears to be...
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGClobberize.h
1 /*
2  * Copyright (C) 2013-2016 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 LazyJSConstant:
125         // We should enable CSE of LazyJSConstant. It's a little annoying since LazyJSValue has
126         // more bits than we currently have in PureValue.
127         return;
128         
129     case ArithIMul:
130     case ArithAbs:
131     case ArithClz32:
132     case ArithMin:
133     case ArithMax:
134     case ArithPow:
135     case ArithSqrt:
136     case ArithFRound:
137     case ArithSin:
138     case ArithCos:
139     case ArithLog:
140     case GetScope:
141     case SkipScope:
142     case GetGlobalObject:
143     case StringCharCodeAt:
144     case CompareStrictEq:
145     case IsUndefined:
146     case IsBoolean:
147     case IsNumber:
148     case IsString:
149     case IsObject:
150     case LogicalNot:
151     case CheckInBounds:
152     case DoubleRep:
153     case ValueRep:
154     case Int52Rep:
155     case BooleanToNumber:
156     case FiatInt52:
157     case MakeRope:
158     case StrCat:
159     case ValueToInt32:
160     case GetExecutable:
161     case BottomValue:
162     case TypeOf:
163         def(PureValue(node));
164         return;
165
166     case BitAnd:
167     case BitOr:
168     case BitXor:
169     case BitLShift:
170     case BitRShift:
171     case BitURShift:
172         if (node->child1().useKind() == UntypedUse || node->child2().useKind() == UntypedUse) {
173             read(World);
174             write(Heap);
175             return;
176         }
177         def(PureValue(node));
178         return;
179
180     case ArithRandom:
181         read(MathDotRandomState);
182         write(MathDotRandomState);
183         return;
184         
185     case HasGenericProperty:
186     case HasStructureProperty:
187     case GetEnumerableLength:
188     case GetPropertyEnumerator: {
189         read(Heap);
190         write(SideState);
191         return;
192     }
193
194     case GetDirectPname: {
195         // This reads and writes heap because it can end up calling a generic getByVal 
196         // if the Structure changed, which could in turn end up calling a getter.
197         read(World);
198         write(Heap);
199         return;
200     }
201
202     case ToIndexString:
203     case GetEnumeratorStructurePname:
204     case GetEnumeratorGenericPname: {
205         def(PureValue(node));
206         return;
207     }
208
209     case HasIndexedProperty: {
210         read(JSObject_butterfly);
211         ArrayMode mode = node->arrayMode();
212         switch (mode.type()) {
213         case Array::Int32: {
214             if (mode.isInBounds()) {
215                 read(Butterfly_publicLength);
216                 read(IndexedInt32Properties);
217                 def(HeapLocation(HasIndexedPropertyLoc, IndexedInt32Properties, node->child1(), node->child2()), LazyNode(node));
218                 return;
219             }
220             read(Heap);
221             return;
222         }
223             
224         case Array::Double: {
225             if (mode.isInBounds()) {
226                 read(Butterfly_publicLength);
227                 read(IndexedDoubleProperties);
228                 def(HeapLocation(HasIndexedPropertyLoc, IndexedDoubleProperties, node->child1(), node->child2()), LazyNode(node));
229                 return;
230             }
231             read(Heap);
232             return;
233         }
234             
235         case Array::Contiguous: {
236             if (mode.isInBounds()) {
237                 read(Butterfly_publicLength);
238                 read(IndexedContiguousProperties);
239                 def(HeapLocation(HasIndexedPropertyLoc, IndexedContiguousProperties, node->child1(), node->child2()), LazyNode(node));
240                 return;
241             }
242             read(Heap);
243             return;
244         }
245
246         case Array::ArrayStorage: {
247             if (mode.isInBounds()) {
248                 read(Butterfly_vectorLength);
249                 read(IndexedArrayStorageProperties);
250                 return;
251             }
252             read(Heap);
253             return;
254         }
255
256         default: {
257             read(World);
258             write(Heap);
259             return;
260         }
261         }
262         RELEASE_ASSERT_NOT_REACHED();
263         return;
264     }
265
266     case StringFromCharCode:
267         switch (node->child1().useKind()) {
268         case Int32Use:
269             def(PureValue(node));
270             return;
271         case UntypedUse:
272             read(World);
273             write(Heap);
274             return;
275         default:
276             DFG_CRASH(graph, node, "Bad use kind");
277         }
278         return;
279
280     case ArithAdd:
281     case ArithNegate:
282     case ArithMod:
283     case DoubleAsInt32:
284     case UInt32ToNumber:
285         def(PureValue(node, node->arithMode()));
286         return;
287
288     case ArithDiv:
289     case ArithMul:
290     case ArithSub:
291         switch (node->binaryUseKind()) {
292         case Int32Use:
293         case Int52RepUse:
294         case DoubleRepUse:
295             def(PureValue(node, node->arithMode()));
296             return;
297         case UntypedUse:
298             read(World);
299             write(Heap);
300             return;
301         default:
302             DFG_CRASH(graph, node, "Bad use kind");
303         }
304
305     case ArithRound:
306     case ArithFloor:
307     case ArithCeil:
308         def(PureValue(node, static_cast<uintptr_t>(node->arithRoundingMode())));
309         return;
310
311     case CheckCell:
312         def(PureValue(CheckCell, AdjacencyList(AdjacencyList::Fixed, node->child1()), node->cellOperand()));
313         return;
314
315     case CheckNotEmpty:
316         def(PureValue(CheckNotEmpty, AdjacencyList(AdjacencyList::Fixed, node->child1())));
317         return;
318
319     case CheckIdent:
320         def(PureValue(CheckIdent, AdjacencyList(AdjacencyList::Fixed, node->child1()), node->uidOperand()));
321         return;
322
323     case ConstantStoragePointer:
324         def(PureValue(node, node->storagePointer()));
325         return;
326          
327     case MovHint:
328     case ZombieHint:
329     case ExitOK:
330     case KillStack:
331     case Upsilon:
332     case Phi:
333     case PhantomLocal:
334     case SetArgument:
335     case Jump:
336     case Branch:
337     case Switch:
338     case Throw:
339     case ForceOSRExit:
340     case CheckBadCell:
341     case Return:
342     case Unreachable:
343     case CheckTierUpInLoop:
344     case CheckTierUpAtReturn:
345     case CheckTierUpAndOSREnter:
346     case LoopHint:
347     case ProfileWillCall:
348     case ProfileDidCall:
349     case ProfileType:
350     case ProfileControlFlow:
351     case StoreBarrier:
352     case PutHint:
353         write(SideState);
354         return;
355         
356     case InvalidationPoint:
357         write(SideState);
358         def(HeapLocation(InvalidationPointLoc, Watchpoint_fire), LazyNode(node));
359         return;
360
361     case Flush:
362         read(AbstractHeap(Stack, node->local()));
363         write(SideState);
364         return;
365
366     case NotifyWrite:
367         write(Watchpoint_fire);
368         write(SideState);
369         return;
370
371     case CreateActivation: {
372         SymbolTable* table = node->castOperand<SymbolTable*>();
373         if (table->singletonScope()->isStillValid())
374             write(Watchpoint_fire);
375         read(HeapObjectCount);
376         write(HeapObjectCount);
377         return;
378     }
379         
380     case CreateDirectArguments:
381     case CreateScopedArguments:
382     case CreateClonedArguments:
383         read(Stack);
384         read(HeapObjectCount);
385         write(HeapObjectCount);
386         return;
387
388     case PhantomDirectArguments:
389     case PhantomClonedArguments:
390         // DFG backend requires that the locals that this reads are flushed. FTL backend can handle those
391         // locals being promoted.
392         if (!isFTL(graph.m_plan.mode))
393             read(Stack);
394         
395         // Even though it's phantom, it still has the property that one can't be replaced with another.
396         read(HeapObjectCount);
397         write(HeapObjectCount);
398         return;
399
400     case ToThis:
401     case CreateThis:
402         read(MiscFields);
403         read(HeapObjectCount);
404         write(HeapObjectCount);
405         return;
406
407     case VarInjectionWatchpoint:
408         read(MiscFields);
409         def(HeapLocation(VarInjectionWatchpointLoc, MiscFields), LazyNode(node));
410         return;
411
412     case IsObjectOrNull:
413         read(MiscFields);
414         def(HeapLocation(IsObjectOrNullLoc, MiscFields, node->child1()), LazyNode(node));
415         return;
416         
417     case IsFunction:
418         read(MiscFields);
419         def(HeapLocation(IsFunctionLoc, MiscFields, node->child1()), LazyNode(node));
420         return;
421         
422     case GetById:
423     case GetByIdFlush:
424     case PutById:
425     case PutByIdFlush:
426     case PutByIdDirect:
427     case PutGetterById:
428     case PutSetterById:
429     case PutGetterSetterById:
430     case PutGetterByVal:
431     case PutSetterByVal:
432     case ArrayPush:
433     case ArrayPop:
434     case Call:
435     case TailCallInlinedCaller:
436     case Construct:
437     case CallVarargs:
438     case CallForwardVarargs:
439     case TailCallVarargsInlinedCaller:
440     case TailCallForwardVarargsInlinedCaller:
441     case ConstructVarargs:
442     case ConstructForwardVarargs:
443     case ToPrimitive:
444     case In:
445     case ValueAdd:
446     case SetFunctionName:
447         read(World);
448         write(Heap);
449         return;
450
451     case TailCall:
452     case TailCallVarargs:
453     case TailCallForwardVarargs:
454         read(World);
455         write(SideState);
456         return;
457         
458     case GetGetter:
459         read(GetterSetter_getter);
460         def(HeapLocation(GetterLoc, GetterSetter_getter, node->child1()), LazyNode(node));
461         return;
462         
463     case GetSetter:
464         read(GetterSetter_setter);
465         def(HeapLocation(SetterLoc, GetterSetter_setter, node->child1()), LazyNode(node));
466         return;
467         
468     case GetCallee:
469         read(AbstractHeap(Stack, JSStack::Callee));
470         def(HeapLocation(StackLoc, AbstractHeap(Stack, JSStack::Callee)), LazyNode(node));
471         return;
472         
473     case GetArgumentCount:
474         read(AbstractHeap(Stack, JSStack::ArgumentCount));
475         def(HeapLocation(StackPayloadLoc, AbstractHeap(Stack, JSStack::ArgumentCount)), LazyNode(node));
476         return;
477
478     case GetRestLength:
479         read(Stack);
480         return;
481         
482     case GetLocal:
483         read(AbstractHeap(Stack, node->local()));
484         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->local())), LazyNode(node));
485         return;
486         
487     case SetLocal:
488         write(AbstractHeap(Stack, node->local()));
489         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->local())), LazyNode(node->child1().node()));
490         return;
491         
492     case GetStack: {
493         AbstractHeap heap(Stack, node->stackAccessData()->local);
494         read(heap);
495         def(HeapLocation(StackLoc, heap), LazyNode(node));
496         return;
497     }
498         
499     case PutStack: {
500         AbstractHeap heap(Stack, node->stackAccessData()->local);
501         write(heap);
502         def(HeapLocation(StackLoc, heap), LazyNode(node->child1().node()));
503         return;
504     }
505         
506     case LoadVarargs: {
507         read(World);
508         write(Heap);
509         LoadVarargsData* data = node->loadVarargsData();
510         write(AbstractHeap(Stack, data->count.offset()));
511         for (unsigned i = data->limit; i--;)
512             write(AbstractHeap(Stack, data->start.offset() + static_cast<int>(i)));
513         return;
514     }
515         
516     case ForwardVarargs: {
517         // We could be way more precise here.
518         read(Stack);
519         
520         LoadVarargsData* data = node->loadVarargsData();
521         write(AbstractHeap(Stack, data->count.offset()));
522         for (unsigned i = data->limit; i--;)
523             write(AbstractHeap(Stack, data->start.offset() + static_cast<int>(i)));
524         return;
525     }
526         
527     case GetLocalUnlinked:
528         read(AbstractHeap(Stack, node->unlinkedLocal()));
529         def(HeapLocation(StackLoc, AbstractHeap(Stack, node->unlinkedLocal())), LazyNode(node));
530         return;
531         
532     case GetByVal: {
533         ArrayMode mode = node->arrayMode();
534         switch (mode.type()) {
535         case Array::SelectUsingPredictions:
536         case Array::Unprofiled:
537         case Array::SelectUsingArguments:
538             // Assume the worst since we don't have profiling yet.
539             read(World);
540             write(Heap);
541             return;
542             
543         case Array::ForceExit:
544             write(SideState);
545             return;
546             
547         case Array::Generic:
548             read(World);
549             write(Heap);
550             return;
551             
552         case Array::String:
553             if (mode.isOutOfBounds()) {
554                 read(World);
555                 write(Heap);
556                 return;
557             }
558             // This appears to read nothing because it's only reading immutable data.
559             def(PureValue(node, mode.asWord()));
560             return;
561             
562         case Array::DirectArguments:
563             read(DirectArgumentsProperties);
564             def(HeapLocation(IndexedPropertyLoc, DirectArgumentsProperties, node->child1(), node->child2()), LazyNode(node));
565             return;
566             
567         case Array::ScopedArguments:
568             read(ScopeProperties);
569             def(HeapLocation(IndexedPropertyLoc, ScopeProperties, node->child1(), node->child2()), LazyNode(node));
570             return;
571             
572         case Array::Int32:
573             if (mode.isInBounds()) {
574                 read(Butterfly_publicLength);
575                 read(IndexedInt32Properties);
576                 def(HeapLocation(IndexedPropertyLoc, IndexedInt32Properties, node->child1(), node->child2()), LazyNode(node));
577                 return;
578             }
579             read(World);
580             write(Heap);
581             return;
582             
583         case Array::Double:
584             if (mode.isInBounds()) {
585                 read(Butterfly_publicLength);
586                 read(IndexedDoubleProperties);
587                 def(HeapLocation(IndexedPropertyLoc, IndexedDoubleProperties, node->child1(), node->child2()), LazyNode(node));
588                 return;
589             }
590             read(World);
591             write(Heap);
592             return;
593             
594         case Array::Contiguous:
595             if (mode.isInBounds()) {
596                 read(Butterfly_publicLength);
597                 read(IndexedContiguousProperties);
598                 def(HeapLocation(IndexedPropertyLoc, IndexedContiguousProperties, node->child1(), node->child2()), LazyNode(node));
599                 return;
600             }
601             read(World);
602             write(Heap);
603             return;
604
605         case Array::Undecided:
606             def(PureValue(node));
607             return;
608             
609         case Array::ArrayStorage:
610         case Array::SlowPutArrayStorage:
611             if (mode.isInBounds()) {
612                 read(Butterfly_vectorLength);
613                 read(IndexedArrayStorageProperties);
614                 return;
615             }
616             read(World);
617             write(Heap);
618             return;
619             
620         case Array::Int8Array:
621         case Array::Int16Array:
622         case Array::Int32Array:
623         case Array::Uint8Array:
624         case Array::Uint8ClampedArray:
625         case Array::Uint16Array:
626         case Array::Uint32Array:
627         case Array::Float32Array:
628         case Array::Float64Array:
629             read(TypedArrayProperties);
630             read(MiscFields);
631             def(HeapLocation(IndexedPropertyLoc, TypedArrayProperties, node->child1(), node->child2()), LazyNode(node));
632             return;
633         // We should not get an AnyTypedArray in a GetByVal as AnyTypedArray is only created from intrinsics, which
634         // are only added from Inline Caching a GetById.
635         case Array::AnyTypedArray:
636             DFG_CRASH(graph, node, "impossible array mode for get");
637             return;
638         }
639         RELEASE_ASSERT_NOT_REACHED();
640         return;
641     }
642         
643     case GetMyArgumentByVal: {
644         read(Stack);
645         // FIXME: It would be trivial to have a def here.
646         // https://bugs.webkit.org/show_bug.cgi?id=143077
647         return;
648     }
649
650     case PutByValDirect:
651     case PutByVal:
652     case PutByValAlias: {
653         ArrayMode mode = node->arrayMode();
654         Node* base = graph.varArgChild(node, 0).node();
655         Node* index = graph.varArgChild(node, 1).node();
656         Node* value = graph.varArgChild(node, 2).node();
657         switch (mode.modeForPut().type()) {
658         case Array::SelectUsingPredictions:
659         case Array::SelectUsingArguments:
660         case Array::Unprofiled:
661         case Array::Undecided:
662             // Assume the worst since we don't have profiling yet.
663             read(World);
664             write(Heap);
665             return;
666             
667         case Array::ForceExit:
668             write(SideState);
669             return;
670             
671         case Array::Generic:
672             read(World);
673             write(Heap);
674             return;
675             
676         case Array::Int32:
677             if (node->arrayMode().isOutOfBounds()) {
678                 read(World);
679                 write(Heap);
680                 return;
681             }
682             read(Butterfly_publicLength);
683             read(Butterfly_vectorLength);
684             read(IndexedInt32Properties);
685             write(IndexedInt32Properties);
686             if (node->arrayMode().mayStoreToHole())
687                 write(Butterfly_publicLength);
688             def(HeapLocation(IndexedPropertyLoc, IndexedInt32Properties, base, index), LazyNode(value));
689             return;
690             
691         case Array::Double:
692             if (node->arrayMode().isOutOfBounds()) {
693                 read(World);
694                 write(Heap);
695                 return;
696             }
697             read(Butterfly_publicLength);
698             read(Butterfly_vectorLength);
699             read(IndexedDoubleProperties);
700             write(IndexedDoubleProperties);
701             if (node->arrayMode().mayStoreToHole())
702                 write(Butterfly_publicLength);
703             def(HeapLocation(IndexedPropertyLoc, IndexedDoubleProperties, base, index), LazyNode(value));
704             return;
705             
706         case Array::Contiguous:
707             if (node->arrayMode().isOutOfBounds()) {
708                 read(World);
709                 write(Heap);
710                 return;
711             }
712             read(Butterfly_publicLength);
713             read(Butterfly_vectorLength);
714             read(IndexedContiguousProperties);
715             write(IndexedContiguousProperties);
716             if (node->arrayMode().mayStoreToHole())
717                 write(Butterfly_publicLength);
718             def(HeapLocation(IndexedPropertyLoc, IndexedContiguousProperties, base, index), LazyNode(value));
719             return;
720             
721         case Array::ArrayStorage:
722         case Array::SlowPutArrayStorage:
723             // Give up on life for now.
724             read(World);
725             write(Heap);
726             return;
727
728         case Array::Int8Array:
729         case Array::Int16Array:
730         case Array::Int32Array:
731         case Array::Uint8Array:
732         case Array::Uint8ClampedArray:
733         case Array::Uint16Array:
734         case Array::Uint32Array:
735         case Array::Float32Array:
736         case Array::Float64Array:
737             read(MiscFields);
738             write(TypedArrayProperties);
739             // FIXME: We can't def() anything here because these operations truncate their inputs.
740             // https://bugs.webkit.org/show_bug.cgi?id=134737
741             return;
742         case Array::AnyTypedArray:
743         case Array::String:
744         case Array::DirectArguments:
745         case Array::ScopedArguments:
746             DFG_CRASH(graph, node, "impossible array mode for put");
747             return;
748         }
749         RELEASE_ASSERT_NOT_REACHED();
750         return;
751     }
752         
753     case CheckStructure:
754         read(JSCell_structureID);
755         return;
756
757     case CheckArray:
758         read(JSCell_indexingType);
759         read(JSCell_typeInfoType);
760         read(JSCell_structureID);
761         return;
762
763     case CheckTypeInfoFlags:
764         read(JSCell_typeInfoFlags);
765         def(HeapLocation(CheckTypeInfoFlagsLoc, JSCell_typeInfoFlags, node->child1()), LazyNode(node));
766         return;
767
768     case OverridesHasInstance:
769         read(JSCell_typeInfoFlags);
770         def(HeapLocation(OverridesHasInstanceLoc, JSCell_typeInfoFlags, node->child1()), LazyNode(node));
771         return;
772
773     case InstanceOf:
774         read(JSCell_structureID);
775         def(HeapLocation(InstanceOfLoc, JSCell_structureID, node->child1(), node->child2()), LazyNode(node));
776         return;
777
778     case InstanceOfCustom:
779         read(World);
780         write(Heap);
781         return;
782
783     case PutStructure:
784         write(JSCell_structureID);
785         write(JSCell_typeInfoType);
786         write(JSCell_typeInfoFlags);
787         write(JSCell_indexingType);
788         return;
789         
790     case AllocatePropertyStorage:
791         write(JSObject_butterfly);
792         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
793         return;
794         
795     case ReallocatePropertyStorage:
796         read(JSObject_butterfly);
797         write(JSObject_butterfly);
798         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
799         return;
800         
801     case GetButterfly:
802         read(JSObject_butterfly);
803         def(HeapLocation(ButterflyLoc, JSObject_butterfly, node->child1()), LazyNode(node));
804         return;
805
806     case Arrayify:
807     case ArrayifyToStructure:
808         read(JSCell_structureID);
809         read(JSCell_indexingType);
810         read(JSObject_butterfly);
811         write(JSCell_structureID);
812         write(JSCell_indexingType);
813         write(JSObject_butterfly);
814         write(Watchpoint_fire);
815         return;
816         
817     case GetIndexedPropertyStorage:
818         if (node->arrayMode().type() == Array::String) {
819             def(PureValue(node, node->arrayMode().asWord()));
820             return;
821         }
822         read(MiscFields);
823         def(HeapLocation(IndexedPropertyStorageLoc, MiscFields, node->child1()), LazyNode(node));
824         return;
825         
826     case GetTypedArrayByteOffset:
827         read(MiscFields);
828         def(HeapLocation(TypedArrayByteOffsetLoc, MiscFields, node->child1()), LazyNode(node));
829         return;
830         
831     case GetByOffset:
832     case GetGetterSetterByOffset: {
833         unsigned identifierNumber = node->storageAccessData().identifierNumber;
834         AbstractHeap heap(NamedProperties, identifierNumber);
835         read(heap);
836         def(HeapLocation(NamedPropertyLoc, heap, node->child2()), LazyNode(node));
837         return;
838     }
839         
840     case MultiGetByOffset: {
841         read(JSCell_structureID);
842         read(JSObject_butterfly);
843         AbstractHeap heap(NamedProperties, node->multiGetByOffsetData().identifierNumber);
844         read(heap);
845         def(HeapLocation(NamedPropertyLoc, heap, node->child1()), LazyNode(node));
846         return;
847     }
848         
849     case MultiPutByOffset: {
850         read(JSCell_structureID);
851         read(JSObject_butterfly);
852         AbstractHeap heap(NamedProperties, node->multiPutByOffsetData().identifierNumber);
853         write(heap);
854         if (node->multiPutByOffsetData().writesStructures())
855             write(JSCell_structureID);
856         if (node->multiPutByOffsetData().reallocatesStorage())
857             write(JSObject_butterfly);
858         def(HeapLocation(NamedPropertyLoc, heap, node->child1()), LazyNode(node->child2().node()));
859         return;
860     }
861         
862     case PutByOffset: {
863         unsigned identifierNumber = node->storageAccessData().identifierNumber;
864         AbstractHeap heap(NamedProperties, identifierNumber);
865         write(heap);
866         def(HeapLocation(NamedPropertyLoc, heap, node->child2()), LazyNode(node->child3().node()));
867         return;
868     }
869         
870     case GetArrayLength: {
871         ArrayMode mode = node->arrayMode();
872         switch (mode.type()) {
873         case Array::Int32:
874         case Array::Double:
875         case Array::Contiguous:
876         case Array::ArrayStorage:
877         case Array::SlowPutArrayStorage:
878             read(Butterfly_publicLength);
879             def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node->child1()), LazyNode(node));
880             return;
881             
882         case Array::String:
883             def(PureValue(node, mode.asWord()));
884             return;
885
886         case Array::DirectArguments:
887         case Array::ScopedArguments:
888             read(MiscFields);
889             def(HeapLocation(ArrayLengthLoc, MiscFields, node->child1()), LazyNode(node));
890             return;
891
892         default:
893             ASSERT(mode.isSomeTypedArrayView());
894             read(MiscFields);
895             def(HeapLocation(ArrayLengthLoc, MiscFields, node->child1()), LazyNode(node));
896             return;
897         }
898     }
899         
900     case GetClosureVar:
901         read(AbstractHeap(ScopeProperties, node->scopeOffset().offset()));
902         def(HeapLocation(ClosureVariableLoc, AbstractHeap(ScopeProperties, node->scopeOffset().offset()), node->child1()), LazyNode(node));
903         return;
904         
905     case PutClosureVar:
906         write(AbstractHeap(ScopeProperties, node->scopeOffset().offset()));
907         def(HeapLocation(ClosureVariableLoc, AbstractHeap(ScopeProperties, node->scopeOffset().offset()), node->child1()), LazyNode(node->child2().node()));
908         return;
909
910     case GetRegExpObjectLastIndex:
911         read(RegExpObject_lastIndex);
912         def(HeapLocation(RegExpObjectLastIndexLoc, RegExpObject_lastIndex, node->child1()), LazyNode(node));
913         return;
914
915     case SetRegExpObjectLastIndex:
916         write(RegExpObject_lastIndex);
917         def(HeapLocation(RegExpObjectLastIndexLoc, RegExpObject_lastIndex, node->child1()), LazyNode(node->child2().node()));
918         return;
919         
920     case GetFromArguments: {
921         AbstractHeap heap(DirectArgumentsProperties, node->capturedArgumentsOffset().offset());
922         read(heap);
923         def(HeapLocation(DirectArgumentsLoc, heap, node->child1()), LazyNode(node));
924         return;
925     }
926         
927     case PutToArguments: {
928         AbstractHeap heap(DirectArgumentsProperties, node->capturedArgumentsOffset().offset());
929         write(heap);
930         def(HeapLocation(DirectArgumentsLoc, heap, node->child1()), LazyNode(node->child2().node()));
931         return;
932     }
933         
934     case GetGlobalVar:
935     case GetGlobalLexicalVariable:
936         read(AbstractHeap(Absolute, node->variablePointer()));
937         def(HeapLocation(GlobalVariableLoc, AbstractHeap(Absolute, node->variablePointer())), LazyNode(node));
938         return;
939         
940     case PutGlobalVariable:
941         write(AbstractHeap(Absolute, node->variablePointer()));
942         def(HeapLocation(GlobalVariableLoc, AbstractHeap(Absolute, node->variablePointer())), LazyNode(node->child2().node()));
943         return;
944
945     case NewArrayWithSize:
946     case NewTypedArray:
947         read(HeapObjectCount);
948         write(HeapObjectCount);
949         return;
950
951     case NewArray: {
952         read(HeapObjectCount);
953         write(HeapObjectCount);
954
955         unsigned numElements = node->numChildren();
956
957         def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node),
958             LazyNode(graph.freeze(jsNumber(numElements))));
959
960         if (!numElements)
961             return;
962
963         AbstractHeap heap;
964         switch (node->indexingType()) {
965         case ALL_DOUBLE_INDEXING_TYPES:
966             heap = IndexedDoubleProperties;
967             break;
968
969         case ALL_INT32_INDEXING_TYPES:
970             heap = IndexedInt32Properties;
971             break;
972
973         case ALL_CONTIGUOUS_INDEXING_TYPES:
974             heap = IndexedContiguousProperties;
975             break;
976
977         default:
978             return;
979         }
980
981         if (numElements < graph.m_uint32ValuesInUse.size()) {
982             for (unsigned operandIdx = 0; operandIdx < numElements; ++operandIdx) {
983                 Edge use = graph.m_varArgChildren[node->firstChild() + operandIdx];
984                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(operandIdx)))),
985                     LazyNode(use.node()));
986             }
987         } else {
988             for (uint32_t operandIdx : graph.m_uint32ValuesInUse) {
989                 if (operandIdx >= numElements)
990                     continue;
991                 Edge use = graph.m_varArgChildren[node->firstChild() + operandIdx];
992                 // operandIdx comes from graph.m_uint32ValuesInUse and thus is guaranteed to be already frozen
993                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(operandIdx)))),
994                     LazyNode(use.node()));
995             }
996         }
997         return;
998     }
999
1000     case NewArrayBuffer: {
1001         read(HeapObjectCount);
1002         write(HeapObjectCount);
1003
1004         unsigned numElements = node->numConstants();
1005         def(HeapLocation(ArrayLengthLoc, Butterfly_publicLength, node),
1006             LazyNode(graph.freeze(jsNumber(numElements))));
1007
1008         AbstractHeap heap;
1009         NodeType op = JSConstant;
1010         switch (node->indexingType()) {
1011         case ALL_DOUBLE_INDEXING_TYPES:
1012             heap = IndexedDoubleProperties;
1013             op = DoubleConstant;
1014             break;
1015
1016         case ALL_INT32_INDEXING_TYPES:
1017             heap = IndexedInt32Properties;
1018             break;
1019
1020         case ALL_CONTIGUOUS_INDEXING_TYPES:
1021             heap = IndexedContiguousProperties;
1022             break;
1023
1024         default:
1025             return;
1026         }
1027
1028         JSValue* data = graph.m_codeBlock->constantBuffer(node->startConstant());
1029         if (numElements < graph.m_uint32ValuesInUse.size()) {
1030             for (unsigned index = 0; index < numElements; ++index) {
1031                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(index)))),
1032                     LazyNode(graph.freeze(data[index]), op));
1033             }
1034         } else {
1035             Vector<uint32_t> possibleIndices;
1036             for (uint32_t index : graph.m_uint32ValuesInUse) {
1037                 if (index >= numElements)
1038                     continue;
1039                 possibleIndices.append(index);
1040             }
1041             for (uint32_t index : possibleIndices) {
1042                 def(HeapLocation(IndexedPropertyLoc, heap, node, LazyNode(graph.freeze(jsNumber(index)))),
1043                     LazyNode(graph.freeze(data[index]), op));
1044             }
1045         }
1046         return;
1047     }
1048
1049     case CopyRest: {
1050         read(Stack);
1051         write(Heap);
1052         return;
1053     }
1054
1055     case NewObject:
1056     case NewRegexp:
1057     case NewStringObject:
1058     case PhantomNewObject:
1059     case MaterializeNewObject:
1060     case PhantomNewFunction:
1061     case PhantomNewGeneratorFunction:
1062     case PhantomCreateActivation:
1063     case MaterializeCreateActivation:
1064         read(HeapObjectCount);
1065         write(HeapObjectCount);
1066         return;
1067     
1068     case NewArrowFunction:
1069     case NewFunction:
1070     case NewGeneratorFunction:
1071         if (node->castOperand<FunctionExecutable*>()->singletonFunction()->isStillValid())
1072             write(Watchpoint_fire);
1073         read(HeapObjectCount);
1074         write(HeapObjectCount);
1075         return;
1076
1077     case RegExpExec:
1078     case RegExpTest:
1079         if (node->child2().useKind() == RegExpObjectUse
1080             && node->child3().useKind() == StringUse) {
1081             read(RegExpState);
1082             write(RegExpState);
1083             return;
1084         }
1085         read(World);
1086         write(Heap);
1087         return;
1088
1089     case StringReplace:
1090         if (node->child1().useKind() == StringUse
1091             && node->child2().useKind() == RegExpObjectUse
1092             && node->child3().useKind() == StringUse) {
1093             read(RegExpState);
1094             write(RegExpState);
1095             return;
1096         }
1097         read(World);
1098         write(Heap);
1099         return;
1100
1101     case StringCharAt:
1102         if (node->arrayMode().isOutOfBounds()) {
1103             read(World);
1104             write(Heap);
1105             return;
1106         }
1107         def(PureValue(node));
1108         return;
1109         
1110     case CompareEq:
1111     case CompareLess:
1112     case CompareLessEq:
1113     case CompareGreater:
1114     case CompareGreaterEq:
1115         if (!node->isBinaryUseKind(UntypedUse)) {
1116             def(PureValue(node));
1117             return;
1118         }
1119         read(World);
1120         write(Heap);
1121         return;
1122         
1123     case ToString:
1124     case CallStringConstructor:
1125         switch (node->child1().useKind()) {
1126         case StringObjectUse:
1127         case StringOrStringObjectUse:
1128             // These don't def a pure value, unfortunately. I'll avoid load-eliminating these for
1129             // now.
1130             return;
1131             
1132         case CellUse:
1133         case UntypedUse:
1134             read(World);
1135             write(Heap);
1136             return;
1137             
1138         default:
1139             RELEASE_ASSERT_NOT_REACHED();
1140             return;
1141         }
1142         
1143     case ThrowReferenceError:
1144         write(SideState);
1145         return;
1146         
1147     case CountExecution:
1148     case CheckWatchdogTimer:
1149         read(InternalState);
1150         write(InternalState);
1151         return;
1152         
1153     case LastNodeType:
1154         RELEASE_ASSERT_NOT_REACHED();
1155         return;
1156     }
1157     
1158     DFG_CRASH(graph, node, toCString("Unrecognized node type: ", Graph::opName(node->op())).data());
1159 }
1160
1161 class NoOpClobberize {
1162 public:
1163     NoOpClobberize() { }
1164     template<typename... T>
1165     void operator()(T...) const { }
1166 };
1167
1168 class CheckClobberize {
1169 public:
1170     CheckClobberize()
1171         : m_result(false)
1172     {
1173     }
1174     
1175     template<typename... T>
1176     void operator()(T...) const { m_result = true; }
1177     
1178     bool result() const { return m_result; }
1179     
1180 private:
1181     mutable bool m_result;
1182 };
1183
1184 bool doesWrites(Graph&, Node*);
1185
1186 class AbstractHeapOverlaps {
1187 public:
1188     AbstractHeapOverlaps(AbstractHeap heap)
1189         : m_heap(heap)
1190         , m_result(false)
1191     {
1192     }
1193     
1194     void operator()(AbstractHeap otherHeap) const
1195     {
1196         if (m_result)
1197             return;
1198         m_result = m_heap.overlaps(otherHeap);
1199     }
1200     
1201     bool result() const { return m_result; }
1202
1203 private:
1204     AbstractHeap m_heap;
1205     mutable bool m_result;
1206 };
1207
1208 bool accessesOverlap(Graph&, Node*, AbstractHeap);
1209 bool writesOverlap(Graph&, Node*, AbstractHeap);
1210
1211 bool clobbersHeap(Graph&, Node*);
1212
1213 // We would have used bind() for these, but because of the overlaoding that we are doing,
1214 // it's quite a bit of clearer to just write this out the traditional way.
1215
1216 template<typename T>
1217 class ReadMethodClobberize {
1218 public:
1219     ReadMethodClobberize(T& value)
1220         : m_value(value)
1221     {
1222     }
1223     
1224     void operator()(AbstractHeap heap) const
1225     {
1226         m_value.read(heap);
1227     }
1228 private:
1229     T& m_value;
1230 };
1231
1232 template<typename T>
1233 class WriteMethodClobberize {
1234 public:
1235     WriteMethodClobberize(T& value)
1236         : m_value(value)
1237     {
1238     }
1239     
1240     void operator()(AbstractHeap heap) const
1241     {
1242         m_value.write(heap);
1243     }
1244 private:
1245     T& m_value;
1246 };
1247
1248 template<typename T>
1249 class DefMethodClobberize {
1250 public:
1251     DefMethodClobberize(T& value)
1252         : m_value(value)
1253     {
1254     }
1255     
1256     void operator()(PureValue value) const
1257     {
1258         m_value.def(value);
1259     }
1260     
1261     void operator()(HeapLocation location, LazyNode node) const
1262     {
1263         m_value.def(location, node);
1264     }
1265
1266 private:
1267     T& m_value;
1268 };
1269
1270 template<typename Adaptor>
1271 void clobberize(Graph& graph, Node* node, Adaptor& adaptor)
1272 {
1273     ReadMethodClobberize<Adaptor> read(adaptor);
1274     WriteMethodClobberize<Adaptor> write(adaptor);
1275     DefMethodClobberize<Adaptor> def(adaptor);
1276     clobberize(graph, node, read, write, def);
1277 }
1278
1279 } } // namespace JSC::DFG
1280
1281 #endif // ENABLE(DFG_JIT)
1282
1283 #endif // DFGClobberize_h
1284