summaryrefslogtreecommitdiff
path: root/js/src/jit/ScalarReplacement.cpp
blob: 9e1485150a9b3a85a217916cf6a445aec55e0e8a (plain)
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
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/ScalarReplacement.h"

#include "mozilla/Vector.h"

#include "jit/IonAnalysis.h"
#include "jit/JitSpewer.h"
#include "jit/MIR.h"
#include "jit/MIRGenerator.h"
#include "jit/MIRGraph.h"
#include "vm/UnboxedObject.h"

#include "jsobjinlines.h"

namespace js {
namespace jit {

template <typename MemoryView>
class EmulateStateOf
{
  private:
    typedef typename MemoryView::BlockState BlockState;

    MIRGenerator* mir_;
    MIRGraph& graph_;

    // Block state at the entrance of all basic blocks.
    Vector<BlockState*, 8, SystemAllocPolicy> states_;

  public:
    EmulateStateOf(MIRGenerator* mir, MIRGraph& graph)
      : mir_(mir),
        graph_(graph)
    {
    }

    bool run(MemoryView& view);
};

template <typename MemoryView>
bool
EmulateStateOf<MemoryView>::run(MemoryView& view)
{
    // Initialize the current block state of each block to an unknown state.
    if (!states_.appendN(nullptr, graph_.numBlocks()))
        return false;

    // Initialize the first block which needs to be traversed in RPO.
    MBasicBlock* startBlock = view.startingBlock();
    if (!view.initStartingState(&states_[startBlock->id()]))
        return false;

    // Iterate over each basic block which has a valid entry state, and merge
    // the state in the successor blocks.
    for (ReversePostorderIterator block = graph_.rpoBegin(startBlock); block != graph_.rpoEnd(); block++) {
        if (mir_->shouldCancel(MemoryView::phaseName))
            return false;

        // Get the block state as the result of the merge of all predecessors
        // which have already been visited in RPO.  This means that backedges
        // are not yet merged into the loop.
        BlockState* state = states_[block->id()];
        if (!state)
            continue;
        view.setEntryBlockState(state);

        // Iterates over resume points, phi and instructions.
        for (MNodeIterator iter(*block); iter; ) {
            // Increment the iterator before visiting the instruction, as the
            // visit function might discard itself from the basic block.
            MNode* ins = *iter++;
            if (ins->isDefinition())
                ins->toDefinition()->accept(&view);
            else
                view.visitResumePoint(ins->toResumePoint());
            if (view.oom())
                return false;
        }

        // For each successor, merge the current state into the state of the
        // successors.
        for (size_t s = 0; s < block->numSuccessors(); s++) {
            MBasicBlock* succ = block->getSuccessor(s);
            if (!view.mergeIntoSuccessorState(*block, succ, &states_[succ->id()]))
                return false;
        }
    }

    states_.clear();
    return true;
}

static bool
IsObjectEscaped(MInstruction* ins, JSObject* objDefault = nullptr);

// Returns False if the lambda is not escaped and if it is optimizable by
// ScalarReplacementOfObject.
static bool
IsLambdaEscaped(MLambda* lambda, JSObject* obj)
{
    JitSpewDef(JitSpew_Escape, "Check lambda\n", lambda);
    JitSpewIndent spewIndent(JitSpew_Escape);

    // The scope chain is not escaped if none of the Lambdas which are
    // capturing it are escaped.
    for (MUseIterator i(lambda->usesBegin()); i != lambda->usesEnd(); i++) {
        MNode* consumer = (*i)->consumer();
        if (!consumer->isDefinition()) {
            // Cannot optimize if it is observable from fun.arguments or others.
            if (!consumer->toResumePoint()->isRecoverableOperand(*i)) {
                JitSpew(JitSpew_Escape, "Observable lambda cannot be recovered");
                return true;
            }
            continue;
        }

        MDefinition* def = consumer->toDefinition();
        if (!def->isFunctionEnvironment()) {
            JitSpewDef(JitSpew_Escape, "is escaped by\n", def);
            return true;
        }

        if (IsObjectEscaped(def->toInstruction(), obj)) {
            JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def);
            return true;
        }
    }
    JitSpew(JitSpew_Escape, "Lambda is not escaped");
    return false;
}

// Returns False if the object is not escaped and if it is optimizable by
// ScalarReplacementOfObject.
//
// For the moment, this code is dumb as it only supports objects which are not
// changing shape, and which are known by TI at the object creation.
static bool
IsObjectEscaped(MInstruction* ins, JSObject* objDefault)
{
    MOZ_ASSERT(ins->type() == MIRType::Object);
    MOZ_ASSERT(ins->isNewObject() || ins->isGuardShape() || ins->isCreateThisWithTemplate() ||
               ins->isNewCallObject() || ins->isFunctionEnvironment());

    JitSpewDef(JitSpew_Escape, "Check object\n", ins);
    JitSpewIndent spewIndent(JitSpew_Escape);

    JSObject* obj = objDefault;
    if (!obj)
        obj = MObjectState::templateObjectOf(ins);

    if (!obj) {
        JitSpew(JitSpew_Escape, "No template object defined.");
        return true;
    }

    // Check if the object is escaped. If the object is not the first argument
    // of either a known Store / Load, then we consider it as escaped. This is a
    // cheap and conservative escape analysis.
    for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) {
        MNode* consumer = (*i)->consumer();
        if (!consumer->isDefinition()) {
            // Cannot optimize if it is observable from fun.arguments or others.
            if (!consumer->toResumePoint()->isRecoverableOperand(*i)) {
                JitSpew(JitSpew_Escape, "Observable object cannot be recovered");
                return true;
            }
            continue;
        }

        MDefinition* def = consumer->toDefinition();
        switch (def->op()) {
          case MDefinition::Op_StoreFixedSlot:
          case MDefinition::Op_LoadFixedSlot:
            // Not escaped if it is the first argument.
            if (def->indexOf(*i) == 0)
                break;

            JitSpewDef(JitSpew_Escape, "is escaped by\n", def);
            return true;

          case MDefinition::Op_LoadUnboxedScalar:
          case MDefinition::Op_StoreUnboxedScalar:
          case MDefinition::Op_LoadUnboxedObjectOrNull:
          case MDefinition::Op_StoreUnboxedObjectOrNull:
          case MDefinition::Op_LoadUnboxedString:
          case MDefinition::Op_StoreUnboxedString:
            // Not escaped if it is the first argument.
            if (def->indexOf(*i) != 0) {
                JitSpewDef(JitSpew_Escape, "is escaped by\n", def);
                return true;
            }

            if (!def->getOperand(1)->isConstant()) {
                JitSpewDef(JitSpew_Escape, "is addressed with unknown index\n", def);
                return true;
            }

            break;

          case MDefinition::Op_PostWriteBarrier:
            break;

          case MDefinition::Op_Slots: {
#ifdef DEBUG
            // Assert that MSlots are only used by MStoreSlot and MLoadSlot.
            MSlots* ins = def->toSlots();
            MOZ_ASSERT(ins->object() != 0);
            for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) {
                // toDefinition should normally never fail, since they don't get
                // captured by resume points.
                MDefinition* def = (*i)->consumer()->toDefinition();
                MOZ_ASSERT(def->op() == MDefinition::Op_StoreSlot ||
                           def->op() == MDefinition::Op_LoadSlot);
            }
#endif
            break;
          }

          case MDefinition::Op_GuardShape: {
            MGuardShape* guard = def->toGuardShape();
            MOZ_ASSERT(!ins->isGuardShape());
            if (obj->maybeShape() != guard->shape()) {
                JitSpewDef(JitSpew_Escape, "has a non-matching guard shape\n", guard);
                return true;
            }
            if (IsObjectEscaped(def->toInstruction(), obj)) {
                JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def);
                return true;
            }
            break;
          }

