summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/backend/mips/instruction-scheduler-mips.cc
blob: 4e6aef52f49f70b38ded0d11e75d25d917fa93a1 (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
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/backend/code-generator.h"
#include "src/compiler/backend/instruction-scheduler.h"

namespace v8 {
namespace internal {
namespace compiler {

bool InstructionScheduler::SchedulerSupported() { return true; }

int InstructionScheduler::GetTargetInstructionFlags(
    const Instruction* instr) const {
  switch (instr->arch_opcode()) {
    case kMipsAbsD:
    case kMipsAbsS:
    case kMipsAdd:
    case kMipsAddD:
    case kMipsAddOvf:
    case kMipsAddPair:
    case kMipsAddS:
    case kMipsAnd:
    case kMipsByteSwap32:
    case kMipsCeilWD:
    case kMipsCeilWS:
    case kMipsClz:
    case kMipsCmp:
    case kMipsCmpD:
    case kMipsCmpS:
    case kMipsCtz:
    case kMipsCvtDS:
    case kMipsCvtDUw:
    case kMipsCvtDW:
    case kMipsCvtSD:
    case kMipsCvtSUw:
    case kMipsCvtSW:
    case kMipsDiv:
    case kMipsDivD:
    case kMipsDivS:
    case kMipsDivU:
    case kMipsExt:
    case kMipsF32x4Abs:
    case kMipsF32x4Add:
    case kMipsF32x4AddHoriz:
    case kMipsF32x4Eq:
    case kMipsF32x4ExtractLane:
    case kMipsF32x4Le:
    case kMipsF32x4Lt:
    case kMipsF32x4Max:
    case kMipsF32x4Min:
    case kMipsF32x4Mul:
    case kMipsF32x4Div:
    case kMipsF32x4Ne:
    case kMipsF32x4Neg:
    case kMipsF32x4RecipApprox:
    case kMipsF32x4RecipSqrtApprox:
    case kMipsF32x4ReplaceLane:
    case kMipsF32x4SConvertI32x4:
    case kMipsF32x4Splat:
    case kMipsF32x4Sub:
    case kMipsF32x4UConvertI32x4:
    case kMipsFloat32Max:
    case kMipsFloat32Min:
    case kMipsFloat32RoundDown:
    case kMipsFloat32RoundTiesEven:
    case kMipsFloat32RoundTruncate:
    case kMipsFloat32RoundUp:
    case kMipsFloat64ExtractHighWord32:
    case kMipsFloat64ExtractLowWord32:
    case kMipsFloat64InsertHighWord32:
    case kMipsFloat64InsertLowWord32:
    case kMipsFloat64Max:
    case kMipsFloat64Min:
    case kMipsFloat64RoundDown:
    case kMipsFloat64RoundTiesEven:
    case kMipsFloat64RoundTruncate:
    case kMipsFloat64RoundUp:
    case kMipsFloat64SilenceNaN:
    case kMipsFloorWD:
    case kMipsFloorWS:
    case kMipsI16x8Add:
    case kMipsI16x8AddHoriz:
    case kMipsI16x8AddSaturateS:
    case kMipsI16x8AddSaturateU:
    case kMipsI16x8Eq:
    case kMipsI16x8ExtractLane:
    case kMipsI16x8GeS:
    case kMipsI16x8GeU:
    case kMipsI16x8GtS:
    case kMipsI16x8GtU:
    case kMipsI16x8MaxS:
    case kMipsI16x8MaxU:
    case kMipsI16x8MinS:
    case kMipsI16x8MinU:
    case kMipsI16x8Mul:
    case kMipsI16x8Ne:
    case kMipsI16x8Neg:
    case kMipsI16x8ReplaceLane:
    case kMipsI16x8SConvertI32x4:
    case kMipsI16x8SConvertI8x16High:
    case kMipsI16x8SConvertI8x16Low:
    case kMipsI16x8Shl:
    case kMipsI16x8ShrS:
    case kMipsI16x8ShrU:
    case kMipsI16x8Splat:
    case kMipsI16x8Sub:
    case kMipsI16x8SubSaturateS:
    case kMipsI16x8SubSaturateU:
    case kMipsI16x8UConvertI32x4:
    case kMipsI16x8UConvertI8x16High:
    case kMipsI16x8UConvertI8x16Low:
    case kMipsI32x4Add:
    case kMipsI32x4AddHoriz:
    case kMipsI32x4Eq:
    case kMipsI32x4ExtractLane:
    case kMipsI32x4GeS:
    case kMipsI32x4GeU:
    case kMipsI32x4GtS:
    case kMipsI32x4GtU:
    case kMipsI32x4MaxS:
    case kMipsI32x4MaxU:
    case kMipsI32x4MinS:
    case kMipsI32x4MinU:
    case kMipsI32x4Mul:
    case kMipsI32x4Ne:
    case kMipsI32x4Neg:
    case kMipsI32x4ReplaceLane:
    case kMipsI32x4SConvertF32x4:
    case kMipsI32x4SConvertI16x8High:
    case kMipsI32x4SConvertI16x8Low:
    case kMipsI32x4Shl:
    case kMipsI32x4ShrS:
    case kMipsI32x4ShrU:
    case kMipsI32x4Splat:
    case kMipsI32x4Sub:
    case kMipsI32x4UConvertF32x4:
    case kMipsI32x4UConvertI16x8High:
    case kMipsI32x4UConvertI16x8Low:
    case kMipsI8x16Add:
    case kMipsI8x16AddSaturateS:
    case kMipsI8x16AddSaturateU:
    case kMipsI8x16Eq:
    case kMipsI8x16ExtractLane:
    case kMipsI8x16GeS:
    case kMipsI8x16GeU:
    case kMipsI8x16GtS:
    case kMipsI8x16GtU:
    case kMipsI8x16MaxS:
    case kMipsI8x16MaxU:
    case kMipsI8x16MinS:
    case kMipsI8x16MinU:
    case kMipsI8x16Mul:
    case kMipsI8x16Ne:
    case kMipsI8x16Neg:
    case kMipsI8x16ReplaceLane:
    case kMipsI8x16SConvertI16x8:
    case kMipsI8x16Shl:
    case kMipsI8x16ShrS:
    case kMipsI8x16ShrU:
    case kMipsI8x16Splat:
    case kMipsI8x16Sub:
    case kMipsI8x16SubSaturateS:
    case kMipsI8x16SubSaturateU:
    case kMipsI8x16UConvertI16x8:
    case kMipsIns:
    case kMipsLsa:
    case kMipsMaddD:
    case kMipsMaddS:
    case kMipsMaxD:
    case kMipsMaxS:
    case kMipsMinD:
    case kMipsMinS:
    case kMipsMod:
    case kMipsModU:
    case kMipsMov:
    case kMipsMsubD:
    case kMipsMsubS:
    case kMipsMul:
    case kMipsMulD:
    case kMipsMulHigh:
    case kMipsMulHighU:
    case kMipsMulOvf:
    case kMipsMulPair:
    case kMipsMulS:
    case kMipsNegD:
    case kMipsNegS:
    case kMipsNor:
    case kMipsOr:
    case kMipsPopcnt:
    case kMipsRor:
    case kMipsRoundWD:
    case kMipsRoundWS:
    case kMipsS128And:
    case kMipsS128Not:
    case kMipsS128Or:
    case kMipsS128Select:
    case kMipsS128Xor:
    case kMipsS128Zero:
    case kMipsS16x2Reverse:
    case kMipsS16x4Reverse:
    case kMipsS16x8InterleaveEven:
    case kMipsS16x8InterleaveLeft:
    case kMipsS16x8InterleaveOdd:
    case kMipsS16x8InterleaveRight:
    case kMipsS16x8PackEven:
    case kMipsS16x8PackOdd:
    case kMipsS1x16AllTrue:
    case kMipsS1x16AnyTrue:
    case kMipsS1x4AllTrue:
    case kMipsS1x4AnyTrue:
    case kMipsS1x8AllTrue:
    case kMipsS1x8AnyTrue:
    case kMipsS32x4InterleaveEven:
    case kMipsS32x4InterleaveLeft:
    case kMipsS32x4InterleaveOdd:
    case kMipsS32x4InterleaveRight:
    case kMipsS32x4PackEven:
    case kMipsS32x4PackOdd:
    case kMipsS32x4Shuffle:
    case kMipsS8x16Concat:
    case kMipsS8x16InterleaveEven:
    case kMipsS8x16InterleaveLeft:
    case kMipsS8x16InterleaveOdd:
    case kMipsS8x16InterleaveRight:
    case kMipsS8x16PackEven:
    case kMipsS8x16PackOdd:
    case kMipsS8x16Shuffle:
    case kMipsS8x2Reverse:
    case kMipsS8x4Reverse:
    case kMipsS8x8Reverse:
    case kMipsSar:
    case kMipsSarPair:
    case kMipsSeb:
    case kMipsSeh:
    case kMipsShl:
    case kMipsShlPair:
    case kMipsShr:
    case kMipsShrPair:
    case kMipsSqrtD:
    case kMipsSqrtS:
    case kMipsSub:
    case kMipsSubD:
    case kMipsSubOvf:
    case kMipsSubPair:
    case kMipsSubS:
    case kMipsTruncUwD:
    case kMipsTruncUwS:
    case kMipsTruncWD:
    case kMipsTruncWS:
    case kMipsTst:
    case kMipsXor:
      return kNoOpcodeFlags;

    case kMipsLb:
    case kMipsLbu:
    case kMipsLdc1:
    case kMipsLh:
    case kMipsLhu:
    case kMipsLw:
    case kMipsLwc1:
    case kMipsMsaLd:
    case kMipsPeek:
    case kMipsUldc1:
    case kMipsUlh:
    case kMipsUlhu:
    case kMipsUlw:
    case kMipsUlwc1:
    case kMipsWord32AtomicPairLoad:
      return kIsLoadOperation;

    case kMipsModD:
    case kMipsModS:
    case kMipsMsaSt:
    case kMipsPush:
    case kMipsSb:
    case kMipsSdc1:
    case kMipsSh:
    case kMipsStackClaim:
    case kMipsStoreToStackSlot:
    case kMipsSw:
    case kMipsSwc1:
    case kMipsUsdc1:
    case kMipsUsh:
    case kMipsUsw:
    case kMipsUswc1:
    case kMipsSync:
    case kMipsWord32AtomicPairStore:
    case kMipsWord32AtomicPairAdd:
    case kMipsWord32AtomicPairSub:
    case kMipsWord32AtomicPairAnd:
    case kMipsWord32AtomicPairOr:
    case kMipsWord32AtomicPairXor:
    case kMipsWord32AtomicPairExchange:
    case kMipsWord32AtomicPairCompareExchange:
      return kHasSideEffect;

#define CASE(Name) case k##Name:
      COMMON_ARCH_OPCODE_LIST(CASE)
#undef CASE
      // Already covered in architecture independent code.
      UNREACHABLE();
  }

  UNREACHABLE();
}

enum Latency {
  BRANCH = 4,  // Estimated max.
  RINT_S = 4,  // Estimated.
  RINT_D = 4,  // Estimated.

  MULT = 4,
  MULTU = 4,
  MADD = 4,
  MADDU = 4,
  MSUB = 4,
  MSUBU = 4,

  MUL = 7,
  MULU = 7,
  MUH = 7,
  MUHU = 7,

  DIV = 50,  // Min:11 Max:50
  DIVU = 50,

  ABS_S = 4,
  ABS_D = 4,
  NEG_S = 4,
  NEG_D = 4,
  ADD_S = 4,
  ADD_D = 4,
  SUB_S = 4,
  SUB_D = 4,
  MAX_S = 4,  // Estimated.
  MAX_D = 4,  // Estimated.
  C_cond_S = 4,
  C_cond_D = 4,
  MUL_S = 4,

  MADD_S = 4,
  MSUB_S = 4,
  NMADD_S = 4,
  NMSUB_S = 4,

  CABS_cond_S = 4,
  CABS_cond_D = 4,

  CVT_D_S = 4,
  CVT_PS_PW = 4,

  CVT_S_W = 4,
  CVT_S_L = 4,
  CVT_D_W = 4,
  CVT_D_L = 4,

  CVT_S_D = 4,

  CVT_W_S = 4,
  CVT_W_D = 4,
  CVT_L_S = 4,
  CVT_L_D = 4,

  CEIL_W_S = 4,
  CEIL_W_D = 4,
  CEIL_L_S = 4,
  CEIL_L_D = 4,

  FLOOR_W_S = 4,
  FLOOR_W_D = 4,
  FLOOR_L_S = 4,
  FLOOR_L_D = 4,

  ROUND_W_S = 4,
  ROUND_W_D = 4,
  ROUND_L_S = 4,
  ROUND_L_D = 4,

  TRUNC_W_S = 4,
  TRUNC_W_D = 4,
  TRUNC_L_S = 4,
  TRUNC_L_D = 4,

  MOV_S = 4,
  MOV_D = 4,

  MOVF_S = 4,
  MOVF_D = 4,

  MOVN_S = 4,
  MOVN_D = 4,

  MOVT_S = 4,
  MOVT_D = 4,

  MOVZ_S = 4,
  MOVZ_D = 4,

  MUL_D = 5,
  MADD_D = 5,
  MSUB_D = 5,
  NMADD_D = 5,
  NMSUB_D = 5,

  RECIP_S = 13,
  RECIP_D = 26,

  RSQRT_S = 17,
  RSQRT_D = 36,

  DIV_S = 17,
  SQRT_S = 17,

  DIV_D = 32,
  SQRT_D = 32,

  MTC1 = 4,
  MTHC1 = 4,
  DMTC1 = 4,
  LWC1 = 4,
  LDC1 = 4,
  LDXC1 = 4,
  LUXC1 = 4,
  LWXC1 = 4,

  MFC1 = 1,
  MFHC1 = 1,
  MFHI = 1,
  MFLO = 1,
  DMFC1 = 1,
  SWC1 = 1,
  SDC1 = 1,
  SDXC1 = 1,
  SUXC1 = 1,
  SWXC1 = 1,
};

int ClzLatency() {
  if (IsMipsArchVariant(kLoongson)) {
    return (6 + 2 * Latency::BRANCH);
  } else {
    return 1;
  }
}

int RorLatency(bool is_operand_register = true) {
  if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    if (is_operand_register) {
      return 4;
    } else {
      return 3;  // Estimated max.
    }
  }
}

int AdduLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    return 1;
  } else {
    return 2;  // Estimated max.
  }
}

int XorLatency(bool is_operand_register = true) {
  return AdduLatency(is_operand_register);
}

int AndLatency(bool is_operand_register = true) {
  return AdduLatency(is_operand_register);
}

int OrLatency(bool is_operand_register = true) {
  return AdduLatency(is_operand_register);
}

int SubuLatency(bool is_operand_register = true) {
  return AdduLatency(is_operand_register);
}

int MulLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    if (IsMipsArchVariant(kLoongson)) {
      return Latency::MULT + 1;
    } else {
      return Latency::MUL + 1;
    }
  } else {
    if (IsMipsArchVariant(kLoongson)) {
      return Latency::MULT + 2;
    } else {
      return Latency::MUL + 2;
    }
  }
}

int NorLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    return 1;
  } else {
    return 2;
  }
}

int InsLatency() {
  if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    return SubuLatency(false) + 7;
  }
}

int ShlPairLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    int latency =
        AndLatency(false) + NorLatency() + OrLatency() + AndLatency(false) + 4;
    if (IsMipsArchVariant(kLoongson) || IsMipsArchVariant(kMips32r6)) {
      return latency + Latency::BRANCH + 2;
    } else {
      return latency + 2;
    }
  } else {
    return 2;
  }
}

int ShrPairLatency(bool is_operand_register = true, uint32_t shift = 0) {
  if (is_operand_register) {
    int latency =
        AndLatency(false) + NorLatency() + OrLatency() + AndLatency(false) + 4;
    if (IsMipsArchVariant(kLoongson) || IsMipsArchVariant(kMips32r6)) {
      return latency + Latency::BRANCH + 2;
    } else {
      return latency + 2;
    }
  } else {
    // Estimated max.
    return (InsLatency() + 2 > OrLatency() + 3) ? InsLatency() + 2
                                                : OrLatency() + 3;
  }
}

int SarPairLatency(bool is_operand_register = true, uint32_t shift = 0) {
  if (is_operand_register) {
    return AndLatency(false) + NorLatency() + OrLatency() + AndLatency(false) +
           Latency::BRANCH + 6;
  } else {
    shift = shift & 0x3F;
    if (shift == 0) {
      return 2;
    } else if (shift < 32) {
      if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
        return InsLatency() + 2;
      } else {
        return OrLatency() + 3;
      }
    } else if (shift == 32) {
      return 2;
    } else {
      return 2;
    }
  }
}

