1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
|
/*
* Copyright (C) 2011-2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DFGPredictionPropagationPhase.h"
#if ENABLE(DFG_JIT)
#include "DFGGraph.h"
#include "DFGPhase.h"
#include "JSCInlines.h"
namespace JSC { namespace DFG {
class PredictionPropagationPhase : public Phase {
public:
PredictionPropagationPhase(Graph& graph)
: Phase(graph, "prediction propagation")
{
}
bool run()
{
ASSERT(m_graph.m_form == ThreadedCPS);
ASSERT(m_graph.m_unificationState == GloballyUnified);
propagateThroughArgumentPositions();
m_pass = PrimaryPass;
propagateToFixpoint();
m_pass = RareCasePass;
propagateToFixpoint();
m_pass = DoubleVotingPass;
do {
m_changed = false;
doRoundOfDoubleVoting();
if (!m_changed)
break;
m_changed = false;
propagateForward();
} while (m_changed);
return true;
}
private:
void propagateToFixpoint()
{
do {
m_changed = false;
// Forward propagation is near-optimal for both topologically-sorted and
// DFS-sorted code.
propagateForward();
if (!m_changed)
break;
// Backward propagation reduces the likelihood that pathological code will
// cause slowness. Loops (especially nested ones) resemble backward flow.
// This pass captures two cases: (1) it detects if the forward fixpoint
// found a sound solution and (2) short-circuits backward flow.
m_changed = false;
propagateBackward();
} while (m_changed);
}
bool setPrediction(SpeculatedType prediction)
{
ASSERT(m_currentNode->hasResult());
// setPrediction() is used when we know that there is no way that we can change
// our minds about what the prediction is going to be. There is no semantic
// difference between setPrediction() and mergeSpeculation() other than the
// increased checking to validate this property.
ASSERT(m_currentNode->prediction() == SpecNone || m_currentNode->prediction() == prediction);
return m_currentNode->predict(prediction);
}
bool mergePrediction(SpeculatedType prediction)
{
ASSERT(m_currentNode->hasResult());
return m_currentNode->predict(prediction);
}
SpeculatedType speculatedDoubleTypeForPrediction(SpeculatedType value)
{
SpeculatedType result = SpecDoubleReal;
if (value & SpecDoubleImpureNaN)
result |= SpecDoubleImpureNaN;
if (value & SpecDoublePureNaN)
result |= SpecDoublePureNaN;
if (!isFullNumberOrBooleanSpeculation(value))
result |= SpecDoublePureNaN;
return result;
}
SpeculatedType speculatedDoubleTypeForPredictions(SpeculatedType left, SpeculatedType right)
{
return speculatedDoubleTypeForPrediction(mergeSpeculations(left, right));
}
void propagate(Node* node)
{
NodeType op = node->op();
bool changed = false;
switch (op) {
case JSConstant: {
SpeculatedType type = speculationFromValue(node->asJSValue());
if (type == SpecInt52AsDouble && enableInt52())
type = SpecInt52;
changed |= setPrediction(type);
break;
}
case DoubleConstant: {
SpeculatedType type = speculationFromValue(node->asJSValue());
changed |= setPrediction(type);
break;
}
case GetLocal: {
VariableAccessData* variable = node->variableAccessData();
SpeculatedType prediction = variable->prediction();
if (!variable->couldRepresentInt52() && (prediction & SpecInt52))
prediction = (prediction | SpecInt52AsDouble) & ~SpecInt52;
if (prediction)
changed |= mergePrediction(prediction);
break;
}
case SetLocal: {
VariableAccessData* variableAccessData = node->variableAccessData();
changed |= variableAccessData->predict(node->child1()->prediction());
break;
}
case BitAnd:
case BitOr:
case BitXor:
case BitRShift:
case BitLShift:
case BitURShift:
case ArithIMul:
case ArithClz32: {
changed |= setPrediction(SpecInt32);
break;
}
case ArrayPop:
case ArrayPush:
case RegExpExec:
case RegExpTest:
case StringReplace:
case GetById:
case GetByIdFlush:
case GetByOffset:
case MultiGetByOffset:
case GetDirectPname:
case Call:
case TailCallInlinedCaller:
case Construct:
case CallVarargs:
case TailCallVarargsInlinedCaller:
case ConstructVarargs:
case CallForwardVarargs:
case ConstructForwardVarargs:
case TailCallForwardVarargsInlinedCaller:
case GetGlobalVar:
case GetGlobalLexicalVariable:
case GetClosureVar:
case GetFromArguments: {
changed |= setPrediction(node->getHeapPrediction());
break;
}
case GetGetterSetterByOffset:
case GetExecutable: {
changed |= setPrediction(SpecCellOther);
break;
}
case GetGetter:
case GetSetter:
case GetCallee:
case NewArrowFunction:
case NewFunction:
case NewGeneratorFunction: {
changed |= setPrediction(SpecFunction);
break;
}
case GetArgumentCount: {
changed |= setPrediction(SpecInt32);
break;
}
case GetRestLength: {
changed |= setPrediction(SpecInt32);
break;
}
case GetTypedArrayByteOffset:
case GetArrayLength: {
changed |= setPrediction(SpecInt32);
break;
}
case StringCharCodeAt: {
changed |= setPrediction(SpecInt32);
break;
}
case UInt32ToNumber: {
// FIXME: Support Int52.
// https://bugs.webkit.org/show_bug.cgi?id=125704
if (node->canSpeculateInt32(m_pass))
changed |= mergePrediction(SpecInt32);
else
changed |= mergePrediction(SpecBytecodeNumber);
break;
}
case ValueAdd: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
if (left && right) {
if (isFullNumberOrBooleanSpeculationExpectingDefined(left)
&& isFullNumberOrBooleanSpeculationExpectingDefined(right)) {
if (m_graph.addSpeculationMode(node, m_pass) != DontSpeculateInt32)
changed |= mergePrediction(SpecInt32);
else if (m_graph.addShouldSpeculateMachineInt(node))
changed |= mergePrediction(SpecInt52);
else
changed |= mergePrediction(speculatedDoubleTypeForPredictions(left, right));
} else if (
!(left & (SpecFullNumber | SpecBoolean))
|| !(right & (SpecFullNumber | SpecBoolean))) {
// left or right is definitely something other than a number.
changed |= mergePrediction(SpecString);
} else
changed |= mergePrediction(SpecString | SpecInt32 | SpecBytecodeDouble);
}
break;
}
case ArithAdd: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
if (left && right) {
if (m_graph.addSpeculationMode(node, m_pass) != DontSpeculateInt32)
changed |= mergePrediction(SpecInt32);
else if (m_graph.addShouldSpeculateMachineInt(node))
changed |= mergePrediction(SpecInt52);
else
changed |= mergePrediction(speculatedDoubleTypeForPredictions(left, right));
}
break;
}
case ArithSub: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
if (left && right) {
if (isFullNumberOrBooleanSpeculationExpectingDefined(left)
&& isFullNumberOrBooleanSpeculationExpectingDefined(right)) {
if (m_graph.addSpeculationMode(node, m_pass) != DontSpeculateInt32)
changed |= mergePrediction(SpecInt32);
else if (m_graph.addShouldSpeculateMachineInt(node))
changed |= mergePrediction(SpecInt52);
else
changed |= mergePrediction(speculatedDoubleTypeForPredictions(left, right));
} else
changed |= mergePrediction(SpecInt32 | SpecBytecodeDouble);
}
break;
}
case ArithNegate:
if (node->child1()->prediction()) {
if (m_graph.unaryArithShouldSpeculateInt32(node, m_pass))
changed |= mergePrediction(SpecInt32);
else if (m_graph.unaryArithShouldSpeculateMachineInt(node, m_pass))
changed |= mergePrediction(SpecInt52);
else
changed |= mergePrediction(speculatedDoubleTypeForPrediction(node->child1()->prediction()));
}
break;
case ArithMin:
case ArithMax: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
if (left && right) {
if (Node::shouldSpeculateInt32OrBooleanForArithmetic(node->child1().node(), node->child2().node())
&& node->canSpeculateInt32(m_pass))
changed |= mergePrediction(SpecInt32);
else
changed |= mergePrediction(speculatedDoubleTypeForPredictions(left, right));
}
break;
}
case ArithMul: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
if (left && right) {
if (isFullNumberOrBooleanSpeculationExpectingDefined(left)
&& isFullNumberOrBooleanSpeculationExpectingDefined(right)) {
if (m_graph.binaryArithShouldSpeculateInt32(node, m_pass))
changed |= mergePrediction(SpecInt32);
else if (m_graph.binaryArithShouldSpeculateMachineInt(node, m_pass))
changed |= mergePrediction(SpecInt52);
else
changed |= mergePrediction(speculatedDoubleTypeForPredictions(left, right));
} else {
if (node->mayHaveNonIntResult())
changed |= mergePrediction(SpecInt32 | SpecBytecodeDouble);
else
changed |= mergePrediction(SpecInt32);
}
}
break;
}
case ArithDiv:
case ArithMod: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
if (left && right) {
if (isFullNumberOrBooleanSpeculationExpectingDefined(left)
&& isFullNumberOrBooleanSpeculationExpectingDefined(right)) {
if (m_graph.binaryArithShouldSpeculateInt32(node, m_pass))
changed |= mergePrediction(SpecInt32);
else
changed |= mergePrediction(SpecBytecodeDouble);
} else
changed |= mergePrediction(SpecInt32 | SpecBytecodeDouble);
}
break;
}
case ArithPow:
case ArithSqrt:
case ArithFRound:
case ArithSin:
case ArithCos:
case ArithLog: {
changed |= setPrediction(SpecBytecodeDouble);
break;
}
case ArithRandom: {
changed |= setPrediction(SpecDoubleReal);
break;
}
case ArithRound:
case ArithFloor:
case ArithCeil: {
if (isInt32OrBooleanSpeculation(node->getHeapPrediction()) && m_graph.roundShouldSpeculateInt32(node, m_pass))
changed |= setPrediction(SpecInt32);
else
changed |= setPrediction(SpecBytecodeDouble);
break;
}
case ArithAbs: {
SpeculatedType child = node->child1()->prediction();
if (isInt32OrBooleanSpeculationForArithmetic(child)
&& node->canSpeculateInt32(m_pass))
changed |= mergePrediction(SpecInt32);
else
changed |= mergePrediction(speculatedDoubleTypeForPrediction(child));
break;
}
case LogicalNot:
case CompareLess:
case CompareLessEq:
case CompareGreater:
case CompareGreaterEq:
case CompareEq:
case CompareStrictEq:
case OverridesHasInstance:
case InstanceOf:
case InstanceOfCustom:
case IsUndefined:
case IsBoolean:
case IsNumber:
case IsString:
case IsObject:
case IsObjectOrNull:
case IsFunction: {
changed |= setPrediction(SpecBoolean);
break;
}
case TypeOf: {
changed |= setPrediction(SpecStringIdent);
break;
}
case GetByVal: {
if (!node->child1()->prediction())
break;
ArrayMode arrayMode = node->arrayMode().refine(
m_graph, node,
node->child1()->prediction(),
node->child2()->prediction(),
SpecNone);
switch (arrayMode.type()) {
case Array::Int32:
if (arrayMode.isOutOfBounds())
changed |= mergePrediction(node->getHeapPrediction() | SpecInt32);
else
changed |= mergePrediction(SpecInt32);
break;
case Array::Double:
if (arrayMode.isOutOfBounds())
changed |= mergePrediction(node->getHeapPrediction() | SpecDoubleReal);
else
changed |= mergePrediction(SpecDoubleReal);
break;
case Array::Float32Array:
case Array::Float64Array:
changed |= mergePrediction(SpecFullDouble);
break;
case Array::Uint32Array:
if (isInt32SpeculationForArithmetic(node->getHeapPrediction()))
changed |= mergePrediction(SpecInt32);
else if (enableInt52())
changed |= mergePrediction(SpecMachineInt);
else
changed |= mergePrediction(SpecInt32 | SpecInt52AsDouble);
break;
case Array::Int8Array:
case Array::Uint8Array:
case Array::Int16Array:
case Array::Uint16Array:
case Array::Int32Array:
changed |= mergePrediction(SpecInt32);
break;
default:
changed |= mergePrediction(node->getHeapPrediction());
break;
}
break;
}
case GetButterfly:
case GetButterflyReadOnly:
case GetIndexedPropertyStorage:
case AllocatePropertyStorage:
case ReallocatePropertyStorage: {
changed |= setPrediction(SpecOther);
break;
}
case ToThis: {
// ToThis in methods for primitive types should speculate primitive types in strict mode.
ECMAMode ecmaMode = m_graph.executableFor(node->origin.semantic)->isStrictMode() ? StrictMode : NotStrictMode;
if (ecmaMode == StrictMode) {
if (node->child1()->shouldSpeculateBoolean()) {
changed |= mergePrediction(SpecBoolean);
break;
}
if (node->child1()->shouldSpeculateInt32()) {
changed |= mergePrediction(SpecInt32);
break;
}
if (enableInt52() && node->child1()->shouldSpeculateMachineInt()) {
changed |= mergePrediction(SpecMachineInt);
break;
}
if (node->child1()->shouldSpeculateNumber()) {
changed |= mergePrediction(SpecMachineInt);
break;
}
if (node->child1()->shouldSpeculateSymbol()) {
changed |= mergePrediction(SpecSymbol);
break;
}
if (node->child1()->shouldSpeculateStringIdent()) {
changed |= mergePrediction(SpecStringIdent);
break;
}
if (node->child1()->shouldSpeculateString()) {
changed |= mergePrediction(SpecString);
break;
}
} else {
if (node->child1()->shouldSpeculateString()) {
changed |= mergePrediction(SpecStringObject);
break;
}
}
SpeculatedType prediction = node->child1()->prediction();
if (prediction) {
if (prediction & ~SpecObject) {
// Wrapper objects are created only in sloppy mode.
if (ecmaMode != StrictMode) {
prediction &= SpecObject;
prediction = mergeSpeculations(prediction, SpecObjectOther);
}
}
changed |= mergePrediction(prediction);
}
break;
}
case SkipScope: {
changed |= setPrediction(SpecObjectOther);
break;
}
case CreateThis:
case NewObject: {
changed |= setPrediction(SpecFinalObject);
break;
}
case NewArray:
case NewArrayWithSize:
case NewArrayBuffer: {
changed |= setPrediction(SpecArray);
break;
}
case NewTypedArray: {
changed |= setPrediction(speculationFromTypedArrayType(node->typedArrayType()));
break;
}
case NewRegexp: {
changed |= setPrediction(SpecRegExpObject);
break;
}
case CreateActivation: {
changed |= setPrediction(SpecObjectOther);
break;
}
case StringFromCharCode: {
changed |= setPrediction(SpecString);
changed |= node->child1()->mergeFlags(NodeBytecodeUsesAsNumber | NodeBytecodeUsesAsInt);
break;
}
case StringCharAt:
case CallStringConstructor:
case ToString:
case MakeRope:
case StrCat: {
changed |= setPrediction(SpecString);
break;
}
case ToPrimitive: {
SpeculatedType child = node->child1()->prediction();
if (child)
changed |= mergePrediction(resultOfToPrimitive(child));
break;
}
case NewStringObject: {
changed |= setPrediction(SpecStringObject);
break;
}
case CreateDirectArguments: {
changed |= setPrediction(SpecDirectArguments);
break;
}
case CreateScopedArguments: {
changed |= setPrediction(SpecScopedArguments);
break;
}
case CreateClonedArguments: {
changed |= setPrediction(SpecObjectOther);
break;
}
case FiatInt52: {
RELEASE_ASSERT(enableInt52());
changed |= setPrediction(SpecMachineInt);
break;
}
case PutByValAlias:
case DoubleAsInt32:
case GetLocalUnlinked:
case CheckArray:
case CheckTypeInfoFlags:
case Arrayify:
case ArrayifyToStructure:
case CheckTierUpInLoop:
case CheckTierUpAtReturn:
case CheckTierUpAndOSREnter:
case CheckTierUpWithNestedTriggerAndOSREnter:
case InvalidationPoint:
case CheckInBounds:
case ValueToInt32:
case DoubleRep:
case ValueRep:
case Int52Rep:
case Int52Constant:
case Identity:
case BooleanToNumber:
case PhantomNewObject:
case PhantomNewFunction:
case PhantomNewGeneratorFunction:
case PhantomCreateActivation:
case PhantomDirectArguments:
case PhantomClonedArguments:
case GetMyArgumentByVal:
case ForwardVarargs:
case PutHint:
case CheckStructureImmediate:
case MaterializeNewObject:
case MaterializeCreateActivation:
case PutStack:
case KillStack:
case StoreBarrier:
case GetStack: {
// This node should never be visible at this stage of compilation. It is
// inserted by fixup(), which follows this phase.
DFG_CRASH(m_graph, node, "Unexpected node during prediction propagation");
break;
}
case Phi:
// Phis should not be visible here since we're iterating the all-but-Phi's
// part of basic blocks.
RELEASE_ASSERT_NOT_REACHED();
break;
case Upsilon:
// These don't get inserted until we go into SSA.
RELEASE_ASSERT_NOT_REACHED();
break;
case GetScope:
changed |= setPrediction(SpecObjectOther);
break;
case In:
changed |= setPrediction(SpecBoolean);
break;
case GetEnumerableLength: {
changed |= setPrediction(SpecInt32);
break;
}
case HasGenericProperty:
case HasStructureProperty:
case HasIndexedProperty: {
changed |= setPrediction(SpecBoolean);
break;
}
case GetPropertyEnumerator: {
changed |= setPrediction(SpecCell);
break;
}
case GetEnumeratorStructurePname: {
changed |= setPrediction(SpecCell | SpecOther);
break;
}
case GetEnumeratorGenericPname: {
changed |= setPrediction(SpecCell | SpecOther);
break;
}
case ToIndexString: {
changed |= setPrediction(SpecString);
break;
}
#ifndef NDEBUG
// These get ignored because they don't return anything.
case PutByValDirect:
case PutByVal:
case PutClosureVar:
case PutToArguments:
case Return:
case TailCall:
case TailCallVarargs:
case TailCallForwardVarargs:
case Throw:
case PutById:
case PutByIdFlush:
case PutByIdDirect:
case PutByOffset:
case MultiPutByOffset:
case PutGetterById:
case PutSetterById:
case PutGetterSetterById:
case PutGetterByVal:
case PutSetterByVal:
case DFG::Jump:
case Branch:
case Switch:
case Breakpoint:
case ProfileWillCall:
case ProfileDidCall:
case ProfileType:
case ProfileControlFlow:
case ThrowReferenceError:
case ForceOSRExit:
case SetArgument:
case CheckStructure:
case CheckCell:
case CheckNotEmpty:
case CheckIdent:
case CheckBadCell:
case PutStructure:
case VarInjectionWatchpoint:
case Phantom:
case Check:
case PutGlobalVariable:
case CheckWatchdogTimer:
case Unreachable:
case LoopHint:
case NotifyWrite:
case ConstantStoragePointer:
case MovHint:
case ZombieHint:
case ExitOK:
case LoadVarargs:
case CopyRest:
break;
// This gets ignored because it only pretends to produce a value.
case BottomValue:
break;
// This gets ignored because it already has a prediction.
case ExtractOSREntryLocal:
break;
// These gets ignored because it doesn't do anything.
case CountExecution:
case PhantomLocal:
case Flush:
break;
case LastNodeType:
RELEASE_ASSERT_NOT_REACHED();
break;
#else
default:
break;
#endif
}
m_changed |= changed;
}
void propagateForward()
{
for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) {
BasicBlock* block = m_graph.block(blockIndex);
if (!block)
continue;
ASSERT(block->isReachable);
for (unsigned i = 0; i < block->size(); ++i) {
m_currentNode = block->at(i);
propagate(m_currentNode);
}
}
}
void propagateBackward()
{
for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
BasicBlock* block = m_graph.block(blockIndex);
if (!block)
continue;
ASSERT(block->isReachable);
for (unsigned i = block->size(); i--;) {
m_currentNode = block->at(i);
propagate(m_currentNode);
}
}
}
void doDoubleVoting(Node* node, float weight)
{
// Loop pre-headers created by OSR entrypoint creation may have NaN weight to indicate
// that we actually don't know they weight. Assume that they execute once. This turns
// out to be an OK assumption since the pre-header doesn't have any meaningful code.
if (weight != weight)
weight = 1;
switch (node->op()) {
case ValueAdd:
case ArithAdd:
case ArithSub: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
DoubleBallot ballot;
if (isFullNumberSpeculation(left)
&& isFullNumberSpeculation(right)
&& !m_graph.addShouldSpeculateInt32(node, m_pass)
&& !m_graph.addShouldSpeculateMachineInt(node))
ballot = VoteDouble;
else
ballot = VoteValue;
m_graph.voteNode(node->child1(), ballot, weight);
m_graph.voteNode(node->child2(), ballot, weight);
break;
}
case ArithMul: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
DoubleBallot ballot;
if (isFullNumberSpeculation(left)
&& isFullNumberSpeculation(right)
&& !m_graph.binaryArithShouldSpeculateInt32(node, m_pass)
&& !m_graph.binaryArithShouldSpeculateMachineInt(node, m_pass))
ballot = VoteDouble;
else
ballot = VoteValue;
m_graph.voteNode(node->child1(), ballot, weight);
m_graph.voteNode(node->child2(), ballot, weight);
break;
}
case ArithMin:
case ArithMax:
case ArithMod:
case ArithDiv: {
SpeculatedType left = node->child1()->prediction();
SpeculatedType right = node->child2()->prediction();
DoubleBallot ballot;
if (isFullNumberSpeculation(left)
&& isFullNumberSpeculation(right)
&& !m_graph.binaryArithShouldSpeculateInt32(node, m_pass))
ballot = VoteDouble;
else
ballot = VoteValue;
m_graph.voteNode(node->child1(), ballot, weight);
m_graph.voteNode(node->child2(), ballot, weight);
break;
}
case ArithAbs:
DoubleBallot ballot;
if (node->child1()->shouldSpeculateNumber()
&& !m_graph.unaryArithShouldSpeculateInt32(node, m_pass))
ballot = VoteDouble;
else
ballot = VoteValue;
m_graph.voteNode(node->child1(), ballot, weight);
break;
case ArithSqrt:
case ArithCos:
case ArithSin:
case ArithLog:
if (node->child1()->shouldSpeculateNumber())
m_graph.voteNode(node->child1(), VoteDouble, weight);
else
m_graph.voteNode(node->child1(), VoteValue, weight);
break;
case SetLocal: {
SpeculatedType prediction = node->child1()->prediction();
if (isDoubleSpeculation(prediction))
node->variableAccessData()->vote(VoteDouble, weight);
else if (
!isFullNumberSpeculation(prediction)
|| isInt32Speculation(prediction) || isMachineIntSpeculation(prediction))
node->variableAccessData()->vote(VoteValue, weight);
break;
}
case PutByValDirect:
case PutByVal:
case PutByValAlias: {
Edge child1 = m_graph.varArgChild(node, 0);
Edge child2 = m_graph.varArgChild(node, 1);
Edge child3 = m_graph.varArgChild(node, 2);
m_graph.voteNode(child1, VoteValue, weight);
m_graph.voteNode(child2, VoteValue, weight);
switch (node->arrayMode().type()) {
case Array::Double:
m_graph.voteNode(child3, VoteDouble, weight);
break;
default:
m_graph.voteNode(child3, VoteValue, weight);
break;
}
break;
}
case MovHint:
// Ignore these since they have no effect on in-DFG execution.
break;
default:
m_graph.voteChildren(node, VoteValue, weight);
break;
}
}
void doRoundOfDoubleVoting()
{
for (unsigned i = 0; i < m_graph.m_variableAccessData.size(); ++i)
m_graph.m_variableAccessData[i].find()->clearVotes();
for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) {
BasicBlock* block = m_graph.block(blockIndex);
if (!block)
continue;
ASSERT(block->isReachable);
for (unsigned i = 0; i < block->size(); ++i) {
m_currentNode = block->at(i);
doDoubleVoting(m_currentNode, block->executionCount);
}
}
for (unsigned i = 0; i < m_graph.m_variableAccessData.size(); ++i) {
VariableAccessData* variableAccessData = &m_graph.m_variableAccessData[i];
if (!variableAccessData->isRoot())
continue;
m_changed |= variableAccessData->tallyVotesForShouldUseDoubleFormat();
}
propagateThroughArgumentPositions();
for (unsigned i = 0; i < m_graph.m_variableAccessData.size(); ++i) {
VariableAccessData* variableAccessData = &m_graph.m_variableAccessData[i];
if (!variableAccessData->isRoot())
continue;
m_changed |= variableAccessData->makePredictionForDoubleFormat();
}
}
void propagateThroughArgumentPositions()
{
for (unsigned i = 0; i < m_graph.m_argumentPositions.size(); ++i)
m_changed |= m_graph.m_argumentPositions[i].mergeArgumentPredictionAwareness();
}
SpeculatedType resultOfToPrimitive(SpeculatedType type)
{
if (type & SpecObject) {
// We try to be optimistic here about StringObjects since it's unlikely that
// someone overrides the valueOf or toString methods.
if (type & SpecStringObject && m_graph.canOptimizeStringObjectAccess(m_currentNode->origin.semantic))
return mergeSpeculations(type & ~SpecObject, SpecString);
return mergeSpeculations(type & ~SpecObject, SpecPrimitive);
}
return type;
}
Node* m_currentNode;
bool m_changed;
PredictionPass m_pass; // We use different logic for considering predictions depending on how far along we are in propagation.
};
bool performPredictionPropagation(Graph& graph)
{
SamplingRegion samplingRegion("DFG Prediction Propagation Phase");
return runPhase<PredictionPropagationPhase>(graph);
}
} } // namespace JSC::DFG
#endif // ENABLE(DFG_JIT)
|