          case MDefinition::Op_Lambda: {
            MLambda* lambda = def->toLambda();
            if (IsLambdaEscaped(lambda, obj)) {
                JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", lambda);
                return true;
            }
            break;
          }

          // This instruction is a no-op used to verify that scalar replacement
          // is working as expected in jit-test.
          case MDefinition::Op_AssertRecoveredOnBailout:
            break;

          default:
            JitSpewDef(JitSpew_Escape, "is escaped by\n", def);
            return true;
        }
    }

    JitSpew(JitSpew_Escape, "Object is not escaped");
    return false;
}

class ObjectMemoryView : public MDefinitionVisitorDefaultNoop
{
  public:
    typedef MObjectState BlockState;
    static const char* phaseName;

  private:
    TempAllocator& alloc_;
    MConstant* undefinedVal_;
    MInstruction* obj_;
    MBasicBlock* startBlock_;
    BlockState* state_;

    // Used to improve the memory usage by sharing common modification.
    const MResumePoint* lastResumePoint_;

    bool oom_;

  public:
    ObjectMemoryView(TempAllocator& alloc, MInstruction* obj);

    MBasicBlock* startingBlock();
    bool initStartingState(BlockState** pState);

    void setEntryBlockState(BlockState* state);
    bool mergeIntoSuccessorState(MBasicBlock* curr, MBasicBlock* succ, BlockState** pSuccState);

#ifdef DEBUG
    void assertSuccess();
#else
    void assertSuccess() {}
#endif

    bool oom() const { return oom_; }

  public:
    void visitResumePoint(MResumePoint* rp);
    void visitObjectState(MObjectState* ins);
    void visitStoreFixedSlot(MStoreFixedSlot* ins);
    void visitLoadFixedSlot(MLoadFixedSlot* ins);
    void visitPostWriteBarrier(MPostWriteBarrier* ins);
    void visitStoreSlot(MStoreSlot* ins);
    void visitLoadSlot(MLoadSlot* ins);
    void visitGuardShape(MGuardShape* ins);
    void visitFunctionEnvironment(MFunctionEnvironment* ins);
    void visitLambda(MLambda* ins);
    void visitStoreUnboxedScalar(MStoreUnboxedScalar* ins);
    void visitLoadUnboxedScalar(MLoadUnboxedScalar* ins);
    void visitStoreUnboxedObjectOrNull(MStoreUnboxedObjectOrNull* ins);
    void visitLoadUnboxedObjectOrNull(MLoadUnboxedObjectOrNull* ins);
    void visitStoreUnboxedString(MStoreUnboxedString* ins);
    void visitLoadUnboxedString(MLoadUnboxedString* ins);

  private:
    void storeOffset(MInstruction* ins, size_t offset, MDefinition* value);
    void loadOffset(MInstruction* ins, size_t offset);
};

const char* ObjectMemoryView::phaseName = "Scalar Replacement of Object";