int ExtLatency() {
  if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    // Estimated max.
    return 2;
  }
}

int LsaLatency() {
  // Estimated max.
  return AdduLatency() + 1;
}

int SltLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    return 1;
  } else {
    return 2;  // Estimated max.
  }
}

int SltuLatency(bool is_operand_register = true) {
  return SltLatency(is_operand_register);
}

int AddPairLatency() { return 3 * AdduLatency() + SltLatency(); }

int SubPairLatency() { return SltuLatency() + 3 * SubuLatency(); }

int MuluLatency(bool is_operand_register = true) {
  int latency = 0;
  if (!is_operand_register) latency++;
  if (!IsMipsArchVariant(kMips32r6)) {
    return latency + Latency::MULTU + 2;
  } else {
    return latency + Latency::MULU + Latency::MUHU;
  }
}

int MulPairLatency() {
  return MuluLatency() + 2 * MulLatency() + 2 * AdduLatency();
}

int MaddSLatency() {
  if (IsMipsArchVariant(kMips32r2)) {
    return Latency::MADD_D;
  } else {
    return Latency::MUL_D + Latency::ADD_D;
  }
}

int MaddDLatency() {
  if (IsMipsArchVariant(kMips32r2)) {
    return Latency::MADD_D;
  } else {
    return Latency::MUL_D + Latency::ADD_D;
  }
}

int MsubSLatency() {
  if (IsMipsArchVariant(kMips32r2)) {
    return Latency::MSUB_S;
  } else {
    return Latency::MUL_S + Latency::SUB_S;
  }
}

int MsubDLatency() {
  if (IsMipsArchVariant(kMips32r2)) {
    return Latency::MSUB_D;
  } else {
    return Latency::MUL_D + Latency::SUB_D;
  }
}

int Mfhc1Latency() {
  if (IsFp32Mode()) {
    return Latency::MFC1;
  } else {
    return 1;
  }
}

int Mthc1Latency() {
  if (IsFp32Mode()) {
    return Latency::MTC1;
  } else {
    return 1;
  }
}

int MoveLatency(bool is_double_register = true) {
  if (!is_double_register) {
    return Latency::MTC1 + 1;
  } else {
    return Mthc1Latency() + 1;  // Estimated.
  }
}

int Float64RoundLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return Latency::RINT_D + 4;
  } else {
    // For ceil_l_d, floor_l_d, round_l_d, trunc_l_d latency is 4.
    return Mfhc1Latency() + ExtLatency() + Latency::BRANCH + Latency::MOV_D +
           4 + MoveLatency() + 1 + Latency::BRANCH + Latency::CVT_D_L;
  }
}

