-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathemitxarch.cpp
21725 lines (18894 loc) · 656 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 emitter::IsSSEInstruction(instruction ins)
{
return (ins >= INS_FIRST_SSE_INSTRUCTION) && (ins <= INS_LAST_SSE_INSTRUCTION);
}
bool emitter::IsSSEOrAVXInstruction(instruction ins)
{
return (ins >= INS_FIRST_SSE_INSTRUCTION) && (ins <= INS_LAST_AVX_INSTRUCTION);
}
//------------------------------------------------------------------------
// IsKInstruction: Does this instruction require K register?
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if this instruction requires K register.
//
bool emitter::IsKInstruction(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & KInstruction) != 0;
}
//------------------------------------------------------------------------
// IsKInstructionWithLBit: Does this instruction require K register and
// LBIT_IN_3BYTE_VEX_PREFIX bit.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if this instruction requires K register and
// LBIT_IN_3BYTE_VEX_PREFIX bit.
//
bool emitter::IsKInstructionWithLBit(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & KInstructionWithLBit) != 0;
}
bool emitter::IsAVXOnlyInstruction(instruction ins)
{
return (ins >= INS_FIRST_AVX_INSTRUCTION) && (ins <= INS_LAST_AVX_INSTRUCTION);
}
//------------------------------------------------------------------------
// IsAvx512OnlyInstruction: Is this an Avx512 instruction?
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if it is a avx512f+ instruction.
//
bool emitter::IsAvx512OnlyInstruction(instruction ins)
{
return (ins >= INS_FIRST_AVX512_INSTRUCTION) && (ins <= INS_LAST_AVX512_INSTRUCTION);
}
bool emitter::IsFMAInstruction(instruction ins)
{
return (ins >= INS_FIRST_FMA_INSTRUCTION) && (ins <= INS_LAST_FMA_INSTRUCTION);
}
bool emitter::IsAVXVNNIInstruction(instruction ins)
{
return (ins >= INS_FIRST_AVXVNNI_INSTRUCTION) && (ins <= INS_LAST_AVXVNNI_INSTRUCTION);
}
bool emitter::IsBMIInstruction(instruction ins)
{
return (ins >= INS_FIRST_BMI_INSTRUCTION) && (ins <= INS_LAST_BMI_INSTRUCTION);
}
//------------------------------------------------------------------------
// IsPermuteVar2xInstruction: Is this an Avx512 permutex2var instruction?
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if it is a permutex2var instruction.
//
bool emitter::IsPermuteVar2xInstruction(instruction ins)
{
switch (ins)
{
case INS_vpermi2d:
case INS_vpermi2pd:
case INS_vpermi2ps:
case INS_vpermi2q:
case INS_vpermt2d:
case INS_vpermt2pd:
case INS_vpermt2ps:
case INS_vpermt2q:
case INS_vpermi2w:
case INS_vpermt2w:
case INS_vpermi2b:
case INS_vpermt2b:
{
return true;
}
default:
{
return false;
}
}
}
regNumber emitter::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 emitter::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:
case INS_vpsraq:
{
return (regNumber)4;
}
case INS_vprold:
case INS_vprolq:
{
return (regNumber)1;
}
case INS_vprord:
case INS_vprorq:
{
return (regNumber)0;
}
default:
{
assert(!"Invalid instruction for SSE2 instruction of the form: opcode reg, immed8");
return REG_NA;
}
}
}
bool emitter::HasVexEncoding(instruction ins) const
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & Encoding_VEX) != 0;
}
bool emitter::HasEvexEncoding(instruction ins) const
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & Encoding_EVEX) != 0;
}
bool emitter::HasRex2Encoding(instruction ins) const
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & Encoding_REX2) != 0;
}
bool emitter::HasApxNdd(instruction ins) const
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & INS_Flags_Has_NDD) != 0;
}
bool emitter::HasApxNf(instruction ins) const
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & INS_Flags_Has_NF) != 0;
}
bool emitter::IsVexEncodableInstruction(instruction ins) const
{
if (!UseVEXEncoding())
{
return false;
}
return HasVexEncoding(ins);
}
//------------------------------------------------------------------------
// IsEvexEncodableInstruction: Answer the question- Can this instruction be Evex encoded.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins can be Evex encoded.
//
bool emitter::IsEvexEncodableInstruction(instruction ins) const
{
if (!UseEvexEncoding())
{
return false;
}
switch (ins)
{
case INS_pclmulqdq:
{
return emitComp->compOpportunisticallyDependsOn(InstructionSet_PCLMULQDQ_V256);
}
default:
{
return HasEvexEncoding(ins);
}
}
}
//------------------------------------------------------------------------
// IsRex2EncodableInstruction: Answer the question- Can this instruction be Rex2 encoded.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins can be Rex2 encoded.
//
bool emitter::IsRex2EncodableInstruction(instruction ins) const
{
if (!UseRex2Encoding())
{
return false;
}
return HasRex2Encoding(ins);
}
//------------------------------------------------------------------------
// IsApxNDDEncodableInstruction: Answer the question- does this instruction have apx ndd form.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins has apx ndd form.
//
bool emitter::IsApxNDDEncodableInstruction(instruction ins) const
{
if (!UsePromotedEVEXEncoding())
{
return false;
}
return HasApxNdd(ins);
}
//------------------------------------------------------------------------
// IsApxNFEncodableInstruction: Answer the question - does this instruction have Evex.nf supported
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins is Evex.nf supported.
//
bool emitter::IsApxNFEncodableInstruction(instruction ins) const
{
if (!UsePromotedEVEXEncoding())
{
return false;
}
return HasApxNf(ins);
}
//------------------------------------------------------------------------
// IsApxExtendedEvexInstruction: Answer the question - does this instruction have apx extended evex form.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins has apx extended evex form.
//
bool emitter::IsApxExtendedEvexInstruction(instruction ins) const
{
if (!UsePromotedEVEXEncoding())
{
return false;
}
return HasApxNdd(ins) || HasApxNf(ins);
}
//------------------------------------------------------------------------
// IsShiftInstruction: Answer the question- is this instruction a shift instruction.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins is a shift instruction.
//
bool emitter::IsShiftInstruction(instruction ins) const
{
switch (ins)
{
case INS_rcl_1:
case INS_rcr_1:
case INS_rol_1:
case INS_ror_1:
case INS_shl_1:
case INS_shr_1:
case INS_sar_1:
case INS_rcl:
case INS_rcr:
case INS_rol:
case INS_ror:
case INS_shl:
case INS_shr:
case INS_sar:
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 true;
default:
return false;
}
}
//------------------------------------------------------------------------
// IsLegacyMap1: Answer the question- Is this instruction on legacy-map-1
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins is a legacy-map-1 instruction.
//
bool emitter::IsLegacyMap1(code_t code) const
{
#ifdef TARGET_AMD64
// Lagacy-Map-1 opcode is defined as 2-byte opcode with a leading byte of 0x0F,
// In JIT, it could be in the following style:
// 2-byte: XX0F
// 3-byte: 0F00XX
// 4-byte: 0FPP00XX
if ((code & 0xFFFF00FF) == 0x0000000F)
{
// 2-byte
return true;
}
if ((code & 0xFFFF0000) == 0x000F0000)
{
// 3-byte
return true;
}
if ((code & 0xFF00FF00) == 0x0F000000)
{
// 4-byte, need to check if PP is prefixs
BYTE prefix = (BYTE)((code & 0xFF0000) >> 16);
return ((prefix == 0xF2) || (prefix == 0xF3) || (prefix == 0x66));
}
return false;
#endif // TARGET_AMD64
return false;
}
//------------------------------------------------------------------------
// Answer the question: Is this a SIMD instruction.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins is a SIMD instruction.
//
bool emitter::IsVexOrEvexEncodableInstruction(instruction ins) const
{
if (!UseVEXEncoding())
{
return false;
}
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & (Encoding_VEX | Encoding_EVEX)) != 0;
}
// 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.
bool emitter::IsDstDstSrcAVXInstruction(instruction ins) const
{
if (!UseVEXEncoding())
{
return false;
}
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & INS_Flags_IsDstDstSrcAVXInstruction) != 0;
}
// Returns true if the AVX instruction requires 3 operands that duplicate the source
// register in the vvvv field.
bool emitter::IsDstSrcSrcAVXInstruction(instruction ins) const
{
if (!UseVEXEncoding())
{
return false;
}
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & INS_Flags_IsDstSrcSrcAVXInstruction) != 0;
}
bool emitter::IsThreeOperandAVXInstruction(instruction ins) const
{
if (!UseSimdEncoding())
{
return false;
}
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & INS_Flags_Is3OperandInstructionMask) != 0;
}
//------------------------------------------------------------------------
// HasRegularWideForm: Many x86/x64 instructions follow a regular encoding scheme where the
// byte-sized version of an instruction has the lowest bit of the opcode cleared
// while the 32-bit version of the instruction (taking potential prefixes to
// override operand size) has the lowest bit set. This function returns true if
// the instruction follows this format.
//
// Note that this bit is called `w` in the encoding table in Section B.2 of
// Volume 2 of the Intel Architecture Software Developer Manual.
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if instruction has a regular form where the 'w' bit needs to be set.
bool emitter::HasRegularWideForm(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & INS_FLAGS_Has_Wbit) != 0;
}
//------------------------------------------------------------------------
// HasRegularWideImmediateForm: As above in HasRegularWideForm, many instructions taking
// immediates have a regular form used to encode whether the instruction takes a sign-extended
// 1-byte immediate or a (in 64-bit sign-extended) 4-byte immediate, by respectively setting and
// clearing the second lowest bit.
//
// Note that this bit is called `s` in the encoding table in Section B.2 of
// Volume 2 of the Intel Architecture Software Developer Manual.
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if instruction has a regular wide immediate form where the 's' bit needs to set.
bool emitter::HasRegularWideImmediateForm(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & INS_FLAGS_Has_Sbit) != 0;
}
//------------------------------------------------------------------------
// 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)
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & Writes_ZF) != 0;
}
//------------------------------------------------------------------------
// DoesWriteParityFlag: check if the instruction write the
// PF flag.
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if instruction writes the PF flag, false otherwise.
//
bool emitter::DoesWriteParityFlag(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & Writes_PF) != 0;
}
//------------------------------------------------------------------------
// DoesWriteSignFlag: check if the instruction writes the
// SF flag.
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if instruction writes the SF flag, false otherwise.
//
bool emitter::DoesWriteSignFlag(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & Writes_SF) != 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)
{
insFlags flags = CodeGenInterface::instInfo[ins];
return (flags & (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;
}
//------------------------------------------------------------------------
// IsRexW0Instruction: check if the instruction always encodes REX.W as 0
//
// Arguments:
// id - instruction to test
//
// Return Value:
// true if the instruction always encodes REX.W as 0; othwerwise, false
//
bool emitter::IsRexW0Instruction(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
if ((flags & REX_W0) != 0)
{
assert((flags & (REX_W1 | REX_WX | REX_W1_EVEX)) == 0);
return true;
}
return false;
}
//------------------------------------------------------------------------
// IsRexW1Instruction: check if the instruction always encodes REX.W as 1
//
// Arguments:
// id - instruction to test
//
// Return Value:
// true if the instruction always encodes REX.W as 1; othwerwise, false
//
bool emitter::IsRexW1Instruction(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
if ((flags & REX_W1) != 0)
{
assert((flags & (REX_W0 | REX_WX | REX_W1_EVEX)) == 0);
return true;
}
return false;
}
//------------------------------------------------------------------------
// IsRexWXInstruction: check if the instruction requires special REX.W encoding
//
// Arguments:
// id - instruction to test
//
// Return Value:
// true if the instruction requires special REX.W encoding; othwerwise, false
//
bool emitter::IsRexWXInstruction(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
if ((flags & REX_WX) != 0)
{
assert((flags & (REX_W0 | REX_W1 | REX_W1_EVEX)) == 0);
return true;
}
return false;
}
//------------------------------------------------------------------------
// IsRexW1EvexInstruction: check if the instruction always encodes REX.W as 1 for EVEX
//
// Arguments:
// id - instruction to test
//
// Return Value:
// true if the instruction always encodes REX.W as 1 for EVEX; othwerwise, false
//
bool emitter::IsRexW1EvexInstruction(instruction ins)
{
insFlags flags = CodeGenInterface::instInfo[ins];
if ((flags & REX_W1_EVEX) != 0)
{
assert((flags & (REX_W0 | REX_W1 | REX_WX)) == 0);
return true;
}
return false;
}
//------------------------------------------------------------------------
// DoJitUseApxNDD: Answer the question: does JIT use APX NDD feature on the given instruction?
//
// Arguments:
// ins - instruction to test
//
// Return Value:
// true if JIT allows APX NDD to be applied on the instructions.
//
bool emitter::DoJitUseApxNDD(instruction ins) const
{
#if !defined(TARGET_AMD64)
return false;
#else
return JitConfig.EnableApxNDD() && IsApxNDDEncodableInstruction(ins);
#endif
}
#ifdef TARGET_64BIT
//------------------------------------------------------------------------
// AreUpperBitsZero: check if some previously emitted
// instruction set the upper bits of reg to zero.
//
// Arguments:
// reg - register of interest
// size - the size of data that the given register of interest is working with;
// remaining upper bits of the register that represent a larger size are the bits that are checked for zero
//
// Return Value:
// true if previous instruction zeroed reg's upper bits.
// false if it did not, or if we can't safely determine.
//
bool emitter::AreUpperBitsZero(regNumber reg, emitAttr size)
{
// Only allow GPRs.
// If not a valid register, then return false.
if (!genIsValidIntReg(reg))
return false;
// Only consider if safe
//
if (!emitCanPeepholeLastIns())
{
return false;
}
bool result = false;
emitPeepholeIterateLastInstrs([&](instrDesc* id) {
if (emitIsInstrWritingToReg(id, reg))
{
switch (id->idIns())
{
// Conservative.
case INS_call:
return PEEPHOLE_ABORT;
// These instructions sign-extend.
case INS_cwde:
case INS_cdq:
case INS_movsx:
case INS_movsxd:
return PEEPHOLE_ABORT;
case INS_movzx:
if ((size == EA_1BYTE) || (size == EA_2BYTE))
{
result = (id->idOpSize() <= size);
}
// movzx always zeroes the upper 32 bits.
else if (size == EA_4BYTE)
{
result = true;
}
return PEEPHOLE_ABORT;
default:
break;
}
// otherwise rely on operation size.
if (size == EA_4BYTE)
{
result = (id->idOpSize() == EA_4BYTE);
}
return PEEPHOLE_ABORT;
}
else
{
return PEEPHOLE_CONTINUE;
}
});
return result;
}
//------------------------------------------------------------------------
// AreUpper32BitsSignExtended: check if some previously emitted
// instruction sign-extended the upper bits.
//
// Arguments:
// reg - register of interest
// size - the size of data that the given register of interest is working with;
// remaining upper bits of the register that represent a larger size are the bits that are checked for
// sign-extended
//
// Return Value:
// true if previous instruction upper bits are sign-extended.
// false if it did not, or if we can't safely determine.
bool emitter::AreUpperBitsSignExtended(regNumber reg, emitAttr size)
{
// Only allow GPRs.
// If not a valid register, then return false.
if (!genIsValidIntReg(reg))
return false;
// Only consider if safe
//
if (!emitCanPeepholeLastIns())
{
return false;
}
instrDesc* id = emitLastIns;
bool result = false;
emitPeepholeIterateLastInstrs([&](instrDesc* id) {
if (emitIsInstrWritingToReg(id, reg))
{
switch (id->idIns())
{
// Conservative.
case INS_call:
return PEEPHOLE_ABORT;
case INS_movsx:
case INS_movsxd:
if ((size == EA_1BYTE) || (size == EA_2BYTE))
{
result = (id->idOpSize() <= size);
}
// movsx/movsxd always sign extends to 8 bytes. W-bit is set.
else if (size == EA_4BYTE)
{
result = true;
}
break;
default:
break;
}
return PEEPHOLE_ABORT;
}
else
{
return PEEPHOLE_CONTINUE;
}
});
return result;
}
#endif // TARGET_64BIT
//------------------------------------------------------------------------
// emitDoesInsModifyFlags: checks if the given instruction modifies flags
//
// Arguments:
// ins - instruction of interest
//
// Return Value:
// true if the instruction modifies flags.
// false if it does not.
//
bool emitter::emitDoesInsModifyFlags(instruction ins)
{
return (CodeGenInterface::instInfo[ins] &
(Resets_OF | Resets_SF | Resets_AF | Resets_PF | Resets_CF | Undefined_OF | Undefined_SF | Undefined_AF |
Undefined_PF | Undefined_CF | Undefined_ZF | Writes_OF | Writes_SF | Writes_AF | Writes_PF | Writes_CF |
Writes_ZF | Restore_SF_ZF_AF_PF_CF));
}
//------------------------------------------------------------------------
// emitIsInstrWritingToReg: checks if the given register is being written to
//
// Arguments:
// id - instruction of interest
// reg - register of interest
//
// Return Value:
// true if the instruction writes to the given register.
// false if it did not.
//
// Note: This only handles integer registers. Also, an INS_call will always return true.
//
bool emitter::emitIsInstrWritingToReg(instrDesc* id, regNumber reg)
{
// This only handles integer registers for now.
assert(genIsValidIntReg(reg));
instruction ins = id->idIns();
// These are special cases since they modify one or more register(s) implicitly.
switch (ins)
{
// This is conservative. We assume a call will write to all registers even if it does not.
case INS_call:
return true;
case INS_imul_AX:
case INS_imul_BP:
case INS_imul_BX:
case INS_imul_CX:
case INS_imul_DI:
case INS_imul_DX:
case INS_imul_SI:
case INS_imul_SP:
#ifdef TARGET_AMD64
case INS_imul_08:
case INS_imul_09:
case INS_imul_10:
case INS_imul_11:
case INS_imul_12:
case INS_imul_13:
case INS_imul_14:
case INS_imul_15:
#endif // TARGET_AMD64
if (reg == inst3opImulReg(ins))
{
return true;
}
break;
// These always write to RAX and RDX.
case INS_idiv:
case INS_div:
case INS_imulEAX:
case INS_mulEAX: