-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
emitxarch.cpp
15955 lines (13686 loc) · 478 KB
/
emitxarch.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX emitX86.cpp XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#if defined(TARGET_XARCH)
/*****************************************************************************/
/*****************************************************************************/
#include "instr.h"
#include "emit.h"
#include "codegen.h"
bool IsSSEInstruction(instruction ins)
{
return (ins >= INS_FIRST_SSE_INSTRUCTION) && (ins <= INS_LAST_SSE_INSTRUCTION);
}
bool IsSSEOrAVXInstruction(instruction ins)
{
return (ins >= INS_FIRST_SSE_INSTRUCTION) && (ins <= INS_LAST_AVX_INSTRUCTION);
}
bool IsAVXOnlyInstruction(instruction ins)
{
return (ins >= INS_FIRST_AVX_INSTRUCTION) && (ins <= INS_LAST_AVX_INSTRUCTION);
}
bool IsFMAInstruction(instruction ins)
{
return (ins >= INS_FIRST_FMA_INSTRUCTION) && (ins <= INS_LAST_FMA_INSTRUCTION);
}
bool IsAVXVNNIInstruction(instruction ins)
{
return (ins >= INS_FIRST_AVXVNNI_INSTRUCTION) && (ins <= INS_LAST_AVXVNNI_INSTRUCTION);
}
bool IsBMIInstruction(instruction ins)
{
return (ins >= INS_FIRST_BMI_INSTRUCTION) && (ins <= INS_LAST_BMI_INSTRUCTION);
}
regNumber getBmiRegNumber(instruction ins)
{
switch (ins)
{
case INS_blsi:
{
return (regNumber)3;
}
case INS_blsmsk:
{
return (regNumber)2;
}
case INS_blsr:
{
return (regNumber)1;
}
default:
{
assert(IsBMIInstruction(ins));
return REG_NA;
}
}
}
regNumber getSseShiftRegNumber(instruction ins)
{
switch (ins)
{
case INS_psrldq:
{
return (regNumber)3;
}
case INS_pslldq:
{
return (regNumber)7;
}
case INS_psrld:
case INS_psrlw:
case INS_psrlq:
{
return (regNumber)2;
}
case INS_pslld:
case INS_psllw:
case INS_psllq:
{
return (regNumber)6;
}
case INS_psrad:
case INS_psraw:
{
return (regNumber)4;
}
default:
{
assert(!"Invalid instruction for SSE2 instruction of the form: opcode reg, immed8");
return REG_NA;
}
}
}
bool emitter::IsAVXInstruction(instruction ins)
{
return UseVEXEncoding() && IsSSEOrAVXInstruction(ins);
}
// Returns true if the AVX instruction is a binary operator that requires 3 operands.
// When we emit an instruction with only two operands, we will duplicate the destination
// as a source.
// TODO-XArch-Cleanup: This is a temporary solution for now. Eventually this needs to
// be formalized by adding an additional field to instruction table to
// to indicate whether a 3-operand instruction.
bool emitter::IsDstDstSrcAVXInstruction(instruction ins)
{
return ((CodeGenInterface::instInfo[ins] & INS_Flags_IsDstDstSrcAVXInstruction) != 0) && IsAVXInstruction(ins);
}
// Returns true if the AVX instruction requires 3 operands that duplicate the source
// register in the vvvv field.
// TODO-XArch-Cleanup: This is a temporary solution for now. Eventually this needs to
// be formalized by adding an additional field to instruction table to
// to indicate whether a 3-operand instruction.
bool emitter::IsDstSrcSrcAVXInstruction(instruction ins)
{
return ((CodeGenInterface::instInfo[ins] & INS_Flags_IsDstSrcSrcAVXInstruction) != 0) && IsAVXInstruction(ins);
}
//------------------------------------------------------------------------
// DoesWriteZeroFlag: check if the instruction write the
// ZF flag.
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if instruction writes the ZF flag, false otherwise.
//
bool emitter::DoesWriteZeroFlag(instruction ins)
{
return (CodeGenInterface::instInfo[ins] & Writes_ZF) != 0;
}
//------------------------------------------------------------------------
// DoesResetOverflowAndCarryFlags: check if the instruction resets the
// OF and CF flag to 0.
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if instruction resets the OF and CF flag, false otherwise.
//
bool emitter::DoesResetOverflowAndCarryFlags(instruction ins)
{
return (CodeGenInterface::instInfo[ins] & (Resets_OF | Resets_CF)) == (Resets_OF | Resets_CF);
}
//------------------------------------------------------------------------
// IsFlagsAlwaysModified: check if the instruction guarantee to modify any flags.
//
// Arguments:
// id - instruction to test
//
// Return Value:
// false, if instruction is guaranteed to not modify any flag.
// true, if instruction will modify some flag.
//
bool emitter::IsFlagsAlwaysModified(instrDesc* id)
{
instruction ins = id->idIns();
insFormat fmt = id->idInsFmt();
if (fmt == IF_RRW_SHF)
{
if (id->idIsLargeCns())
{
return true;
}
else if (id->idSmallCns() == 0)
{
switch (ins)
{
// If shift-amount for below instructions is 0, then flags are unaffected.
case INS_rcl_N:
case INS_rcr_N:
case INS_rol_N:
case INS_ror_N:
case INS_shl_N:
case INS_shr_N:
case INS_sar_N:
return false;
default:
return true;
}
}
}
else if (fmt == IF_RRW)
{
switch (ins)
{
// If shift-amount for below instructions is 0, then flags are unaffected.
// So, to be conservative, do not optimize if the instruction has register
// as the shift-amount operand.
case INS_rcl:
case INS_rcr:
case INS_rol:
case INS_ror:
case INS_shl:
case INS_shr:
case INS_sar:
return false;
default:
return true;
}
}
return true;
}
//------------------------------------------------------------------------
// AreUpper32BitsZero: check if some previously emitted
// instruction set the upper 32 bits of reg to zero.
//
// Arguments:
// reg - register of interest
//
// Return Value:
// true if previous instruction zeroed reg's upper 32 bits.
// false if it did not, or if we can't safely determine.
//
// Notes:
// Currently only looks back one instruction.
//
// movsx eax, ... might seem viable but we always encode this
// instruction with a 64 bit destination. See TakesRexWPrefix.
bool emitter::AreUpper32BitsZero(regNumber reg)
{
// If there are no instructions in this IG, we can look back at
// the previous IG's instructions if this IG is an extension.
//
if ((emitCurIGinsCnt == 0) && ((emitCurIG->igFlags & IGF_EXTEND) == 0))
{
return false;
}
instrDesc* id = emitLastIns;
insFormat fmt = id->idInsFmt();
// This isn't meant to be a comprehensive check. Just look for what
// seems to be common.
switch (fmt)
{
case IF_RWR_CNS:
case IF_RRW_CNS:
case IF_RRW_SHF:
case IF_RWR_RRD:
case IF_RRW_RRD:
case IF_RWR_MRD:
case IF_RWR_SRD:
case IF_RWR_ARD:
// Bail if not writing to the right register
if (id->idReg1() != reg)
{
return false;
}
// Bail if movsx, we always have movsx sign extend to 8 bytes
if (id->idIns() == INS_movsx)
{
return false;
}
// movzx always zeroes the upper 32 bits.
if (id->idIns() == INS_movzx)
{
return true;
}
// Else rely on operation size.
return (id->idOpSize() == EA_4BYTE);
default:
break;
}
return false;
}
//------------------------------------------------------------------------
// AreFlagsSetToZeroCmp: Checks if the previous instruction set the SZ, and optionally OC, flags to
// the same values as if there were a compare to 0
//
// Arguments:
// reg - register of interest
// opSize - size of register
// treeOps - type of tree node operation
//
// Return Value:
// true if the previous instruction set the flags for reg
// false if not, or if we can't safely determine
//
// Notes:
// Currently only looks back one instruction.
bool emitter::AreFlagsSetToZeroCmp(regNumber reg, emitAttr opSize, genTreeOps treeOps)
{
assert(reg != REG_NA);
// Don't look back across IG boundaries (possible control flow)
if (emitCurIGinsCnt == 0 && ((emitCurIG->igFlags & IGF_EXTEND) == 0))
{
return false;
}
instrDesc* id = emitLastIns;
instruction lastIns = id->idIns();
insFormat fmt = id->idInsFmt();
// make sure op1 is a reg
switch (fmt)
{
case IF_RWR_CNS:
case IF_RRW_CNS:
case IF_RRW_SHF:
case IF_RWR_RRD:
case IF_RRW_RRD:
case IF_RWR_MRD:
case IF_RWR_SRD:
case IF_RRW_SRD:
case IF_RWR_ARD:
case IF_RRW_ARD:
case IF_RWR:
case IF_RRD:
case IF_RRW:
break;
default:
return false;
}
if (id->idReg1() != reg)
{
return false;
}
// Certain instruction like and, or and xor modifies exactly same flags
// as "test" instruction.
// They reset OF and CF to 0 and modifies SF, ZF and PF.
if (DoesResetOverflowAndCarryFlags(lastIns))
{
return id->idOpSize() == opSize;
}
if ((treeOps == GT_EQ) || (treeOps == GT_NE))
{
if (DoesWriteZeroFlag(lastIns) && IsFlagsAlwaysModified(id))
{
return id->idOpSize() == opSize;
}
}
return false;
}
//------------------------------------------------------------------------
// IsDstSrcImmAvxInstruction: Checks if the instruction has a "reg, reg/mem, imm" or
// "reg/mem, reg, imm" form for the legacy, VEX, and EVEX
// encodings.
//
// Arguments:
// instruction -- processor instruction to check
//
// Return Value:
// true if instruction has a "reg, reg/mem, imm" or "reg/mem, reg, imm" encoding
// form for the legacy, VEX, and EVEX encodings.
//
// That is, the instruction takes two operands, one of which is immediate, and it
// does not need to encode any data in the VEX.vvvv field.
//
static bool IsDstSrcImmAvxInstruction(instruction ins)
{
switch (ins)
{
case INS_aeskeygenassist:
case INS_extractps:
case INS_pextrb:
case INS_pextrw:
case INS_pextrd:
case INS_pextrq:
case INS_pshufd:
case INS_pshufhw:
case INS_pshuflw:
case INS_roundpd:
case INS_roundps:
return true;
default:
return false;
}
}
// -------------------------------------------------------------------
// Is4ByteSSEInstruction: Returns true if the SSE instruction is a 4-byte opcode.
//
// Arguments:
// ins - instruction
//
// Note that this should be true for any of the instructions in instrsXArch.h
// that use the SSE38 or SSE3A macro but returns false if the VEX encoding is
// in use, since that encoding does not require an additional byte.
bool emitter::Is4ByteSSEInstruction(instruction ins)
{
return !UseVEXEncoding() && EncodedBySSE38orSSE3A(ins);
}
// Returns true if this instruction requires a VEX prefix
// All AVX instructions require a VEX prefix
bool emitter::TakesVexPrefix(instruction ins)
{
// special case vzeroupper as it requires 2-byte VEX prefix
// special case the fencing, movnti and the prefetch instructions as they never take a VEX prefix
switch (ins)
{
case INS_lfence:
case INS_mfence:
case INS_movnti:
case INS_prefetchnta:
case INS_prefetcht0:
case INS_prefetcht1:
case INS_prefetcht2:
case INS_sfence:
case INS_vzeroupper:
return false;
default:
break;
}
return IsAVXInstruction(ins);
}
// Add base VEX prefix without setting W, R, X, or B bits
// L bit will be set based on emitter attr.
//
// 2-byte VEX prefix = C5 <R,vvvv,L,pp>
// 3-byte VEX prefix = C4 <R,X,B,m-mmmm> <W,vvvv,L,pp>
// - R, X, B, W - bits to express corresponding REX prefixes
// - m-mmmmm (5-bit)
// 0-00001 - implied leading 0F opcode byte
// 0-00010 - implied leading 0F 38 opcode bytes
// 0-00011 - implied leading 0F 3A opcode bytes
// Rest - reserved for future use and usage of them will uresult in Undefined instruction exception
//
// - vvvv (4-bits) - register specifier in 1's complement form; must be 1111 if unused
// - L - scalar or AVX-128 bit operations (L=0), 256-bit operations (L=1)
// - pp (2-bits) - opcode extension providing equivalent functionality of a SIMD size prefix
// these prefixes are treated mandatory when used with escape opcode 0Fh for
// some SIMD instructions
// 00 - None (0F - packed float)
// 01 - 66 (66 0F - packed double)
// 10 - F3 (F3 0F - scalar float
// 11 - F2 (F2 0F - scalar double)
#define DEFAULT_3BYTE_VEX_PREFIX 0xC4E07800000000ULL
#define DEFAULT_3BYTE_VEX_PREFIX_MASK 0xFFFFFF00000000ULL
#define LBIT_IN_3BYTE_VEX_PREFIX 0x00000400000000ULL
emitter::code_t emitter::AddVexPrefix(instruction ins, code_t code, emitAttr attr)
{
// The 2-byte VEX encoding is preferred when possible, but actually emitting
// it depends on a number of factors that we may not know until much later.
//
// In order to handle this "easily", we just carry the 3-byte encoding all
// the way through and "fix-up" the encoding when the VEX prefix is actually
// emitted, by simply checking that all the requirements were met.
// Only AVX instructions require VEX prefix
assert(IsAVXInstruction(ins));
// Shouldn't have already added VEX prefix
assert(!hasVexPrefix(code));
assert((code & DEFAULT_3BYTE_VEX_PREFIX_MASK) == 0);
code |= DEFAULT_3BYTE_VEX_PREFIX;
if (attr == EA_32BYTE)
{
// Set L bit to 1 in case of instructions that operate on 256-bits.
code |= LBIT_IN_3BYTE_VEX_PREFIX;
}
return code;
}
// Returns true if this instruction, for the given EA_SIZE(attr), will require a REX.W prefix
bool TakesRexWPrefix(instruction ins, emitAttr attr)
{
// Because the current implementation of AVX does not have a way to distinguish between the register
// size specification (128 vs. 256 bits) and the operand size specification (32 vs. 64 bits), where both are
// required, the instruction must be created with the register size attribute (EA_16BYTE or EA_32BYTE),
// and here we must special case these by the opcode.
switch (ins)
{
case INS_vpermpd:
case INS_vpermq:
case INS_vpsrlvq:
case INS_vpsllvq:
case INS_pinsrq:
case INS_pextrq:
case INS_vfmadd132pd:
case INS_vfmadd213pd:
case INS_vfmadd231pd:
case INS_vfmadd132sd:
case INS_vfmadd213sd:
case INS_vfmadd231sd:
case INS_vfmaddsub132pd:
case INS_vfmaddsub213pd:
case INS_vfmaddsub231pd:
case INS_vfmsubadd132pd:
case INS_vfmsubadd213pd:
case INS_vfmsubadd231pd:
case INS_vfmsub132pd:
case INS_vfmsub213pd:
case INS_vfmsub231pd:
case INS_vfmsub132sd:
case INS_vfmsub213sd:
case INS_vfmsub231sd:
case INS_vfnmadd132pd:
case INS_vfnmadd213pd:
case INS_vfnmadd231pd:
case INS_vfnmadd132sd:
case INS_vfnmadd213sd:
case INS_vfnmadd231sd:
case INS_vfnmsub132pd:
case INS_vfnmsub213pd:
case INS_vfnmsub231pd:
case INS_vfnmsub132sd:
case INS_vfnmsub213sd:
case INS_vfnmsub231sd:
case INS_vpmaskmovq:
case INS_vpgatherdq:
case INS_vpgatherqq:
case INS_vgatherdpd:
case INS_vgatherqpd:
return true;
default:
break;
}
#ifdef TARGET_AMD64
// movsx should always sign extend out to 8 bytes just because we don't track
// whether the dest should be 4 bytes or 8 bytes (attr indicates the size
// of the source, not the dest).
// A 4-byte movzx is equivalent to an 8 byte movzx, so it is not special
// cased here.
//
// Rex_jmp = jmp with rex prefix always requires rex.w prefix.
if (ins == INS_movsx || ins == INS_rex_jmp)
{
return true;
}
if (EA_SIZE(attr) != EA_8BYTE)
{
return false;
}
if (IsSSEOrAVXInstruction(ins))
{
switch (ins)
{
case INS_movd: // TODO-Cleanup: replace with movq, https://github.com/dotnet/runtime/issues/47943.
case INS_andn:
case INS_bextr:
case INS_blsi:
case INS_blsmsk:
case INS_blsr:
case INS_bzhi:
case INS_cvttsd2si:
case INS_cvttss2si:
case INS_cvtsd2si:
case INS_cvtss2si:
case INS_cvtsi2sd:
case INS_cvtsi2ss:
case INS_movnti:
case INS_mulx:
case INS_pdep:
case INS_pext:
case INS_rorx:
return true;
default:
return false;
}
}
// TODO-XArch-Cleanup: Better way to not emit REX.W when we don't need it, than just testing all these
// opcodes...
// These are all the instructions that default to 8-byte operand without the REX.W bit
// With 1 special case: movzx because the 4 byte version still zeros-out the hi 4 bytes
// so we never need it
if ((ins != INS_push) && (ins != INS_pop) && (ins != INS_movq) && (ins != INS_movzx) && (ins != INS_push_hide) &&
(ins != INS_pop_hide) && (ins != INS_ret) && (ins != INS_call) && !((ins >= INS_i_jmp) && (ins <= INS_l_jg)))
{
return true;
}
else
{
return false;
}
#else //! TARGET_AMD64 = TARGET_X86
return false;
#endif //! TARGET_AMD64
}
// Returns true if using this register will require a REX.* prefix.
// Since XMM registers overlap with YMM registers, this routine
// can also be used to know whether a YMM register if the
// instruction in question is AVX.
bool IsExtendedReg(regNumber reg)
{
#ifdef TARGET_AMD64
return ((reg >= REG_R8) && (reg <= REG_R15)) || ((reg >= REG_XMM8) && (reg <= REG_XMM15));
#else
// X86 JIT operates in 32-bit mode and hence extended reg are not available.
return false;
#endif
}
// Returns true if using this register, for the given EA_SIZE(attr), will require a REX.* prefix
bool IsExtendedReg(regNumber reg, emitAttr attr)
{
#ifdef TARGET_AMD64
// Not a register, so doesn't need a prefix
if (reg > REG_XMM15)
{
return false;
}
// Opcode field only has 3 bits for the register, these high registers
// need a 4th bit, that comes from the REX prefix (eiter REX.X, REX.R, or REX.B)
if (IsExtendedReg(reg))
{
return true;
}
if (EA_SIZE(attr) != EA_1BYTE)
{
return false;
}
// There are 12 one byte registers addressible 'below' r8b:
// al, cl, dl, bl, ah, ch, dh, bh, spl, bpl, sil, dil.
// The first 4 are always addressible, the last 8 are divided into 2 sets:
// ah, ch, dh, bh
// -- or --
// spl, bpl, sil, dil
// Both sets are encoded exactly the same, the difference is the presence
// of a REX prefix, even a REX prefix with no other bits set (0x40).
// So in order to get to the second set we need a REX prefix (but no bits).
//
// TODO-AMD64-CQ: if we ever want to start using the first set, we'll need a different way of
// encoding/tracking/encoding registers.
return (reg >= REG_RSP);
#else
// X86 JIT operates in 32-bit mode and hence extended reg are not available.
return false;
#endif
}
// Since XMM registers overlap with YMM registers, this routine
// can also used to know whether a YMM register in case of AVX instructions.
bool IsXMMReg(regNumber reg)
{
#ifdef TARGET_AMD64
return (reg >= REG_XMM0) && (reg <= REG_XMM15);
#else // !TARGET_AMD64
return (reg >= REG_XMM0) && (reg <= REG_XMM7);
#endif // !TARGET_AMD64
}
// Returns bits to be encoded in instruction for the given register.
unsigned RegEncoding(regNumber reg)
{
static_assert((REG_XMM0 & 0x7) == 0, "bad XMMBASE");
return (unsigned)(reg & 0x7);
}
// Utility routines that abstract the logic of adding REX.W, REX.R, REX.X, REX.B and REX prefixes
// SSE2: separate 1-byte prefix gets added before opcode.
// AVX: specific bits within VEX prefix need to be set in bit-inverted form.
emitter::code_t emitter::AddRexWPrefix(instruction ins, code_t code)
{
if (UseVEXEncoding() && IsAVXInstruction(ins))
{
if (TakesVexPrefix(ins))
{
// W-bit is available only in 3-byte VEX prefix that starts with byte C4.
assert(hasVexPrefix(code));
// W-bit is the only bit that is added in non bit-inverted form.
return emitter::code_t(code | 0x00008000000000ULL);
}
}
#ifdef TARGET_AMD64
return emitter::code_t(code | 0x4800000000ULL);
#else
assert(!"UNREACHED");
return code;
#endif
}
#ifdef TARGET_AMD64
emitter::code_t emitter::AddRexRPrefix(instruction ins, code_t code)
{
if (UseVEXEncoding() && IsAVXInstruction(ins))
{
if (TakesVexPrefix(ins))
{
// R-bit is supported by both 2-byte and 3-byte VEX prefix
assert(hasVexPrefix(code));
// R-bit is added in bit-inverted form.
return code & 0xFF7FFFFFFFFFFFULL;
}
}
return code | 0x4400000000ULL;
}
emitter::code_t emitter::AddRexXPrefix(instruction ins, code_t code)
{
if (UseVEXEncoding() && IsAVXInstruction(ins))
{
if (TakesVexPrefix(ins))
{
// X-bit is available only in 3-byte VEX prefix that starts with byte C4.
assert(hasVexPrefix(code));
// X-bit is added in bit-inverted form.
return code & 0xFFBFFFFFFFFFFFULL;
}
}
return code | 0x4200000000ULL;
}
emitter::code_t emitter::AddRexBPrefix(instruction ins, code_t code)
{
if (UseVEXEncoding() && IsAVXInstruction(ins))
{
if (TakesVexPrefix(ins))
{
// B-bit is available only in 3-byte VEX prefix that starts with byte C4.
assert(hasVexPrefix(code));
// B-bit is added in bit-inverted form.
return code & 0xFFDFFFFFFFFFFFULL;
}
}
return code | 0x4100000000ULL;
}
// Adds REX prefix (0x40) without W, R, X or B bits set
emitter::code_t emitter::AddRexPrefix(instruction ins, code_t code)
{
assert(!UseVEXEncoding() || !IsAVXInstruction(ins));
return code | 0x4000000000ULL;
}
#endif // TARGET_AMD64
bool isPrefix(BYTE b)
{
assert(b != 0); // Caller should check this
assert(b != 0x67); // We don't use the address size prefix
assert(b != 0x65); // The GS segment override prefix is emitted separately
assert(b != 0x64); // The FS segment override prefix is emitted separately
assert(b != 0xF0); // The lock prefix is emitted separately
assert(b != 0x2E); // We don't use the CS segment override prefix
assert(b != 0x3E); // Or the DS segment override prefix
assert(b != 0x26); // Or the ES segment override prefix
assert(b != 0x36); // Or the SS segment override prefix
// That just leaves the size prefixes used in SSE opcodes:
// Scalar Double Scalar Single Packed Double
return ((b == 0xF2) || (b == 0xF3) || (b == 0x66));
}
// Outputs VEX prefix (in case of AVX instructions) and REX.R/X/W/B otherwise.
unsigned emitter::emitOutputRexOrVexPrefixIfNeeded(instruction ins, BYTE* dst, code_t& code)
{
if (hasVexPrefix(code))
{
// Only AVX instructions should have a VEX prefix
assert(UseVEXEncoding() && IsAVXInstruction(ins));
code_t vexPrefix = (code >> 32) & 0x00FFFFFF;
code &= 0x00000000FFFFFFFFLL;
WORD leadingBytes = 0;
BYTE check = (code >> 24) & 0xFF;
if (check != 0)
{
// 3-byte opcode: with the bytes ordered as 0x2211RM33 or
// 4-byte opcode: with the bytes ordered as 0x22114433
// check for a prefix in the 11 position
BYTE sizePrefix = (code >> 16) & 0xFF;
if ((sizePrefix != 0) && isPrefix(sizePrefix))
{
// 'pp' bits in byte2 of VEX prefix allows us to encode SIMD size prefixes as two bits
//
// 00 - None (0F - packed float)
// 01 - 66 (66 0F - packed double)
// 10 - F3 (F3 0F - scalar float
// 11 - F2 (F2 0F - scalar double)
switch (sizePrefix)
{
case 0x66:
if (IsBMIInstruction(ins))
{
switch (ins)
{
case INS_rorx:
case INS_pdep:
case INS_mulx:
{
vexPrefix |= 0x03;
break;
}
case INS_pext:
{
vexPrefix |= 0x02;
break;
}
default:
{
vexPrefix |= 0x00;
break;
}
}
}
else
{
vexPrefix |= 0x01;
}
break;
case 0xF3:
vexPrefix |= 0x02;
break;
case 0xF2:
vexPrefix |= 0x03;
break;
default:
assert(!"unrecognized SIMD size prefix");
unreached();
}
// Now the byte in the 22 position must be an escape byte 0F
leadingBytes = check;
assert(leadingBytes == 0x0F);
// Get rid of both sizePrefix and escape byte
code &= 0x0000FFFFLL;
// Check the byte in the 33 position to see if it is 3A or 38.
// In such a case escape bytes must be 0x0F3A or 0x0F38
check = code & 0xFF;
if (check == 0x3A || check == 0x38)
{
leadingBytes = (leadingBytes << 8) | check;
code &= 0x0000FF00LL;
}
}
}
else
{
// 2-byte opcode with the bytes ordered as 0x0011RM22
// the byte in position 11 must be an escape byte.
leadingBytes = (code >> 16) & 0xFF;
assert(leadingBytes == 0x0F || leadingBytes == 0x00);
code &= 0xFFFF;
}
// If there is an escape byte it must be 0x0F or 0x0F3A or 0x0F38
// m-mmmmm bits in byte 1 of VEX prefix allows us to encode these
// implied leading bytes. 0x0F is supported by both the 2-byte and
// 3-byte encoding. While 0x0F3A and 0x0F38 are only supported by
// the 3-byte version.
switch (leadingBytes)
{
case 0x00:
// there is no leading byte
break;
case 0x0F:
vexPrefix |= 0x0100;
break;
case 0x0F38:
vexPrefix |= 0x0200;
break;
case 0x0F3A:
vexPrefix |= 0x0300;
break;
default:
assert(!"encountered unknown leading bytes");
unreached();
}
// At this point
// VEX.2211RM33 got transformed as VEX.0000RM33
// VEX.0011RM22 got transformed as VEX.0000RM22
//
// Now output VEX prefix leaving the 4-byte opcode
// The 2-byte VEX encoding, requires that the X and B-bits are set (these
// bits are inverted from the REX values so set means off), the W-bit is
// not set (this bit is not inverted), and that the m-mmmm bits are 0-0001
// (the 2-byte VEX encoding only supports the 0x0F leading byte). When these
// conditions are met, we can change byte-0 from 0xC4 to 0xC5 and then
// byte-1 is the logical-or of bit 7 from byte-1 and bits 0-6 from byte 2
// from the 3-byte VEX encoding.
//
// Given the above, the check can be reduced to a simple mask and comparison.
// * 0xFFFF7F80 is a mask that ignores any bits whose value we don't care about:
// * R can be set or unset (0x7F ignores bit 7)
// * vvvv can be any value (0x80 ignores bits 3-6)
// * L can be set or unset (0x80 ignores bit 2)
// * pp can be any value (0x80 ignores bits 0-1)
// * 0x00C46100 is a value that signifies the requirements listed above were met:
// * We must be a three-byte VEX opcode (0x00C4)
// * X and B must be set (0x61 validates bits 5-6)
// * m-mmmm must be 0-00001 (0x61 validates bits 0-4)
// * W must be unset (0x00 validates bit 7)
if ((vexPrefix & 0xFFFF7F80) == 0x00C46100)
{
// Encoding optimization calculation is not done while estimating the instruction
// size and thus over-predict instruction size by 1 byte.
// If there are IGs that will be aligned, do not optimize encoding so the
// estimated alignment sizes are accurate.
if (emitCurIG->igNum > emitLastAlignedIgNum)
{
emitOutputByte(dst, 0xC5);
emitOutputByte(dst + 1, ((vexPrefix >> 8) & 0x80) | (vexPrefix & 0x7F));
return 2;
}
}
emitOutputByte(dst, ((vexPrefix >> 16) & 0xFF));
emitOutputByte(dst + 1, ((vexPrefix >> 8) & 0xFF));
emitOutputByte(dst + 2, vexPrefix & 0xFF);
return 3;
}
#ifdef TARGET_AMD64
if (code > 0x00FFFFFFFFLL)
{
BYTE prefix = (code >> 32) & 0xFF;
noway_assert(prefix >= 0x40 && prefix <= 0x4F);
code &= 0x00000000FFFFFFFFLL;
// TODO-AMD64-Cleanup: when we remove the prefixes (just the SSE opcodes right now)
// we can remove this code as well
// The REX prefix is required to come after all other prefixes.
// Some of our 'opcodes' actually include some prefixes, if that
// is the case, shift them over and place the REX prefix after
// the other prefixes, and emit any prefix that got moved out.
BYTE check = (code >> 24) & 0xFF;
if (check == 0)
{
// 3-byte opcode: with the bytes ordered as 0x00113322
// check for a prefix in the 11 position
check = (code >> 16) & 0xFF;
if (check != 0 && isPrefix(check))
{
// Swap the rex prefix and whatever this prefix is