int Float32RoundLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return Latency::RINT_S + 4;
  } else {
    // For ceil_w_s, floor_w_s, round_w_s, trunc_w_s latency is 4.
    return Latency::MFC1 + ExtLatency() + Latency::BRANCH + Latency::MOV_S + 4 +
           Latency::MFC1 + Latency::BRANCH + Latency::CVT_S_W;
  }
}

int CvtDUwLatency() {
  if (IsFp64Mode()) {
    return Latency::MTC1 + Mthc1Latency() + Latency::CVT_D_L;
  } else {
    return Latency::BRANCH + Latency::MTC1 + 1 + Latency::MTC1 +
           Mthc1Latency() + Latency::CVT_D_W + Latency::BRANCH +
           Latency::ADD_D + Latency::CVT_D_W;
  }
}

int CvtSUwLatency() { return CvtDUwLatency() + Latency::CVT_S_D; }

int Floor_w_dLatency() {
  if (IsMipsArchVariant(kLoongson)) {
    return Mfhc1Latency() + Latency::FLOOR_W_D + Mthc1Latency();
  } else {
    return Latency::FLOOR_W_D;
  }
}

int FloorWDLatency() { return Floor_w_dLatency() + Latency::MFC1; }

int Ceil_w_dLatency() {
  if (IsMipsArchVariant(kLoongson)) {
    return Mfhc1Latency() + Latency::CEIL_W_D + Mthc1Latency();
  } else {
    return Latency::CEIL_W_D;
  }
}

int CeilWDLatency() { return Ceil_w_dLatency() + Latency::MFC1; }

int Round_w_dLatency() {
  if (IsMipsArchVariant(kLoongson)) {
    return Mfhc1Latency() + Latency::ROUND_W_D + Mthc1Latency();
  } else {
    return Latency::ROUND_W_D;
  }
}

int RoundWDLatency() { return Round_w_dLatency() + Latency::MFC1; }

int Trunc_w_dLatency() {
  if (IsMipsArchVariant(kLoongson)) {
    return Mfhc1Latency() + Latency::TRUNC_W_D + Mthc1Latency();
  } else {
    return Latency::TRUNC_W_D;
  }
}

int MovnLatency() {
  if (IsMipsArchVariant(kLoongson) || IsMipsArchVariant(kMips32r6)) {
    return Latency::BRANCH + 1;
  } else {
    return 1;
  }
}

int Trunc_uw_dLatency() {
  return 1 + Latency::MTC1 + Mthc1Latency() + Latency::BRANCH + Latency::SUB_D +
         Latency::TRUNC_W_D + Latency::MFC1 + OrLatency(false) +
         Latency::BRANCH + Latency::TRUNC_W_D + Latency::MFC1;
}

int Trunc_uw_sLatency() {
  return 1 + Latency::MTC1 + Latency::BRANCH + Latency::SUB_S +
         Latency::TRUNC_W_S + Latency::MFC1 + OrLatency(false) +
         Latency::TRUNC_W_S + Latency::MFC1;
}

int MovzLatency() {
  if (IsMipsArchVariant(kLoongson) || IsMipsArchVariant(kMips32r6)) {
    return Latency::BRANCH + 1;
  } else {
    return 1;
  }
}

int FmoveLowLatency() {
  if (IsFp32Mode()) {
    return Latency::MTC1;
  } else {
    return Latency::MFHC1 + Latency::MTC1 + Latency::MTHC1;
  }
}

int SebLatency() {
  if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    return 2;
  }
}

int SehLatency() {
  if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    return 2;
  }
}

int UlhuLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    return 4;
  }
}

int UlhLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    return 4;
  }
}

int AdjustBaseAndOffsetLatency() {
  return 3;  // Estimated max.
}

int UshLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    return AdjustBaseAndOffsetLatency() + 4;  // Estimated max.
  }
}

int UlwLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    return AdjustBaseAndOffsetLatency() + 3;  // Estimated max.
  }
}

int UswLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return 1;
  } else {
    return AdjustBaseAndOffsetLatency() + 2;
  }
}

int Ulwc1Latency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return Latency::LWC1;
  } else {
    return UlwLatency() + Latency::MTC1;
  }
}

int Uswc1Latency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return Latency::SWC1;
  } else {
    return Latency::MFC1 + UswLatency();
  }
}

int Ldc1Latency() {
  int latency = AdjustBaseAndOffsetLatency() + Latency::LWC1;
  if (IsFp32Mode()) {
    return latency + Latency::LWC1;
  } else {
    return latency + 1 + Mthc1Latency();
  }
}

int Uldc1Latency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return Ldc1Latency();
  } else {
    return 2 * UlwLatency() + Latency::MTC1 + Mthc1Latency();
  }
}

int Sdc1Latency() {
  int latency = AdjustBaseAndOffsetLatency() + Latency::SWC1;
  if (IsFp32Mode()) {
    return latency + Latency::SWC1;
  } else {
    return latency + Mfhc1Latency() + 1;
  }
}

int Usdc1Latency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return Sdc1Latency();
  } else {
    return Latency::MFC1 + 2 * UswLatency() + Mfhc1Latency();
  }
}

int PushRegisterLatency() { return AdduLatency(false) + 1; }

int ByteSwapSignedLatency() {
  // operand_size == 4
  if (IsMipsArchVariant(kMips32r2) || IsMipsArchVariant(kMips32r6)) {
    return 2;
  } else if (IsMipsArchVariant(kMips32r1) || IsMipsArchVariant(kLoongson)) {
    return 10;
  }
}

int LlLatency(int offset) {
  bool is_one_instruction =
      IsMipsArchVariant(kMips32r6) ? is_int9(offset) : is_int16(offset);
  if (is_one_instruction) {
    return 1;
  } else {
    return 3;
  }
}

int ExtractBitsLatency(int size, bool sign_extend) {
  int latency = 1 + ExtLatency();
  if (size == 8) {
    if (sign_extend) {
      return latency + SebLatency();
    } else {
      return 0;
    }
  } else if (size == 16) {
    if (sign_extend) {
      return latency + SehLatency();
    } else {
      return 0;
    }
  } else {
    UNREACHABLE();
  }
}

int NegLatency() { return 1; }

int InsertBitsLatency() {
  return RorLatency() + InsLatency() + SubuLatency(false) + NegLatency() +
         RorLatency();
}

int ScLatency(int offset) {
  bool is_one_instruction =
      IsMipsArchVariant(kMips32r6) ? is_int9(offset) : is_int16(offset);
  if (is_one_instruction) {
    return 1;
  } else {
    return 3;
  }
}