ObjectMemoryView::ObjectMemoryView(TempAllocator& alloc, MInstruction* obj)
  : alloc_(alloc),
    obj_(obj),
    startBlock_(obj->block()),
    state_(nullptr),
    lastResumePoint_(nullptr),
    oom_(false)
{
    // Annotate snapshots RValue such that we recover the store first.
    obj_->setIncompleteObject();

    // Annotate the instruction such that we do not replace it by a
    // Magic(JS_OPTIMIZED_OUT) in case of removed uses.
    obj_->setImplicitlyUsedUnchecked();
}

MBasicBlock*
ObjectMemoryView::startingBlock()
{
    return startBlock_;
}

bool
ObjectMemoryView::initStartingState(BlockState** pState)
{
    // Uninitialized slots have an "undefined" value.
    undefinedVal_ = MConstant::New(alloc_, UndefinedValue());
    startBlock_->insertBefore(obj_, undefinedVal_);

    // Create a new block state and insert at it at the location of the new object.
    BlockState* state = BlockState::New(alloc_, obj_);
    if (!state)
        return false;

    startBlock_->insertAfter(obj_, state);

    // Initialize the properties of the object state.
    if (!state->initFromTemplateObject(alloc_, undefinedVal_))
        return false;

    // Hold out of resume point until it is visited.
    state->setInWorklist();

    *pState = state;
    return true;
}

void
ObjectMemoryView::setEntryBlockState(BlockState* state)
{
    state_ = state;
}

bool
ObjectMemoryView::mergeIntoSuccessorState(MBasicBlock* curr, MBasicBlock* succ,
                                          BlockState** pSuccState)
{
    BlockState* succState = *pSuccState;

    // When a block has no state yet, create an empty one for the
    // successor.
    if (!succState) {
        // If the successor is not dominated then the object cannot flow
        // in this basic block without a Phi.  We know that no Phi exist
        // in non-dominated successors as the conservative escaped
        // analysis fails otherwise.  Such condition can succeed if the
        // successor is a join at the end of a if-block and the object
        // only exists within the branch.
        if (!startBlock_->dominates(succ))
            return true;

        // If there is only one predecessor, carry over the last state of the
        // block to the successor.  As the block state is immutable, if the
        // current block has multiple successors, they will share the same entry
        // state.
        if (succ->numPredecessors() <= 1 || !state_->numSlots()) {
            *pSuccState = state_;
            return true;
        }

        // If we have multiple predecessors, then we allocate one Phi node for
        // each predecessor, and create a new block state which only has phi
        // nodes.  These would later be removed by the removal of redundant phi
        // nodes.
        succState = BlockState::Copy(alloc_, state_);
        if (!succState)
            return false;

        size_t numPreds = succ->numPredecessors();
        for (size_t slot = 0; slot < state_->numSlots(); slot++) {
            MPhi* phi = MPhi::New(alloc_);
            if (!phi->reserveLength(numPreds))
                return false;

            // Fill the input of the successors Phi with undefined
            // values, and each block later fills the Phi inputs.
            for (size_t p = 0; p < numPreds; p++)
                phi->addInput(undefinedVal_);

            // Add Phi in the list of Phis of the basic block.
            succ->addPhi(phi);
            succState->setSlot(slot, phi);
        }

        // Insert the newly created block state instruction at the beginning
        // of the successor block, after all the phi nodes.  Note that it
        // would be captured by the entry resume point of the successor
        // block.
        succ->insertBefore(succ->safeInsertTop(), succState);
        *pSuccState = succState;
    }

    MOZ_ASSERT_IF(succ == startBlock_, startBlock_->isLoopHeader());
    if (succ->numPredecessors() > 1 && succState->numSlots() && succ != startBlock_) {
        // We need to re-compute successorWithPhis as the previous EliminatePhis
        // phase might have removed all the Phis from the successor block.
        size_t currIndex;
        MOZ_ASSERT(!succ->phisEmpty());
        if (curr->successorWithPhis()) {
            MOZ_ASSERT(curr->successorWithPhis() == succ);
            currIndex = curr->positionInPhiSuccessor();
        } else {
            currIndex = succ->indexForPredecessor(curr);
            curr->setSuccessorWithPhis(succ, currIndex);
        }
        MOZ_ASSERT(succ->getPredecessor(currIndex) == curr);

        // Copy the current slot states to the index of current block in all the
        // Phi created during the first visit of the successor.
        for (size_t slot = 0; slot < state_->numSlots(); slot++) {
            MPhi* phi = succState->getSlot(slot)->toPhi();
            phi->replaceOperand(currIndex, state_->getSlot(slot));
        }
    }

    return true;
}

#ifdef DEBUG
void
ObjectMemoryView::assertSuccess()
{
    for (MUseIterator i(obj_->usesBegin()); i != obj_->usesEnd(); i++) {
        MNode* ins = (*i)->consumer();
        MDefinition* def = nullptr;

        // Resume points have been replaced by the object state.
        if (ins->isResumePoint() || (def = ins->toDefinition())->isRecoveredOnBailout()) {
            MOZ_ASSERT(obj_->isIncompleteObject());
            continue;
        }

        // The only remaining uses would be removed by DCE, which will also
        // recover the object on bailouts.
        MOZ_ASSERT(def->isSlots() || def->isLambda());
        MOZ_ASSERT(!def->hasDefUses());
    }
}
#endif

