aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/compiler/qv4regalloc.cpp
blob: f95cd55b04df6aa352eab4a2d35bed9bd0e5fc1f (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
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the V4VM module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qv4regalloc_p.h"
#include <private/qv4value_p.h>

#include <algorithm>

//#define DEBUG_REGALLOC

namespace {
struct Use {
    enum RegisterFlag { MustHaveRegister = 0, CouldHaveRegister = 1 };
    unsigned flag : 1;
    unsigned pos  : 31;

    Use(): flag(MustHaveRegister), pos(0) {}
    Use(int pos, RegisterFlag flag): flag(flag), pos(pos) {}

    bool mustHaveRegister() const { return flag == MustHaveRegister; }
};
}

QT_BEGIN_NAMESPACE

Q_DECLARE_TYPEINFO(Use, Q_MOVABLE_TYPE);

using namespace QQmlJS::V4IR;

namespace QQmlJS {
namespace MASM {

class RegAllocInfo: public IRDecoder
{
    struct Def {
        unsigned defStmt : 30;
        unsigned canHaveReg : 1;
        unsigned isPhiTarget : 1;

        Def(): defStmt(0), canHaveReg(0), isPhiTarget(0) {}
        Def(int defStmt, bool canHaveReg, bool isPhiTarget)
            : defStmt(defStmt), canHaveReg(canHaveReg), isPhiTarget(isPhiTarget)
        {
            Q_ASSERT(defStmt > 0);
            Q_ASSERT(defStmt < (1 << 30));
        }

        bool isValid() const { return defStmt != 0; } // 0 is invalid, as stmt numbers start at 1.
    };

    Stmt *_currentStmt;
    QHash<Temp, Def> _defs;
    QHash<Temp, QList<Use> > _uses;
    QList<int> _calls;
    QHash<Temp, QList<Temp> > _hints;

public:
    RegAllocInfo(): _currentStmt(0) {}

    void collect(Function *function)
    {
        foreach (BasicBlock *bb, function->basicBlocks) {
            foreach (Stmt *s, bb->statements) {
                Q_ASSERT(s->id > 0);
                _currentStmt = s;
                s->accept(this);
            }
        }
    }

    QList<Use> uses(const Temp &t) const { return _uses[t]; }
    bool useMustHaveReg(const Temp &t, int position) {
        foreach (const Use &use, uses(t))
            if (use.pos == position)
                return use.mustHaveRegister();
        return false;
    }

    int def(const Temp &t) const {
        Q_ASSERT(_defs[t].isValid());
        return _defs[t].defStmt;
    }
    bool canHaveRegister(const Temp &t) const {
        Q_ASSERT(_defs[t].isValid());
        return _defs[t].canHaveReg;
    }
    bool isPhiTarget(const Temp &t) const {
        Q_ASSERT(_defs[t].isValid());
        return _defs[t].isPhiTarget;
    }

    const QList<int> &calls() const { return _calls; }
    QList<Temp> hints(const Temp &t) const { return _hints[t]; }
    void addHint(const Temp &t, int physicalRegister)
    {
        Temp hint;
        hint.init(Temp::PhysicalRegister, physicalRegister, 0);
        _hints[t].append(hint);
    }

#ifdef DEBUG_REGALLOC
    void dump() const
    {
        QTextStream qout(stdout, QIODevice::WriteOnly);

        qout << "RegAllocInfo:" << endl << "Defs/uses:" << endl;
        QList<Temp> temps = _defs.keys();
        std::sort(temps.begin(), temps.end());
        foreach (const Temp &t, temps) {
            t.dump(qout);
            qout << " def at " << _defs[t].defStmt << " ("
                 << (_defs[t].canHaveReg ? "can" : "can NOT")
                 << " have a register, and "
                 << (isPhiTarget(t) ? "is" : "is NOT")
                 << " defined by a phi node), uses at: ";
            const QList<Use> &uses = _uses[t];
            for (int i = 0; i < uses.size(); ++i) {
                if (i > 0) qout << ", ";
                qout << uses[i].pos;
                if (uses[i].mustHaveRegister()) qout << "(R)"; else qout << "(S)";
            }
            qout << endl;
        }

        qout << "Calls at: ";
        for (int i = 0; i < _calls.size(); ++i) {
            if (i > 0) qout << ", ";
            qout << _calls[i];
        }
        qout << endl;

        qout << "Hints:" << endl;
        QList<Temp> hinted = _hints.keys();
        if (hinted.isEmpty())
            qout << "\t(none)" << endl;
        std::sort(hinted.begin(), hinted.end());
        foreach (const Temp &t, hinted) {
            qout << "\t";
            t.dump(qout);
            qout << ": ";
            QList<Temp> hints = _hints[t];
            for (int i = 0; i < hints.size(); ++i) {
                if (i > 0) qout << ", ";
                hints[i].dump(qout);
            }
            qout << endl;
        }
    }
#endif // DEBUG_REGALLOC

protected: // IRDecoder
    virtual void callBuiltinInvalid(V4IR::Name *, V4IR::ExprList *, V4IR::Temp *) {}
    virtual void callBuiltinTypeofMember(V4IR::Expr *, const QString &, V4IR::Temp *) {}
    virtual void callBuiltinTypeofSubscript(V4IR::Expr *, V4IR::Expr *, V4IR::Temp *) {}
    virtual void callBuiltinTypeofName(const QString &, V4IR::Temp *) {}
    virtual void callBuiltinTypeofValue(V4IR::Expr *, V4IR::Temp *) {}
    virtual void callBuiltinDeleteMember(V4IR::Temp *, const QString &, V4IR::Temp *) {}
    virtual void callBuiltinDeleteSubscript(V4IR::Temp *, V4IR::Expr *, V4IR::Temp *) {}
    virtual void callBuiltinDeleteName(const QString &, V4IR::Temp *) {}
    virtual void callBuiltinDeleteValue(V4IR::Temp *) {}
    virtual void callBuiltinThrow(V4IR::Expr *) {}
    virtual void callBuiltinReThrow() {}
    virtual void callBuiltinUnwindException(V4IR::Temp *) {}
    virtual void callBuiltinPushCatchScope(const QString &) {};
    virtual void callBuiltinForeachIteratorObject(V4IR::Temp *, V4IR::Temp *) {}
    virtual void callBuiltinForeachNextProperty(V4IR::Temp *, V4IR::Temp *) {}
    virtual void callBuiltinForeachNextPropertyname(V4IR::Temp *, V4IR::Temp *) {}
    virtual void callBuiltinPushWithScope(V4IR::Temp *) {}
    virtual void callBuiltinPopScope() {}
    virtual void callBuiltinDeclareVar(bool , const QString &) {}
    virtual void callBuiltinDefineGetterSetter(V4IR::Temp *, const QString &, V4IR::Temp *, V4IR::Temp *) {}
    virtual void callBuiltinDefineProperty(V4IR::Temp *, const QString &, V4IR::Expr *) {}
    virtual void callBuiltinDefineArray(V4IR::Temp *, V4IR::ExprList *) {}
    virtual void callBuiltinDefineObjectLiteral(V4IR::Temp *, V4IR::ExprList *) {}
    virtual void callBuiltinSetupArgumentObject(V4IR::Temp *) {}
    virtual void callBuiltinConvertThisToObject() {}

    virtual void callValue(V4IR::Temp *value, V4IR::ExprList *args, V4IR::Temp *result)
    {
        addDef(result);
        addUses(value, Use::CouldHaveRegister);
        addUses(args, Use::CouldHaveRegister);
        addCall();
    }

    virtual void callProperty(V4IR::Expr *base, const QString &name, V4IR::ExprList *args,
                              V4IR::Temp *result)
    {
        Q_UNUSED(name)

        addDef(result);
        addUses(base->asTemp(), Use::CouldHaveRegister);
        addUses(args, Use::CouldHaveRegister);
        addCall();
    }

    virtual void callSubscript(V4IR::Expr *base, V4IR::Expr *index, V4IR::ExprList *args,
                               V4IR::Temp *result)
    {
        addDef(result);
        addUses(base->asTemp(), Use::CouldHaveRegister);
        addUses(index->asTemp(), Use::CouldHaveRegister);
        addUses(args, Use::CouldHaveRegister);
        addCall();
    }

    virtual void convertType(V4IR::Temp *source, V4IR::Temp *target)
    {
        addDef(target);

        bool needsCall = true;
        Use::RegisterFlag sourceReg = Use::CouldHaveRegister;

        switch (target->type) {
        case DoubleType:
            switch (source->type) {
            case UInt32Type:
            case SInt32Type:
            case NullType:
            case UndefinedType:
            case BoolType:
                needsCall = false;
                break;
            default:
                break;
            }
            break;
        case BoolType:
            switch (source->type) {
            case UInt32Type:
                sourceReg = Use::MustHaveRegister;
                needsCall = false;
                break;
            case DoubleType:
            case UndefinedType:
            case NullType:
            case SInt32Type:
                needsCall = false;
                break;
            default:
                break;
            }
            break;
        case SInt32Type:
            switch (source->type) {
            case UInt32Type:
            case NullType:
            case UndefinedType:
            case BoolType:
                needsCall = false;
            default:
                break;
            }
            break;
        case UInt32Type:
            switch (source->type) {
            case SInt32Type:
            case NullType:
            case UndefinedType:
            case BoolType:
                needsCall = false;
            default:
                break;
            }
            break;
        default:
            break;
        }

        addUses(source, sourceReg);

        if (needsCall)
            addCall();
        else
            addHint(target, source);
    }

    virtual void constructActivationProperty(V4IR::Name *, V4IR::ExprList *args, V4IR::Temp *result)
    {
        addDef(result);
        addUses(args, Use::CouldHaveRegister);
        addCall();
    }

    virtual void constructProperty(V4IR::Temp *base, const QString &, V4IR::ExprList *args, V4IR::Temp *result)
    {
        addDef(result);
        addUses(base, Use::CouldHaveRegister);
        addUses(args, Use::CouldHaveRegister);
        addCall();
    }

    virtual void constructValue(V4IR::Temp *value, V4IR::ExprList *args, V4IR::Temp *result)
    {
        addDef(result);
        addUses(value, Use::CouldHaveRegister);
        addUses(args, Use::CouldHaveRegister);
        addCall();
    }

    virtual void loadThisObject(V4IR::Temp *temp)
    {
        addDef(temp);
    }

    virtual void loadQmlIdArray(V4IR::Temp *temp)
    {
        addDef(temp);
        addCall();
    }

    virtual void loadQmlImportedScripts(V4IR::Temp *temp)
    {
        addDef(temp);
        addCall();
    }

    virtual void loadQmlContextObject(Temp *temp)
    {
        addDef(temp);
        addCall();
    }

    virtual void loadQmlScopeObject(Temp *temp)
    {
        Q_UNUSED(temp);

        addDef(temp);
        addCall();
    }

    virtual void loadQmlSingleton(const QString &/*name*/, Temp *temp)
    {
        Q_UNUSED(temp);

        addDef(temp);
        addCall();
    }

    virtual void loadConst(V4IR::Const *sourceConst, V4IR::Temp *targetTemp)
    {
        Q_UNUSED(sourceConst);

        addDef(targetTemp);
    }

    virtual void loadString(const QString &str, V4IR::Temp *targetTemp)
    {
        Q_UNUSED(str);

        addDef(targetTemp);
    }

    virtual void loadRegexp(V4IR::RegExp *sourceRegexp, V4IR::Temp *targetTemp)
    {
        Q_UNUSED(sourceRegexp);

        addDef(targetTemp);
        addCall();
    }

    virtual void getActivationProperty(const V4IR::Name *, V4IR::Temp *temp)
    {
        addDef(temp);
        addCall();
    }

    virtual void setActivationProperty(V4IR::Expr *source, const QString &)
    {
        addUses(source->asTemp(), Use::CouldHaveRegister);
        addCall();
    }

    virtual void initClosure(V4IR::Closure *closure, V4IR::Temp *target)
    {
        Q_UNUSED(closure);

        addDef(target);
        addCall();
    }

    virtual void getProperty(V4IR::Expr *base, const QString &, V4IR::Temp *target)
    {
        addDef(target);
        addUses(base->asTemp(), Use::CouldHaveRegister);
        addCall();
    }

    virtual void setProperty(V4IR::Expr *source, V4IR::Expr *targetBase, const QString &)
    {
        addUses(source->asTemp(), Use::CouldHaveRegister);
        addUses(targetBase->asTemp(), Use::CouldHaveRegister);
        addCall();
    }

    virtual void setQObjectProperty(V4IR::Expr *source, V4IR::Expr *targetBase, int /*propertyIndex*/)
    {
        addUses(source->asTemp(), Use::CouldHaveRegister);
        addUses(targetBase->asTemp(), Use::CouldHaveRegister);
        addCall();
    }

    virtual void getQObjectProperty(V4IR::Expr *base, int /*propertyIndex*/, bool /*captureRequired*/, int /*attachedPropertiesId*/, V4IR::Temp *target)
    {
        addDef(target);
        addUses(base->asTemp(), Use::CouldHaveRegister);
        addCall();
    }

    virtual void getElement(V4IR::Expr *base, V4IR::Expr *index, V4IR::Temp *target)
    {
        addDef(target);
        addUses(base->asTemp(), Use::CouldHaveRegister);
        addUses(index->asTemp(), Use::CouldHaveRegister);
        addCall();
    }

    virtual void setElement(V4IR::Expr *source, V4IR::Expr *targetBase, V4IR::Expr *targetIndex)
    {
        addUses(source->asTemp(), Use::CouldHaveRegister);
        addUses(targetBase->asTemp(), Use::CouldHaveRegister);
        addUses(targetIndex->asTemp(), Use::CouldHaveRegister);
        addCall();
    }

    virtual void copyValue(V4IR::Temp *sourceTemp, V4IR::Temp *targetTemp)
    {
        addDef(targetTemp);
        addUses(sourceTemp, Use::CouldHaveRegister);
        addHint(targetTemp, sourceTemp);
    }

    virtual void swapValues(V4IR::Temp *, V4IR::Temp *)
    {
        // Inserted by the register allocator, so it cannot occur here.
        Q_UNREACHABLE();
    }

    virtual void unop(AluOp oper, Temp *sourceTemp, Temp *targetTemp)
    {
        addDef(targetTemp);

        bool needsCall = true;
        if (oper == OpNot && sourceTemp->type == V4IR::BoolType && targetTemp->type == V4IR::BoolType)
            needsCall = false;

#if 0 // TODO: change masm to generate code
        switch (oper) {
        case OpIfTrue:
        case OpNot:
        case OpUMinus:
        case OpUPlus:
        case OpCompl:
            needsCall = sourceTemp->type & ~NumberType && sourceTemp->type != BoolType;
            break;

        case OpIncrement:
        case OpDecrement:
        default:
            Q_UNREACHABLE();
        }
#endif

        if (needsCall) {
            addUses(sourceTemp, Use::CouldHaveRegister);
            addCall();
        } else {
            addUses(sourceTemp, Use::MustHaveRegister);
        }
    }

    virtual void binop(AluOp oper, Expr *leftSource, Expr *rightSource, Temp *target)
    {
        bool needsCall = true;

        if (oper == OpStrictEqual || oper == OpStrictNotEqual) {
            bool noCall = leftSource->type == NullType || rightSource->type == NullType
                    || leftSource->type == UndefinedType || rightSource->type == UndefinedType
                    || leftSource->type == BoolType || rightSource->type == BoolType;
            needsCall = !noCall;
        } else if (leftSource->type == DoubleType && rightSource->type == DoubleType) {
            if (oper == OpMul || oper == OpAdd || oper == OpDiv || oper == OpSub
                    || (oper >= OpGt && oper <= OpStrictNotEqual)) {
                needsCall = false;
            }
        } else if (oper == OpBitAnd || oper == OpBitOr || oper == OpBitXor || oper == OpLShift || oper == OpRShift || oper == OpURShift) {
            needsCall = false;
        } else if (oper == OpAdd
                   || oper == OpMul
                   ||
                   oper == OpSub
                   ) {
            if (leftSource->type == SInt32Type && rightSource->type == SInt32Type)
                needsCall = false;
        }

        addDef(target);

        if (needsCall) {
            addUses(leftSource->asTemp(), Use::CouldHaveRegister);
            addUses(rightSource->asTemp(), Use::CouldHaveRegister);
            addCall();
        } else {
            addUses(leftSource->asTemp(), Use::MustHaveRegister);
            addHint(target, leftSource->asTemp());

            addUses(rightSource->asTemp(), Use::MustHaveRegister);
            switch (oper) {
            case OpAdd:
            case OpMul:
                addHint(target, rightSource->asTemp());
                break;
            default:
                break;
            }
        }
    }

    virtual void visitJump(V4IR::Jump *) {}
    virtual void visitCJump(V4IR::CJump *s)
    {
        if (Temp *t = s->cond->asTemp()) {
#if 0 // TODO: change masm to generate code
            addUses(t, Use::MustHaveRegister);
#else
            addUses(t, Use::CouldHaveRegister);
            addCall();
#endif
        } else if (Binop *b = s->cond->asBinop()) {
            binop(b->op, b->left, b->right, 0);
        } else if (s->cond->asConst()) {
            // TODO: SSA optimization for constant condition evaluation should remove this.
            // See also visitCJump() in masm.
            addCall();
        } else {
            Q_UNREACHABLE();
        }
    }

    virtual void visitRet(V4IR::Ret *s)
    { addUses(s->expr->asTemp(), Use::CouldHaveRegister); }

    virtual void visitPhi(V4IR::Phi *s)
    {
        addDef(s->targetTemp, true);
        foreach (Expr *e, s->d->incoming) {
            if (Temp *t = e->asTemp()) {
                addUses(t, Use::CouldHaveRegister);
                addHint(s->targetTemp, t);
            }
        }
    }

protected:
    virtual void callBuiltin(V4IR::Call *c, V4IR::Temp *result)
    {
        addDef(result);
        addUses(c->base->asTemp(), Use::CouldHaveRegister);
        addUses(c->args, Use::CouldHaveRegister);
        addCall();
    }

private:
    void addDef(Temp *t, bool isPhiTarget = false)
    {
        if (!t || t->kind != Temp::VirtualRegister)
            return;
        Q_ASSERT(!_defs.contains(*t));
        bool canHaveReg = true;
        switch (t->type) {
        case QObjectType:
        case VarType:
        case StringType:
        case UndefinedType:
        case NullType:
            canHaveReg = false;
            break;
        default:
            break;
        }

        _defs[*t] = Def(_currentStmt->id, canHaveReg, isPhiTarget);
    }

    void addUses(Temp *t, Use::RegisterFlag flag)
    {
        Q_ASSERT(_currentStmt->id > 0);
        if (t && t->kind == Temp::VirtualRegister)
            _uses[*t].append(Use(_currentStmt->id, flag));
    }

    void addUses(ExprList *l, Use::RegisterFlag flag)
    {
        for (ExprList *it = l; it; it = it->next)
            addUses(it->expr->asTemp(), flag);
    }

    void addCall()
    {
        _calls.append(_currentStmt->id);
    }

    void addHint(Temp *hinted, Temp *hint1, Temp *hint2 = 0)
    {
        if (!hinted || hinted->kind != Temp::VirtualRegister)
            return;
        if (hint1 && hint1->kind == Temp::VirtualRegister && hinted->type == hint1->type)
            _hints[*hinted].append(*hint1);
        if (hint2 && hint2->kind == Temp::VirtualRegister && hinted->type == hint2->type)
            _hints[*hinted].append(*hint2);
    }
};

} // MASM namespace
} // MOTH namespace
QT_END_NAMESPACE

QT_USE_NAMESPACE

using namespace QT_PREPEND_NAMESPACE(QQmlJS::MASM);
using namespace QT_PREPEND_NAMESPACE(QQmlJS::V4IR);
using namespace QT_PREPEND_NAMESPACE(QQmlJS);

namespace {
class ResolutionPhase: protected StmtVisitor, protected ExprVisitor {
    const QVector<LifeTimeInterval> &_intervals;
    QVector<const LifeTimeInterval *> _unprocessed;
    Function *_function;
#if !defined(QT_NO_DEBUG)
    RegAllocInfo *_info;
#endif
    const QHash<V4IR::Temp, int> &_assignedSpillSlots;
    QHash<V4IR::Temp, const LifeTimeInterval *> _intervalForTemp;
    const QVector<int> &_intRegs;
    const QVector<int> &_fpRegs;

    Stmt *_currentStmt;
    QVector<Move *> _loads;
    QVector<Move *> _stores;

    QHash<BasicBlock *, QList<const LifeTimeInterval *> > _liveAtStart;
    QHash<BasicBlock *, QList<const LifeTimeInterval *> > _liveAtEnd;

public:
    ResolutionPhase(const QVector<LifeTimeInterval> &intervals, Function *function, RegAllocInfo *info,
                    const QHash<V4IR::Temp, int> &assignedSpillSlots,
                    const QVector<int> &intRegs, const QVector<int> &fpRegs)
        : _intervals(intervals)
        , _function(function)
#if !defined(QT_NO_DEBUG)
        , _info(info)
#endif
        , _assignedSpillSlots(assignedSpillSlots)
        , _intRegs(intRegs)
        , _fpRegs(fpRegs)
    {
#if defined(QT_NO_DEBUG)
        Q_UNUSED(info)
#endif

        _unprocessed.resize(_intervals.size());
        for (int i = 0, ei = _intervals.size(); i != ei; ++i)
            _unprocessed[i] = &_intervals[i];

        _liveAtStart.reserve(function->basicBlocks.size());
        _liveAtEnd.reserve(function->basicBlocks.size());
    }

    void run() {
        renumber();
        Optimizer::showMeTheCode(_function);
        resolve();
    }

private:
    void renumber()
    {
        foreach (BasicBlock *bb, _function->basicBlocks) {
            QVector<Stmt *> newStatements;
            newStatements.reserve(bb->statements.size() + 7);

            bool seenFirstNonPhiStmt = false;
            for (int i = 0, ei = bb->statements.size(); i != ei; ++i) {
                _currentStmt = bb->statements[i];
                _loads.clear();
                _stores.clear();
                addNewIntervals();
                if (!seenFirstNonPhiStmt && !_currentStmt->asPhi()) {
                    seenFirstNonPhiStmt = true;
                    _liveAtStart[bb] = _intervalForTemp.values();
                }
                _currentStmt->accept(this);
                foreach (Move *load, _loads)
                    newStatements.append(load);
                if (_currentStmt->asPhi())
                    newStatements.prepend(_currentStmt);
                else
                    newStatements.append(_currentStmt);
                foreach (Move *store, _stores)
                    newStatements.append(store);
            }

            cleanOldIntervals();
            _liveAtEnd[bb] = _intervalForTemp.values();

#ifdef DEBUG_REGALLOC
            QTextStream os(stdout, QIODevice::WriteOnly);
            os << "Intervals live at the start of L" << bb->index << ":" << endl;
            if (_liveAtStart[bb].isEmpty())
                os << "\t(none)" << endl;
            foreach (const LifeTimeInterval &i, _liveAtStart[bb]) {
                os << "\t";
                i.dump(os);
                os << endl;
            }
            os << "Intervals live at the end of L" << bb->index << ":" << endl;
            if (_liveAtEnd[bb].isEmpty())
                os << "\t(none)" << endl;
            foreach (const LifeTimeInterval &i, _liveAtEnd[bb]) {
                os << "\t";
                i.dump(os);
                os << endl;
            }
#endif

            bb->statements = newStatements;
        }

    }

    void activate(const LifeTimeInterval *i)
    {
        Q_ASSERT(!i->isFixedInterval());
        _intervalForTemp[i->temp()] = i;

        if (i->reg() != LifeTimeInterval::Invalid) {
            // check if we need to generate spill/unspill instructions
            if (i->start() == _currentStmt->id) {
                if (i->isSplitFromInterval()) {
                    int pReg = platformRegister(*i);
                    _loads.append(generateUnspill(i->temp(), pReg));
                } else {
                    int pReg = platformRegister(*i);
                    int spillSlot = _assignedSpillSlots.value(i->temp(), -1);
                    if (spillSlot != -1)
                        _stores.append(generateSpill(spillSlot, i->temp().type, pReg));
                }
            }
        }
    }

    void addNewIntervals()
    {
        if (Phi *phi = _currentStmt->asPhi()) {
            // for phi nodes, only activate the range belonging to that node
            for (int it = 0, eit = _unprocessed.size(); it != eit; ++it) {
                const LifeTimeInterval *i = _unprocessed.at(it);
                if (i->start() > _currentStmt->id)
                    break;
                if (i->temp() == *phi->targetTemp) {
                    activate(i);
                    _unprocessed.remove(it);
                    break;
                }
            }
            return;
        }

        while (!_unprocessed.isEmpty()) {
            const LifeTimeInterval *i = _unprocessed.first();
            if (i->start() > _currentStmt->id)
                break;

            activate(i);

            _unprocessed.removeFirst();
        }
    }

    void cleanOldIntervals()
    {
        const int id = _currentStmt->id;
        QMutableHashIterator<Temp, const LifeTimeInterval *> it(_intervalForTemp);
        while (it.hasNext()) {
            const LifeTimeInterval *i = it.next().value();
            if (i->end() < id || i->isFixedInterval())
                it.remove();
        }
    }

    void resolve()
    {
        foreach (BasicBlock *bb, _function->basicBlocks) {
            foreach (BasicBlock *bbOut, bb->out)
                resolveEdge(bb, bbOut);
        }
    }

    void resolveEdge(BasicBlock *predecessor, BasicBlock *successor)
    {
#ifdef DEBUG_REGALLOC
        Optimizer::showMeTheCode(_function);
        qDebug() << "Resolving edge" << predecessor->index << "->" << successor->index;
#endif // DEBUG_REGALLOC

        MoveMapping mapping;

        const int predecessorEnd = predecessor->statements.last()->id; // the terminator is always last and always has an id set...
        Q_ASSERT(predecessorEnd > 0); // ... but we verify it anyway for good measure.

        int successorStart = -1;
        foreach (Stmt *s, successor->statements) {
            if (s && s->id > 0) {
                successorStart = s->id;
                break;
            }
        }

        Q_ASSERT(successorStart > 0);

        foreach (const LifeTimeInterval *it, _liveAtStart[successor]) {
            if (it->end() < successorStart)
                continue;

            bool lifeTimeHole = false;
            bool isPhiTarget = false;
            Expr *moveFrom = 0;

            if (it->start() == successorStart) {
                foreach (Stmt *s, successor->statements) {
                    if (!s || s->id < 1)
                        continue;
                    if (Phi *phi = s->asPhi()) {
                        if (*phi->targetTemp == it->temp()) {
                            isPhiTarget = true;
                            Expr *opd = phi->d->incoming[successor->in.indexOf(predecessor)];
                            if (opd->asConst()) {
                                moveFrom = opd;
                            } else {
                                Temp *t = opd->asTemp();
                                Q_ASSERT(t);

                                foreach (const LifeTimeInterval *it2, _liveAtEnd[predecessor]) {
                                    if (it2->temp() == *t
                                            && it2->reg() != LifeTimeInterval::Invalid
                                            && it2->covers(predecessorEnd)) {
                                        moveFrom = createTemp(Temp::PhysicalRegister,
                                                              platformRegister(*it2), t->type);
                                        break;
                                    }
                                }
                                if (!moveFrom)
                                    moveFrom = createTemp(Temp::StackSlot,
                                                          _assignedSpillSlots.value(*t, -1),
                                                          t->type);
                            }
                        }
                    } else {
                        break;
                    }
                }
            } else {
                foreach (const LifeTimeInterval *predIt, _liveAtEnd[predecessor]) {
                    if (predIt->temp() == it->temp()) {
                        if (predIt->reg() != LifeTimeInterval::Invalid
                                && predIt->covers(predecessorEnd)) {
                            moveFrom = createTemp(Temp::PhysicalRegister, platformRegister(*predIt),
                                                  predIt->temp().type);
                        } else {
                            int spillSlot = _assignedSpillSlots.value(predIt->temp(), -1);
                            if (spillSlot == -1)
                                lifeTimeHole = true;
                            else
                                moveFrom = createTemp(Temp::StackSlot, spillSlot, predIt->temp().type);
                        }
                        break;
                    }
                }
            }
            if (!moveFrom) {
#if defined(QT_NO_DEBUG)
                Q_UNUSED(lifeTimeHole);
#else
                Q_ASSERT(!_info->isPhiTarget(it->temp()) || it->isSplitFromInterval() || lifeTimeHole);
                if (_info->def(it->temp()) != successorStart && !it->isSplitFromInterval()) {
                    const int successorEnd = successor->statements.last()->id;
                    const int idx = successor->in.indexOf(predecessor);
                    foreach (const Use &use, _info->uses(it->temp())) {
                        if (use.pos == static_cast<unsigned>(successorStart)) {
                            // only check the current edge, not all other possible ones. This is
                            // important for phi nodes: they have uses that are only valid when
                            // coming in over a specific edge.
                            foreach (Stmt *s, successor->statements) {
                                if (Phi *phi = s->asPhi()) {
                                    Q_ASSERT(it->temp().index != phi->targetTemp->index);
                                    Q_ASSERT(phi->d->incoming[idx]->asTemp() == 0
                                             || it->temp().index != phi->d->incoming[idx]->asTemp()->index);
                                } else {
                                    // TODO: check that the first non-phi statement does not use
                                    // the temp.
                                    break;
                                }
                            }
                        } else {
                            Q_ASSERT(use.pos < static_cast<unsigned>(successorStart) ||
                                     use.pos > static_cast<unsigned>(successorEnd));
                        }
                    }
                }
#endif

                continue;
            }

            Temp *moveTo;
            if (it->reg() == LifeTimeInterval::Invalid || !it->covers(successorStart)) {
                if (!isPhiTarget) // if it->temp() is a phi target, skip it.
                    continue;
                const int spillSlot = _assignedSpillSlots.value(it->temp(), -1);
                if (spillSlot == -1)
                    continue; // it has a life-time hole here.
                moveTo = createTemp(Temp::StackSlot, spillSlot, it->temp().type);
            } else {
                moveTo = createTemp(Temp::PhysicalRegister, platformRegister(*it), it->temp().type);
                const int spillSlot = _assignedSpillSlots.value(it->temp(), -1);
                if (isPhiTarget && spillSlot != -1)
                    mapping.add(moveFrom, createTemp(Temp::StackSlot, spillSlot, it->temp().type));
            }

            // add move to mapping
            mapping.add(moveFrom, moveTo);
        }

        mapping.order();
#ifdef DEBUG_REGALLOC
        mapping.dump();
#endif // DEBUG_REGALLOC

        bool insertIntoPredecessor = successor->in.size() > 1;
        mapping.insertMoves(insertIntoPredecessor ? predecessor : successor, _function,
                            insertIntoPredecessor);
    }

    Temp *createTemp(Temp::Kind kind, int index, Type type) const
    {
        Q_ASSERT(index >= 0);
        Temp *t = _function->New<Temp>();
        t->init(kind, index, 0);
        t->type = type;
        return t;
    }

    int platformRegister(const LifeTimeInterval &i) const
    {
        if (i.isFP())
            return _fpRegs.value(i.reg(), -1);
        else
            return _intRegs.value(i.reg(), -1);
    }

    Move *generateSpill(int spillSlot, Type type, int pReg) const
    {
        Q_ASSERT(spillSlot >= 0);

        Move *store = _function->New<Move>();
        store->init(createTemp(Temp::StackSlot, spillSlot, type),
                    createTemp(Temp::PhysicalRegister, pReg, type));
        return store;
    }

    Move *generateUnspill(const Temp &t, int pReg) const
    {
        Q_ASSERT(pReg >= 0);
        int spillSlot = _assignedSpillSlots.value(t, -1);
        Q_ASSERT(spillSlot != -1);
        Move *load = _function->New<Move>();
        load->init(createTemp(Temp::PhysicalRegister, pReg, t.type),
                   createTemp(Temp::StackSlot, spillSlot, t.type));
        return load;
    }

protected:
    virtual void visitTemp(Temp *t)
    {
        if (t->kind != Temp::VirtualRegister)
            return;

        const LifeTimeInterval *i = _intervalForTemp[*t];
        Q_ASSERT(i->isValid());
        if (i->reg() != LifeTimeInterval::Invalid && i->covers(_currentStmt->id)) {
            int pReg = platformRegister(*i);
            t->kind = Temp::PhysicalRegister;
            t->index = pReg;
        } else {
            int stackSlot = _assignedSpillSlots.value(*t, -1);
            Q_ASSERT(stackSlot >= 0);
            t->kind = Temp::StackSlot;
            t->index = stackSlot;
        }
    }

    virtual void visitConst(Const *) {}
    virtual void visitString(String *) {}
    virtual void visitRegExp(RegExp *) {}
    virtual void visitName(Name *) {}
    virtual void visitClosure(Closure *) {}
    virtual void visitConvert(Convert *e) { e->expr->accept(this); }
    virtual void visitUnop(Unop *e) { e->expr->accept(this); }
    virtual void visitBinop(Binop *e) { e->left->accept(this); e->right->accept(this); }
    virtual void visitSubscript(Subscript *e) { e->base->accept(this); e->index->accept(this); }
    virtual void visitMember(Member *e) { e->base->accept(this); }

    virtual void visitCall(Call *e) {
        e->base->accept(this);
        for (ExprList *it = e->args; it; it = it->next)
            it->expr->accept(this);
    }

    virtual void visitNew(New *e) {
        e->base->accept(this);
        for (ExprList *it = e->args; it; it = it->next)
            it->expr->accept(this);
    }

    virtual void visitExp(Exp *s) { s->expr->accept(this); }
    virtual void visitMove(Move *s) { s->source->accept(this); s->target->accept(this); }
    virtual void visitJump(Jump *) {}
    virtual void visitCJump(CJump *s) { s->cond->accept(this); }
    virtual void visitRet(Ret *s) { s->expr->accept(this); }
    virtual void visitPhi(Phi *) {}
};
} // anonymous namespace

RegisterAllocator::RegisterAllocator(const QVector<int> &normalRegisters, const QVector<int> &fpRegisters)
    : _normalRegisters(normalRegisters)
    , _fpRegisters(fpRegisters)
{
}

RegisterAllocator::~RegisterAllocator()
{
}

void RegisterAllocator::run(Function *function, const Optimizer &opt)
{
    _activeSpillSlots.resize(function->tempCount);

#ifdef DEBUG_REGALLOC
    qDebug() << "*** Running regalloc for function" << (function->name ? qPrintable(*function->name) : "NO NAME") << "***";
#endif // DEBUG_REGALLOC

    _unhandled = opt.lifeRanges();

    _info.reset(new RegAllocInfo);
    _info->collect(function);

#ifdef DEBUG_REGALLOC
    {
        QTextStream qout(stdout, QIODevice::WriteOnly);
        qout << "Ranges:" << endl;
        QVector<LifeTimeInterval> intervals = _unhandled;
        std::sort(intervals.begin(), intervals.end(), LifeTimeInterval::lessThanForTemp);
        foreach (const LifeTimeInterval &r, intervals) {
            r.dump(qout);
            qout << endl;
        }
    }
    _info->dump();
#endif // DEBUG_REGALLOC

    prepareRanges();

    _handled.reserve(_unhandled.size());
    _active.reserve(32);
    _inactive.reserve(16);

    Optimizer::showMeTheCode(function);

    linearScan();

#ifdef DEBUG_REGALLOC
    dump();
#endif // DEBUG_REGALLOC

    std::sort(_handled.begin(), _handled.end(), LifeTimeInterval::lessThan);
    ResolutionPhase(_handled, function, _info.data(), _assignedSpillSlots, _normalRegisters, _fpRegisters).run();

    function->tempCount = QSet<int>::fromList(_assignedSpillSlots.values()).size();

    Optimizer::showMeTheCode(function);

#ifdef DEBUG_REGALLOC
    qDebug() << "*** Finished regalloc for function" << (function->name ? qPrintable(*function->name) : "NO NAME") << "***";
#endif // DEBUG_REGALLOC
}

static inline LifeTimeInterval createFixedInterval(int reg, bool isFP, int rangeCount)
{
    Temp t;
    t.init(Temp::PhysicalRegister, reg, 0);
    t.type = isFP ? V4IR::DoubleType : V4IR::SInt32Type;
    LifeTimeInterval i;
    i.setTemp(t);
    i.setReg(reg);
    i.setFixedInterval(true);
    i.reserveRanges(rangeCount);
    return i;
}

void RegisterAllocator::prepareRanges()
{
    const int regCount = _normalRegisters.size();
    _fixedRegisterRanges.resize(regCount);
    for (int reg = 0; reg < regCount; ++reg)
        _fixedRegisterRanges[reg] = createFixedInterval(reg, false, _info->calls().size());

    const int fpRegCount = _fpRegisters.size();
    _fixedFPRegisterRanges.resize(fpRegCount);
    for (int fpReg = 0; fpReg < fpRegCount; ++fpReg) {
        _fixedFPRegisterRanges[fpReg] = createFixedInterval(fpReg, true, _info->calls().size());
    }

    foreach (int callPosition, _info->calls()) {
        for (int reg = 0; reg < regCount; ++reg)
            _fixedRegisterRanges[reg].addRange(callPosition, callPosition);
        for (int fpReg = 0; fpReg < fpRegCount; ++fpReg)
            _fixedFPRegisterRanges[fpReg].addRange(callPosition, callPosition);
    }
    for (int reg = 0; reg < regCount; ++reg)
        if (_fixedRegisterRanges[reg].isValid())
            _active.append(_fixedRegisterRanges[reg]);
    for (int fpReg = 0; fpReg < fpRegCount; ++fpReg)
        if (_fixedFPRegisterRanges[fpReg].isValid())
            _active.append(_fixedFPRegisterRanges[fpReg]);

    std::sort(_active.begin(), _active.end(), LifeTimeInterval::lessThan);
}

void RegisterAllocator::linearScan()
{
    while (!_unhandled.isEmpty()) {
        LifeTimeInterval current = _unhandled.first();
        _unhandled.removeFirst();
        int position = current.start();

        // check for intervals in active that are handled or inactive
        for (int i = 0; i < _active.size(); ) {
            const LifeTimeInterval &it = _active[i];
            if (it.end() < position) {
                if (!it.isFixedInterval())
                    _handled += it;
                _active.remove(i);
            } else if (!it.covers(position)) {
                _inactive += it;
                _active.remove(i);
            } else {
                ++i;
            }
        }

        // check for intervals in inactive that are handled or active
        for (int i = 0; i < _inactive.size(); ) {
            LifeTimeInterval &it = _inactive[i];
            if (it.end() < position) {
                if (!it.isFixedInterval())
                    _handled += it;
                _inactive.remove(i);
            } else if (it.covers(position)) {
                if (it.reg() != LifeTimeInterval::Invalid) {
                    _active += it;
                    _inactive.remove(i);
                } else {
                    // although this interval is now active, it has no register allocated (always
                    // spilled), so leave it in inactive.
                    ++i;
                }
            } else {
                ++i;
            }
        }

        Q_ASSERT(!current.isFixedInterval());

        if (_info->canHaveRegister(current.temp())) {
            tryAllocateFreeReg(current, position);
            if (current.reg() == LifeTimeInterval::Invalid)
                allocateBlockedReg(current, position);
            if (current.reg() != LifeTimeInterval::Invalid)
                _active += current;
        } else {
            assignSpillSlot(current.temp(), current.start(), current.end());
            _inactive += current;
#ifdef DEBUG_REGALLOC
            qDebug() << "*** allocating stack slot" << _assignedSpillSlots[current.temp()]
                     << "for %" << current.temp().index << "as it cannot be loaded in a register";
#endif // DEBUG_REGALLOC
        }
    }

    foreach (const LifeTimeInterval &r, _active)
        if (!r.isFixedInterval())
            _handled.append(r);
    _active.clear();
    foreach (const LifeTimeInterval &r, _inactive)
        if (!r.isFixedInterval())
            _handled.append(r);
    _inactive.clear();
}

static inline int indexOfRangeCoveringPosition(const LifeTimeInterval::Ranges &ranges, int position)
{
    for (int i = 0, ei = ranges.size(); i != ei; ++i) {
        if (position <= ranges[i].end)
            return i;
    }
    return -1;
}

static inline int intersectionPosition(const LifeTimeInterval::Range &one, const LifeTimeInterval::Range &two)
{
    if (one.covers(two.start))
        return two.start;
    if (two.covers(one.start))
        return one.start;
    return -1;
}

static inline bool isFP(const Temp &t)
{ return t.type == DoubleType; }

void RegisterAllocator::tryAllocateFreeReg(LifeTimeInterval &current, const int position)
{
    Q_ASSERT(!current.isFixedInterval());
    Q_ASSERT(current.reg() == LifeTimeInterval::Invalid);

    const bool needsFPReg = isFP(current.temp());
    QVector<int> freeUntilPos(needsFPReg ? _fpRegisters.size() : _normalRegisters.size(), INT_MAX);
    Q_ASSERT(freeUntilPos.size() > 0);

    const bool isPhiTarget = _info->isPhiTarget(current.temp());
    foreach (const LifeTimeInterval &it, _active) {
        if (it.isFP() == needsFPReg) {
            if (!isPhiTarget && it.isFixedInterval() && !current.isSplitFromInterval()) {
                const int idx = indexOfRangeCoveringPosition(it.ranges(), position);
                if (it.ranges().at(idx).end == current.start()) {
                    if (it.ranges().size() > idx + 1)
                        freeUntilPos[it.reg()] = it.ranges().at(idx + 1).start;
                    continue;
                }
            }

            if (isPhiTarget || it.end() >= current.firstPossibleUsePosition(isPhiTarget))
                freeUntilPos[it.reg()] = 0; // mark register as unavailable
        }
    }

    foreach (const LifeTimeInterval &it, _inactive) {
        if (current.isSplitFromInterval() || it.isFixedInterval()) {
            if (it.isFP() == needsFPReg && it.reg() != LifeTimeInterval::Invalid) {
                const int intersectionPos = nextIntersection(current, it, position);
                if (!isPhiTarget && it.isFixedInterval() && current.end() == intersectionPos)
                    freeUntilPos[it.reg()] = qMin(freeUntilPos[it.reg()], intersectionPos + 1);
                else if (intersectionPos != -1)
                    freeUntilPos[it.reg()] = qMin(freeUntilPos[it.reg()], intersectionPos);
            }
        }
    }

    int reg = LifeTimeInterval::Invalid;
    int freeUntilPos_reg = 0;

    foreach (const Temp &hint, _info->hints(current.temp())) {
        int candidate;
        if (hint.kind == Temp::PhysicalRegister)
            candidate = hint.index;
        else
            candidate = _lastAssignedRegister.value(hint, LifeTimeInterval::Invalid);

        const int end = current.end();
        if (candidate != LifeTimeInterval::Invalid) {
            if (current.isFP() == (hint.type == DoubleType)) {
                int fp = freeUntilPos[candidate];
                if ((freeUntilPos_reg < end && fp > freeUntilPos_reg)
                        || (freeUntilPos_reg >= end && fp >= end && freeUntilPos_reg > fp)) {
                    reg = candidate;
                    freeUntilPos_reg = fp;
                }
            }
        }
    }

    if (reg == LifeTimeInterval::Invalid)
        longestAvailableReg(freeUntilPos, reg, freeUntilPos_reg, current.end());

    if (freeUntilPos_reg == 0) {
        // no register available without spilling
#ifdef DEBUG_REGALLOC
        qDebug() << "*** no register available for %" << current.temp().index;
#endif // DEBUG_REGALLOC
        return;
    } else if (current.end() < freeUntilPos_reg) {
        // register available for the whole interval
#ifdef DEBUG_REGALLOC
        qDebug() << "*** allocating register" << reg << "for the whole interval of %" << current.temp().index;
#endif // DEBUG_REGALLOC
        current.setReg(reg);
        _lastAssignedRegister.insert(current.temp(), reg);
    } else {
        // register available for the first part of the interval
        current.setReg(reg);
        _lastAssignedRegister.insert(current.temp(), reg);
#ifdef DEBUG_REGALLOC
        qDebug() << "*** allocating register" << reg << "for the first part of interval of %" << current.temp().index;
#endif // DEBUG_REGALLOC
        split(current, freeUntilPos_reg, true);
    }
}

void RegisterAllocator::allocateBlockedReg(LifeTimeInterval &current, const int position)
{
    Q_ASSERT(!current.isFixedInterval());
    Q_ASSERT(current.reg() == LifeTimeInterval::Invalid);

    const bool isPhiTarget = _info->isPhiTarget(current.temp());
    if (isPhiTarget && !current.isSplitFromInterval()) {
        split(current, position + 1, true);
        _inactive.append(current);
        return;
    }

    const bool needsFPReg = isFP(current.temp());
    QVector<int> nextUsePos(needsFPReg ? _fpRegisters.size() : _normalRegisters.size(), INT_MAX);
    QVector<LifeTimeInterval *> nextUseRangeForReg(nextUsePos.size(), 0);
    Q_ASSERT(nextUsePos.size() > 0);

    for (int i = 0, ei = _active.size(); i != ei; ++i) {
        LifeTimeInterval &it = _active[i];
        if (it.isFP() == needsFPReg) {
            int nu = it.isFixedInterval() ? 0 : nextUse(it.temp(), current.firstPossibleUsePosition(isPhiTarget));
            if (nu != -1 && nu < nextUsePos[it.reg()]) {
                nextUsePos[it.reg()] = nu;
                nextUseRangeForReg[it.reg()] = &it;
            } else if (nu == -1 && nextUsePos[it.reg()] == INT_MAX) {
                // in a loop, the range can be active, but only used before the current position (e.g. in a loop header or phi node)
                nextUseRangeForReg[it.reg()] = &it;
            }
        }
    }

    for (int i = 0, ei = _inactive.size(); i != ei; ++i) {
        LifeTimeInterval &it = _inactive[i];
        if (current.isSplitFromInterval() || it.isFixedInterval()) {
            if (it.isFP() == needsFPReg && it.reg() != LifeTimeInterval::Invalid) {
                if (nextIntersection(current, it, position) != -1) {
                    int nu = nextUse(it.temp(), current.firstPossibleUsePosition(isPhiTarget));
                    if (nu != -1 && nu < nextUsePos[it.reg()]) {
                        nextUsePos[it.reg()] = nu;
                        nextUseRangeForReg[it.reg()] = &it;
                    }
                }
            }
        }
    }

    int reg, nextUsePos_reg;
    longestAvailableReg(nextUsePos, reg, nextUsePos_reg, current.end());

    if (current.start() > nextUsePos_reg) {
        // all other intervals are used before current, so it is best to spill current itself
#ifdef DEBUG_REGALLOC
        QTextStream out(stderr, QIODevice::WriteOnly);
        out << "*** splitting current for range ";current.dump(out);out<<endl;
#endif // DEBUG_REGALLOC
        Q_ASSERT(!_info->useMustHaveReg(current.temp(), position));
        split(current, position + 1, true);
        _inactive.append(current);
    } else {
        // spill intervals that currently block reg
#ifdef DEBUG_REGALLOC
        QTextStream out(stderr, QIODevice::WriteOnly);
        out << "*** spilling intervals that block reg "<<reg<<" for interval ";current.dump(out);out<<endl;
#endif // DEBUG_REGALLOC
        current.setReg(reg);
        _lastAssignedRegister.insert(current.temp(), reg);
        Q_ASSERT(nextUseRangeForReg[reg]);
        Q_ASSERT(!nextUseRangeForReg[reg]->isFixedInterval());
        split(*nextUseRangeForReg[reg], position);
        splitInactiveAtEndOfLifetimeHole(reg, needsFPReg, position);

        // make sure that current does not intersect with the fixed interval for reg
        const LifeTimeInterval &fixedRegRange = needsFPReg ? _fixedFPRegisterRanges[reg]
                                                           : _fixedRegisterRanges[reg];
        int ni = nextIntersection(current, fixedRegRange, position);
        if (ni != -1) {
#ifdef DEBUG_REGALLOC
            out << "***-- current range intersects with a fixed reg use at "<<ni<<", so splitting it."<<endl;
#endif // DEBUG_REGALLOC
            split(current, ni, true);
        }
    }
}

void RegisterAllocator::longestAvailableReg(const QVector<int> &nextUses, int &reg,
                                            int &freeUntilPos_reg, int lastUse) const
{
    reg = LifeTimeInterval::Invalid;
    freeUntilPos_reg = 0;

    for (int candidate = 0, candidateEnd = nextUses.size(); candidate != candidateEnd; ++candidate) {
        int fp = nextUses[candidate];
        if ((freeUntilPos_reg < lastUse && fp > freeUntilPos_reg)
                || (freeUntilPos_reg >= lastUse && fp >= lastUse && freeUntilPos_reg > fp)) {
            reg = candidate;
            freeUntilPos_reg = fp;
        }
    }
}

int RegisterAllocator::nextIntersection(const LifeTimeInterval &current,
                                        const LifeTimeInterval &another, const int position) const
{
    LifeTimeInterval::Ranges currentRanges = current.ranges();
    int currentIt = indexOfRangeCoveringPosition(currentRanges, position);
    if (currentIt == -1)
        return -1;

    LifeTimeInterval::Ranges anotherRanges = another.ranges();
    const int anotherItStart = indexOfRangeCoveringPosition(anotherRanges, position);
    if (anotherItStart == -1)
        return -1;

    for (int currentEnd = currentRanges.size(); currentIt < currentEnd; ++currentIt) {
        const LifeTimeInterval::Range currentRange = currentRanges.at(currentIt);
        for (int anotherIt = anotherItStart, anotherEnd = anotherRanges.size(); anotherIt < anotherEnd; ++anotherIt) {
            const LifeTimeInterval::Range anotherRange = anotherRanges.at(anotherIt);
            if (anotherRange.start > currentRange.end)
                break;
            int intersectPos = intersectionPosition(currentRange, anotherRange);
            if (intersectPos != -1)
                return intersectPos;
        }
    }

    return -1;
}

int RegisterAllocator::nextUse(const Temp &t, int startPosition) const
{
    QList<Use> usePositions = _info->uses(t);
    for (int i = 0, ei = usePositions.size(); i != ei; ++i) {
        int usePos = usePositions[i].pos;
        if (usePos >= startPosition)
            return usePos;
    }

    return -1;
}

static inline void insertSorted(QVector<LifeTimeInterval> &intervals, const LifeTimeInterval &newInterval)
{
    newInterval.validate();
    for (int i = 0, ei = intervals.size(); i != ei; ++i) {
        if (LifeTimeInterval::lessThan(newInterval, intervals.at(i))) {
            intervals.insert(i, newInterval);
            return;
        }
    }
    intervals.append(newInterval);
}

void RegisterAllocator::split(LifeTimeInterval &current, int beforePosition,
                              bool skipOptionalRegisterUses)
{ // TODO: check if we can always skip the optional register uses
    Q_ASSERT(!current.isFixedInterval());

#ifdef DEBUG_REGALLOC
    QTextStream out(stderr, QIODevice::WriteOnly);
    out << "***** split request for range ";current.dump(out);out<<" before position "<<beforePosition<<" and skipOptionalRegisterUses = "<<skipOptionalRegisterUses<<endl;
#endif // DEBUG_REGALLOC

    assignSpillSlot(current.temp(), current.start(), current.end());

    const int defPosition = _info->def(current.temp());
    if (beforePosition < defPosition) {
#ifdef DEBUG_REGALLOC
        out << "***** split before position is before or at definition, so not splitting."<<endl;
#endif // DEBUG_REGALLOC
        return;
    }

    int lastUse = -1;
    if (defPosition < beforePosition)
        lastUse = defPosition;
    int nextUse = -1;
    QList<Use> usePositions = _info->uses(current.temp());
    for (int i = 0, ei = usePositions.size(); i != ei; ++i) {
        const Use &usePosition = usePositions[i];
        const int usePos = usePosition.pos;
        if (lastUse < usePos && usePos < beforePosition) {
            lastUse = usePos;
        } else if (usePos >= beforePosition) {
            if (!skipOptionalRegisterUses || usePosition.mustHaveRegister()) {
                nextUse = usePos;
                break;
            }
        }
    }
    if (lastUse == -1)
        lastUse = beforePosition - 1;

    Q_ASSERT(lastUse < beforePosition);

#ifdef DEBUG_REGALLOC
    out << "***** last use = "<<lastUse<<", nextUse = " << nextUse<<endl;
#endif // DEBUG_REGALLOC
    LifeTimeInterval newInterval = current.split(lastUse, nextUse);
#ifdef DEBUG_REGALLOC
    out << "***** new interval: "; newInterval.dump(out); out << endl;
    out << "***** preceding interval: "; current.dump(out); out << endl;
#endif // DEBUG_REGALLOC
    if (newInterval.isValid()) {
        if (current.reg() != LifeTimeInterval::Invalid)
            _info->addHint(current.temp(), current.reg());
        newInterval.setReg(LifeTimeInterval::Invalid);
        insertSorted(_unhandled, newInterval);
    }
}

void RegisterAllocator::splitInactiveAtEndOfLifetimeHole(int reg, bool isFPReg, int position)
{
    for (int i = 0, ei = _inactive.size(); i != ei; ++i) {
        LifeTimeInterval &interval = _inactive[i];
        if (interval.isFixedInterval())
            continue;
        if (isFPReg == interval.isFP() && interval.reg() == reg) {
            LifeTimeInterval::Ranges ranges = interval.ranges();
            int endOfLifetimeHole = -1;
            for (int j = 0, ej = ranges.size(); j != ej; ++j) {
                if (position < ranges[j].start)
                    endOfLifetimeHole = ranges[j].start;
            }
            if (endOfLifetimeHole != -1)
                split(interval, endOfLifetimeHole);
        }
    }
}

void RegisterAllocator::assignSpillSlot(const Temp &t, int startPos, int endPos)
{
    if (_assignedSpillSlots.contains(t))
        return;

    for (int i = 0, ei = _activeSpillSlots.size(); i != ei; ++i) {
        if (_activeSpillSlots[i] < startPos) {
            _activeSpillSlots[i] = endPos;
            _assignedSpillSlots.insert(t, i);
            return;
        }
    }

    Q_UNREACHABLE();
}

void RegisterAllocator::dump() const
{
#ifdef DEBUG_REGALLOC
    QTextStream qout(stdout, QIODevice::WriteOnly);

    {
        qout << "Ranges:" << endl;
        QVector<LifeTimeInterval> handled = _handled;
        std::sort(handled.begin(), handled.end(), LifeTimeInterval::lessThanForTemp);
        foreach (const LifeTimeInterval &r, handled) {
            r.dump(qout);
            qout << endl;
        }
    }

    {
        qout << "Spill slots:" << endl;
        QList<Temp> temps = _assignedSpillSlots.keys();
        if (temps.isEmpty())
            qout << "\t(none)" << endl;
        std::sort(temps.begin(), temps.end());
        foreach (const Temp &t, temps) {
            qout << "\t";
            t.dump(qout);
            qout << " -> " << _assignedSpillSlots[t] << endl;
        }
    }
#endif // DEBUG_REGALLOC
}