FixupPhase should be more eager to demote bit math to untyped
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGFixupPhase.cpp
1 /*
2  * Copyright (C) 2012-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 #include "config.h"
27 #include "DFGFixupPhase.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "ArrayPrototype.h"
32 #include "DFGGraph.h"
33 #include "DFGInferredTypeCheck.h"
34 #include "DFGInsertionSet.h"
35 #include "DFGPhase.h"
36 #include "DFGPredictionPropagationPhase.h"
37 #include "DFGVariableAccessDataDump.h"
38 #include "JSCInlines.h"
39 #include "TypeLocation.h"
40
41 namespace JSC { namespace DFG {
42
43 class FixupPhase : public Phase {
44 public:
45     FixupPhase(Graph& graph)
46         : Phase(graph, "fixup")
47         , m_insertionSet(graph)
48     {
49     }
50     
51     bool run()
52     {
53         ASSERT(m_graph.m_fixpointState == BeforeFixpoint);
54         ASSERT(m_graph.m_form == ThreadedCPS);
55         
56         m_profitabilityChanged = false;
57         for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex)
58             fixupBlock(m_graph.block(blockIndex));
59         
60         while (m_profitabilityChanged) {
61             m_profitabilityChanged = false;
62             
63             for (unsigned i = m_graph.m_argumentPositions.size(); i--;)
64                 m_graph.m_argumentPositions[i].mergeArgumentUnboxingAwareness();
65             
66             for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex)
67                 fixupGetAndSetLocalsInBlock(m_graph.block(blockIndex));
68         }
69         
70         for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex)
71             fixupChecksInBlock(m_graph.block(blockIndex));
72
73         m_graph.m_planStage = PlanStage::AfterFixup;
74
75         return true;
76     }
77
78 private:
79     void fixupBlock(BasicBlock* block)
80     {
81         if (!block)
82             return;
83         ASSERT(block->isReachable);
84         m_block = block;
85         for (m_indexInBlock = 0; m_indexInBlock < block->size(); ++m_indexInBlock) {
86             m_currentNode = block->at(m_indexInBlock);
87             fixupNode(m_currentNode);
88         }
89         m_insertionSet.execute(block);
90     }
91     
92     void fixupNode(Node* node)
93     {
94         NodeType op = node->op();
95
96         switch (op) {
97         case SetLocal: {
98             // This gets handled by fixupGetAndSetLocalsInBlock().
99             return;
100         }
101             
102         case BitAnd:
103         case BitOr:
104         case BitXor:
105         case BitRShift:
106         case BitLShift:
107         case BitURShift: {
108             if (Node::shouldSpeculateUntypedForBitOps(node->child1().node(), node->child2().node())) {
109                 fixEdge<UntypedUse>(node->child1());
110                 fixEdge<UntypedUse>(node->child2());
111                 break;
112             }
113             fixIntConvertingEdge(node->child1());
114             fixIntConvertingEdge(node->child2());
115             break;
116         }
117
118         case ArithIMul: {
119             fixIntConvertingEdge(node->child1());
120             fixIntConvertingEdge(node->child2());
121             node->setOp(ArithMul);
122             node->setArithMode(Arith::Unchecked);
123             node->child1().setUseKind(Int32Use);
124             node->child2().setUseKind(Int32Use);
125             break;
126         }
127
128         case ArithClz32: {
129             fixIntConvertingEdge(node->child1());
130             node->setArithMode(Arith::Unchecked);
131             break;
132         }
133             
134         case UInt32ToNumber: {
135             fixIntConvertingEdge(node->child1());
136             if (bytecodeCanTruncateInteger(node->arithNodeFlags()))
137                 node->convertToIdentity();
138             else if (node->canSpeculateInt32(FixupPass))
139                 node->setArithMode(Arith::CheckOverflow);
140             else {
141                 node->setArithMode(Arith::DoOverflow);
142                 node->clearFlags(NodeMustGenerate);
143                 node->setResult(enableInt52() ? NodeResultInt52 : NodeResultDouble);
144             }
145             break;
146         }
147             
148         case ValueAdd: {
149             if (attemptToMakeIntegerAdd(node)) {
150                 node->setOp(ArithAdd);
151                 break;
152             }
153             if (Node::shouldSpeculateNumberOrBooleanExpectingDefined(node->child1().node(), node->child2().node())) {
154                 fixDoubleOrBooleanEdge(node->child1());
155                 fixDoubleOrBooleanEdge(node->child2());
156                 node->setOp(ArithAdd);
157                 node->setResult(NodeResultDouble);
158                 break;
159             }
160             
161             if (attemptToMakeFastStringAdd(node))
162                 break;
163
164             fixEdge<UntypedUse>(node->child1());
165             fixEdge<UntypedUse>(node->child2());
166             node->setResult(NodeResultJS);
167             break;
168         }
169
170         case StrCat: {
171             if (attemptToMakeFastStringAdd(node))
172                 break;
173
174             // FIXME: Remove empty string arguments and possibly turn this into a ToString operation. That
175             // would require a form of ToString that takes a KnownPrimitiveUse. This is necessary because
176             // the implementation of StrCat doesn't dynamically optimize for empty strings.
177             // https://bugs.webkit.org/show_bug.cgi?id=148540
178             m_graph.doToChildren(
179                 node,
180                 [&] (Edge& edge) {
181                     fixEdge<KnownPrimitiveUse>(edge);
182                 });
183             break;
184         }
185             
186         case MakeRope: {
187             fixupMakeRope(node);
188             break;
189         }
190             
191         case ArithAdd:
192         case ArithSub: {
193             if (op == ArithSub
194                 && Node::shouldSpeculateUntypedForArithmetic(node->child1().node(), node->child2().node())) {
195                 fixEdge<UntypedUse>(node->child1());
196                 fixEdge<UntypedUse>(node->child2());
197                 node->setResult(NodeResultJS);
198                 break;
199             }
200             if (attemptToMakeIntegerAdd(node))
201                 break;
202             fixDoubleOrBooleanEdge(node->child1());
203             fixDoubleOrBooleanEdge(node->child2());
204             node->setResult(NodeResultDouble);
205             break;
206         }
207             
208         case ArithNegate: {
209             if (m_graph.unaryArithShouldSpeculateInt32(node, FixupPass)) {
210                 fixIntOrBooleanEdge(node->child1());
211                 if (bytecodeCanTruncateInteger(node->arithNodeFlags()))
212                     node->setArithMode(Arith::Unchecked);
213                 else if (bytecodeCanIgnoreNegativeZero(node->arithNodeFlags()))
214                     node->setArithMode(Arith::CheckOverflow);
215                 else
216                     node->setArithMode(Arith::CheckOverflowAndNegativeZero);
217                 break;
218             }
219             if (m_graph.unaryArithShouldSpeculateAnyInt(node, FixupPass)) {
220                 fixEdge<Int52RepUse>(node->child1());
221                 if (bytecodeCanIgnoreNegativeZero(node->arithNodeFlags()))
222                     node->setArithMode(Arith::CheckOverflow);
223                 else
224                     node->setArithMode(Arith::CheckOverflowAndNegativeZero);
225                 node->setResult(NodeResultInt52);
226                 break;
227             }
228             fixDoubleOrBooleanEdge(node->child1());
229             node->setResult(NodeResultDouble);
230             break;
231         }
232             
233         case ArithMul: {
234             Edge& leftChild = node->child1();
235             Edge& rightChild = node->child2();
236             if (Node::shouldSpeculateUntypedForArithmetic(leftChild.node(), rightChild.node())) {
237                 fixEdge<UntypedUse>(leftChild);
238                 fixEdge<UntypedUse>(rightChild);
239                 node->setResult(NodeResultJS);
240                 break;
241             }
242             if (m_graph.binaryArithShouldSpeculateInt32(node, FixupPass)) {
243                 fixIntOrBooleanEdge(leftChild);
244                 fixIntOrBooleanEdge(rightChild);
245                 if (bytecodeCanTruncateInteger(node->arithNodeFlags()))
246                     node->setArithMode(Arith::Unchecked);
247                 else if (bytecodeCanIgnoreNegativeZero(node->arithNodeFlags())
248                     || leftChild.node() == rightChild.node())
249                     node->setArithMode(Arith::CheckOverflow);
250                 else
251                     node->setArithMode(Arith::CheckOverflowAndNegativeZero);
252                 break;
253             }
254             if (m_graph.binaryArithShouldSpeculateAnyInt(node, FixupPass)) {
255                 fixEdge<Int52RepUse>(leftChild);
256                 fixEdge<Int52RepUse>(rightChild);
257                 if (bytecodeCanIgnoreNegativeZero(node->arithNodeFlags())
258                     || leftChild.node() == rightChild.node())
259                     node->setArithMode(Arith::CheckOverflow);
260                 else
261                     node->setArithMode(Arith::CheckOverflowAndNegativeZero);
262                 node->setResult(NodeResultInt52);
263                 break;
264             }
265             fixDoubleOrBooleanEdge(leftChild);
266             fixDoubleOrBooleanEdge(rightChild);
267             node->setResult(NodeResultDouble);
268             break;
269         }
270
271         case ArithDiv:
272         case ArithMod: {
273             Edge& leftChild = node->child1();
274             Edge& rightChild = node->child2();
275             if (op == ArithDiv
276                 && Node::shouldSpeculateUntypedForArithmetic(leftChild.node(), rightChild.node())
277                 && m_graph.hasExitSite(node->origin.semantic, BadType)) {
278                 fixEdge<UntypedUse>(leftChild);
279                 fixEdge<UntypedUse>(rightChild);
280                 node->setResult(NodeResultJS);
281                 break;
282             }
283             if (m_graph.binaryArithShouldSpeculateInt32(node, FixupPass)) {
284                 if (optimizeForX86() || optimizeForARM64() || optimizeForARMv7IDIVSupported()) {
285                     fixIntOrBooleanEdge(leftChild);
286                     fixIntOrBooleanEdge(rightChild);
287                     if (bytecodeCanTruncateInteger(node->arithNodeFlags()))
288                         node->setArithMode(Arith::Unchecked);
289                     else if (bytecodeCanIgnoreNegativeZero(node->arithNodeFlags()))
290                         node->setArithMode(Arith::CheckOverflow);
291                     else
292                         node->setArithMode(Arith::CheckOverflowAndNegativeZero);
293                     break;
294                 }
295                 
296                 // This will cause conversion nodes to be inserted later.
297                 fixDoubleOrBooleanEdge(leftChild);
298                 fixDoubleOrBooleanEdge(rightChild);
299                 
300                 // We don't need to do ref'ing on the children because we're stealing them from
301                 // the original division.
302                 Node* newDivision = m_insertionSet.insertNode(
303                     m_indexInBlock, SpecBytecodeDouble, *node);
304                 newDivision->setResult(NodeResultDouble);
305                 
306                 node->setOp(DoubleAsInt32);
307                 node->children.initialize(Edge(newDivision, DoubleRepUse), Edge(), Edge());
308                 if (bytecodeCanIgnoreNegativeZero(node->arithNodeFlags()))
309                     node->setArithMode(Arith::CheckOverflow);
310                 else
311                     node->setArithMode(Arith::CheckOverflowAndNegativeZero);
312                 break;
313             }
314             fixDoubleOrBooleanEdge(leftChild);
315             fixDoubleOrBooleanEdge(rightChild);
316             node->setResult(NodeResultDouble);
317             break;
318         }
319             
320         case ArithMin:
321         case ArithMax: {
322             if (m_graph.binaryArithShouldSpeculateInt32(node, FixupPass)) {
323                 fixIntOrBooleanEdge(node->child1());
324                 fixIntOrBooleanEdge(node->child2());
325                 break;
326             }
327             fixDoubleOrBooleanEdge(node->child1());
328             fixDoubleOrBooleanEdge(node->child2());
329             node->setResult(NodeResultDouble);
330             break;
331         }
332             
333         case ArithAbs: {
334             if (m_graph.unaryArithShouldSpeculateInt32(node, FixupPass)) {
335                 fixIntOrBooleanEdge(node->child1());
336                 if (bytecodeCanTruncateInteger(node->arithNodeFlags()))
337                     node->setArithMode(Arith::Unchecked);
338                 else
339                     node->setArithMode(Arith::CheckOverflow);
340                 break;
341             }
342             fixDoubleOrBooleanEdge(node->child1());
343             node->setResult(NodeResultDouble);
344             break;
345         }
346
347         case ArithPow: {
348             if (node->child2()->shouldSpeculateInt32OrBooleanForArithmetic()) {
349                 fixDoubleOrBooleanEdge(node->child1());
350                 fixIntOrBooleanEdge(node->child2());
351                 break;
352             }
353
354             fixDoubleOrBooleanEdge(node->child1());
355             fixDoubleOrBooleanEdge(node->child2());
356             break;
357         }
358
359         case ArithRandom: {
360             node->setResult(NodeResultDouble);
361             break;
362         }
363
364         case ArithRound:
365         case ArithFloor:
366         case ArithCeil:
367         case ArithTrunc: {
368             if (m_graph.unaryArithShouldSpeculateInt32(node, FixupPass)) {
369                 fixIntOrBooleanEdge(node->child1());
370                 insertCheck<Int32Use>(m_indexInBlock, node->child1().node());
371                 node->convertToIdentity();
372                 break;
373             }
374             fixDoubleOrBooleanEdge(node->child1());
375
376             if (isInt32OrBooleanSpeculation(node->getHeapPrediction()) && m_graph.roundShouldSpeculateInt32(node, FixupPass)) {
377                 node->setResult(NodeResultInt32);
378                 if (bytecodeCanIgnoreNegativeZero(node->arithNodeFlags()))
379                     node->setArithRoundingMode(Arith::RoundingMode::Int32);
380                 else
381                     node->setArithRoundingMode(Arith::RoundingMode::Int32WithNegativeZeroCheck);
382             } else {
383                 node->setResult(NodeResultDouble);
384                 node->setArithRoundingMode(Arith::RoundingMode::Double);
385             }
386             break;
387         }
388             
389         case ArithSqrt:
390         case ArithFRound:
391         case ArithSin:
392         case ArithCos:
393         case ArithLog: {
394             fixDoubleOrBooleanEdge(node->child1());
395             node->setResult(NodeResultDouble);
396             break;
397         }
398             
399         case LogicalNot: {
400             if (node->child1()->shouldSpeculateBoolean()) {
401                 if (node->child1()->result() == NodeResultBoolean) {
402                     // This is necessary in case we have a bytecode instruction implemented by:
403                     //
404                     // a: CompareEq(...)
405                     // b: LogicalNot(@a)
406                     //
407                     // In that case, CompareEq might have a side-effect. Then, we need to make
408                     // sure that we know that Branch does not exit.
409                     fixEdge<KnownBooleanUse>(node->child1());
410                 } else
411                     fixEdge<BooleanUse>(node->child1());
412             } else if (node->child1()->shouldSpeculateObjectOrOther())
413                 fixEdge<ObjectOrOtherUse>(node->child1());
414             else if (node->child1()->shouldSpeculateInt32OrBoolean())
415                 fixIntOrBooleanEdge(node->child1());
416             else if (node->child1()->shouldSpeculateNumber())
417                 fixEdge<DoubleRepUse>(node->child1());
418             else if (node->child1()->shouldSpeculateString())
419                 fixEdge<StringUse>(node->child1());
420             else if (node->child1()->shouldSpeculateStringOrOther())
421                 fixEdge<StringOrOtherUse>(node->child1());
422             break;
423         }
424
425         case CompareEq:
426         case CompareLess:
427         case CompareLessEq:
428         case CompareGreater:
429         case CompareGreaterEq: {
430             if (node->op() == CompareEq
431                 && Node::shouldSpeculateBoolean(node->child1().node(), node->child2().node())) {
432                 fixEdge<BooleanUse>(node->child1());
433                 fixEdge<BooleanUse>(node->child2());
434                 node->clearFlags(NodeMustGenerate);
435                 break;
436             }
437             if (Node::shouldSpeculateInt32OrBoolean(node->child1().node(), node->child2().node())) {
438                 fixIntOrBooleanEdge(node->child1());
439                 fixIntOrBooleanEdge(node->child2());
440                 node->clearFlags(NodeMustGenerate);
441                 break;
442             }
443             if (enableInt52()
444                 && Node::shouldSpeculateAnyInt(node->child1().node(), node->child2().node())) {
445                 fixEdge<Int52RepUse>(node->child1());
446                 fixEdge<Int52RepUse>(node->child2());
447                 node->clearFlags(NodeMustGenerate);
448                 break;
449             }
450             if (Node::shouldSpeculateNumberOrBoolean(node->child1().node(), node->child2().node())) {
451                 fixDoubleOrBooleanEdge(node->child1());
452                 fixDoubleOrBooleanEdge(node->child2());
453             }
454             if (node->op() != CompareEq
455                 && node->child1()->shouldSpeculateNotCell()
456                 && node->child2()->shouldSpeculateNotCell()) {
457                 if (node->child1()->shouldSpeculateNumberOrBoolean())
458                     fixDoubleOrBooleanEdge(node->child1());
459                 else
460                     fixEdge<DoubleRepUse>(node->child1());
461                 if (node->child2()->shouldSpeculateNumberOrBoolean())
462                     fixDoubleOrBooleanEdge(node->child2());
463                 else
464                     fixEdge<DoubleRepUse>(node->child2());
465                 node->clearFlags(NodeMustGenerate);
466                 break;
467             }
468             if (node->child1()->shouldSpeculateStringIdent() && node->child2()->shouldSpeculateStringIdent()) {
469                 fixEdge<StringIdentUse>(node->child1());
470                 fixEdge<StringIdentUse>(node->child2());
471                 node->clearFlags(NodeMustGenerate);
472                 break;
473             }
474             if (node->child1()->shouldSpeculateString() && node->child2()->shouldSpeculateString() && GPRInfo::numberOfRegisters >= 7) {
475                 fixEdge<StringUse>(node->child1());
476                 fixEdge<StringUse>(node->child2());
477                 node->clearFlags(NodeMustGenerate);
478                 break;
479             }
480
481             if (node->op() != CompareEq)
482                 break;
483             if (Node::shouldSpeculateSymbol(node->child1().node(), node->child2().node())) {
484                 fixEdge<SymbolUse>(node->child1());
485                 fixEdge<SymbolUse>(node->child2());
486                 node->clearFlags(NodeMustGenerate);
487                 break;
488             }
489             if (node->child1()->shouldSpeculateObject() && node->child2()->shouldSpeculateObject()) {
490                 fixEdge<ObjectUse>(node->child1());
491                 fixEdge<ObjectUse>(node->child2());
492                 node->clearFlags(NodeMustGenerate);
493                 break;
494             }
495
496             // If either child can be proved to be Null or Undefined, comparing them is greatly simplified.
497             bool oneArgumentIsUsedAsSpecOther = false;
498             if (node->child1()->isUndefinedOrNullConstant()) {
499                 fixEdge<OtherUse>(node->child1());
500                 oneArgumentIsUsedAsSpecOther = true;
501             } else if (node->child1()->shouldSpeculateOther()) {
502                 m_insertionSet.insertNode(m_indexInBlock, SpecNone, Check, node->origin,
503                     Edge(node->child1().node(), OtherUse));
504                 fixEdge<OtherUse>(node->child1());
505                 oneArgumentIsUsedAsSpecOther = true;
506             }
507             if (node->child2()->isUndefinedOrNullConstant()) {
508                 fixEdge<OtherUse>(node->child2());
509                 oneArgumentIsUsedAsSpecOther = true;
510             } else if (node->child2()->shouldSpeculateOther()) {
511                 m_insertionSet.insertNode(m_indexInBlock, SpecNone, Check, node->origin,
512                     Edge(node->child2().node(), OtherUse));
513                 fixEdge<OtherUse>(node->child2());
514                 oneArgumentIsUsedAsSpecOther = true;
515             }
516             if (oneArgumentIsUsedAsSpecOther) {
517                 node->clearFlags(NodeMustGenerate);
518                 break;
519             }
520
521             if (node->child1()->shouldSpeculateObject() && node->child2()->shouldSpeculateObjectOrOther()) {
522                 fixEdge<ObjectUse>(node->child1());
523                 fixEdge<ObjectOrOtherUse>(node->child2());
524                 node->clearFlags(NodeMustGenerate);
525                 break;
526             }
527             if (node->child1()->shouldSpeculateObjectOrOther() && node->child2()->shouldSpeculateObject()) {
528                 fixEdge<ObjectOrOtherUse>(node->child1());
529                 fixEdge<ObjectUse>(node->child2());
530                 node->clearFlags(NodeMustGenerate);
531                 break;
532             }
533
534             break;
535         }
536             
537         case CompareStrictEq: {
538             if (Node::shouldSpeculateBoolean(node->child1().node(), node->child2().node())) {
539                 fixEdge<BooleanUse>(node->child1());
540                 fixEdge<BooleanUse>(node->child2());
541                 break;
542             }
543             if (Node::shouldSpeculateInt32(node->child1().node(), node->child2().node())) {
544                 fixEdge<Int32Use>(node->child1());
545                 fixEdge<Int32Use>(node->child2());
546                 break;
547             }
548             if (enableInt52()
549                 && Node::shouldSpeculateAnyInt(node->child1().node(), node->child2().node())) {
550                 fixEdge<Int52RepUse>(node->child1());
551                 fixEdge<Int52RepUse>(node->child2());
552                 break;
553             }
554             if (Node::shouldSpeculateNumber(node->child1().node(), node->child2().node())) {
555                 fixEdge<DoubleRepUse>(node->child1());
556                 fixEdge<DoubleRepUse>(node->child2());
557                 break;
558             }
559             if (Node::shouldSpeculateSymbol(node->child1().node(), node->child2().node())) {
560                 fixEdge<SymbolUse>(node->child1());
561                 fixEdge<SymbolUse>(node->child2());
562                 break;
563             }
564             if (node->child1()->shouldSpeculateStringIdent() && node->child2()->shouldSpeculateStringIdent()) {
565                 fixEdge<StringIdentUse>(node->child1());
566                 fixEdge<StringIdentUse>(node->child2());
567                 break;
568             }
569             if (node->child1()->shouldSpeculateString() && node->child2()->shouldSpeculateString() && ((GPRInfo::numberOfRegisters >= 7) || isFTL(m_graph.m_plan.mode))) {
570                 fixEdge<StringUse>(node->child1());
571                 fixEdge<StringUse>(node->child2());
572                 break;
573             }
574             WatchpointSet* masqueradesAsUndefinedWatchpoint = m_graph.globalObjectFor(node->origin.semantic)->masqueradesAsUndefinedWatchpoint();
575             if (masqueradesAsUndefinedWatchpoint->isStillValid()) {
576                 
577                 if (node->child1()->shouldSpeculateObject()) {
578                     m_graph.watchpoints().addLazily(masqueradesAsUndefinedWatchpoint);
579                     fixEdge<ObjectUse>(node->child1());
580                     break;
581                 }
582                 if (node->child2()->shouldSpeculateObject()) {
583                     m_graph.watchpoints().addLazily(masqueradesAsUndefinedWatchpoint);
584                     fixEdge<ObjectUse>(node->child2());
585                     break;
586                 }
587                 
588             } else if (node->child1()->shouldSpeculateObject() && node->child2()->shouldSpeculateObject()) {
589                 fixEdge<ObjectUse>(node->child1());
590                 fixEdge<ObjectUse>(node->child2());
591                 break;
592             }
593             if (node->child1()->shouldSpeculateMisc()) {
594                 fixEdge<MiscUse>(node->child1());
595                 break;
596             }
597             if (node->child2()->shouldSpeculateMisc()) {
598                 fixEdge<MiscUse>(node->child2());
599                 break;
600             }
601             if (node->child1()->shouldSpeculateStringIdent()
602                 && node->child2()->shouldSpeculateNotStringVar()) {
603                 fixEdge<StringIdentUse>(node->child1());
604                 fixEdge<NotStringVarUse>(node->child2());
605                 break;
606             }
607             if (node->child2()->shouldSpeculateStringIdent()
608                 && node->child1()->shouldSpeculateNotStringVar()) {
609                 fixEdge<StringIdentUse>(node->child2());
610                 fixEdge<NotStringVarUse>(node->child1());
611                 break;
612             }
613             if (node->child1()->shouldSpeculateString() && ((GPRInfo::numberOfRegisters >= 8) || isFTL(m_graph.m_plan.mode))) {
614                 fixEdge<StringUse>(node->child1());
615                 break;
616             }
617             if (node->child2()->shouldSpeculateString() && ((GPRInfo::numberOfRegisters >= 8) || isFTL(m_graph.m_plan.mode))) {
618                 fixEdge<StringUse>(node->child2());
619                 break;
620             }
621             break;
622         }
623
624         case StringFromCharCode:
625             if (node->child1()->shouldSpeculateInt32())
626                 fixEdge<Int32Use>(node->child1());
627             else
628                 fixEdge<UntypedUse>(node->child1());
629             break;
630
631         case StringCharAt:
632         case StringCharCodeAt: {
633             // Currently we have no good way of refining these.
634             ASSERT(node->arrayMode() == ArrayMode(Array::String));
635             blessArrayOperation(node->child1(), node->child2(), node->child3());
636             fixEdge<KnownCellUse>(node->child1());
637             fixEdge<Int32Use>(node->child2());
638             break;
639         }
640
641         case GetByVal: {
642             if (!node->prediction()) {
643                 m_insertionSet.insertNode(
644                     m_indexInBlock, SpecNone, ForceOSRExit, node->origin);
645             }
646             
647             node->setArrayMode(
648                 node->arrayMode().refine(
649                     m_graph, node,
650                     node->child1()->prediction(),
651                     node->child2()->prediction(),
652                     SpecNone));
653             
654             blessArrayOperation(node->child1(), node->child2(), node->child3());
655             
656             ArrayMode arrayMode = node->arrayMode();
657             switch (arrayMode.type()) {
658             case Array::Contiguous:
659             case Array::Double:
660                 if (arrayMode.arrayClass() == Array::OriginalArray
661                     && arrayMode.speculation() == Array::InBounds) {
662                     JSGlobalObject* globalObject = m_graph.globalObjectFor(node->origin.semantic);
663                     if (globalObject->arrayPrototypeChainIsSane()) {
664                         // Check if SaneChain will work on a per-type basis. Note that:
665                         //
666                         // 1) We don't want double arrays to sometimes return undefined, since
667                         // that would require a change to the return type and it would pessimise
668                         // things a lot. So, we'd only want to do that if we actually had
669                         // evidence that we could read from a hole. That's pretty annoying.
670                         // Likely the best way to handle that case is with an equivalent of
671                         // SaneChain for OutOfBounds. For now we just detect when Undefined and
672                         // NaN are indistinguishable according to backwards propagation, and just
673                         // use SaneChain in that case. This happens to catch a lot of cases.
674                         //
675                         // 2) We don't want int32 array loads to have to do a hole check just to
676                         // coerce to Undefined, since that would mean twice the checks.
677                         //
678                         // This has two implications. First, we have to do more checks than we'd
679                         // like. It's unfortunate that we have to do the hole check. Second,
680                         // some accesses that hit a hole will now need to take the full-blown
681                         // out-of-bounds slow path. We can fix that with:
682                         // https://bugs.webkit.org/show_bug.cgi?id=144668
683                         
684                         bool canDoSaneChain = false;
685                         switch (arrayMode.type()) {
686                         case Array::Contiguous:
687                             // This is happens to be entirely natural. We already would have
688                             // returned any JSValue, and now we'll return Undefined. We still do
689                             // the check but it doesn't require taking any kind of slow path.
690                             canDoSaneChain = true;
691                             break;
692                             
693                         case Array::Double:
694                             if (!(node->flags() & NodeBytecodeUsesAsOther)) {
695                                 // Holes look like NaN already, so if the user doesn't care
696                                 // about the difference between Undefined and NaN then we can
697                                 // do this.
698                                 canDoSaneChain = true;
699                             }
700                             break;
701                             
702                         default:
703                             break;
704                         }
705                         
706                         if (canDoSaneChain) {
707                             m_graph.watchpoints().addLazily(
708                                 globalObject->arrayPrototype()->structure()->transitionWatchpointSet());
709                             m_graph.watchpoints().addLazily(
710                                 globalObject->objectPrototype()->structure()->transitionWatchpointSet());
711                             if (globalObject->arrayPrototypeChainIsSane())
712                                 node->setArrayMode(arrayMode.withSpeculation(Array::SaneChain));
713                         }
714                     }
715                 }
716                 break;
717                 
718             case Array::String:
719                 if ((node->prediction() & ~SpecString)
720                     || m_graph.hasExitSite(node->origin.semantic, OutOfBounds))
721                     node->setArrayMode(arrayMode.withSpeculation(Array::OutOfBounds));
722                 break;
723                 
724             default:
725                 break;
726             }
727             
728             arrayMode = node->arrayMode();
729             switch (arrayMode.type()) {
730             case Array::SelectUsingPredictions:
731             case Array::Unprofiled:
732                 RELEASE_ASSERT_NOT_REACHED();
733                 break;
734             case Array::Generic:
735 #if USE(JSVALUE32_64)
736                 fixEdge<CellUse>(node->child1()); // Speculating cell due to register pressure on 32-bit.
737 #endif
738                 break;
739             case Array::ForceExit:
740                 break;
741             default:
742                 fixEdge<KnownCellUse>(node->child1());
743                 fixEdge<Int32Use>(node->child2());
744                 break;
745             }
746             
747             switch (arrayMode.type()) {
748             case Array::Double:
749                 if (!arrayMode.isOutOfBounds())
750                     node->setResult(NodeResultDouble);
751                 break;
752                 
753             case Array::Float32Array:
754             case Array::Float64Array:
755                 node->setResult(NodeResultDouble);
756                 break;
757                 
758             case Array::Uint32Array:
759                 if (node->shouldSpeculateInt32())
760                     break;
761                 if (node->shouldSpeculateAnyInt() && enableInt52())
762                     node->setResult(NodeResultInt52);
763                 else
764                     node->setResult(NodeResultDouble);
765                 break;
766                 
767             default:
768                 break;
769             }
770             
771             break;
772         }
773
774         case PutByValDirect:
775         case PutByVal:
776         case PutByValAlias: {
777             Edge& child1 = m_graph.varArgChild(node, 0);
778             Edge& child2 = m_graph.varArgChild(node, 1);
779             Edge& child3 = m_graph.varArgChild(node, 2);
780
781             node->setArrayMode(
782                 node->arrayMode().refine(
783                     m_graph, node,
784                     child1->prediction(),
785                     child2->prediction(),
786                     child3->prediction()));
787             
788             blessArrayOperation(child1, child2, m_graph.varArgChild(node, 3));
789             
790             switch (node->arrayMode().modeForPut().type()) {
791             case Array::SelectUsingPredictions:
792             case Array::SelectUsingArguments:
793             case Array::Unprofiled:
794             case Array::Undecided:
795                 RELEASE_ASSERT_NOT_REACHED();
796                 break;
797             case Array::ForceExit:
798             case Array::Generic:
799 #if USE(JSVALUE32_64)
800                 // Due to register pressure on 32-bit, we speculate cell and
801                 // ignore the base-is-not-cell case entirely by letting the
802                 // baseline JIT handle it.
803                 fixEdge<CellUse>(child1);
804 #endif
805                 break;
806             case Array::Int32:
807                 fixEdge<KnownCellUse>(child1);
808                 fixEdge<Int32Use>(child2);
809                 fixEdge<Int32Use>(child3);
810                 break;
811             case Array::Double:
812                 fixEdge<KnownCellUse>(child1);
813                 fixEdge<Int32Use>(child2);
814                 fixEdge<DoubleRepRealUse>(child3);
815                 break;
816             case Array::Int8Array:
817             case Array::Int16Array:
818             case Array::Int32Array:
819             case Array::Uint8Array:
820             case Array::Uint8ClampedArray:
821             case Array::Uint16Array:
822             case Array::Uint32Array:
823                 fixEdge<KnownCellUse>(child1);
824                 fixEdge<Int32Use>(child2);
825                 if (child3->shouldSpeculateInt32())
826                     fixIntOrBooleanEdge(child3);
827                 else if (child3->shouldSpeculateAnyInt())
828                     fixEdge<Int52RepUse>(child3);
829                 else
830                     fixDoubleOrBooleanEdge(child3);
831                 break;
832             case Array::Float32Array:
833             case Array::Float64Array:
834                 fixEdge<KnownCellUse>(child1);
835                 fixEdge<Int32Use>(child2);
836                 fixDoubleOrBooleanEdge(child3);
837                 break;
838             case Array::Contiguous:
839             case Array::ArrayStorage:
840             case Array::SlowPutArrayStorage:
841                 fixEdge<KnownCellUse>(child1);
842                 fixEdge<Int32Use>(child2);
843                 speculateForBarrier(child3);
844                 break;
845             default:
846                 fixEdge<KnownCellUse>(child1);
847                 fixEdge<Int32Use>(child2);
848                 break;
849             }
850             break;
851         }
852             
853         case ArrayPush: {
854             // May need to refine the array mode in case the value prediction contravenes
855             // the array prediction. For example, we may have evidence showing that the
856             // array is in Int32 mode, but the value we're storing is likely to be a double.
857             // Then we should turn this into a conversion to Double array followed by the
858             // push. On the other hand, we absolutely don't want to refine based on the
859             // base prediction. If it has non-cell garbage in it, then we want that to be
860             // ignored. That's because ArrayPush can't handle any array modes that aren't
861             // array-related - so if refine() turned this into a "Generic" ArrayPush then
862             // that would break things.
863             node->setArrayMode(
864                 node->arrayMode().refine(
865                     m_graph, node,
866                     node->child1()->prediction() & SpecCell,
867                     SpecInt32Only,
868                     node->child2()->prediction()));
869             blessArrayOperation(node->child1(), Edge(), node->child3());
870             fixEdge<KnownCellUse>(node->child1());
871             
872             switch (node->arrayMode().type()) {
873             case Array::Int32:
874                 fixEdge<Int32Use>(node->child2());
875                 break;
876             case Array::Double:
877                 fixEdge<DoubleRepRealUse>(node->child2());
878                 break;
879             case Array::Contiguous:
880             case Array::ArrayStorage:
881                 speculateForBarrier(node->child2());
882                 break;
883             default:
884                 break;
885             }
886             break;
887         }
888             
889         case ArrayPop: {
890             blessArrayOperation(node->child1(), Edge(), node->child2());
891             fixEdge<KnownCellUse>(node->child1());
892             break;
893         }
894             
895         case RegExpExec:
896         case RegExpTest: {
897             fixEdge<KnownCellUse>(node->child1());
898             
899             if (node->child2()->shouldSpeculateRegExpObject()) {
900                 fixEdge<RegExpObjectUse>(node->child2());
901
902                 if (node->child3()->shouldSpeculateString())
903                     fixEdge<StringUse>(node->child3());
904             }
905             break;
906         }
907
908         case StringReplace:
909         case StringReplaceRegExp: {
910             if (node->child2()->shouldSpeculateString()) {
911                 m_insertionSet.insertNode(
912                     m_indexInBlock, SpecNone, Check, node->origin,
913                     Edge(node->child2().node(), StringUse));
914                 fixEdge<StringUse>(node->child2());
915             } else if (op == StringReplace) {
916                 if (node->child2()->shouldSpeculateRegExpObject())
917                     addStringReplacePrimordialChecks(node->child2().node());
918                 else 
919                     m_insertionSet.insertNode(
920                         m_indexInBlock, SpecNone, ForceOSRExit, node->origin);
921             }
922
923             if (node->child1()->shouldSpeculateString()
924                 && node->child2()->shouldSpeculateRegExpObject()
925                 && node->child3()->shouldSpeculateString()) {
926
927                 fixEdge<StringUse>(node->child1());
928                 fixEdge<RegExpObjectUse>(node->child2());
929                 fixEdge<StringUse>(node->child3());
930                 break;
931             }
932             break;
933         }
934             
935         case Branch: {
936             if (node->child1()->shouldSpeculateBoolean()) {
937                 if (node->child1()->result() == NodeResultBoolean) {
938                     // This is necessary in case we have a bytecode instruction implemented by:
939                     //
940                     // a: CompareEq(...)
941                     // b: Branch(@a)
942                     //
943                     // In that case, CompareEq might have a side-effect. Then, we need to make
944                     // sure that we know that Branch does not exit.
945                     fixEdge<KnownBooleanUse>(node->child1());
946                 } else
947                     fixEdge<BooleanUse>(node->child1());
948             } else if (node->child1()->shouldSpeculateObjectOrOther())
949                 fixEdge<ObjectOrOtherUse>(node->child1());
950             else if (node->child1()->shouldSpeculateInt32OrBoolean())
951                 fixIntOrBooleanEdge(node->child1());
952             else if (node->child1()->shouldSpeculateNumber())
953                 fixEdge<DoubleRepUse>(node->child1());
954             else if (node->child1()->shouldSpeculateString())
955                 fixEdge<StringUse>(node->child1());
956             else if (node->child1()->shouldSpeculateStringOrOther())
957                 fixEdge<StringOrOtherUse>(node->child1());
958             break;
959         }
960             
961         case Switch: {
962             SwitchData* data = node->switchData();
963             switch (data->kind) {
964             case SwitchImm:
965                 if (node->child1()->shouldSpeculateInt32())
966                     fixEdge<Int32Use>(node->child1());
967                 break;
968             case SwitchChar:
969                 if (node->child1()->shouldSpeculateString())
970                     fixEdge<StringUse>(node->child1());
971                 break;
972             case SwitchString:
973                 if (node->child1()->shouldSpeculateStringIdent())
974                     fixEdge<StringIdentUse>(node->child1());
975                 else if (node->child1()->shouldSpeculateString())
976                     fixEdge<StringUse>(node->child1());
977                 break;
978             case SwitchCell:
979                 if (node->child1()->shouldSpeculateCell())
980                     fixEdge<CellUse>(node->child1());
981                 // else it's fine for this to have UntypedUse; we will handle this by just making
982                 // non-cells take the default case.
983                 break;
984             }
985             break;
986         }
987             
988         case ToPrimitive: {
989             fixupToPrimitive(node);
990             break;
991         }
992             
993         case ToString:
994         case CallStringConstructor: {
995             fixupToStringOrCallStringConstructor(node);
996             break;
997         }
998             
999         case NewStringObject: {
1000             fixEdge<KnownStringUse>(node->child1());
1001             break;
1002         }
1003             
1004         case NewArray: {
1005             watchHavingABadTime(node);
1006             
1007             for (unsigned i = m_graph.varArgNumChildren(node); i--;) {
1008                 node->setIndexingType(
1009                     leastUpperBoundOfIndexingTypeAndType(
1010                         node->indexingType(), m_graph.varArgChild(node, i)->prediction()));
1011             }
1012             switch (node->indexingType()) {
1013             case ALL_BLANK_INDEXING_TYPES:
1014                 CRASH();
1015                 break;
1016             case ALL_UNDECIDED_INDEXING_TYPES:
1017                 if (node->numChildren()) {
1018                     // This will only happen if the children have no type predictions. We
1019                     // would have already exited by now, but insert a forced exit just to
1020                     // be safe.
1021                     m_insertionSet.insertNode(
1022                         m_indexInBlock, SpecNone, ForceOSRExit, node->origin);
1023                 }
1024                 break;
1025             case ALL_INT32_INDEXING_TYPES:
1026                 for (unsigned operandIndex = 0; operandIndex < node->numChildren(); ++operandIndex)
1027                     fixEdge<Int32Use>(m_graph.m_varArgChildren[node->firstChild() + operandIndex]);
1028                 break;
1029             case ALL_DOUBLE_INDEXING_TYPES:
1030                 for (unsigned operandIndex = 0; operandIndex < node->numChildren(); ++operandIndex)
1031                     fixEdge<DoubleRepRealUse>(m_graph.m_varArgChildren[node->firstChild() + operandIndex]);
1032                 break;
1033             case ALL_CONTIGUOUS_INDEXING_TYPES:
1034             case ALL_ARRAY_STORAGE_INDEXING_TYPES:
1035                 break;
1036             default:
1037                 CRASH();
1038                 break;
1039             }
1040             break;
1041         }
1042             
1043         case NewTypedArray: {
1044             watchHavingABadTime(node);
1045             
1046             if (node->child1()->shouldSpeculateInt32()) {
1047                 fixEdge<Int32Use>(node->child1());
1048                 node->clearFlags(NodeMustGenerate);
1049                 break;
1050             }
1051             break;
1052         }
1053             
1054         case NewArrayWithSize: {
1055             watchHavingABadTime(node);
1056             fixEdge<Int32Use>(node->child1());
1057             break;
1058         }
1059
1060         case CallObjectConstructor: {
1061             if (node->child1()->shouldSpeculateObject()) {
1062                 fixEdge<ObjectUse>(node->child1());
1063                 node->convertToIdentity();
1064                 break;
1065             }
1066
1067             fixEdge<UntypedUse>(node->child1());
1068             break;
1069         }
1070
1071         case ToThis: {
1072             fixupToThis(node);
1073             break;
1074         }
1075             
1076         case PutStructure: {
1077             fixEdge<KnownCellUse>(node->child1());
1078             break;
1079         }
1080             
1081         case GetClosureVar:
1082         case GetFromArguments: {
1083             fixEdge<KnownCellUse>(node->child1());
1084             break;
1085         }
1086
1087         case PutClosureVar:
1088         case PutToArguments: {
1089             fixEdge<KnownCellUse>(node->child1());
1090             speculateForBarrier(node->child2());
1091             break;
1092         }
1093
1094         case SkipScope:
1095         case GetScope:
1096         case GetGetter:
1097         case GetSetter:
1098         case GetGlobalObject: {
1099             fixEdge<KnownCellUse>(node->child1());
1100             break;
1101         }
1102             
1103         case AllocatePropertyStorage:
1104         case ReallocatePropertyStorage: {
1105             fixEdge<KnownCellUse>(node->child1());
1106             break;
1107         }
1108
1109         case TryGetById: {
1110             if (node->child1()->shouldSpeculateCell())
1111                 fixEdge<CellUse>(node->child1());
1112             break;
1113         }
1114
1115         case GetById:
1116         case GetByIdFlush: {
1117             // FIXME: This should be done in the ByteCodeParser based on reading the
1118             // PolymorphicAccess, which will surely tell us that this is a AccessCase::ArrayLength.
1119             // https://bugs.webkit.org/show_bug.cgi?id=154990
1120             if (node->child1()->shouldSpeculateCellOrOther()
1121                 && !m_graph.hasExitSite(node->origin.semantic, BadType)
1122                 && !m_graph.hasExitSite(node->origin.semantic, BadCache)
1123                 && !m_graph.hasExitSite(node->origin.semantic, BadIndexingType)
1124                 && !m_graph.hasExitSite(node->origin.semantic, ExoticObjectMode)) {
1125                 
1126                 auto uid = m_graph.identifiers()[node->identifierNumber()];
1127                 
1128                 if (uid == vm().propertyNames->length.impl()) {
1129                     attemptToMakeGetArrayLength(node);
1130                     break;
1131                 }
1132
1133                 if (uid == vm().propertyNames->lastIndex.impl()
1134                     && node->child1()->shouldSpeculateRegExpObject()) {
1135                     node->setOp(GetRegExpObjectLastIndex);
1136                     node->clearFlags(NodeMustGenerate);
1137                     fixEdge<RegExpObjectUse>(node->child1());
1138                     break;
1139                 }
1140             }
1141
1142             if (node->child1()->shouldSpeculateCell())
1143                 fixEdge<CellUse>(node->child1());
1144             break;
1145         }
1146
1147         case PutById:
1148         case PutByIdFlush:
1149         case PutByIdDirect: {
1150             if (node->child1()->shouldSpeculateCellOrOther()
1151                 && !m_graph.hasExitSite(node->origin.semantic, BadType)
1152                 && !m_graph.hasExitSite(node->origin.semantic, BadCache)
1153                 && !m_graph.hasExitSite(node->origin.semantic, BadIndexingType)
1154                 && !m_graph.hasExitSite(node->origin.semantic, ExoticObjectMode)) {
1155                 
1156                 auto uid = m_graph.identifiers()[node->identifierNumber()];
1157                 
1158                 if (uid == vm().propertyNames->lastIndex.impl()
1159                     && node->child1()->shouldSpeculateRegExpObject()) {
1160                     node->setOp(SetRegExpObjectLastIndex);
1161                     fixEdge<RegExpObjectUse>(node->child1());
1162                     speculateForBarrier(node->child2());
1163                     break;
1164                 }
1165             }
1166             
1167             fixEdge<CellUse>(node->child1());
1168             break;
1169         }
1170
1171         case PutGetterById:
1172         case PutSetterById: {
1173             fixEdge<KnownCellUse>(node->child1());
1174             fixEdge<KnownCellUse>(node->child2());
1175             break;
1176         }
1177
1178         case PutGetterSetterById: {
1179             fixEdge<KnownCellUse>(node->child1());
1180             break;
1181         }
1182
1183         case PutGetterByVal:
1184         case PutSetterByVal: {
1185             fixEdge<KnownCellUse>(node->child1());
1186             fixEdge<KnownCellUse>(node->child3());
1187             break;
1188         }
1189
1190         case GetExecutable: {
1191             fixEdge<FunctionUse>(node->child1());
1192             break;
1193         }
1194
1195         case OverridesHasInstance:
1196         case CheckStructure:
1197         case CheckCell:
1198         case CreateThis:
1199         case GetButterfly: {
1200             fixEdge<CellUse>(node->child1());
1201             break;
1202         }
1203
1204         case CheckIdent: {
1205             UniquedStringImpl* uid = node->uidOperand();
1206             if (uid->isSymbol())
1207                 fixEdge<SymbolUse>(node->child1());
1208             else
1209                 fixEdge<StringIdentUse>(node->child1());
1210             break;
1211         }
1212             
1213         case Arrayify:
1214         case ArrayifyToStructure: {
1215             fixEdge<CellUse>(node->child1());
1216             if (node->child2())
1217                 fixEdge<Int32Use>(node->child2());
1218             break;
1219         }
1220             
1221         case GetByOffset:
1222         case GetGetterSetterByOffset: {
1223             if (!node->child1()->hasStorageResult())
1224                 fixEdge<KnownCellUse>(node->child1());
1225             fixEdge<KnownCellUse>(node->child2());
1226             break;
1227         }
1228             
1229         case MultiGetByOffset: {
1230             fixEdge<CellUse>(node->child1());
1231             break;
1232         }
1233             
1234         case PutByOffset: {
1235             if (!node->child1()->hasStorageResult())
1236                 fixEdge<KnownCellUse>(node->child1());
1237             fixEdge<KnownCellUse>(node->child2());
1238             insertInferredTypeCheck(
1239                 m_insertionSet, m_indexInBlock, node->origin, node->child3().node(),
1240                 node->storageAccessData().inferredType);
1241             speculateForBarrier(node->child3());
1242             break;
1243         }
1244             
1245         case MultiPutByOffset: {
1246             fixEdge<CellUse>(node->child1());
1247             speculateForBarrier(node->child2());
1248             break;
1249         }
1250             
1251         case InstanceOf: {
1252             if (!(node->child1()->prediction() & ~SpecCell))
1253                 fixEdge<CellUse>(node->child1());
1254             fixEdge<CellUse>(node->child2());
1255             break;
1256         }
1257
1258         case InstanceOfCustom:
1259             fixEdge<CellUse>(node->child2());
1260             break;
1261
1262         case In: {
1263             // FIXME: We should at some point have array profiling on op_in, in which
1264             // case we would be able to turn this into a kind of GetByVal.
1265             
1266             fixEdge<CellUse>(node->child2());
1267             break;
1268         }
1269
1270         case Check: {
1271             m_graph.doToChildren(
1272                 node,
1273                 [&] (Edge& edge) {
1274                     switch (edge.useKind()) {
1275                     case NumberUse:
1276                         if (edge->shouldSpeculateInt32ForArithmetic())
1277                             edge.setUseKind(Int32Use);
1278                         break;
1279                     default:
1280                         break;
1281                     }
1282                     observeUseKindOnEdge(edge);
1283                 });
1284             break;
1285         }
1286
1287         case Phantom:
1288             // Phantoms are meaningless past Fixup. We recreate them on-demand in the backend.
1289             node->remove();
1290             break;
1291
1292         case FiatInt52: {
1293             RELEASE_ASSERT(enableInt52());
1294             node->convertToIdentity();
1295             fixEdge<Int52RepUse>(node->child1());
1296             node->setResult(NodeResultInt52);
1297             break;
1298         }
1299
1300         case GetArrayLength: {
1301             fixEdge<KnownCellUse>(node->child1());
1302             break;
1303         }
1304
1305         case GetTypedArrayByteOffset: {
1306             fixEdge<KnownCellUse>(node->child1());
1307             break;
1308         }
1309
1310         case Phi:
1311         case Upsilon:
1312         case GetIndexedPropertyStorage:
1313         case LastNodeType:
1314         case CheckTierUpInLoop:
1315         case CheckTierUpAtReturn:
1316         case CheckTierUpAndOSREnter:
1317         case InvalidationPoint:
1318         case CheckArray:
1319         case CheckInBounds:
1320         case ConstantStoragePointer:
1321         case DoubleAsInt32:
1322         case ValueToInt32:
1323         case DoubleRep:
1324         case ValueRep:
1325         case Int52Rep:
1326         case Int52Constant:
1327         case Identity: // This should have been cleaned up.
1328         case BooleanToNumber:
1329         case PhantomNewObject:
1330         case PhantomNewFunction:
1331         case PhantomNewGeneratorFunction:
1332         case PhantomCreateActivation:
1333         case PhantomDirectArguments:
1334         case PhantomClonedArguments:
1335         case ForwardVarargs:
1336         case GetMyArgumentByVal:
1337         case GetMyArgumentByValOutOfBounds:
1338         case PutHint:
1339         case CheckStructureImmediate:
1340         case MaterializeNewObject:
1341         case MaterializeCreateActivation:
1342         case PutStack:
1343         case KillStack:
1344         case GetStack:
1345         case StoreBarrier:
1346         case GetRegExpObjectLastIndex:
1347         case SetRegExpObjectLastIndex:
1348         case RecordRegExpCachedResult:
1349             // These are just nodes that we don't currently expect to see during fixup.
1350             // If we ever wanted to insert them prior to fixup, then we just have to create
1351             // fixup rules for them.
1352             DFG_CRASH(m_graph, node, "Unexpected node during fixup");
1353             break;
1354         
1355         case PutGlobalVariable: {
1356             fixEdge<CellUse>(node->child1());
1357             speculateForBarrier(node->child2());
1358             break;
1359         }
1360
1361         case IsString:
1362             if (node->child1()->shouldSpeculateString()) {
1363                 m_insertionSet.insertNode(
1364                     m_indexInBlock, SpecNone, Check, node->origin,
1365                     Edge(node->child1().node(), StringUse));
1366                 m_graph.convertToConstant(node, jsBoolean(true));
1367                 observeUseKindOnNode<StringUse>(node);
1368             }
1369             break;
1370
1371         case IsObject:
1372             if (node->child1()->shouldSpeculateObject()) {
1373                 m_insertionSet.insertNode(
1374                     m_indexInBlock, SpecNone, Check, node->origin,
1375                     Edge(node->child1().node(), ObjectUse));
1376                 m_graph.convertToConstant(node, jsBoolean(true));
1377                 observeUseKindOnNode<ObjectUse>(node);
1378             }
1379             break;
1380
1381         case GetEnumerableLength: {
1382             fixEdge<CellUse>(node->child1());
1383             break;
1384         }
1385         case HasGenericProperty: {
1386             fixEdge<CellUse>(node->child2());
1387             break;
1388         }
1389         case HasStructureProperty: {
1390             fixEdge<StringUse>(node->child2());
1391             fixEdge<KnownCellUse>(node->child3());
1392             break;
1393         }
1394         case HasIndexedProperty: {
1395             node->setArrayMode(
1396                 node->arrayMode().refine(
1397                     m_graph, node,
1398                     node->child1()->prediction(),
1399                     node->child2()->prediction(),
1400                     SpecNone));
1401             
1402             blessArrayOperation(node->child1(), node->child2(), node->child3());
1403             fixEdge<CellUse>(node->child1());
1404             fixEdge<KnownInt32Use>(node->child2());
1405             break;
1406         }
1407         case GetDirectPname: {
1408             Edge& base = m_graph.varArgChild(node, 0);
1409             Edge& property = m_graph.varArgChild(node, 1);
1410             Edge& index = m_graph.varArgChild(node, 2);
1411             Edge& enumerator = m_graph.varArgChild(node, 3);
1412             fixEdge<CellUse>(base);
1413             fixEdge<KnownCellUse>(property);
1414             fixEdge<KnownInt32Use>(index);
1415             fixEdge<KnownCellUse>(enumerator);
1416             break;
1417         }
1418         case GetPropertyEnumerator: {
1419             fixEdge<CellUse>(node->child1());
1420             break;
1421         }
1422         case GetEnumeratorStructurePname: {
1423             fixEdge<KnownCellUse>(node->child1());
1424             fixEdge<KnownInt32Use>(node->child2());
1425             break;
1426         }
1427         case GetEnumeratorGenericPname: {
1428             fixEdge<KnownCellUse>(node->child1());
1429             fixEdge<KnownInt32Use>(node->child2());
1430             break;
1431         }
1432         case ToIndexString: {
1433             fixEdge<KnownInt32Use>(node->child1());
1434             break;
1435         }
1436         case ProfileType: {
1437             // We want to insert type checks based on the instructionTypeSet of the TypeLocation, not the globalTypeSet.
1438             // Because the instructionTypeSet is contained in globalTypeSet, if we produce a type check for
1439             // type T for the instructionTypeSet, the global type set must also have information for type T.
1440             // So if it the type check succeeds for type T in the instructionTypeSet, a type check for type T 
1441             // in the globalTypeSet would've also succeeded.
1442             // (The other direction does not hold in general).
1443
1444             RefPtr<TypeSet> typeSet = node->typeLocation()->m_instructionTypeSet;
1445             RuntimeTypeMask seenTypes = typeSet->seenTypes();
1446             if (typeSet->doesTypeConformTo(TypeAnyInt)) {
1447                 if (node->child1()->shouldSpeculateInt32())
1448                     fixEdge<Int32Use>(node->child1());
1449                 else
1450                     fixEdge<AnyIntUse>(node->child1());
1451                 node->remove();
1452             } else if (typeSet->doesTypeConformTo(TypeNumber | TypeAnyInt)) {
1453                 fixEdge<NumberUse>(node->child1());
1454                 node->remove();
1455             } else if (typeSet->doesTypeConformTo(TypeString)) {
1456                 fixEdge<StringUse>(node->child1());
1457                 node->remove();
1458             } else if (typeSet->doesTypeConformTo(TypeBoolean)) {
1459                 fixEdge<BooleanUse>(node->child1());
1460                 node->remove();
1461             } else if (typeSet->doesTypeConformTo(TypeUndefined | TypeNull) && (seenTypes & TypeUndefined) && (seenTypes & TypeNull)) {
1462                 fixEdge<OtherUse>(node->child1());
1463                 node->remove();
1464             } else if (typeSet->doesTypeConformTo(TypeObject)) {
1465                 StructureSet set = typeSet->structureSet();
1466                 if (!set.isEmpty()) {
1467                     fixEdge<CellUse>(node->child1());
1468                     node->convertToCheckStructure(m_graph.addStructureSet(set));
1469                 }
1470             }
1471
1472             break;
1473         }
1474
1475         case CreateScopedArguments:
1476         case CreateActivation:
1477         case NewFunction:
1478         case NewGeneratorFunction: {
1479             fixEdge<CellUse>(node->child1());
1480             break;
1481         }
1482
1483         case SetFunctionName: {
1484             // The first child is guaranteed to be a cell because op_set_function_name is only used
1485             // on a newly instantiated function object (the first child).
1486             fixEdge<KnownCellUse>(node->child1());
1487             fixEdge<UntypedUse>(node->child2());
1488             break;
1489         }
1490
1491         case CopyRest: {
1492             fixEdge<KnownCellUse>(node->child1());
1493             fixEdge<KnownInt32Use>(node->child2());
1494             break;
1495         }
1496
1497         case ResolveScope:
1498         case GetDynamicVar:
1499         case PutDynamicVar: {
1500             fixEdge<KnownCellUse>(node->child1());
1501             break;
1502         }
1503
1504 #if !ASSERT_DISABLED
1505         // Have these no-op cases here to ensure that nobody forgets to add handlers for new opcodes.
1506         case SetArgument:
1507         case JSConstant:
1508         case LazyJSConstant:
1509         case DoubleConstant:
1510         case GetLocal:
1511         case GetCallee:
1512         case GetArgumentCount:
1513         case GetRestLength:
1514         case Flush:
1515         case PhantomLocal:
1516         case GetLocalUnlinked:
1517         case GetGlobalVar:
1518         case GetGlobalLexicalVariable:
1519         case NotifyWrite:
1520         case VarInjectionWatchpoint:
1521         case Call:
1522         case CheckTypeInfoFlags:
1523         case TailCallInlinedCaller:
1524         case Construct:
1525         case CallVarargs:
1526         case TailCallVarargsInlinedCaller:
1527         case ConstructVarargs:
1528         case CallForwardVarargs:
1529         case ConstructForwardVarargs:
1530         case TailCallForwardVarargs:
1531         case TailCallForwardVarargsInlinedCaller:
1532         case LoadVarargs:
1533         case ProfileControlFlow:
1534         case NewObject:
1535         case NewArrayBuffer:
1536         case NewRegexp:
1537         case DeleteById:
1538         case DeleteByVal:
1539         case IsArrayObject:
1540         case IsJSArray:
1541         case IsArrayConstructor:
1542         case IsEmpty:
1543         case IsUndefined:
1544         case IsBoolean:
1545         case IsNumber:
1546         case IsObjectOrNull:
1547         case IsFunction:
1548         case IsRegExpObject:
1549         case CreateDirectArguments:
1550         case CreateClonedArguments:
1551         case Jump:
1552         case Return:
1553         case TailCall:
1554         case TailCallVarargs:
1555         case Throw:
1556         case ThrowReferenceError:
1557         case CountExecution:
1558         case ForceOSRExit:
1559         case CheckBadCell:
1560         case CheckNotEmpty:
1561         case CheckWatchdogTimer:
1562         case LogShadowChickenPrologue:
1563         case LogShadowChickenTail:
1564         case Unreachable:
1565         case ExtractOSREntryLocal:
1566         case LoopHint:
1567         case MovHint:
1568         case ZombieHint:
1569         case ExitOK:
1570         case BottomValue:
1571         case TypeOf:
1572         case GetByIdWithThis:
1573         case PutByIdWithThis:
1574         case PutByValWithThis:
1575         case GetByValWithThis:
1576             break;
1577             
1578             break;
1579 #else
1580         default:
1581             break;
1582 #endif
1583         }
1584     }
1585
1586     void watchHavingABadTime(Node* node)
1587     {
1588         JSGlobalObject* globalObject = m_graph.globalObjectFor(node->origin.semantic);
1589
1590         // If this global object is not having a bad time, watch it. We go down this path anytime the code
1591         // does an array allocation. The types of array allocations may change if we start to have a bad
1592         // time. It's easier to reason about this if we know that whenever the types change after we start
1593         // optimizing, the code just gets thrown out. Doing this at FixupPhase is just early enough, since
1594         // prior to this point nobody should have been doing optimizations based on the indexing type of
1595         // the allocation.
1596         if (!globalObject->isHavingABadTime())
1597             m_graph.watchpoints().addLazily(globalObject->havingABadTimeWatchpoint());
1598     }
1599     
1600     template<UseKind useKind>
1601     void createToString(Node* node, Edge& edge)
1602     {
1603         edge.setNode(m_insertionSet.insertNode(
1604             m_indexInBlock, SpecString, ToString, node->origin,
1605             Edge(edge.node(), useKind)));
1606     }
1607     
1608     template<UseKind useKind>
1609     void attemptToForceStringArrayModeByToStringConversion(ArrayMode& arrayMode, Node* node)
1610     {
1611         ASSERT(arrayMode == ArrayMode(Array::Generic));
1612         
1613         if (!m_graph.canOptimizeStringObjectAccess(node->origin.semantic))
1614             return;
1615         
1616         createToString<useKind>(node, node->child1());
1617         arrayMode = ArrayMode(Array::String);
1618     }
1619     
1620     template<UseKind useKind>
1621     bool isStringObjectUse()
1622     {
1623         switch (useKind) {
1624         case StringObjectUse:
1625         case StringOrStringObjectUse:
1626             return true;
1627         default:
1628             return false;
1629         }
1630     }
1631     
1632     template<UseKind useKind>
1633     void convertStringAddUse(Node* node, Edge& edge)
1634     {
1635         if (useKind == StringUse) {
1636             observeUseKindOnNode<StringUse>(edge.node());
1637             m_insertionSet.insertNode(
1638                 m_indexInBlock, SpecNone, Check, node->origin,
1639                 Edge(edge.node(), StringUse));
1640             edge.setUseKind(KnownStringUse);
1641             return;
1642         }
1643         
1644         observeUseKindOnNode<useKind>(edge.node());
1645         createToString<useKind>(node, edge);
1646     }
1647     
1648     void convertToMakeRope(Node* node)
1649     {
1650         node->setOpAndDefaultFlags(MakeRope);
1651         fixupMakeRope(node);
1652     }
1653     
1654     void fixupMakeRope(Node* node)
1655     {
1656         for (unsigned i = 0; i < AdjacencyList::Size; ++i) {
1657             Edge& edge = node->children.child(i);
1658             if (!edge)
1659                 break;
1660             edge.setUseKind(KnownStringUse);
1661             JSString* string = edge->dynamicCastConstant<JSString*>();
1662             if (!string)
1663                 continue;
1664             if (string->length())
1665                 continue;
1666             
1667             // Don't allow the MakeRope to have zero children.
1668             if (!i && !node->child2())
1669                 break;
1670             
1671             node->children.removeEdge(i--);
1672         }
1673         
1674         if (!node->child2()) {
1675             ASSERT(!node->child3());
1676             node->convertToIdentity();
1677         }
1678     }
1679
1680     void fixupToThis(Node* node)
1681     {
1682         ECMAMode ecmaMode = m_graph.executableFor(node->origin.semantic)->isStrictMode() ? StrictMode : NotStrictMode;
1683
1684         if (ecmaMode == StrictMode) {
1685             if (node->child1()->shouldSpeculateBoolean()) {
1686                 fixEdge<BooleanUse>(node->child1());
1687                 node->convertToIdentity();
1688                 return;
1689             }
1690
1691             if (node->child1()->shouldSpeculateInt32()) {
1692                 fixEdge<Int32Use>(node->child1());
1693                 node->convertToIdentity();
1694                 return;
1695             }
1696
1697             if (enableInt52() && node->child1()->shouldSpeculateAnyInt()) {
1698                 fixEdge<Int52RepUse>(node->child1());
1699                 node->convertToIdentity();
1700                 node->setResult(NodeResultInt52);
1701                 return;
1702             }
1703
1704             if (node->child1()->shouldSpeculateNumber()) {
1705                 fixEdge<DoubleRepUse>(node->child1());
1706                 node->convertToIdentity();
1707                 node->setResult(NodeResultDouble);
1708                 return;
1709             }
1710
1711             if (node->child1()->shouldSpeculateSymbol()) {
1712                 fixEdge<SymbolUse>(node->child1());
1713                 node->convertToIdentity();
1714                 return;
1715             }
1716
1717             if (node->child1()->shouldSpeculateStringIdent()) {
1718                 fixEdge<StringIdentUse>(node->child1());
1719                 node->convertToIdentity();
1720                 return;
1721             }
1722
1723             if (node->child1()->shouldSpeculateString()) {
1724                 fixEdge<StringUse>(node->child1());
1725                 node->convertToIdentity();
1726                 return;
1727             }
1728         }
1729
1730         if (node->child1()->shouldSpeculateOther()) {
1731             if (ecmaMode == StrictMode) {
1732                 fixEdge<OtherUse>(node->child1());
1733                 node->convertToIdentity();
1734                 return;
1735             }
1736
1737             m_insertionSet.insertNode(
1738                 m_indexInBlock, SpecNone, Check, node->origin,
1739                 Edge(node->child1().node(), OtherUse));
1740             observeUseKindOnNode<OtherUse>(node->child1().node());
1741             m_graph.convertToConstant(
1742                 node, m_graph.globalThisObjectFor(node->origin.semantic));
1743             return;
1744         }
1745
1746         // FIXME: This should cover other use cases but we don't have use kinds for them. It's not critical,
1747         // however, since we cover all the missing cases in constant folding.
1748         // https://bugs.webkit.org/show_bug.cgi?id=157213
1749         if (node->child1()->shouldSpeculateStringObject()) {
1750             fixEdge<StringObjectUse>(node->child1());
1751             node->convertToIdentity();
1752             return;
1753         }
1754
1755         if (isFinalObjectSpeculation(node->child1()->prediction())) {
1756             fixEdge<FinalObjectUse>(node->child1());
1757             node->convertToIdentity();
1758             return;
1759         }
1760     }
1761     
1762     void fixupToPrimitive(Node* node)
1763     {
1764         if (node->child1()->shouldSpeculateInt32()) {
1765             fixEdge<Int32Use>(node->child1());
1766             node->convertToIdentity();
1767             return;
1768         }
1769         
1770         if (node->child1()->shouldSpeculateString()) {
1771             fixEdge<StringUse>(node->child1());
1772             node->convertToIdentity();
1773             return;
1774         }
1775         
1776         if (node->child1()->shouldSpeculateStringObject()
1777             && m_graph.canOptimizeStringObjectAccess(node->origin.semantic)) {
1778             fixEdge<StringObjectUse>(node->child1());
1779             node->convertToToString();
1780             return;
1781         }
1782         
1783         if (node->child1()->shouldSpeculateStringOrStringObject()
1784             && m_graph.canOptimizeStringObjectAccess(node->origin.semantic)) {
1785             fixEdge<StringOrStringObjectUse>(node->child1());
1786             node->convertToToString();
1787             return;
1788         }
1789     }
1790     
1791     void fixupToStringOrCallStringConstructor(Node* node)
1792     {
1793         if (node->child1()->shouldSpeculateString()) {
1794             fixEdge<StringUse>(node->child1());
1795             node->convertToIdentity();
1796             return;
1797         }
1798         
1799         if (node->child1()->shouldSpeculateStringObject()
1800             && m_graph.canOptimizeStringObjectAccess(node->origin.semantic)) {
1801             fixEdge<StringObjectUse>(node->child1());
1802             return;
1803         }
1804         
1805         if (node->child1()->shouldSpeculateStringOrStringObject()
1806             && m_graph.canOptimizeStringObjectAccess(node->origin.semantic)) {
1807             fixEdge<StringOrStringObjectUse>(node->child1());
1808             return;
1809         }
1810         
1811         if (node->child1()->shouldSpeculateCell()) {
1812             fixEdge<CellUse>(node->child1());
1813             return;
1814         }
1815     }
1816
1817     bool attemptToMakeFastStringAdd(Node* node)
1818     {
1819         bool goodToGo = true;
1820         m_graph.doToChildren(
1821             node,
1822             [&] (Edge& edge) {
1823                 if (edge->shouldSpeculateString())
1824                     return;
1825                 if (m_graph.canOptimizeStringObjectAccess(node->origin.semantic)) {
1826                     if (edge->shouldSpeculateStringObject())
1827                         return;
1828                     if (edge->shouldSpeculateStringOrStringObject())
1829                         return;
1830                 }
1831                 goodToGo = false;
1832             });
1833         if (!goodToGo)
1834             return false;
1835
1836         m_graph.doToChildren(
1837             node,
1838             [&] (Edge& edge) {
1839                 if (edge->shouldSpeculateString()) {
1840                     convertStringAddUse<StringUse>(node, edge);
1841                     return;
1842                 }
1843                 ASSERT(m_graph.canOptimizeStringObjectAccess(node->origin.semantic));
1844                 if (edge->shouldSpeculateStringObject()) {
1845                     convertStringAddUse<StringObjectUse>(node, edge);
1846                     return;
1847                 }
1848                 if (edge->shouldSpeculateStringOrStringObject()) {
1849                     convertStringAddUse<StringOrStringObjectUse>(node, edge);
1850                     return;
1851                 }
1852                 RELEASE_ASSERT_NOT_REACHED();
1853             });
1854         
1855         convertToMakeRope(node);
1856         return true;
1857     }
1858
1859     void fixupGetAndSetLocalsInBlock(BasicBlock* block)
1860     {
1861         if (!block)
1862             return;
1863         ASSERT(block->isReachable);
1864         m_block = block;
1865         for (m_indexInBlock = 0; m_indexInBlock < block->size(); ++m_indexInBlock) {
1866             Node* node = m_currentNode = block->at(m_indexInBlock);
1867             if (node->op() != SetLocal && node->op() != GetLocal)
1868                 continue;
1869             
1870             VariableAccessData* variable = node->variableAccessData();
1871             switch (node->op()) {
1872             case GetLocal:
1873                 switch (variable->flushFormat()) {
1874                 case FlushedDouble:
1875                     node->setResult(NodeResultDouble);
1876                     break;
1877                 case FlushedInt52:
1878                     node->setResult(NodeResultInt52);
1879                     break;
1880                 default:
1881                     break;
1882                 }
1883                 break;
1884                 
1885             case SetLocal:
1886                 // NOTE: Any type checks we put here may get hoisted by fixupChecksInBlock(). So, if we
1887                 // add new type checking use kind for SetLocals, we need to modify that code as well.
1888                 
1889                 switch (variable->flushFormat()) {
1890                 case FlushedJSValue:
1891                     break;
1892                 case FlushedDouble:
1893                     fixEdge<DoubleRepUse>(node->child1());
1894                     break;
1895                 case FlushedInt32:
1896                     fixEdge<Int32Use>(node->child1());
1897                     break;
1898                 case FlushedInt52:
1899                     fixEdge<Int52RepUse>(node->child1());
1900                     break;
1901                 case FlushedCell:
1902                     fixEdge<CellUse>(node->child1());
1903                     break;
1904                 case FlushedBoolean:
1905                     fixEdge<BooleanUse>(node->child1());
1906                     break;
1907                 default:
1908                     RELEASE_ASSERT_NOT_REACHED();
1909                     break;
1910                 }
1911                 break;
1912                 
1913             default:
1914                 RELEASE_ASSERT_NOT_REACHED();
1915                 break;
1916             }
1917         }
1918         m_insertionSet.execute(block);
1919     }
1920     
1921     void addStringReplacePrimordialChecks(Node* searchRegExp)
1922     {
1923         Node* node = m_currentNode;
1924
1925         // Check that structure of searchRegExp is RegExp object
1926         m_insertionSet.insertNode(
1927             m_indexInBlock, SpecNone, Check, node->origin,
1928             Edge(searchRegExp, RegExpObjectUse));
1929
1930         auto emitPrimordialCheckFor = [&] (JSValue primordialProperty, UniquedStringImpl* propertyUID) {
1931             unsigned index = m_graph.identifiers().ensure(propertyUID);
1932
1933             Node* actualProperty = m_insertionSet.insertNode(
1934                 m_indexInBlock, SpecNone, TryGetById, node->origin,
1935                 OpInfo(index), OpInfo(SpecFunction), Edge(searchRegExp, CellUse));
1936
1937             m_insertionSet.insertNode(
1938                 m_indexInBlock, SpecNone, CheckCell, node->origin,
1939                 OpInfo(m_graph.freeze(primordialProperty)), Edge(actualProperty, CellUse));
1940         };
1941
1942         JSGlobalObject* globalObject = m_graph.globalObjectFor(node->origin.semantic);
1943
1944         // Check that searchRegExp.exec is the primordial RegExp.prototype.exec
1945         emitPrimordialCheckFor(globalObject->regExpProtoExecFunction(), vm().propertyNames->exec.impl());
1946         // Check that searchRegExp.global is the primordial RegExp.prototype.global
1947         emitPrimordialCheckFor(globalObject->regExpProtoGlobalGetter(), vm().propertyNames->global.impl());
1948         // Check that searchRegExp.unicode is the primordial RegExp.prototype.unicode
1949         emitPrimordialCheckFor(globalObject->regExpProtoUnicodeGetter(), vm().propertyNames->unicode.impl());
1950         // Check that searchRegExp[Symbol.match] is the primordial RegExp.prototype[Symbol.replace]
1951         emitPrimordialCheckFor(globalObject->regExpProtoSymbolReplaceFunction(), vm().propertyNames->replaceSymbol.impl());
1952     }
1953
1954     Node* checkArray(ArrayMode arrayMode, const NodeOrigin& origin, Node* array, Node* index, bool (*storageCheck)(const ArrayMode&) = canCSEStorage)
1955     {
1956         ASSERT(arrayMode.isSpecific());
1957         
1958         if (arrayMode.type() == Array::String) {
1959             m_insertionSet.insertNode(
1960                 m_indexInBlock, SpecNone, Check, origin, Edge(array, StringUse));
1961         } else {
1962             // Note that we only need to be using a structure check if we opt for SaneChain, since
1963             // that needs to protect against JSArray's __proto__ being changed.
1964             Structure* structure = arrayMode.originalArrayStructure(m_graph, origin.semantic);
1965         
1966             Edge indexEdge = index ? Edge(index, Int32Use) : Edge();
1967             
1968             if (arrayMode.doesConversion()) {
1969                 if (structure) {
1970                     m_insertionSet.insertNode(
1971                         m_indexInBlock, SpecNone, ArrayifyToStructure, origin,
1972                         OpInfo(structure), OpInfo(arrayMode.asWord()), Edge(array, CellUse), indexEdge);
1973                 } else {
1974                     m_insertionSet.insertNode(
1975                         m_indexInBlock, SpecNone, Arrayify, origin,
1976                         OpInfo(arrayMode.asWord()), Edge(array, CellUse), indexEdge);
1977                 }
1978             } else {
1979                 if (structure) {
1980                     m_insertionSet.insertNode(
1981                         m_indexInBlock, SpecNone, CheckStructure, origin,
1982                         OpInfo(m_graph.addStructureSet(structure)), Edge(array, CellUse));
1983                 } else {
1984                     m_insertionSet.insertNode(
1985                         m_indexInBlock, SpecNone, CheckArray, origin,
1986                         OpInfo(arrayMode.asWord()), Edge(array, CellUse));
1987                 }
1988             }
1989         }
1990         
1991         if (!storageCheck(arrayMode))
1992             return 0;
1993         
1994         if (arrayMode.usesButterfly()) {
1995             return m_insertionSet.insertNode(
1996                 m_indexInBlock, SpecNone, GetButterfly, origin, Edge(array, CellUse));
1997         }
1998         
1999         return m_insertionSet.insertNode(
2000             m_indexInBlock, SpecNone, GetIndexedPropertyStorage, origin,
2001             OpInfo(arrayMode.asWord()), Edge(array, KnownCellUse));
2002     }
2003     
2004     void blessArrayOperation(Edge base, Edge index, Edge& storageChild)
2005     {
2006         Node* node = m_currentNode;
2007         
2008         switch (node->arrayMode().type()) {
2009         case Array::ForceExit: {
2010             m_insertionSet.insertNode(
2011                 m_indexInBlock, SpecNone, ForceOSRExit, node->origin);
2012             return;
2013         }
2014             
2015         case Array::SelectUsingPredictions:
2016         case Array::Unprofiled:
2017             RELEASE_ASSERT_NOT_REACHED();
2018             return;
2019             
2020         case Array::Generic:
2021             return;
2022             
2023         default: {
2024             Node* storage = checkArray(node->arrayMode(), node->origin, base.node(), index.node());
2025             if (!storage)
2026                 return;
2027             
2028             storageChild = Edge(storage);
2029             return;
2030         } }
2031     }
2032     
2033     bool alwaysUnboxSimplePrimitives()
2034     {
2035 #if USE(JSVALUE64)
2036         return false;
2037 #else
2038         // Any boolean, int, or cell value is profitable to unbox on 32-bit because it
2039         // reduces traffic.
2040         return true;
2041 #endif
2042     }
2043
2044     template<UseKind useKind>
2045     void observeUseKindOnNode(Node* node)
2046     {
2047         if (useKind == UntypedUse)
2048             return;
2049         observeUseKindOnNode(node, useKind);
2050     }
2051
2052     void observeUseKindOnEdge(Edge edge)
2053     {
2054         observeUseKindOnNode(edge.node(), edge.useKind());
2055     }
2056
2057     void observeUseKindOnNode(Node* node, UseKind useKind)
2058     {
2059         if (node->op() != GetLocal)
2060             return;
2061         
2062         // FIXME: The way this uses alwaysUnboxSimplePrimitives() is suspicious.
2063         // https://bugs.webkit.org/show_bug.cgi?id=121518
2064         
2065         VariableAccessData* variable = node->variableAccessData();
2066         switch (useKind) {
2067         case Int32Use:
2068         case KnownInt32Use:
2069             if (alwaysUnboxSimplePrimitives()
2070                 || isInt32Speculation(variable->prediction()))
2071                 m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
2072             break;
2073         case NumberUse:
2074         case RealNumberUse:
2075         case DoubleRepUse:
2076         case DoubleRepRealUse:
2077             if (variable->doubleFormatState() == UsingDoubleFormat)
2078                 m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
2079             break;
2080         case BooleanUse:
2081         case KnownBooleanUse:
2082             if (alwaysUnboxSimplePrimitives()
2083                 || isBooleanSpeculation(variable->prediction()))
2084                 m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
2085             break;
2086         case Int52RepUse:
2087             if (isAnyIntSpeculation(variable->prediction()))
2088                 m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
2089             break;
2090         case CellUse:
2091         case KnownCellUse:
2092         case ObjectUse:
2093         case FunctionUse:
2094         case StringUse:
2095         case KnownStringUse:
2096         case SymbolUse:
2097         case StringObjectUse:
2098         case StringOrStringObjectUse:
2099             if (alwaysUnboxSimplePrimitives()
2100                 || isCellSpeculation(variable->prediction()))
2101                 m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
2102             break;
2103         default:
2104             break;
2105         }
2106     }
2107     
2108     template<UseKind useKind>
2109     void fixEdge(Edge& edge)
2110     {
2111         observeUseKindOnNode<useKind>(edge.node());
2112         edge.setUseKind(useKind);
2113     }
2114     
2115     void speculateForBarrier(Edge value)
2116     {
2117         // Currently, the DFG won't take advantage of this speculation. But, we want to do it in
2118         // the DFG anyway because if such a speculation would be wrong, we want to know before
2119         // we do an expensive compile.
2120         
2121         if (value->shouldSpeculateInt32()) {
2122             insertCheck<Int32Use>(m_indexInBlock, value.node());
2123             return;
2124         }
2125             
2126         if (value->shouldSpeculateBoolean()) {
2127             insertCheck<BooleanUse>(m_indexInBlock, value.node());
2128             return;
2129         }
2130             
2131         if (value->shouldSpeculateOther()) {
2132             insertCheck<OtherUse>(m_indexInBlock, value.node());
2133             return;
2134         }
2135             
2136         if (value->shouldSpeculateNumber()) {
2137             insertCheck<NumberUse>(m_indexInBlock, value.node());
2138             return;
2139         }
2140             
2141         if (value->shouldSpeculateNotCell()) {
2142             insertCheck<NotCellUse>(m_indexInBlock, value.node());
2143             return;
2144         }
2145     }
2146     
2147     template<UseKind useKind>
2148     void insertCheck(unsigned indexInBlock, Node* node)
2149     {
2150         observeUseKindOnNode<useKind>(node);
2151         m_insertionSet.insertNode(
2152             indexInBlock, SpecNone, Check, m_currentNode->origin, Edge(node, useKind));
2153     }
2154
2155     void fixIntConvertingEdge(Edge& edge)
2156     {
2157         Node* node = edge.node();
2158         if (node->shouldSpeculateInt32OrBoolean()) {
2159             fixIntOrBooleanEdge(edge);
2160             return;
2161         }
2162         
2163         UseKind useKind;
2164         if (node->shouldSpeculateAnyInt())
2165             useKind = Int52RepUse;
2166         else if (node->shouldSpeculateNumber())
2167             useKind = DoubleRepUse;
2168         else
2169             useKind = NotCellUse;
2170         Node* newNode = m_insertionSet.insertNode(
2171             m_indexInBlock, SpecInt32Only, ValueToInt32, m_currentNode->origin,
2172             Edge(node, useKind));
2173         observeUseKindOnNode(node, useKind);
2174         
2175         edge = Edge(newNode, KnownInt32Use);
2176     }
2177     
2178     void fixIntOrBooleanEdge(Edge& edge)
2179     {
2180         Node* node = edge.node();
2181         if (!node->sawBooleans()) {
2182             fixEdge<Int32Use>(edge);
2183             return;
2184         }
2185         
2186         UseKind useKind;
2187         if (node->shouldSpeculateBoolean())
2188             useKind = BooleanUse;
2189         else
2190             useKind = UntypedUse;
2191         Node* newNode = m_insertionSet.insertNode(
2192             m_indexInBlock, SpecInt32Only, BooleanToNumber, m_currentNode->origin,
2193             Edge(node, useKind));
2194         observeUseKindOnNode(node, useKind);
2195         
2196         edge = Edge(newNode, Int32Use);
2197     }
2198     
2199     void fixDoubleOrBooleanEdge(Edge& edge)
2200     {
2201         Node* node = edge.node();
2202         if (!node->sawBooleans()) {
2203             fixEdge<DoubleRepUse>(edge);
2204             return;
2205         }
2206         
2207         UseKind useKind;
2208         if (node->shouldSpeculateBoolean())
2209             useKind = BooleanUse;
2210         else
2211             useKind = UntypedUse;
2212         Node* newNode = m_insertionSet.insertNode(
2213             m_indexInBlock, SpecInt32Only, BooleanToNumber, m_currentNode->origin,
2214             Edge(node, useKind));
2215         observeUseKindOnNode(node, useKind);
2216         
2217         edge = Edge(newNode, DoubleRepUse);
2218     }
2219     
2220     void truncateConstantToInt32(Edge& edge)
2221     {
2222         Node* oldNode = edge.node();
2223         
2224         JSValue value = oldNode->asJSValue();
2225         if (value.isInt32())
2226             return;
2227         
2228         value = jsNumber(JSC::toInt32(value.asNumber()));
2229         ASSERT(value.isInt32());
2230         edge.setNode(m_insertionSet.insertNode(
2231             m_indexInBlock, SpecInt32Only, JSConstant, m_currentNode->origin,
2232             OpInfo(m_graph.freeze(value))));
2233     }
2234     
2235     void truncateConstantsIfNecessary(Node* node, AddSpeculationMode mode)
2236     {
2237         if (mode != SpeculateInt32AndTruncateConstants)
2238             return;
2239         
2240         ASSERT(node->child1()->hasConstant() || node->child2()->hasConstant());
2241         if (node->child1()->hasConstant())
2242             truncateConstantToInt32(node->child1());
2243         else
2244             truncateConstantToInt32(node->child2());
2245     }
2246     
2247     bool attemptToMakeIntegerAdd(Node* node)
2248     {
2249         AddSpeculationMode mode = m_graph.addSpeculationMode(node, FixupPass);
2250         if (mode != DontSpeculateInt32) {
2251             truncateConstantsIfNecessary(node, mode);
2252             fixIntOrBooleanEdge(node->child1());
2253             fixIntOrBooleanEdge(node->child2());
2254             if (bytecodeCanTruncateInteger(node->arithNodeFlags()))
2255                 node->setArithMode(Arith::Unchecked);
2256             else
2257                 node->setArithMode(Arith::CheckOverflow);
2258             return true;
2259         }
2260         
2261         if (m_graph.addShouldSpeculateAnyInt(node)) {
2262             fixEdge<Int52RepUse>(node->child1());
2263             fixEdge<Int52RepUse>(node->child2());
2264             node->setArithMode(Arith::CheckOverflow);
2265             node->setResult(NodeResultInt52);
2266             return true;
2267         }
2268         
2269         return false;
2270     }
2271     
2272     bool attemptToMakeGetArrayLength(Node* node)
2273     {
2274         if (!isInt32Speculation(node->prediction()))
2275             return false;
2276         CodeBlock* profiledBlock = m_graph.baselineCodeBlockFor(node->origin.semantic);
2277         ArrayProfile* arrayProfile = 
2278             profiledBlock->getArrayProfile(node->origin.semantic.bytecodeIndex);
2279         ArrayMode arrayMode = ArrayMode(Array::SelectUsingPredictions);
2280         if (arrayProfile) {
2281             ConcurrentJITLocker locker(profiledBlock->m_lock);
2282             arrayProfile->computeUpdatedPrediction(locker, profiledBlock);
2283             arrayMode = ArrayMode::fromObserved(locker, arrayProfile, Array::Read, false);
2284             if (arrayMode.type() == Array::Unprofiled) {
2285                 // For normal array operations, it makes sense to treat Unprofiled
2286                 // accesses as ForceExit and get more data rather than using
2287                 // predictions and then possibly ending up with a Generic. But here,
2288                 // we treat anything that is Unprofiled as Generic and keep the
2289                 // GetById. I.e. ForceExit = Generic. So, there is no harm - and only
2290                 // profit - from treating the Unprofiled case as
2291                 // SelectUsingPredictions.
2292                 arrayMode = ArrayMode(Array::SelectUsingPredictions);
2293             }
2294         }
2295             
2296         arrayMode = arrayMode.refine(
2297             m_graph, node, node->child1()->prediction(), node->prediction());
2298             
2299         if (arrayMode.type() == Array::Generic) {
2300             // Check if the input is something that we can't get array length for, but for which we
2301             // could insert some conversions in order to transform it into something that we can do it
2302             // for.
2303             if (node->child1()->shouldSpeculateStringObject())
2304                 attemptToForceStringArrayModeByToStringConversion<StringObjectUse>(arrayMode, node);
2305             else if (node->child1()->shouldSpeculateStringOrStringObject())
2306                 attemptToForceStringArrayModeByToStringConversion<StringOrStringObjectUse>(arrayMode, node);
2307         }
2308             
2309         if (!arrayMode.supportsSelfLength())
2310             return false;
2311         
2312         convertToGetArrayLength(node, arrayMode);
2313         return true;
2314     }
2315
2316     void convertToGetArrayLength(Node* node, ArrayMode arrayMode)
2317     {
2318         node->setOp(GetArrayLength);
2319         node->clearFlags(NodeMustGenerate);
2320         fixEdge<KnownCellUse>(node->child1());
2321         node->setArrayMode(arrayMode);
2322             
2323         Node* storage = checkArray(arrayMode, node->origin, node->child1().node(), 0, lengthNeedsStorage);
2324         if (!storage)
2325             return;
2326             
2327         node->child2() = Edge(storage);
2328     }
2329     
2330     Node* prependGetArrayLength(NodeOrigin origin, Node* child, ArrayMode arrayMode)
2331     {
2332         Node* storage = checkArray(arrayMode, origin, child, 0, lengthNeedsStorage);
2333         return m_insertionSet.insertNode(
2334             m_indexInBlock, SpecInt32Only, GetArrayLength, origin,
2335             OpInfo(arrayMode.asWord()), Edge(child, KnownCellUse), Edge(storage));
2336     }
2337     
2338     void fixupChecksInBlock(BasicBlock* block)
2339     {
2340         if (!block)
2341             return;
2342         ASSERT(block->isReachable);
2343         m_block = block;
2344         unsigned indexForChecks = UINT_MAX;
2345         NodeOrigin originForChecks;
2346         for (unsigned indexInBlock = 0; indexInBlock < block->size(); ++indexInBlock) {
2347             Node* node = block->at(indexInBlock);
2348
2349             // If this is a node at which we could exit, then save its index. If nodes after this one
2350             // cannot exit, then we will hoist checks to here.
2351             if (node->origin.exitOK) {
2352                 indexForChecks = indexInBlock;
2353                 originForChecks = node->origin;
2354             }
2355
2356             originForChecks = originForChecks.withSemantic(node->origin.semantic);
2357
2358             // First, try to relax the representational demands of each node, in order to have
2359             // fewer conversions.
2360             switch (node->op()) {
2361             case MovHint:
2362             case Check:
2363                 m_graph.doToChildren(
2364                     node,
2365                     [&] (Edge& edge) {
2366                         switch (edge.useKind()) {
2367                         case DoubleRepUse:
2368                         case DoubleRepRealUse:
2369                             if (edge->hasDoubleResult())
2370                                 break;
2371             
2372                             if (edge->hasInt52Result())
2373                                 edge.setUseKind(Int52RepUse);
2374                             else if (edge.useKind() == DoubleRepUse)
2375                                 edge.setUseKind(NumberUse);
2376                             break;
2377             
2378                         case Int52RepUse:
2379                             // Nothing we can really do.
2380                             break;
2381             
2382                         case UntypedUse:
2383                         case NumberUse:
2384                             if (edge->hasDoubleResult())
2385                                 edge.setUseKind(DoubleRepUse);
2386                             else if (edge->hasInt52Result())
2387                                 edge.setUseKind(Int52RepUse);
2388                             break;
2389             
2390                         case RealNumberUse:
2391                             if (edge->hasDoubleResult())
2392                                 edge.setUseKind(DoubleRepRealUse);
2393                             else if (edge->hasInt52Result())
2394                                 edge.setUseKind(Int52RepUse);
2395                             break;
2396             
2397                         default:
2398                             break;
2399                         }
2400                     });
2401                 break;
2402                 
2403             case ValueToInt32:
2404                 if (node->child1().useKind() == DoubleRepUse
2405                     && !node->child1()->hasDoubleResult()) {
2406                     node->child1().setUseKind(NumberUse);
2407                     break;
2408                 }
2409                 break;
2410                 
2411             default:
2412                 break;
2413             }
2414
2415             // Now, insert type conversions if necessary.
2416             m_graph.doToChildren(
2417                 node,
2418                 [&] (Edge& edge) {
2419                     Node* result = nullptr;
2420
2421                     switch (edge.useKind()) {
2422                     case DoubleRepUse:
2423                     case DoubleRepRealUse:
2424                     case DoubleRepAnyIntUse: {
2425                         if (edge->hasDoubleResult())
2426                             break;
2427             
2428                         if (edge->isNumberConstant()) {
2429                             result = m_insertionSet.insertNode(
2430                                 indexForChecks, SpecBytecodeDouble, DoubleConstant, originForChecks,
2431                                 OpInfo(m_graph.freeze(jsDoubleNumber(edge->asNumber()))));
2432                         } else if (edge->hasInt52Result()) {
2433                             result = m_insertionSet.insertNode(
2434                                 indexForChecks, SpecAnyIntAsDouble, DoubleRep, originForChecks,
2435                                 Edge(edge.node(), Int52RepUse));
2436                         } else {
2437                             UseKind useKind;
2438                             if (edge->shouldSpeculateDoubleReal())
2439                                 useKind = RealNumberUse;
2440                             else if (edge->shouldSpeculateNumber())
2441                                 useKind = NumberUse;
2442                             else
2443                                 useKind = NotCellUse;
2444
2445                             result = m_insertionSet.insertNode(
2446                                 indexForChecks, SpecBytecodeDouble, DoubleRep, originForChecks,
2447                                 Edge(edge.node(), useKind));
2448                         }
2449
2450                         edge.setNode(result);
2451                         break;
2452                     }
2453             
2454                     case Int52RepUse: {
2455                         if (edge->hasInt52Result())
2456                             break;
2457             
2458                         if (edge->isAnyIntConstant()) {
2459                             result = m_insertionSet.insertNode(
2460                                 indexForChecks, SpecAnyInt, Int52Constant, originForChecks,
2461                                 OpInfo(edge->constant()));
2462                         } else if (edge->hasDoubleResult()) {
2463                             result = m_insertionSet.insertNode(
2464                                 indexForChecks, SpecAnyInt, Int52Rep, originForChecks,
2465                                 Edge(edge.node(), DoubleRepAnyIntUse));
2466                         } else if (edge->shouldSpeculateInt32ForArithmetic()) {
2467                             result = m_insertionSet.insertNode(
2468                                 indexForChecks, SpecInt32Only, Int52Rep, originForChecks,
2469                                 Edge(edge.node(), Int32Use));
2470                         } else {
2471                             result = m_insertionSet.insertNode(
2472                                 indexForChecks, SpecAnyInt, Int52Rep, originForChecks,
2473                                 Edge(edge.node(), AnyIntUse));
2474                         }
2475
2476                         edge.setNode(result);
2477                         break;
2478                     }
2479
2480                     default: {
2481                         if (!edge->hasDoubleResult() && !edge->hasInt52Result())
2482                             break;
2483             
2484                         if (edge->hasDoubleResult()) {
2485                             result = m_insertionSet.insertNode(
2486                                 indexForChecks, SpecBytecodeDouble, ValueRep, originForChecks,
2487                                 Edge(edge.node(), DoubleRepUse));
2488                         } else {
2489                             result = m_insertionSet.insertNode(
2490                                 indexForChecks, SpecInt32Only | SpecAnyIntAsDouble, ValueRep,
2491                                 originForChecks, Edge(edge.node(), Int52RepUse));
2492                         }
2493
2494                         edge.setNode(result);
2495                         break;
2496                     } }
2497
2498                     // It's remotely possible that this node cannot do type checks, but we now have a
2499                     // type check on this node. We don't have to handle the general form of this
2500                     // problem. It only arises when ByteCodeParser emits an immediate SetLocal, rather
2501                     // than a delayed one. So, we only worry about those checks that we may have put on
2502                     // a SetLocal. Note that "indexForChecks != indexInBlock" is just another way of
2503                     // saying "!node->origin.exitOK".
2504                     if (indexForChecks != indexInBlock && mayHaveTypeCheck(edge.useKind())) {
2505                         UseKind knownUseKind;
2506                         
2507                         switch (edge.useKind()) {
2508                         case Int32Use:
2509                             knownUseKind = KnownInt32Use;
2510                             break;
2511                         case CellUse:
2512                             knownUseKind = KnownCellUse;
2513                             break;
2514                         case BooleanUse:
2515                             knownUseKind = KnownBooleanUse;
2516                             break;
2517                         default:
2518                             // This can only arise if we have a Check node, and in that case, we can
2519                             // just remove the original check.
2520                             DFG_ASSERT(m_graph, node, node->op() == Check);
2521                             knownUseKind = UntypedUse;
2522                             break;
2523                         }
2524
2525                         m_insertionSet.insertNode(
2526                             indexForChecks, SpecNone, Check, originForChecks, edge);
2527
2528                         edge.setUseKind(knownUseKind);
2529                     }
2530                 });
2531         }
2532         
2533         m_insertionSet.execute(block);
2534     }
2535     
2536     BasicBlock* m_block;
2537     unsigned m_indexInBlock;
2538     Node* m_currentNode;
2539     InsertionSet m_insertionSet;
2540     bool m_profitabilityChanged;
2541 };
2542     
2543 bool performFixup(Graph& graph)
2544 {
2545     return runPhase<FixupPhase>(graph);
2546 }
2547
2548 } } // namespace JSC::DFG
2549
2550 #endif // ENABLE(DFG_JIT)
2551