/
LIR.cpp
3034 lines (2730 loc) · 92.5 KB
/
LIR.cpp
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
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
/* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is [Open Source Virtual Machine].
*
* The Initial Developer of the Original Code is
* Adobe System Incorporated.
* Portions created by the Initial Developer are Copyright (C) 2004-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Adobe AS3 Team
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nanojit.h"
namespace nanojit
{
using namespace avmplus;
#ifdef FEATURE_NANOJIT
const uint8_t repKinds[] = {
#define OP___(op, number, repKind, retType) \
LRK_##repKind,
#include "LIRopcode.tbl"
#undef OP___
0
};
const LTy retTypes[] = {
#define OP___(op, number, repKind, retType) \
LTy_##retType,
#include "LIRopcode.tbl"
#undef OP___
LTy_Void
};
// LIR verbose specific
#ifdef NJ_VERBOSE
const char* lirNames[] = {
#define OP___(op, number, repKind, retType) \
#op,
#include "LIRopcode.tbl"
#undef OP___
NULL
};
#endif /* NANOJIT_VERBOSE */
// implementation
#ifdef NJ_VERBOSE
void ReverseLister::finish()
{
_logc->printf("\n");
_logc->printf("=== BEGIN %s ===\n", _title);
int j = 0;
for (Seq<char*>* p = _strs.get(); p != NULL; p = p->tail)
_logc->printf(" %02d: %s\n", j++, p->head);
_logc->printf("=== END %s ===\n", _title);
_logc->printf("\n");
}
LInsp ReverseLister::read()
{
LInsp i = in->read();
const char* str = _names->formatIns(i);
char* cpy = new (_alloc) char[strlen(str)+1];
VMPI_strcpy(cpy, str);
_strs.insert(cpy);
return i;
}
#endif
#ifdef NJ_PROFILE
// @todo fixup move to nanojit.h
#undef counter_value
#define counter_value(x) x
#endif /* NJ_PROFILE */
// LCompressedBuffer
LirBuffer::LirBuffer(Allocator& alloc) :
#ifdef NJ_VERBOSE
names(NULL),
#endif
abi(ABI_FASTCALL), state(NULL), param1(NULL), sp(NULL), rp(NULL),
_allocator(alloc)
{
clear();
}
void LirBuffer::clear()
{
// clear the stats, etc
_unused = 0;
_limit = 0;
_bytesAllocated = 0;
_stats.lir = 0;
for (int i = 0; i < NumSavedRegs; ++i)
savedRegs[i] = NULL;
chunkAlloc();
}
void LirBuffer::chunkAlloc()
{
_unused = (uintptr_t) _allocator.alloc(CHUNK_SZB);
NanoAssert(_unused != 0); // Allocator.alloc() never returns null. See Allocator.h
_limit = _unused + CHUNK_SZB;
}
int32_t LirBuffer::insCount()
{
return _stats.lir;
}
size_t LirBuffer::byteCount()
{
return _bytesAllocated - (_limit - _unused);
}
// Allocate a new page, and write the first instruction to it -- a skip
// linking to last instruction of the previous page.
void LirBuffer::moveToNewChunk(uintptr_t addrOfLastLInsOnCurrentChunk)
{
chunkAlloc();
// Link LIR stream back to prior instruction.
// Unlike all the ins*() functions, we don't call makeRoom() here
// because we know we have enough space, having just started a new
// page.
LInsSk* insSk = (LInsSk*)_unused;
LIns* ins = insSk->getLIns();
ins->initLInsSk((LInsp)addrOfLastLInsOnCurrentChunk);
_unused += sizeof(LInsSk);
verbose_only(_stats.lir++);
}
// Make room for a single instruction.
uintptr_t LirBuffer::makeRoom(size_t szB)
{
// Make sure the size is ok
NanoAssert(0 == szB % sizeof(void*));
NanoAssert(sizeof(LIns) <= szB && szB <= sizeof(LInsSti)); // LInsSti is the biggest one
NanoAssert(_unused < _limit);
debug_only( bool moved = false; )
// If the instruction won't fit on the current chunk, get a new chunk
if (_unused + szB > _limit) {
uintptr_t addrOfLastLInsOnChunk = _unused - sizeof(LIns);
moveToNewChunk(addrOfLastLInsOnChunk);
debug_only( moved = true; )
}
// We now know that we are on a chunk that has the requested amount of
// room: record the starting address of the requested space and bump
// the pointer.
uintptr_t startOfRoom = _unused;
_unused += szB;
verbose_only(_stats.lir++); // count the instruction
// If there's no more space on this chunk, move to a new one.
// (This will only occur if the asked-for size filled up exactly to
// the end of the chunk.) This ensures that next time we enter this
// function, _unused won't be pointing one byte past the end of
// the chunk, which would break everything.
if (_unused >= _limit) {
// Check we used exactly the remaining space
NanoAssert(_unused == _limit);
NanoAssert(!moved); // shouldn't need to moveToNewChunk twice
uintptr_t addrOfLastLInsOnChunk = _unused - sizeof(LIns);
moveToNewChunk(addrOfLastLInsOnChunk);
}
// Make sure it's word-aligned.
NanoAssert(0 == startOfRoom % sizeof(void*));
return startOfRoom;
}
LInsp LirBufWriter::insStore(LOpcode op, LInsp val, LInsp base, int32_t d)
{
LInsSti* insSti = (LInsSti*)_buf->makeRoom(sizeof(LInsSti));
LIns* ins = insSti->getLIns();
ins->initLInsSti(op, val, base, d);
return ins;
}
LInsp LirBufWriter::ins0(LOpcode op)
{
LInsOp0* insOp0 = (LInsOp0*)_buf->makeRoom(sizeof(LInsOp0));
LIns* ins = insOp0->getLIns();
ins->initLInsOp0(op);
return ins;
}
LInsp LirBufWriter::ins1(LOpcode op, LInsp o1)
{
LInsOp1* insOp1 = (LInsOp1*)_buf->makeRoom(sizeof(LInsOp1));
LIns* ins = insOp1->getLIns();
ins->initLInsOp1(op, o1);
return ins;
}
LInsp LirBufWriter::ins2(LOpcode op, LInsp o1, LInsp o2)
{
LInsOp2* insOp2 = (LInsOp2*)_buf->makeRoom(sizeof(LInsOp2));
LIns* ins = insOp2->getLIns();
ins->initLInsOp2(op, o1, o2);
return ins;
}
LInsp LirBufWriter::ins3(LOpcode op, LInsp o1, LInsp o2, LInsp o3)
{
LInsOp3* insOp3 = (LInsOp3*)_buf->makeRoom(sizeof(LInsOp3));
LIns* ins = insOp3->getLIns();
ins->initLInsOp3(op, o1, o2, o3);
return ins;
}
LInsp LirBufWriter::insLoad(LOpcode op, LInsp base, int32_t d)
{
LInsLd* insLd = (LInsLd*)_buf->makeRoom(sizeof(LInsLd));
LIns* ins = insLd->getLIns();
ins->initLInsLd(op, base, d);
return ins;
}
LInsp LirBufWriter::insGuard(LOpcode op, LInsp c, GuardRecord *gr)
{
debug_only( if (LIR_x == op || LIR_xbarrier == op) NanoAssert(!c); )
return ins2(op, c, (LIns*)gr);
}
LInsp LirBufWriter::insGuardXov(LOpcode op, LInsp a, LInsp b, GuardRecord *gr)
{
return ins3(op, a, b, (LIns*)gr);
}
LInsp LirBufWriter::insBranch(LOpcode op, LInsp condition, LInsp toLabel)
{
NanoAssert((op == LIR_j && !condition) ||
((op == LIR_jf || op == LIR_jt) && condition));
return ins2(op, condition, toLabel);
}
LIns* LirBufWriter::insJtbl(LIns* index, uint32_t size)
{
LInsJtbl* insJtbl = (LInsJtbl*) _buf->makeRoom(sizeof(LInsJtbl));
LIns** table = new (_buf->_allocator) LIns*[size];
LIns* ins = insJtbl->getLIns();
VMPI_memset(table, 0, size * sizeof(LIns*));
ins->initLInsJtbl(index, size, table);
return ins;
}
LInsp LirBufWriter::insAlloc(int32_t size)
{
size = (size+3)>>2; // # of required 32bit words
LInsI* insI = (LInsI*)_buf->makeRoom(sizeof(LInsI));
LIns* ins = insI->getLIns();
ins->initLInsI(LIR_alloc, size);
return ins;
}
LInsp LirBufWriter::insParam(int32_t arg, int32_t kind)
{
LInsP* insP = (LInsP*)_buf->makeRoom(sizeof(LInsP));
LIns* ins = insP->getLIns();
ins->initLInsP(arg, kind);
if (kind) {
NanoAssert(arg < NumSavedRegs);
_buf->savedRegs[arg] = ins;
}
return ins;
}
LInsp LirBufWriter::insImm(int32_t imm)
{
LInsI* insI = (LInsI*)_buf->makeRoom(sizeof(LInsI));
LIns* ins = insI->getLIns();
ins->initLInsI(LIR_int, imm);
return ins;
}
#ifdef NANOJIT_64BIT
LInsp LirBufWriter::insImmq(uint64_t imm)
{
LInsN64* insN64 = (LInsN64*)_buf->makeRoom(sizeof(LInsN64));
LIns* ins = insN64->getLIns();
ins->initLInsN64(LIR_quad, imm);
return ins;
}
#endif
LInsp LirBufWriter::insImmf(double d)
{
LInsN64* insN64 = (LInsN64*)_buf->makeRoom(sizeof(LInsN64));
LIns* ins = insN64->getLIns();
union {
double d;
uint64_t q;
} u;
u.d = d;
ins->initLInsN64(LIR_float, u.q);
return ins;
}
// Reads the next non-skip instruction.
LInsp LirReader::read()
{
static const uint8_t insSizes[] = {
// LIR_start is treated specially -- see below.
#define OP___(op, number, repKind, retType) \
((number) == LIR_start ? 0 : sizeof(LIns##repKind)),
#include "LIRopcode.tbl"
#undef OP___
0
};
// Check the invariant: _i never points to a skip.
NanoAssert(_i && !_i->isop(LIR_skip));
// Step back one instruction. Use a table lookup rather than a switch
// to avoid branch mispredictions. LIR_start is given a special size
// of zero so that we don't step back past the start of the block.
// (Callers of this function should stop once they see a LIR_start.)
LInsp ret = _i;
_i = (LInsp)(uintptr_t(_i) - insSizes[_i->opcode()]);
// Ensure _i doesn't end up pointing to a skip.
while (_i->isop(LIR_skip)) {
NanoAssert(_i->prevLIns() != _i);
_i = _i->prevLIns();
}
return ret;
}
LOpcode f64arith_to_i32arith(LOpcode op)
{
switch (op) {
case LIR_fneg: return LIR_neg;
case LIR_fadd: return LIR_add;
case LIR_fsub: return LIR_sub;
case LIR_fmul: return LIR_mul;
default: NanoAssert(0); return LIR_skip;
}
}
#ifdef NANOJIT_64BIT
LOpcode i32cmp_to_i64cmp(LOpcode op)
{
switch (op) {
case LIR_eq: return LIR_qeq;
case LIR_lt: return LIR_qlt;
case LIR_gt: return LIR_qgt;
case LIR_le: return LIR_qle;
case LIR_ge: return LIR_qge;
case LIR_ult: return LIR_qult;
case LIR_ugt: return LIR_qugt;
case LIR_ule: return LIR_qule;
case LIR_uge: return LIR_quge;
default: NanoAssert(0); return LIR_skip;
}
}
#endif
// This is never called, but that's ok because it contains only static
// assertions.
void LIns::staticSanityCheck()
{
// LIns must be word-sized.
NanoStaticAssert(sizeof(LIns) == 1*sizeof(void*));
// LInsXYZ have expected sizes too.
NanoStaticAssert(sizeof(LInsOp0) == 1*sizeof(void*));
NanoStaticAssert(sizeof(LInsOp1) == 2*sizeof(void*));
NanoStaticAssert(sizeof(LInsOp2) == 3*sizeof(void*));
NanoStaticAssert(sizeof(LInsOp3) == 4*sizeof(void*));
NanoStaticAssert(sizeof(LInsLd) == 3*sizeof(void*));
NanoStaticAssert(sizeof(LInsSti) == 4*sizeof(void*));
NanoStaticAssert(sizeof(LInsSk) == 2*sizeof(void*));
NanoStaticAssert(sizeof(LInsC) == 3*sizeof(void*));
NanoStaticAssert(sizeof(LInsP) == 2*sizeof(void*));
NanoStaticAssert(sizeof(LInsI) == 2*sizeof(void*));
#if defined NANOJIT_64BIT
NanoStaticAssert(sizeof(LInsN64) == 2*sizeof(void*));
#else
NanoStaticAssert(sizeof(LInsN64) == 3*sizeof(void*));
#endif
// oprnd_1 must be in the same position in LIns{Op1,Op2,Op3,Ld,Sti}
// because oprnd1() is used for all of them.
NanoStaticAssert( (offsetof(LInsOp1, ins) - offsetof(LInsOp1, oprnd_1)) ==
(offsetof(LInsOp2, ins) - offsetof(LInsOp2, oprnd_1)) );
NanoStaticAssert( (offsetof(LInsOp2, ins) - offsetof(LInsOp2, oprnd_1)) ==
(offsetof(LInsOp3, ins) - offsetof(LInsOp3, oprnd_1)) );
NanoStaticAssert( (offsetof(LInsOp3, ins) - offsetof(LInsOp3, oprnd_1)) ==
(offsetof(LInsLd, ins) - offsetof(LInsLd, oprnd_1)) );
NanoStaticAssert( (offsetof(LInsLd, ins) - offsetof(LInsLd, oprnd_1)) ==
(offsetof(LInsSti, ins) - offsetof(LInsSti, oprnd_1)) );
// oprnd_2 must be in the same position in LIns{Op2,Op3,Sti}
// because oprnd2() is used for both of them.
NanoStaticAssert( (offsetof(LInsOp2, ins) - offsetof(LInsOp2, oprnd_2)) ==
(offsetof(LInsOp3, ins) - offsetof(LInsOp3, oprnd_2)) );
NanoStaticAssert( (offsetof(LInsOp3, ins) - offsetof(LInsOp3, oprnd_2)) ==
(offsetof(LInsSti, ins) - offsetof(LInsSti, oprnd_2)) );
}
LIns* LirWriter::ins2i(LOpcode v, LIns* oprnd1, int32_t imm)
{
return ins2(v, oprnd1, insImm(imm));
}
bool insIsS16(LInsp i)
{
if (i->isconst()) {
int c = i->imm32();
return isS16(c);
}
if (i->isCmov()) {
return insIsS16(i->oprnd2()) && insIsS16(i->oprnd3());
}
if (i->isCmp())
return true;
// many other possibilities too.
return false;
}
LIns* ExprFilter::ins1(LOpcode v, LIns* oprnd)
{
switch (v) {
#ifdef NANOJIT_64BIT
case LIR_q2i:
if (oprnd->isconstq())
return insImm(oprnd->imm64_0());
break;
#endif
#if NJ_SOFTFLOAT_SUPPORTED
case LIR_qlo:
if (oprnd->isconstq())
return insImm(oprnd->imm64_0());
if (oprnd->isop(LIR_qjoin))
return oprnd->oprnd1();
break;
case LIR_qhi:
if (oprnd->isconstq())
return insImm(oprnd->imm64_1());
if (oprnd->isop(LIR_qjoin))
return oprnd->oprnd2();
break;
#endif
case LIR_not:
if (oprnd->isconst())
return insImm(~oprnd->imm32());
involution:
if (v == oprnd->opcode())
return oprnd->oprnd1();
break;
case LIR_neg:
if (oprnd->isconst())
return insImm(-oprnd->imm32());
if (oprnd->isop(LIR_sub)) // -(a-b) = b-a
return out->ins2(LIR_sub, oprnd->oprnd2(), oprnd->oprnd1());
goto involution;
case LIR_fneg:
if (oprnd->isconstq())
return insImmf(-oprnd->imm64f());
if (oprnd->isop(LIR_fsub))
return out->ins2(LIR_fsub, oprnd->oprnd2(), oprnd->oprnd1());
goto involution;
case LIR_i2f:
if (oprnd->isconst())
return insImmf(oprnd->imm32());
break;
case LIR_f2i:
if (oprnd->isconstq())
return insImm(int32_t(oprnd->imm64f()));
break;
case LIR_u2f:
if (oprnd->isconst())
return insImmf(uint32_t(oprnd->imm32()));
break;
default:
;
}
return out->ins1(v, oprnd);
}
// This is an ugly workaround for an apparent compiler
// bug; in VC2008, compiling with optimization on
// will produce spurious errors if this code is inlined
// into ExprFilter::ins2(). See https://bugzilla.mozilla.org/show_bug.cgi?id=538504
inline double do_join(int32_t c1, int32_t c2)
{
union {
double d;
uint64_t u64;
} u;
u.u64 = uint32_t(c1) | uint64_t(c2)<<32;
return u.d;
}
LIns* ExprFilter::ins2(LOpcode v, LIns* oprnd1, LIns* oprnd2)
{
NanoAssert(oprnd1 && oprnd2);
if (oprnd1 == oprnd2)
{
switch (v) {
case LIR_xor:
case LIR_sub:
case LIR_ult:
case LIR_ugt:
case LIR_gt:
case LIR_lt:
return insImm(0);
case LIR_or:
case LIR_and:
return oprnd1;
case LIR_le:
case LIR_ule:
case LIR_ge:
case LIR_uge:
// x <= x == 1; x >= x == 1
return insImm(1);
default:
;
}
}
if (oprnd1->isconst() && oprnd2->isconst())
{
int32_t c1 = oprnd1->imm32();
int32_t c2 = oprnd2->imm32();
double d;
int32_t r;
switch (v) {
#if NJ_SOFTFLOAT_SUPPORTED
case LIR_qjoin:
return insImmf(do_join(c1, c2));
#endif
case LIR_eq:
return insImm(c1 == c2);
case LIR_lt:
return insImm(c1 < c2);
case LIR_gt:
return insImm(c1 > c2);
case LIR_le:
return insImm(c1 <= c2);
case LIR_ge:
return insImm(c1 >= c2);
case LIR_ult:
return insImm(uint32_t(c1) < uint32_t(c2));
case LIR_ugt:
return insImm(uint32_t(c1) > uint32_t(c2));
case LIR_ule:
return insImm(uint32_t(c1) <= uint32_t(c2));
case LIR_uge:
return insImm(uint32_t(c1) >= uint32_t(c2));
case LIR_rsh:
return insImm(int32_t(c1) >> int32_t(c2));
case LIR_lsh:
return insImm(int32_t(c1) << int32_t(c2));
case LIR_ush:
return insImm(uint32_t(c1) >> int32_t(c2));
case LIR_or:
return insImm(uint32_t(c1) | int32_t(c2));
case LIR_and:
return insImm(uint32_t(c1) & int32_t(c2));
case LIR_xor:
return insImm(uint32_t(c1) ^ int32_t(c2));
case LIR_add:
d = double(c1) + double(c2);
fold:
r = int32_t(d);
if (r == d)
return insImm(r);
break;
case LIR_sub:
d = double(c1) - double(c2);
goto fold;
case LIR_mul:
d = double(c1) * double(c2);
goto fold;
CASE86(LIR_div:)
CASE86(LIR_mod:)
#if defined NANOJIT_IA32 || defined NANOJIT_X64
// We can't easily fold div and mod, since folding div makes it
// impossible to calculate the mod that refers to it. The
// frontend shouldn't emit div and mod with constant operands.
NanoAssert(0);
#endif
default:
;
}
}
else if (oprnd1->isconstq() && oprnd2->isconstq())
{
double c1 = oprnd1->imm64f();
double c2 = oprnd2->imm64f();
switch (v) {
case LIR_feq:
return insImm(c1 == c2);
case LIR_flt:
return insImm(c1 < c2);
case LIR_fgt:
return insImm(c1 > c2);
case LIR_fle:
return insImm(c1 <= c2);
case LIR_fge:
return insImm(c1 >= c2);
case LIR_fadd:
return insImmf(c1 + c2);
case LIR_fsub:
return insImmf(c1 - c2);
case LIR_fmul:
return insImmf(c1 * c2);
case LIR_fdiv:
return insImmf(c1 / c2);
default:
;
}
}
else if (oprnd1->isconst() && !oprnd2->isconst())
{
LIns* t;
switch (v) {
case LIR_add:
CASE32(LIR_iaddp:)
case LIR_mul:
case LIR_fadd:
case LIR_fmul:
case LIR_xor:
case LIR_or:
case LIR_and:
case LIR_eq:
// move const to rhs
t = oprnd2;
oprnd2 = oprnd1;
oprnd1 = t;
break;
default:
if (v >= LIR_lt && v <= LIR_uge) {
NanoStaticAssert((LIR_lt ^ 1) == LIR_gt);
NanoStaticAssert((LIR_le ^ 1) == LIR_ge);
NanoStaticAssert((LIR_ult ^ 1) == LIR_ugt);
NanoStaticAssert((LIR_ule ^ 1) == LIR_uge);
// move const to rhs, swap the operator
LIns *t = oprnd2;
oprnd2 = oprnd1;
oprnd1 = t;
v = LOpcode(v^1);
}
break;
}
}
if (oprnd2->isconst())
{
int c = oprnd2->imm32();
switch (v) {
case LIR_add:
if (oprnd1->isop(LIR_add) && oprnd1->oprnd2()->isconst()) {
// add(add(x,c1),c2) => add(x,c1+c2)
c += oprnd1->oprnd2()->imm32();
oprnd2 = insImm(c);
oprnd1 = oprnd1->oprnd1();
}
break;
case LIR_sub:
if (oprnd1->isop(LIR_add) && oprnd1->oprnd2()->isconst()) {
// sub(add(x,c1),c2) => add(x,c1-c2)
c = oprnd1->oprnd2()->imm32() - c;
oprnd2 = insImm(c);
oprnd1 = oprnd1->oprnd1();
v = LIR_add;
}
break;
case LIR_rsh:
if (c == 16 && oprnd1->isop(LIR_lsh) &&
oprnd1->oprnd2()->isconstval(16) &&
insIsS16(oprnd1->oprnd1())) {
// rsh(lhs(x,16),16) == x, if x is S16
return oprnd1->oprnd1();
}
break;
default:
;
}
if (c == 0) {
switch (v) {
case LIR_add:
CASE32(LIR_iaddp:)
case LIR_or:
case LIR_xor:
case LIR_sub:
case LIR_lsh:
case LIR_rsh:
case LIR_ush:
return oprnd1;
case LIR_and:
case LIR_mul:
return oprnd2;
case LIR_eq:
if (oprnd1->isop(LIR_or) &&
oprnd1->oprnd2()->isconst() &&
oprnd1->oprnd2()->imm32() != 0) {
// (x or c) != 0 if c != 0
return insImm(0);
}
default:
;
}
} else if (c == -1 || (c == 1 && oprnd1->isCmp())) {
switch (v) {
case LIR_or:
// x | -1 = -1, cmp | 1 = 1
return oprnd2;
case LIR_and:
// x & -1 = x, cmp & 1 = cmp
return oprnd1;
default:
;
}
} else if (c == 1 && v == LIR_mul) {
return oprnd1;
}
}
#if NJ_SOFTFLOAT_SUPPORTED
LInsp ins;
if (v == LIR_qjoin && oprnd1->isop(LIR_qlo) && oprnd2->isop(LIR_qhi) &&
(ins = oprnd1->oprnd1()) == oprnd2->oprnd1()) {
// qjoin(qlo(x),qhi(x)) == x
return ins;
}
#endif
return out->ins2(v, oprnd1, oprnd2);
}
LIns* ExprFilter::ins3(LOpcode v, LIns* oprnd1, LIns* oprnd2, LIns* oprnd3)
{
NanoAssert(oprnd1 && oprnd2 && oprnd3);
NanoAssert(isCmovOpcode(v));
if (oprnd2 == oprnd3) {
// c ? a : a => a
return oprnd2;
}
if (oprnd1->isconst()) {
// const ? x : y => return x or y depending on const
return oprnd1->imm32() ? oprnd2 : oprnd3;
}
if (oprnd1->isop(LIR_eq) &&
((oprnd1->oprnd2() == oprnd2 && oprnd1->oprnd1() == oprnd3) ||
(oprnd1->oprnd1() == oprnd2 && oprnd1->oprnd2() == oprnd3))) {
// (y == x) ? x : y => y
// (x == y) ? x : y => y
return oprnd3;
}
return out->ins3(v, oprnd1, oprnd2, oprnd3);
}
LIns* ExprFilter::insGuard(LOpcode v, LInsp c, GuardRecord *gr)
{
if (v == LIR_xt || v == LIR_xf) {
if (c->isconst()) {
if ((v == LIR_xt && !c->imm32()) || (v == LIR_xf && c->imm32())) {
return 0; // no guard needed
}
else {
#ifdef JS_TRACER
// We're emitting a guard that will always fail. Any code
// emitted after this guard is dead code. We could
// silently optimize out the rest of the emitted code, but
// this could indicate a performance problem or other bug,
// so assert in debug builds.
NanoAssertMsg(0, "Constantly false guard detected");
#endif
return out->insGuard(LIR_x, NULL, gr);
}
}
else {
NanoStaticAssert((LIR_xt ^ 1) == LIR_xf);
while (c->isop(LIR_eq) && c->oprnd1()->isCmp() &&
c->oprnd2()->isconstval(0)) {
// xt(eq(cmp,0)) => xf(cmp) or xf(eq(cmp,0)) => xt(cmp)
v = LOpcode(v^1);
c = c->oprnd1();
}
}
}
return out->insGuard(v, c, gr);
}
LIns* ExprFilter::insGuardXov(LOpcode op, LInsp oprnd1, LInsp oprnd2, GuardRecord *gr)
{
if (oprnd1->isconst() && oprnd2->isconst()) {
int32_t c1 = oprnd1->imm32();
int32_t c2 = oprnd2->imm32();
double d;
switch (op) {
case LIR_addxov: d = double(c1) + double(c2); break;
case LIR_subxov: d = double(c1) - double(c2); break;
case LIR_mulxov: d = double(c1) * double(c2); break;
default: NanoAssert(0); break;
}
int32_t r = int32_t(d);
if (r == d)
return insImm(r);
} else if (oprnd1->isconst() && !oprnd2->isconst()) {
switch (op) {
case LIR_addxov:
case LIR_mulxov: {
// move const to rhs
LIns* t = oprnd2;
oprnd2 = oprnd1;
oprnd1 = t;
break;
}
case LIR_subxov:
break;
default:
NanoAssert(0);
}
}
if (oprnd2->isconst()) {
int c = oprnd2->imm32();
if (c == 0) {
switch (op) {
case LIR_addxov:
case LIR_subxov:
return oprnd1;
case LIR_mulxov:
return oprnd2;
default:
;
}
} else if (c == 1 && op == LIR_mulxov) {
return oprnd1;
}
}
return out->insGuardXov(op, oprnd1, oprnd2, gr);
}
LIns* ExprFilter::insBranch(LOpcode v, LIns *c, LIns *t)
{
switch (v) {
case LIR_jt:
case LIR_jf:
while (c->isop(LIR_eq) && c->oprnd1()->isCmp() && c->oprnd2()->isconstval(0)) {
// jt(eq(cmp,0)) => jf(cmp) or jf(eq(cmp,0)) => jt(cmp)
v = LOpcode(v ^ 1);
c = c->oprnd1();
}
break;
default:
;
}
return out->insBranch(v, c, t);
}
LIns* ExprFilter::insLoad(LOpcode op, LIns* base, int32_t off) {
if (base->isconstp() && !isS8(off)) {
// if the effective address is constant, then transform:
// ld const[bigconst] => ld (const+bigconst)[0]
// note: we don't do this optimization for <8bit field offsets,
// under the assumption that we're more likely to CSE-match the
// constant base address if we dont const-fold small offsets.
uintptr_t p = (uintptr_t)base->constvalp() + off;
return out->insLoad(op, insImmPtr((void*)p), 0);
}
return out->insLoad(op, base, off);
}
LIns* LirWriter::ins_eq0(LIns* oprnd1)
{
return ins2i(LIR_eq, oprnd1, 0);
}
LIns* LirWriter::ins_peq0(LIns* oprnd1)
{
return ins2(LIR_peq, oprnd1, insImmWord(0));
}
LIns* LirWriter::ins_i2p(LIns* intIns)
{
#ifdef NANOJIT_64BIT
return ins1(LIR_i2q, intIns);
#else
return intIns;
#endif
}
LIns* LirWriter::ins_u2p(LIns* uintIns)
{
#ifdef NANOJIT_64BIT
return ins1(LIR_u2q, uintIns);
#else
return uintIns;
#endif
}
LIns* LirWriter::insStorei(LIns* value, LIns* base, int32_t d)
{
// Determine which kind of store should be used for 'value' based on
// its type.
LOpcode op = LOpcode(0);
switch (value->retType()) {
case LTy_I32: op = LIR_sti; break;
#ifdef NANOJIT_64BIT
case LTy_I64: op = LIR_stqi; break;
#endif
case LTy_F64: op = LIR_stfi; break;
case LTy_Void: NanoAssert(0); break;
default: NanoAssert(0); break;
}
return insStore(op, value, base, d);
}
#if NJ_SOFTFLOAT_SUPPORTED
LIns* LirWriter::qjoin(LInsp lo, LInsp hi)
{
return ins2(LIR_qjoin, lo, hi);
}
#endif
LIns* LirWriter::insImmWord(intptr_t value)
{
#ifdef NANOJIT_64BIT
return insImmq(value);
#else
return insImm(value);
#endif
}
LIns* LirWriter::insImmPtr(const void *ptr)
{
#ifdef NANOJIT_64BIT
return insImmq((uint64_t)ptr);
#else
return insImm((int32_t)ptr);
#endif
}
LIns* LirWriter::ins_choose(LIns* cond, LIns* iftrue, LIns* iffalse, bool use_cmov)
{
// if not a conditional, make it implicitly an ==0 test (then flop results)
if (!cond->isCmp())
{
cond = ins_eq0(cond);
LInsp tmp = iftrue;
iftrue = iffalse;
iffalse = tmp;
}
if (use_cmov) {
LOpcode op = LIR_cmov;
if (iftrue->isI32() && iffalse->isI32()) {
op = LIR_cmov;
#ifdef NANOJIT_64BIT
} else if (iftrue->isI64() && iffalse->isI64()) {
op = LIR_qcmov;
#endif
} else if (iftrue->isF64() && iffalse->isF64()) {
NanoAssertMsg(0, "LIR_fcmov doesn't exist yet, sorry");
} else {