int BranchShortHelperR6Latency() {
  return 2;  // Estimated max.
}

int BranchShortHelperLatency() {
  return SltLatency() + 2;  // Estimated max.
}

int BranchShortLatency(BranchDelaySlot bdslot = PROTECT) {
  if (IsMipsArchVariant(kMips32r6) && bdslot == PROTECT) {
    return BranchShortHelperR6Latency();
  } else {
    return BranchShortHelperLatency();
  }
}

int Word32AtomicExchangeLatency(bool sign_extend, int size) {
  return AdduLatency() + 1 + SubuLatency() + 2 + LlLatency(0) +
         ExtractBitsLatency(size, sign_extend) + InsertBitsLatency() +
         ScLatency(0) + BranchShortLatency() + 1;
}

int Word32AtomicCompareExchangeLatency(bool sign_extend, int size) {
  return AdduLatency() + 1 + SubuLatency() + 2 + LlLatency(0) +
         ExtractBitsLatency(size, sign_extend) + BranchShortLatency() + 1;
}

int AddOverflowLatency() {
  return 6;  // Estimated max.
}

int SubOverflowLatency() {
  return 6;  // Estimated max.
}

int MulhLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    if (!IsMipsArchVariant(kMips32r6)) {
      return Latency::MULT + Latency::MFHI;
    } else {
      return Latency::MUH;
    }
  } else {
    if (!IsMipsArchVariant(kMips32r6)) {
      return 1 + Latency::MULT + Latency::MFHI;
    } else {
      return 1 + Latency::MUH;
    }
  }
}

int MulhuLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    if (!IsMipsArchVariant(kMips32r6)) {
      return Latency::MULTU + Latency::MFHI;
    } else {
      return Latency::MUHU;
    }
  } else {
    if (!IsMipsArchVariant(kMips32r6)) {
      return 1 + Latency::MULTU + Latency::MFHI;
    } else {
      return 1 + Latency::MUHU;
    }
  }
}

int MulOverflowLatency() {
  return MulLatency() + 4;  // Estimated max.
}

int ModLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    if (!IsMipsArchVariant(kMips32r6)) {
      return Latency::DIV + Latency::MFHI;
    } else {
      return 1;
    }
  } else {
    if (!IsMipsArchVariant(kMips32r6)) {
      return 1 + Latency::DIV + Latency::MFHI;
    } else {
      return 2;
    }
  }
}

int ModuLatency(bool is_operand_register = true) {
  return ModLatency(is_operand_register);
}

int DivLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    if (!IsMipsArchVariant(kMips32r6)) {
      return Latency::DIV + Latency::MFLO;
    } else {
      return Latency::DIV;
    }
  } else {
    if (!IsMipsArchVariant(kMips32r6)) {
      return 1 + Latency::DIV + Latency::MFLO;
    } else {
      return 1 + Latency::DIV;
    }
  }
}

int DivuLatency(bool is_operand_register = true) {
  if (is_operand_register) {
    if (!IsMipsArchVariant(kMips32r6)) {
      return Latency::DIVU + Latency::MFLO;
    } else {
      return Latency::DIVU;
    }
  } else {
    if (!IsMipsArchVariant(kMips32r6)) {
      return 1 + Latency::DIVU + Latency::MFLO;
    } else {
      return 1 + Latency::DIVU;
    }
  }
}

int CtzLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return RorLatency(false) + 2 + ClzLatency();
  } else {
    return AdduLatency(false) + XorLatency() + AndLatency() + ClzLatency() + 1 +
           SubuLatency();
  }
}

int PopcntLatency() {
  return 4 * AndLatency() + SubuLatency() + 2 * AdduLatency() + MulLatency() +
         8;
}

int CompareFLatency() { return Latency::C_cond_S; }

int CompareIsNanFLatency() { return CompareFLatency(); }

int CompareIsNanF32Latency() { return CompareIsNanFLatency(); }

int Neg_sLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return Latency::NEG_S;
  } else {
    // Estimated.
    return CompareIsNanF32Latency() + 2 * Latency::BRANCH + Latency::NEG_S +
           Latency::MFC1 + 1 + XorLatency() + Latency::MTC1;
  }
}

int CompareIsNanF64Latency() { return CompareIsNanFLatency(); }

int Neg_dLatency() {
  if (IsMipsArchVariant(kMips32r6)) {
    return Latency::NEG_D;
  } else {
    // Estimated.
    return CompareIsNanF64Latency() + 2 * Latency::BRANCH + Latency::NEG_D +
           Mfhc1Latency() + 1 + XorLatency() + Mthc1Latency();
  }
}

int CompareF32Latency() { return CompareFLatency(); }

int Move_sLatency() {
  return Latency::MOV_S;  // Estimated max.
}

int Float32MaxLatency() {
  // Estimated max.
  int latency = CompareIsNanF32Latency() + Latency::BRANCH;
  if (IsMipsArchVariant(kMips32r6)) {
    return latency + Latency::MAX_S;
  } else {
    return latency + 5 * Latency::BRANCH + 2 * CompareF32Latency() +
           Latency::MFC1 + Move_sLatency();
  }
}

int CompareF64Latency() { return CompareF32Latency(); }

int Move_dLatency() {
  return Latency::MOV_D;  // Estimated max.
}

int Float64MaxLatency() {
  // Estimated max.
  int latency = CompareIsNanF64Latency() + Latency::BRANCH;
  if (IsMipsArchVariant(kMips32r6)) {
    return latency + Latency::MAX_D;
  } else {
    return latency + 5 * Latency::BRANCH + 2 * CompareF64Latency() +
           Latency::MFHC1 + 2 * Move_dLatency();
  }
}

int PrepareCallCFunctionLatency() {
  int frame_alignment = TurboAssembler::ActivationFrameAlignment();
  if (frame_alignment > kSystemPointerSize) {
    return 1 + SubuLatency(false) + AndLatency(false) + 1;
  } else {
    return SubuLatency(false);
  }
}

int MovToFloatParametersLatency() { return 2 * MoveLatency(); }

int CallLatency() {
  // Estimated.
  return AdduLatency(false) + Latency::BRANCH + 3;
}

int CallCFunctionHelperLatency() {
  // Estimated.
  int latency = AndLatency(false) + Latency::BRANCH + 2 + CallLatency();
  if (base::OS::ActivationFrameAlignment() > kSystemPointerSize) {
    latency++;
  } else {
    latency += AdduLatency(false);
  }
  return latency;
}

int CallCFunctionLatency() { return 1 + CallCFunctionHelperLatency(); }

int MovFromFloatResultLatency() { return MoveLatency(); }