void
ObjectMemoryView::visitResumePoint(MResumePoint* rp)
{
    // As long as the MObjectState is not yet seen next to the allocation, we do
    // not patch the resume point to recover the side effects.
    if (!state_->isInWorklist()) {
        rp->addStore(alloc_, state_, lastResumePoint_);
        lastResumePoint_ = rp;
    }
}

void
ObjectMemoryView::visitObjectState(MObjectState* ins)
{
    if (ins->isInWorklist())
        ins->setNotInWorklist();
}

void
ObjectMemoryView::visitStoreFixedSlot(MStoreFixedSlot* ins)
{
    // Skip stores made on other objects.
    if (ins->object() != obj_)
        return;

    // Clone the state and update the slot value.
    if (state_->hasFixedSlot(ins->slot())) {
        state_ = BlockState::Copy(alloc_, state_);
        if (!state_) {
            oom_ = true;
            return;
        }

        state_->setFixedSlot(ins->slot(), ins->value());
        ins->block()->insertBefore(ins->toInstruction(), state_);
    } else {
        // UnsafeSetReserveSlot can access baked-in slots which are guarded by
        // conditions, which are not seen by the escape analysis.
        MBail* bailout = MBail::New(alloc_, Bailout_Inevitable);
        ins->block()->insertBefore(ins, bailout);
    }

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::visitLoadFixedSlot(MLoadFixedSlot* ins)
{
    // Skip loads made on other objects.
    if (ins->object() != obj_)
        return;

    // Replace load by the slot value.
    if (state_->hasFixedSlot(ins->slot())) {
        ins->replaceAllUsesWith(state_->getFixedSlot(ins->slot()));
    } else {
        // UnsafeGetReserveSlot can access baked-in slots which are guarded by
        // conditions, which are not seen by the escape analysis.
        MBail* bailout = MBail::New(alloc_, Bailout_Inevitable);
        ins->block()->insertBefore(ins, bailout);
        ins->replaceAllUsesWith(undefinedVal_);
    }

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::visitPostWriteBarrier(MPostWriteBarrier* ins)
{
    // Skip loads made on other objects.
    if (ins->object() != obj_)
        return;

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::visitStoreSlot(MStoreSlot* ins)
{
    // Skip stores made on other objects.
    MSlots* slots = ins->slots()->toSlots();
    if (slots->object() != obj_) {
        // Guard objects are replaced when they are visited.
        MOZ_ASSERT(!slots->object()->isGuardShape() || slots->object()->toGuardShape()->object() != obj_);
        return;
    }

    // Clone the state and update the slot value.
    if (state_->hasDynamicSlot(ins->slot())) {
        state_ = BlockState::Copy(alloc_, state_);
        if (!state_) {
            oom_ = true;
            return;
        }

        state_->setDynamicSlot(ins->slot(), ins->value());
        ins->block()->insertBefore(ins->toInstruction(), state_);
    } else {
        // UnsafeSetReserveSlot can access baked-in slots which are guarded by
        // conditions, which are not seen by the escape analysis.
        MBail* bailout = MBail::New(alloc_, Bailout_Inevitable);
        ins->block()->insertBefore(ins, bailout);
    }

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::visitLoadSlot(MLoadSlot* ins)
{
    // Skip loads made on other objects.
    MSlots* slots = ins->slots()->toSlots();
    if (slots->object() != obj_) {
        // Guard objects are replaced when they are visited.
        MOZ_ASSERT(!slots->object()->isGuardShape() || slots->object()->toGuardShape()->object() != obj_);
        return;
    }

    // Replace load by the slot value.
    if (state_->hasDynamicSlot(ins->slot())) {
        ins->replaceAllUsesWith(state_->getDynamicSlot(ins->slot()));
    } else {
        // UnsafeGetReserveSlot can access baked-in slots which are guarded by
        // conditions, which are not seen by the escape analysis.
        MBail* bailout = MBail::New(alloc_, Bailout_Inevitable);
        ins->block()->insertBefore(ins, bailout);
        ins->replaceAllUsesWith(undefinedVal_);
    }

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::visitGuardShape(MGuardShape* ins)
{
    // Skip loads made on other objects.
    if (ins->object() != obj_)
        return;

    // Replace the shape guard by its object.
    ins->replaceAllUsesWith(obj_);

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::visitFunctionEnvironment(MFunctionEnvironment* ins)
{
    // Skip function environment which are not aliases of the NewCallObject.
    MDefinition* input = ins->input();
    if (!input->isLambda() || input->toLambda()->environmentChain() != obj_)
        return;

    // Replace the function environment by the scope chain of the lambda.
    ins->replaceAllUsesWith(obj_);

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::visitLambda(MLambda* ins)
{
    if (ins->environmentChain() != obj_)
        return;

    // In order to recover the lambda we need to recover the scope chain, as the
    // lambda is holding it.
    ins->setIncompleteObject();
}

static size_t
GetOffsetOf(MDefinition* index, size_t width, int32_t baseOffset)
{
    int32_t idx = index->toConstant()->toInt32();
    MOZ_ASSERT(idx >= 0);
    MOZ_ASSERT(baseOffset >= 0 && size_t(baseOffset) >= UnboxedPlainObject::offsetOfData());
    return idx * width + baseOffset - UnboxedPlainObject::offsetOfData();
}

static size_t
GetOffsetOf(MDefinition* index, Scalar::Type type, int32_t baseOffset)
{
    return GetOffsetOf(index, Scalar::byteSize(type), baseOffset);
}

void
ObjectMemoryView::storeOffset(MInstruction* ins, size_t offset, MDefinition* value)
{
    // Clone the state and update the slot value.
    MOZ_ASSERT(state_->hasOffset(offset));
    state_ = BlockState::Copy(alloc_, state_);
    if (!state_) {
        oom_ = true;
        return;
    }

    state_->setOffset(offset, value);
    ins->block()->insertBefore(ins, state_);

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::loadOffset(MInstruction* ins, size_t offset)
{
    // Replace load by the slot value.
    MOZ_ASSERT(state_->hasOffset(offset));
    ins->replaceAllUsesWith(state_->getOffset(offset));

    // Remove original instruction.
    ins->block()->discard(ins);
}

void
ObjectMemoryView::visitStoreUnboxedScalar(MStoreUnboxedScalar* ins)
{
    // Skip stores made on other objects.
    if (ins->elements() != obj_)
        return;

    size_t offset = GetOffsetOf(ins->index(), ins->storageType(), ins->offsetAdjustment());
    storeOffset(ins, offset, ins->value());
}

void
ObjectMemoryView::visitLoadUnboxedScalar(MLoadUnboxedScalar* ins)
{
    // Skip loads made on other objects.
    if (ins->elements() != obj_)
        return;

    // Replace load by the slot value.
    size_t offset = GetOffsetOf(ins->index(), ins->storageType(), ins->offsetAdjustment());
    loadOffset(ins, offset);
}

void
ObjectMemoryView::visitStoreUnboxedObjectOrNull(MStoreUnboxedObjectOrNull* ins)
{
    // Skip stores made on other objects.
    if (ins->elements() != obj_)
        return;

    // Clone the state and update the slot value.
    size_t offset = GetOffsetOf(ins->index(), sizeof(uintptr_t), ins->offsetAdjustment());
    storeOffset(ins, offset, ins->value());
}

void
ObjectMemoryView::visitLoadUnboxedObjectOrNull(MLoadUnboxedObjectOrNull* ins)
{
    // Skip loads made on other objects.
    if (ins->elements() != obj_)
        return;

    // Replace load by the slot value.
    size_t offset = GetOffsetOf(ins->index(), sizeof(uintptr_t), ins->offsetAdjustment());
    loadOffset(ins, offset);
}

void
ObjectMemoryView::visitStoreUnboxedString(MStoreUnboxedString* ins)
{
    // Skip stores made on other objects.
    if (ins->elements() != obj_)
        return;

    // Clone the state and update the slot value.
    size_t offset = GetOffsetOf(ins->index(), sizeof(uintptr_t), ins->offsetAdjustment());
    storeOffset(ins, offset, ins->value());
}

void
ObjectMemoryView::visitLoadUnboxedString(MLoadUnboxedString* ins)
{
    // Skip loads made on other objects.
    if (ins->elements() != obj_)
        return;

    // Replace load by the slot value.
    size_t offset = GetOffsetOf(ins->index(), sizeof(uintptr_t), ins->offsetAdjustment());
    loadOffset(ins, offset);
}

static bool
IndexOf(MDefinition* ins, int32_t* res)
{
    MOZ_ASSERT(ins->isLoadElement() || ins->isStoreElement());
    MDefinition* indexDef = ins->getOperand(1); // ins->index();
    if (indexDef->isBoundsCheck())
        indexDef = indexDef->toBoundsCheck()->index();
    if (indexDef->isToInt32())
        indexDef = indexDef->toToInt32()->getOperand(0);
    MConstant* indexDefConst = indexDef->maybeConstantValue();
    if (!indexDefConst || indexDefConst->type() != MIRType::Int32)
        return false;
    *res = indexDefConst->toInt32();
    return true;
}

// Returns False if the elements is not escaped and if it is optimizable by
// ScalarReplacementOfArray.
static bool
IsElementEscaped(MElements* def, uint32_t arraySize)
{
    JitSpewDef(JitSpew_Escape, "Check elements\n", def);
    JitSpewIndent spewIndent(JitSpew_Escape);

    for (MUseIterator i(def->usesBegin()); i != def->usesEnd(); i++) {
        // The MIRType::Elements cannot be captured in a resume point as
        // it does not represent a value allocation.
        MDefinition* access = (*i)->consumer()->toDefinition();

        switch (access->op()) {
          case MDefinition::Op_LoadElement: {
            MOZ_ASSERT(access->toLoadElement()->elements() == def);

            // If we need hole checks, then the array cannot be escaped
            // as the array might refer to the prototype chain to look
            // for properties, thus it might do additional side-effects
            // which are not reflected by the alias set, is we are
            // bailing on holes.
            if (access->toLoadElement()->needsHoleCheck()) {
                JitSpewDef(JitSpew_Escape,
                           "has a load element with a hole check\n", access);
                return true;
            }

            // If the index is not a constant then this index can alias
            // all others. We do not handle this case.
            int32_t index;
            if (!IndexOf(access, &index)) {
                JitSpewDef(JitSpew_Escape,
                           "has a load element with a non-trivial index\n", access);
                return true;
            }
            if (index < 0 || arraySize <= uint32_t(index)) {
                JitSpewDef(JitSpew_Escape,
                           "has a load element with an out-of-bound index\n", access);
                return true;
            }
            break;
          }

          case MDefinition::Op_StoreElement: {
            MOZ_ASSERT(access->toStoreElement()->elements() == def);

            // If we need hole checks, then the array cannot be escaped
            // as the array might refer to the prototype chain to look
            // for properties, thus it might do additional side-effects
            // which are not reflected by the alias set, is we are
            // bailing on holes.
            if (access->toStoreElement()->needsHoleCheck()) {
                JitSpewDef(JitSpew_Escape,
                           "has a store element with a hole check\n", access);
                return true;
            }

            // If the index is not a constant then this index can alias
            // all others. We do not handle this case.
            int32_t index;
            if (!IndexOf(access, &index)) {
                JitSpewDef(JitSpew_Escape, "has a store element with a non-trivial index\n", access);
                return true;
            }
            if (index < 0 || arraySize <= uint32_t(index)) {
                JitSpewDef(JitSpew_Escape, "has a store element with an out-of-bound index\n", access);
                return true;
            }

            // We are not yet encoding magic hole constants in resume points.
            if (access->toStoreElement()->value()->type() == MIRType::MagicHole) {
                JitSpewDef(JitSpew_Escape, "has a store element with an magic-hole constant\n", access);
                return true;
            }
            break;
          }

          case MDefinition::Op_SetInitializedLength:
            MOZ_ASSERT(access->toSetInitializedLength()->elements() == def);
            break;

          case MDefinition::Op_InitializedLength:
            MOZ_ASSERT(access->toInitializedLength()->elements() == def);
            break;

          case MDefinition::Op_ArrayLength:
            MOZ_ASSERT(access->toArrayLength()->elements() == def);
            break;

          default:
            JitSpewDef(JitSpew_Escape, "is escaped by\n", access);
            return true;
        }
    }
    JitSpew(JitSpew_Escape, "Elements is not escaped");
    return false;
}

// Returns False if the array is not escaped and if it is optimizable by
// ScalarReplacementOfArray.
//
// For the moment, this code is dumb as it only supports arrays which are not
// changing length, with only access with known constants.
static bool
IsArrayEscaped(MInstruction* ins)
{
    MOZ_ASSERT(ins->type() == MIRType::Object);
    MOZ_ASSERT(ins->isNewArray());
    uint32_t length = ins->toNewArray()->length();

    JitSpewDef(JitSpew_Escape, "Check array\n", ins);
    JitSpewIndent spewIndent(JitSpew_Escape);

    JSObject* obj = ins->toNewArray()->templateObject();
    if (!obj) {
        JitSpew(JitSpew_Escape, "No template object defined.");
        return true;
    }

    if (length >= 16) {
        JitSpew(JitSpew_Escape, "Array has too many elements");
        return true;
    }

    // Check if the object is escaped. If the object is not the first argument
    // of either a known Store / Load, then we consider it as escaped. This is a
    // cheap and conservative escape analysis.
    for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) {
        MNode* consumer = (*i)->consumer();
        if (!consumer->isDefinition()) {
            // Cannot optimize if it is observable from fun.arguments or others.
            if (!consumer->toResumePoint()->isRecoverableOperand(*i)) {
                JitSpew(JitSpew_Escape, "Observable array cannot be recovered");
                return true;
            }
            continue;
        }

        MDefinition* def = consumer->toDefinition();
        switch (def->op()) {
          case MDefinition::Op_Elements: {
            MElements *elem = def->toElements();
            MOZ_ASSERT(elem->object() == ins);
            if (IsElementEscaped(elem, length)) {
                JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", elem);
                return true;
            }

            break;
          }

          // This instruction is a no-op used to verify that scalar replacement
          // is working as expected in jit-test.
          case MDefinition::Op_AssertRecoveredOnBailout:
            break;

          default:
            JitSpewDef(JitSpew_Escape, "is escaped by\n", def);
            return true;
        }
    }

    JitSpew(JitSpew_Escape, "Array is not escaped");
    return false;
}

// This class replaces every MStoreElement and MSetInitializedLength by an
// MArrayState which emulates the content of the array. All MLoadElement,
// MInitializedLength and MArrayLength are replaced by the corresponding value.
//
// In order to restore the value of the array correctly in case of bailouts, we
// replace all reference of the allocation by the MArrayState definition.
class ArrayMemoryView : public MDefinitionVisitorDefaultNoop
{
  public:
    typedef MArrayState BlockState;
    static const char* phaseName;

  private:
    TempAllocator& alloc_;
    MConstant* undefinedVal_;
    MConstant* length_;
    MInstruction* arr_;
    MBasicBlock* startBlock_;
    BlockState* state_;

    // Used to improve the memory usage by sharing common modification.
    const MResumePoint* lastResumePoint_;

    bool oom_;

  public:
    ArrayMemoryView(TempAllocator& alloc, MInstruction* arr);

    MBasicBlock* startingBlock();
    bool initStartingState(BlockState** pState);

    void setEntryBlockState(BlockState* state);
    bool mergeIntoSuccessorState(MBasicBlock* curr, MBasicBlock* succ, BlockState** pSuccState);

#ifdef DEBUG
    void assertSuccess();
#else
    void assertSuccess() {}
#endif

    bool oom() const { return oom_; }

  private:
    bool isArrayStateElements(MDefinition* elements);
    void discardInstruction(MInstruction* ins, MDefinition* elements);

  public:
    void visitResumePoint(MResumePoint* rp);
    void visitArrayState(MArrayState* ins);
    void visitStoreElement(MStoreElement* ins);
    void visitLoadElement(MLoadElement* ins);
    void visitSetInitializedLength(MSetInitializedLength* ins);
    void visitInitializedLength(MInitializedLength* ins);
    void visitArrayLength(MArrayLength* ins);
};

const char* ArrayMemoryView::phaseName = "Scalar Replacement of Array";

ArrayMemoryView::ArrayMemoryView(TempAllocator& alloc, MInstruction* arr)
  : alloc_(alloc),
    undefinedVal_(nullptr),
    length_(nullptr),
    arr_(arr),
    startBlock_(arr->block()),
    state_(nullptr),
    lastResumePoint_(nullptr),
    oom_(false)
{
    // Annotate snapshots RValue such that we recover the store first.
    arr_->setIncompleteObject();

    // Annotate the instruction such that we do not replace it by a
    // Magic(JS_OPTIMIZED_OUT) in case of removed uses.
    arr_->setImplicitlyUsedUnchecked();
}

MBasicBlock*
ArrayMemoryView::startingBlock()
{
    return startBlock_;
}

bool
ArrayMemoryView::initStartingState(BlockState** pState)
{
    // Uninitialized elements have an "undefined" value.
    undefinedVal_ = MConstant::New(alloc_, UndefinedValue());
    MConstant* initLength = MConstant::New(alloc_, Int32Value(0));
    arr_->block()->insertBefore(arr_, undefinedVal_);
    arr_->block()->insertBefore(arr_, initLength);

    // Create a new block state and insert at it at the location of the new array.
    BlockState* state = BlockState::New(alloc_, arr_, undefinedVal_, initLength);
    if (!state)
        return false;

    startBlock_->insertAfter(arr_, state);

    // Hold out of resume point until it is visited.
    state->setInWorklist();

    *pState = state;
    return true;
}

void
ArrayMemoryView::setEntryBlockState(BlockState* state)
{
    state_ = state;
}

bool
ArrayMemoryView::mergeIntoSuccessorState(MBasicBlock* curr, MBasicBlock* succ,
                                          BlockState** pSuccState)
{
    BlockState* succState = *pSuccState;

    // When a block has no state yet, create an empty one for the
    // successor.
    if (!succState) {
        // If the successor is not dominated then the array cannot flow
        // in this basic block without a Phi.  We know that no Phi exist
        // in non-dominated successors as the conservative escaped
        // analysis fails otherwise.  Such condition can succeed if the
        // successor is a join at the end of a if-block and the array
        // only exists within the branch.
        if (!startBlock_->dominates(succ))
            return true;

        // If there is only one predecessor, carry over the last state of the
        // block to the successor.  As the block state is immutable, if the
        // current block has multiple successors, they will share the same entry
        // state.
        if (succ->numPredecessors() <= 1 || !state_->numElements()) {
            *pSuccState = state_;
            return true;
        }

        // If we have multiple predecessors, then we allocate one Phi node for
        // each predecessor, and create a new block state which only has phi
        // nodes.  These would later be removed by the removal of redundant phi
        // nodes.
        succState = BlockState::Copy(alloc_, state_);
        if (!succState)
            return false;

        size_t numPreds = succ->numPredecessors();
        for (size_t index = 0; index < state_->numElements(); index++) {
            MPhi* phi = MPhi::New(alloc_);
            if (!phi->reserveLength(numPreds))
                return false;

            // Fill the input of the successors Phi with undefined
            // values, and each block later fills the Phi inputs.
            for (size_t p = 0; p < numPreds; p++)
                phi->addInput(undefinedVal_);

            // Add Phi in the list of Phis of the basic block.
            succ->addPhi(phi);
            succState->setElement(index, phi);
        }

        // Insert the newly created block state instruction at the beginning
        // of the successor block, after all the phi nodes.  Note that it
        // would be captured by the entry resume point of the successor
        // block.
        succ->insertBefore(succ->safeInsertTop(), succState);
        *pSuccState = succState;
    }

    MOZ_ASSERT_IF(succ == startBlock_, startBlock_->isLoopHeader());
    if (succ->numPredecessors() > 1 && succState->numElements() && succ != startBlock_) {
        // We need to re-compute successorWithPhis as the previous EliminatePhis
        // phase might have removed all the Phis from the successor block.
        size_t currIndex;
        MOZ_ASSERT(!succ->phisEmpty());
        if (curr->successorWithPhis()) {
            MOZ_ASSERT(curr->successorWithPhis() == succ);
            currIndex = curr->positionInPhiSuccessor();
        } else {
            currIndex = succ->indexForPredecessor(curr);
            curr->setSuccessorWithPhis(succ, currIndex);
        }
        MOZ_ASSERT(succ->getPredecessor(currIndex) == curr);

        // Copy the current element states to the index of current block in all
        // the Phi created during the first visit of the successor.
        for (size_t index = 0; index < state_->numElements(); index++) {
            MPhi* phi = succState->getElement(index)->toPhi();
            phi->replaceOperand(currIndex, state_->getElement(index));
        }
    }

    return true;
}

#ifdef DEBUG
void
ArrayMemoryView::assertSuccess()
{
    MOZ_ASSERT(!arr_->hasLiveDefUses());
}
#endif

void
ArrayMemoryView::visitResumePoint(MResumePoint* rp)
{
    // As long as the MArrayState is not yet seen next to the allocation, we do
    // not patch the resume point to recover the side effects.
    if (!state_->isInWorklist()) {
        rp->addStore(alloc_, state_, lastResumePoint_);
        lastResumePoint_ = rp;
    }
}

void
ArrayMemoryView::visitArrayState(MArrayState* ins)
{
    if (ins->isInWorklist())
        ins->setNotInWorklist();
}

bool
ArrayMemoryView::isArrayStateElements(MDefinition* elements)
{
    return elements->isElements() && elements->toElements()->object() == arr_;
}

void
ArrayMemoryView::discardInstruction(MInstruction* ins, MDefinition* elements)
{
    MOZ_ASSERT(elements->isElements());
    ins->block()->discard(ins);
    if (!elements->hasLiveDefUses())
        elements->block()->discard(elements->toInstruction());
}

void
ArrayMemoryView::visitStoreElement(MStoreElement* ins)
{
    // Skip other array objects.
    MDefinition* elements = ins->elements();
    if (!isArrayStateElements(elements))
        return;

    // Register value of the setter in the state.
    int32_t index;
    MOZ_ALWAYS_TRUE(IndexOf(ins, &index));
    state_ = BlockState::Copy(alloc_, state_);
    if (!state_) {
        oom_ = true;
        return;
    }

    state_->setElement(index, ins->value());
    ins->block()->insertBefore(ins, state_);

    // Remove original instruction.
    discardInstruction(ins, elements);
}

void
ArrayMemoryView::visitLoadElement(MLoadElement* ins)
{
    // Skip other array objects.
    MDefinition* elements = ins->elements();
    if (!isArrayStateElements(elements))
        return;

    // Replace by the value contained at the index.
    int32_t index;
    MOZ_ALWAYS_TRUE(IndexOf(ins, &index));
    ins->replaceAllUsesWith(state_->getElement(index));

    // Remove original instruction.
    discardInstruction(ins, elements);
}

void
ArrayMemoryView::visitSetInitializedLength(MSetInitializedLength* ins)
{
    // Skip other array objects.
    MDefinition* elements = ins->elements();
    if (!isArrayStateElements(elements))
        return;

    // Replace by the new initialized length.  Note that the argument of
    // MSetInitalizedLength is the last index and not the initialized length.
    // To obtain the length, we need to add 1 to it, and thus we need to create
    // a new constant that we register in the ArrayState.
    state_ = BlockState::Copy(alloc_, state_);
    if (!state_) {
        oom_ = true;
        return;
    }

    int32_t initLengthValue = ins->index()->maybeConstantValue()->toInt32() + 1;
    MConstant* initLength = MConstant::New(alloc_, Int32Value(initLengthValue));
    ins->block()->insertBefore(ins, initLength);
    ins->block()->insertBefore(ins, state_);
    state_->setInitializedLength(initLength);

    // Remove original instruction.
    discardInstruction(ins, elements);
}

void
ArrayMemoryView::visitInitializedLength(MInitializedLength* ins)
{
    // Skip other array objects.
    MDefinition* elements = ins->elements();
    if (!isArrayStateElements(elements))
        return;

    // Replace by the value of the length.
    ins->replaceAllUsesWith(state_->initializedLength());

    // Remove original instruction.
    discardInstruction(ins, elements);
}

void
ArrayMemoryView::visitArrayLength(MArrayLength* ins)
{
    // Skip other array objects.
    MDefinition* elements = ins->elements();
    if (!isArrayStateElements(elements))
        return;

    // Replace by the value of the length.
    if (!length_) {
        length_ = MConstant::New(alloc_, Int32Value(state_->numElements()));
        arr_->block()->insertBefore(arr_, length_);
    }
    ins->replaceAllUsesWith(length_);

    // Remove original instruction.
    discardInstruction(ins, elements);
}

bool
ScalarReplacement(MIRGenerator* mir, MIRGraph& graph)
{
    EmulateStateOf<ObjectMemoryView> replaceObject(mir, graph);
    EmulateStateOf<ArrayMemoryView> replaceArray(mir, graph);
    bool addedPhi = false;

    for (ReversePostorderIterator block = graph.rpoBegin(); block != graph.rpoEnd(); block++) {
        if (mir->shouldCancel("Scalar Replacement (main loop)"))
            return false;

        for (MInstructionIterator ins = block->begin(); ins != block->end(); ins++) {
            if ((ins->isNewObject() || ins->isCreateThisWithTemplate() || ins->isNewCallObject()) &&
                !IsObjectEscaped(*ins))
            {
                ObjectMemoryView view(graph.alloc(), *ins);
                if (!replaceObject.run(view))
                    return false;
                view.assertSuccess();
                addedPhi = true;
                continue;
            }

            if (ins->isNewArray() && !IsArrayEscaped(*ins)) {
                ArrayMemoryView view(graph.alloc(), *ins);
                if (!replaceArray.run(view))
                    return false;
                view.assertSuccess();
                addedPhi = true;
                continue;
            }
        }
    }

    if (addedPhi) {
        // Phis added by Scalar Replacement are only redundant Phis which are
        // not directly captured by any resume point but only by the MDefinition
        // state. The conservative observability only focuses on Phis which are
        // not used as resume points operands.
        AssertExtendedGraphCoherency(graph);
        if (!EliminatePhis(mir, graph, ConservativeObservability))
            return false;
    }

    return true;
}

} /* namespace jit */
} /* namespace js */