int Float32MinLatency() {
  // Estimated max.
  return CompareIsNanF32Latency() + Latency::BRANCH +
         2 * (CompareF32Latency() + Latency::BRANCH) + Latency::MFC1 +
         2 * Latency::BRANCH + Move_sLatency();
}

int Float64MinLatency() {
  // Estimated max.
  return CompareIsNanF64Latency() + Latency::BRANCH +
         2 * (CompareF64Latency() + Latency::BRANCH) + Mfhc1Latency() +
         2 * Latency::BRANCH + Move_dLatency();
}

int SmiUntagLatency() { return 1; }

int PrepareForTailCallLatency() {
  // Estimated max.
  return 2 * (LsaLatency() + AdduLatency(false)) + 2 + Latency::BRANCH +
         Latency::BRANCH + 2 * SubuLatency(false) + 2 + Latency::BRANCH + 1;
}

int AssemblePopArgumentsAdaptorFrameLatency() {
  return 1 + Latency::BRANCH + 1 + SmiUntagLatency() +
         PrepareForTailCallLatency();
}

int JumpLatency() {
  // Estimated max.
  return 1 + AdduLatency(false) + Latency::BRANCH + 2;
}

int AssertLatency() { return 1; }

int MultiPushLatency() {
  int latency = SubuLatency(false);
  for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
    latency++;
  }
  return latency;
}

int MultiPushFPULatency() {
  int latency = SubuLatency(false);
  for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
    latency += Sdc1Latency();
  }
  return latency;
}

int PushCallerSavedLatency(SaveFPRegsMode fp_mode) {
  int latency = MultiPushLatency();
  if (fp_mode == kSaveFPRegs) {
    latency += MultiPushFPULatency();
  }
  return latency;
}

int MultiPopFPULatency() {
  int latency = 0;
  for (int16_t i = 0; i < kNumRegisters; i++) {
    latency += Ldc1Latency();
  }
  return latency++;
}

int MultiPopLatency() {
  int latency = 0;
  for (int16_t i = 0; i < kNumRegisters; i++) {
    latency++;
  }
  return latency++;
}

int PopCallerSavedLatency(SaveFPRegsMode fp_mode) {
  int latency = 0;
  if (fp_mode == kSaveFPRegs) {
    latency += MultiPopFPULatency();
  }
  return latency + MultiPopLatency();
}

int AssembleArchJumpLatency() {
  // Estimated max.
  return Latency::BRANCH;
}

int AssembleArchLookupSwitchLatency(int cases) {
  return cases * (1 + Latency::BRANCH) + AssembleArchJumpLatency();
}

int AssembleArchBinarySearchSwitchLatency(int cases) {
  if (cases < CodeGenerator::kBinarySearchSwitchMinimalCases) {
    return AssembleArchLookupSwitchLatency(cases);
  }
  return 1 + Latency::BRANCH + AssembleArchBinarySearchSwitchLatency(cases / 2);
}

int GenerateSwitchTableLatency() {
  int latency = 0;
  if (kArchVariant >= kMips32r6) {
    latency = LsaLatency() + 2;
  } else {
    latency = 6;
  }
  latency += 2;
  return latency;
}

int AssembleArchTableSwitchLatency() {
  return Latency::BRANCH + GenerateSwitchTableLatency();
}

int AssembleReturnLatency() {
  // Estimated max.
  return AdduLatency(false) + MultiPopLatency() + MultiPopFPULatency() +
         Latency::BRANCH + 1 + AdduLatency() + 8;
}

int TryInlineTruncateDoubleToILatency() {
  return 2 + Latency::TRUNC_W_D + Latency::MFC1 + 2 + AndLatency(false) +
         Latency::BRANCH;
}

int CallStubDelayedLatency() { return 1 + CallLatency(); }

int TruncateDoubleToIDelayedLatency() {
  // TODO(mips): This no longer reflects how TruncateDoubleToI is called.
  return TryInlineTruncateDoubleToILatency() + 1 + SubuLatency(false) +
         Sdc1Latency() + CallStubDelayedLatency() + AdduLatency(false) + 1;
}

int CheckPageFlagLatency() {
  return 2 * AndLatency(false) + 1 + Latency::BRANCH;
}

int InstructionScheduler::GetInstructionLatency(const Instruction* instr) {
  // Basic latency modeling for MIPS32 instructions. They have been determined
  // in an empirical way.
  switch (instr->arch_opcode()) {
    case kArchCallCodeObject:
    case kArchCallWasmFunction:
      return CallLatency();
    case kArchTailCallCodeObjectFromJSFunction:
    case kArchTailCallCodeObject: {
      int latency = 0;
      if (instr->arch_opcode() == kArchTailCallCodeObjectFromJSFunction) {
        latency = AssemblePopArgumentsAdaptorFrameLatency();
      }
      return latency + JumpLatency();
    }
    case kArchTailCallWasm:
    case kArchTailCallAddress:
      return JumpLatency();
    case kArchCallJSFunction: {
      int latency = 0;
      if (FLAG_debug_code) {
        latency = 1 + AssertLatency();
      }
      return latency + 1 + AdduLatency(false) + CallLatency();
    }
    case kArchPrepareCallCFunction:
      return PrepareCallCFunctionLatency();
    case kArchSaveCallerRegisters: {
      auto fp_mode =
          static_cast<SaveFPRegsMode>(MiscField::decode(instr->opcode()));
      return PushCallerSavedLatency(fp_mode);
    }
    case kArchRestoreCallerRegisters: {
      auto fp_mode =
          static_cast<SaveFPRegsMode>(MiscField::decode(instr->opcode()));
      return PopCallerSavedLatency(fp_mode);
    }
    case kArchPrepareTailCall:
      return 2;  // Estimated max.
    case kArchCallCFunction:
      return CallCFunctionLatency();
    case kArchJmp:
      return AssembleArchJumpLatency();
    case kArchBinarySearchSwitch:
      return AssembleArchBinarySearchSwitchLatency((instr->InputCount() - 2) /
                                                   2);
    case kArchLookupSwitch:
      return AssembleArchLookupSwitchLatency((instr->InputCount() - 2) / 2);
    case kArchTableSwitch:
      return AssembleArchTableSwitchLatency();
    case kArchAbortCSAAssert:
      return CallLatency() + 1;
    case kArchComment:
    case kArchDeoptimize:
      return 0;
    case kArchRet:
      return AssembleReturnLatency();
    case kArchTruncateDoubleToI:
      return TruncateDoubleToIDelayedLatency();
    case kArchStoreWithWriteBarrier:
      return AdduLatency() + 1 + CheckPageFlagLatency();
    case kArchStackSlot: {
      // Estimated max.
      return AdduLatency(false) + AndLatency(false) + AssertLatency() +
             AdduLatency(false) + AndLatency(false) + BranchShortLatency() + 1 +
             SubuLatency() + AdduLatency();
    }
    case kArchWordPoisonOnSpeculation:
      return AndLatency();
    case kIeee754Float64Acos:
    case kIeee754Float64Acosh:
    case kIeee754Float64Asin:
    case kIeee754Float64Asinh:
    case kIeee754Float64Atan:
    case kIeee754Float64Atanh:
    case kIeee754Float64Atan2:
    case kIeee754Float64Cos:
    case kIeee754Float64Cosh:
    case kIeee754Float64Cbrt:
    case kIeee754Float64Exp:
    case kIeee754Float64Expm1:
    case kIeee754Float64Log:
    case kIeee754Float64Log1p:
    case kIeee754Float64Log10:
    case kIeee754Float64Log2:
    case kIeee754Float64Pow:
    case kIeee754Float64Sin:
    case kIeee754Float64Sinh:
    case kIeee754Float64Tan:
    case kIeee754Float64Tanh:
      return PrepareCallCFunctionLatency() + MovToFloatParametersLatency() +
             CallCFunctionLatency() + MovFromFloatResultLatency();
    case kMipsAdd:
      return AdduLatency(instr->InputAt(1)->IsRegister());
    case kMipsAnd:
      return AndLatency(instr->InputAt(1)->IsRegister());
    case kMipsOr:
      return OrLatency(instr->InputAt(1)->IsRegister());
    case kMipsXor:
      return XorLatency(instr->InputAt(1)->IsRegister());
    case kMipsSub:
      return SubuLatency(instr->InputAt(1)->IsRegister());
    case kMipsNor:
      return NorLatency(instr->InputAt(1)->IsRegister());
    case kMipsAddOvf:
      return AddOverflowLatency();
    case kMipsSubOvf:
      return SubOverflowLatency();
    case kMipsMul:
      return MulLatency(false);
    case kMipsMulHigh:
      return MulhLatency(instr->InputAt(1)->IsRegister());
    case kMipsMulHighU:
      return MulhuLatency(instr->InputAt(1)->IsRegister());
    case kMipsMulOvf:
      return MulOverflowLatency();
    case kMipsMod:
      return ModLatency(instr->InputAt(1)->IsRegister());
    case kMipsModU:
      return ModuLatency(instr->InputAt(1)->IsRegister());
    case kMipsDiv: {
      int latency = DivLatency(instr->InputAt(1)->IsRegister());
      if (IsMipsArchVariant(kMips32r6)) {
        return latency++;
      } else {
        return latency + MovzLatency();
      }
    }
    case kMipsDivU: {
      int latency = DivuLatency(instr->InputAt(1)->IsRegister());
      if (IsMipsArchVariant(kMips32r6)) {
        return latency++;
      } else {
        return latency + MovzLatency();
      }
    }
    case kMipsClz:
      return ClzLatency();
    case kMipsCtz:
      return CtzLatency();
    case kMipsPopcnt:
      return PopcntLatency();
    case kMipsShlPair: {
      if (instr->InputAt(2)->IsRegister()) {
        return ShlPairLatency();
      } else {
        return ShlPairLatency(false);
      }
    }
    case kMipsShrPair: {
      if (instr->InputAt(2)->IsRegister()) {
        return ShrPairLatency();
      } else {
        // auto immediate_operand = ImmediateOperand::cast(instr->InputAt(2));
        // return ShrPairLatency(false, immediate_operand->inline_value());
        return 1;
      }
    }
    case kMipsSarPair: {
      if (instr->InputAt(2)->IsRegister()) {
        return SarPairLatency();
      } else {
        return SarPairLatency(false);
      }
    }
    case kMipsExt:
      return ExtLatency();
    case kMipsIns:
      return InsLatency();
    case kMipsRor:
      return RorLatency(instr->InputAt(1)->IsRegister());
    case kMipsLsa:
      return LsaLatency();
    case kMipsModS:
    case kMipsModD:
      return PrepareCallCFunctionLatency() + MovToFloatParametersLatency() +
             CallCFunctionLatency() + MovFromFloatResultLatency();
    case kMipsAddPair:
      return AddPairLatency();
    case kMipsSubPair:
      return SubPairLatency();
    case kMipsMulPair:
      return MulPairLatency();
    case kMipsMaddS:
      return MaddSLatency();
    case kMipsMaddD:
      return MaddDLatency();
    case kMipsMsubS:
      return MsubSLatency();
    case kMipsMsubD:
      return MsubDLatency();
    case kMipsNegS:
      return Neg_sLatency();
    case kMipsNegD:
      return Neg_dLatency();
    case kMipsFloat64RoundDown:
    case kMipsFloat64RoundTruncate:
    case kMipsFloat64RoundUp:
    case kMipsFloat64RoundTiesEven:
      return Float64RoundLatency();
    case kMipsFloat32RoundDown:
    case kMipsFloat32RoundTruncate:
    case kMipsFloat32RoundUp:
    case kMipsFloat32RoundTiesEven:
      return Float32RoundLatency();
    case kMipsFloat32Max:
      return Float32MaxLatency();
    case kMipsFloat64Max:
      return Float64MaxLatency();
    case kMipsFloat32Min:
      return Float32MinLatency();
    case kMipsFloat64Min:
      return Float64MinLatency();
    case kMipsCvtSUw:
      return CvtSUwLatency();
    case kMipsCvtDUw:
      return CvtDUwLatency();
    case kMipsFloorWD:
      return FloorWDLatency();
    case kMipsCeilWD:
      return CeilWDLatency();
    case kMipsRoundWD:
      return RoundWDLatency();
    case kMipsTruncWD:
      return Trunc_w_dLatency() + Latency::MFC1;
    case kMipsTruncWS:
      return Latency::TRUNC_W_S + Latency::MFC1 + AdduLatency(false) +
             SltLatency() + MovnLatency();
    case kMipsTruncUwD:
      return Trunc_uw_dLatency();
    case kMipsTruncUwS:
      return Trunc_uw_sLatency() + AdduLatency(false) + MovzLatency();
    case kMipsFloat64ExtractLowWord32:
      return Latency::MFC1;
    case kMipsFloat64ExtractHighWord32:
      return Mfhc1Latency();
    case kMipsFloat64InsertLowWord32: {
      if (IsFp32Mode()) {
        return Latency::MTC1;
      } else {
        return Latency::MFHC1 + Latency::MTC1 + Latency::MTHC1;
      }
    }
    case kMipsFloat64InsertHighWord32:
      return Mthc1Latency();
    case kMipsFloat64SilenceNaN:
      return Latency::SUB_D;
    case kMipsSeb:
      return SebLatency();
    case kMipsSeh:
      return SehLatency();
    case kMipsUlhu:
      return UlhuLatency();
    case kMipsUlh:
      return UlhLatency();
    case kMipsUsh:
      return UshLatency();
    case kMipsUlw:
      return UlwLatency();
    case kMipsUsw:
      return UswLatency();
    case kMipsUlwc1:
      return Ulwc1Latency();
    case kMipsSwc1:
      return MoveLatency(false) + Latency::SWC1;  // Estimated max.
    case kMipsUswc1:
      return MoveLatency(false) + Uswc1Latency();  // Estimated max.
    case kMipsLdc1:
      return Ldc1Latency();
    case kMipsUldc1:
      return Uldc1Latency();
    case kMipsSdc1:
      return MoveLatency(false) + Sdc1Latency();  // Estimated max.
    case kMipsUsdc1:
      return MoveLatency(false) + Usdc1Latency();  // Estimated max.
    case kMipsPush: {
      if (instr->InputAt(0)->IsFPRegister()) {
        auto op = LocationOperand::cast(instr->InputAt(0));
        switch (op->representation()) {
          case MachineRepresentation::kFloat32:
            return Latency::SWC1 + SubuLatency(false);
            break;
          case MachineRepresentation::kFloat64:
            return Sdc1Latency() + SubuLatency(false);
            break;
          default: {
            UNREACHABLE();
            break;
          }
        }
      } else {
        return PushRegisterLatency();
      }
      break;
    }
    case kMipsPeek: {
      if (instr->OutputAt(0)->IsFPRegister()) {
        auto op = LocationOperand::cast(instr->OutputAt(0));
        if (op->representation() == MachineRepresentation::kFloat64) {
          return Ldc1Latency();
        } else {
          return Latency::LWC1;
        }
      } else {
        return 1;
      }
      break;
    }
    case kMipsStackClaim:
      return SubuLatency(false);
    case kMipsStoreToStackSlot: {
      if (instr->InputAt(0)->IsFPRegister()) {
        auto op = LocationOperand::cast(instr->InputAt(0));
        if (op->representation() == MachineRepresentation::kFloat64) {
          return Sdc1Latency();
        } else if (op->representation() == MachineRepresentation::kFloat32) {
          return Latency::SWC1;
        } else {
          return 1;  // Estimated value.
        }
      } else {
        return 1;
      }
      break;
    }
    case kMipsByteSwap32:
      return ByteSwapSignedLatency();
    case kWord32AtomicLoadInt8:
    case kWord32AtomicLoadUint8:
    case kWord32AtomicLoadInt16:
    case kWord32AtomicLoadUint16:
    case kWord32AtomicLoadWord32:
      return 2;
    case kWord32AtomicStoreWord8:
    case kWord32AtomicStoreWord16:
    case kWord32AtomicStoreWord32:
      return 3;
    case kWord32AtomicExchangeInt8:
      return Word32AtomicExchangeLatency(true, 8);
    case kWord32AtomicExchangeUint8:
      return Word32AtomicExchangeLatency(false, 8);
    case kWord32AtomicExchangeInt16:
      return Word32AtomicExchangeLatency(true, 16);
    case kWord32AtomicExchangeUint16:
      return Word32AtomicExchangeLatency(false, 16);
    case kWord32AtomicExchangeWord32: {
      return 1 + AdduLatency() + Ldc1Latency() + 1 + ScLatency(0) +
             BranchShortLatency() + 1;
    }
    case kWord32AtomicCompareExchangeInt8:
      return Word32AtomicCompareExchangeLatency(true, 8);
    case kWord32AtomicCompareExchangeUint8:
      return Word32AtomicCompareExchangeLatency(false, 8);
    case kWord32AtomicCompareExchangeInt16:
      return Word32AtomicCompareExchangeLatency(true, 16);
    case kWord32AtomicCompareExchangeUint16:
      return Word32AtomicCompareExchangeLatency(false, 16);
    case kWord32AtomicCompareExchangeWord32:
      return AdduLatency() + 1 + LlLatency(0) + BranchShortLatency() + 1;
    case kMipsTst:
      return AndLatency(instr->InputAt(1)->IsRegister());
    case kMipsCmpS:
      return MoveLatency() + CompareF32Latency();
    case kMipsCmpD:
      return MoveLatency() + CompareF64Latency();
    case kArchNop:
    case kArchThrowTerminator:
    case kMipsCmp:
      return 0;
    case kArchDebugBreak:
    case kArchFramePointer:
    case kArchParentFramePointer:
    case kMipsShl:
    case kMipsShr:
    case kMipsSar:
    case kMipsMov:
    case kMipsMaxS:
    case kMipsMinS:
    case kMipsMaxD:
    case kMipsMinD:
    case kMipsLbu:
    case kMipsLb:
    case kMipsSb:
    case kMipsLhu:
    case kMipsLh:
    case kMipsSh:
    case kMipsLw:
    case kMipsSw:
    case kMipsLwc1:
      return 1;
    case kMipsAddS:
      return Latency::ADD_S;
    case kMipsSubS:
      return Latency::SUB_S;
    case kMipsMulS:
      return Latency::MUL_S;
    case kMipsAbsS:
      return Latency::ABS_S;
    case kMipsAddD:
      return Latency::ADD_D;
    case kMipsSubD:
      return Latency::SUB_D;
    case kMipsAbsD:
      return Latency::ABS_D;
    case kMipsCvtSD:
      return Latency::CVT_S_D;
    case kMipsCvtDS:
      return Latency::CVT_D_S;
    case kMipsMulD:
      return Latency::MUL_D;
    case kMipsFloorWS:
      return Latency::FLOOR_W_S;
    case kMipsCeilWS:
      return Latency::CEIL_W_S;
    case kMipsRoundWS:
      return Latency::ROUND_W_S;
    case kMipsCvtDW:
      return Latency::CVT_D_W;
    case kMipsCvtSW:
      return Latency::CVT_S_W;
    case kMipsDivS:
      return Latency::DIV_S;
    case kMipsSqrtS:
      return Latency::SQRT_S;
    case kMipsDivD:
      return Latency::DIV_D;
    case kMipsSqrtD:
      return Latency::SQRT_D;
    default:
      return 1;
  }
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8