forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitxarch.cpp
18167 lines (15687 loc) · 549 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);
}
//------------------------------------------------------------------------
// IsAvx512OrPriorInstruction: Is this an Avx512 or Avx or Sse instruction.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if it is a sse or avx or avx512 instruction.
//
bool emitter::IsAvx512OrPriorInstruction(instruction ins)
{
// TODO-XArch-AVX512: Fix check once AVX512 instructions are added.
return (ins >= INS_FIRST_SSE_INSTRUCTION) && (ins <= INS_LAST_AVX512_INSTRUCTION);
}
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);
}
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:
{
return (regNumber)4;
}
default:
{
assert(!"Invalid instruction for SSE2 instruction of the form: opcode reg, immed8");
return REG_NA;
}
}
}
bool emitter::IsVexEncodedInstruction(instruction ins) const
{
return UseVEXEncoding() && IsSSEOrAVXInstruction(ins);
}
//------------------------------------------------------------------------
// IsEvexEncodedInstruction: 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::IsEvexEncodedInstruction(instruction ins) const
{
if (!UseEvexEncoding())
{
return false;
}
// TODO-XArch-AVX512: Explore adding this as a flag to instr table.
switch (ins)
{
// No EVEX Encoding exists at all.
case INS_pmovmskb:
case INS_movmskpd:
case INS_movmskps:
case INS_dppd:
case INS_dpps:
case INS_maskmovdqu:
case INS_haddps:
case INS_haddpd:
case INS_hsubps:
case INS_hsubpd:
case INS_addsubps:
case INS_addsubpd:
case INS_rcpps:
case INS_rcpss:
case INS_rsqrtps:
case INS_rsqrtss:
case INS_psignb:
case INS_psignd:
case INS_psignw:
case INS_roundps:
case INS_roundss:
case INS_roundpd:
case INS_roundsd:
case INS_blendps:
case INS_blendpd:
case INS_blendvps:
case INS_pblendw:
case INS_pblendvb:
case INS_blendvpd:
case INS_ptest:
case INS_phaddw:
case INS_phsubw:
case INS_phaddd:
case INS_phsubd:
case INS_phaddsw:
case INS_phsubsw:
case INS_lddqu:
case INS_phminposuw:
case INS_mpsadbw:
case INS_pclmulqdq:
case INS_aesenc:
case INS_aesenclast:
case INS_aesdec:
case INS_aesdeclast:
case INS_aesimc:
case INS_aeskeygenassist:
case INS_vzeroupper:
case INS_vperm2i128:
case INS_vperm2f128:
case INS_vpblendd:
case INS_vblendvps:
case INS_vblendvpd:
case INS_vpblendvb:
case INS_vtestps:
case INS_vtestpd:
case INS_vmaskmovps:
case INS_vmaskmovpd:
case INS_vpmaskmovd:
case INS_vpmaskmovq:
case INS_andn:
case INS_blsi:
case INS_blsmsk:
case INS_blsr:
case INS_bextr:
case INS_rorx:
case INS_pdep:
case INS_pext:
case INS_bzhi:
case INS_mulx:
#ifdef TARGET_AMD64
case INS_shlx:
case INS_sarx:
case INS_shrx:
#endif
case INS_lfence:
case INS_mfence:
case INS_movnti:
case INS_prefetchnta:
case INS_prefetcht0:
case INS_prefetcht1:
case INS_prefetcht2:
case INS_sfence:
// Might need new INS_<INS_NAME>*suffix* instructions for these.
case INS_vbroadcastf128: // INS_vbroadcastf32x4, INS_vbroadcastf64x2.
case INS_vbroadcasti128: // INS_vbroadcasti32x4, INS_vbroadcasti64x2.
// TODO-XARCH-AVX512 these need to be encoded with the proper individual EVEX instructions (movdqu8,
// movdqu16 etc)
// For implementation speed, I have set it up so the standing instruction will default to the 32-bit operand
// type
// i.e., movdqu => movdqu32 etc
// Since we are not using k registers yet, this will have no impact on correctness but will affect things
// once
// k registers are used (as that is the point of the "break out operand type" of these instructions)
// case INS_movdqa: // INS_movdqa32, INS_movdqa64.
// case INS_movdqu: // INS_movdqu8, INS_movdqu16, INS_movdqu32, INS_movdqu64.
// case INS_pand: // INS_pandd, INS_pandq.
// case INS_pandn: // INS_pandnd, INS_pandnq.
// case INS_por: // INS_pord, INS_porq.
// case INS_pxor: // INS_pxord, INS_pxorq
// case INS_vextractf128: // INS_vextractf32x4, INS_vextractf64x2.
// case INS_vextracti128: // INS_vextracti32x4, INS_vextracti64x2.
// case INS_vinsertf128: // INS_vinsertf32x4, INS_vinsertf64x2.
// case INS_vinserti128: // INS_vinserti32x4, INS_vinserti64x2.
{
return false;
}
default:
{
break;
}
}
return IsAvx512OrPriorInstruction(ins);
}
//------------------------------------------------------------------------
// Answer the question: Is this a SIMD instruction.
//
// Arguments:
// ins - The instruction to check.
//
// Returns:
// `true` if ins is a SIMD instruction.
//
bool emitter::IsVexOrEvexEncodedInstruction(instruction ins) const
{
return IsEvexEncodedInstruction(ins) || IsVexEncodedInstruction(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) &&
IsVexOrEvexEncodedInstruction(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) &&
IsVexOrEvexEncodedInstruction(ins);
}
//------------------------------------------------------------------------
// 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)
{
return ((CodeGenInterface::instInfo[ins] & 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)
{
return ((CodeGenInterface::instInfo[ins] & 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)
{
return (CodeGenInterface::instInfo[ins] & Writes_ZF) != 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)
{
return (CodeGenInterface::instInfo[ins] & 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)
{
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;
}
#ifdef TARGET_64BIT
//------------------------------------------------------------------------
// 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.
//
bool emitter::AreUpper32BitsZero(regNumber reg)
{
// 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;
// movzx always zeroes the upper 32 bits.
case INS_movzx:
result = true;
return PEEPHOLE_ABORT;
default:
break;
}
// otherwise rely on operation size.
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 32 bits.
//
// Arguments:
// reg - register of interest
//
// Return Value:
// true if previous instruction upper 32 bits are sign-extended.
// false if it did not, or if we can't safely determine.
bool emitter::AreUpper32BitsSignExtended(regNumber reg)
{
// 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;
if (id->idReg1() != reg)
{
return false;
}
// movsx always sign extends to 8 bytes. W-bit is set.
if (id->idIns() == INS_movsx)
{
return true;
}
// movsxd is always an 8 byte operation. W-bit is set.
if (id->idIns() == INS_movsxd)
{
return true;
}
return false;
}
#endif // TARGET_64BIT
//------------------------------------------------------------------------
// 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;
// These always write to RAX and RDX.
case INS_idiv:
case INS_div:
case INS_imulEAX:
case INS_mulEAX:
if ((reg == REG_RAX) || (reg == REG_RDX))
{
return true;
}
break;
// Always writes to RAX.
case INS_cmpxchg:
if (reg == REG_RAX)
{
return true;
}
break;
case INS_movsb:
case INS_movsd:
#ifdef TARGET_AMD64
case INS_movsq:
#endif // TARGET_AMD64
if ((reg == REG_RDI) || (reg == REG_RSI))
{
return true;
}
break;
case INS_stosb:
case INS_stosd:
#ifdef TARGET_AMD64
case INS_stosq:
#endif // TARGET_AMD64
if (reg == REG_RDI)
{
return true;
}
break;
case INS_r_movsb:
case INS_r_movsd:
#ifdef TARGET_AMD64
case INS_r_movsq:
#endif // TARGET_AMD64
if ((reg == REG_RDI) || (reg == REG_RSI) || (reg == REG_RCX))
{
return true;
}
break;
case INS_r_stosb:
case INS_r_stosd:
#ifdef TARGET_AMD64
case INS_r_stosq:
#endif // TARGET_AMD64
if ((reg == REG_RDI) || (reg == REG_RCX))
{
return true;
}
break;
default:
break;
}
#ifdef TARGET_64BIT
// This is a special case for cdq/cwde.
switch (ins)
{
case INS_cwde:
if (reg == REG_RAX)
{
return true;
}
break;
case INS_cdq:
if (reg == REG_RDX)
{
return true;
}
break;
default:
break;
}
#endif // TARGET_64BIT
switch (id->idInsFmt())
{
case IF_RWR:
case IF_RRW:
case IF_RWR_CNS:
case IF_RRW_CNS:
case IF_RRW_SHF:
case IF_RWR_RRD:
case IF_RRW_RRD:
case IF_RRW_RRW:
case IF_RRW_RRW_CNS:
case IF_RWR_RRD_RRD:
case IF_RWR_RRD_RRD_CNS:
case IF_RWR_RRD_RRD_RRD:
case IF_RWR_MRD:
case IF_RRW_MRD:
case IF_RRW_MRD_CNS:
case IF_RWR_RRD_MRD:
case IF_RWR_MRD_CNS:
case IF_RWR_RRD_MRD_CNS:
case IF_RWR_RRD_MRD_RRD:
case IF_RWR_MRD_OFF:
case IF_RWR_SRD:
case IF_RRW_SRD:
case IF_RRW_SRD_CNS:
case IF_RWR_RRD_SRD:
case IF_RWR_SRD_CNS:
case IF_RWR_RRD_SRD_CNS:
case IF_RWR_RRD_SRD_RRD:
case IF_RWR_ARD:
case IF_RRW_ARD:
case IF_RRW_ARD_CNS:
case IF_RWR_RRD_ARD:
case IF_RWR_ARD_CNS:
case IF_RWR_ARD_RRD:
case IF_RWR_RRD_ARD_CNS:
case IF_RWR_RRD_ARD_RRD:
{
if (id->idReg1() != reg)
{
switch (id->idInsFmt())
{
// Handles instructions who write to two registers.
case IF_RRW_RRW:
case IF_RRW_RRW_CNS:
{
if (id->idReg2() == reg)
{
return true;
}
break;
}
default:
break;
}
return false;
}
return true;
}
default:
{
return false;
}
}
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
// cond - the condition being checked
//
// 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, GenCondition cond)
{
assert(reg != REG_NA);
if (!emitComp->opts.OptimizationEnabled())
{
return false;
}
// Only consider if safe
//
if (!emitCanPeepholeLastIns())
{
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:
case IF_RWR_RRD_RRD:
case IF_RWR_RRD_MRD:
case IF_RWR_RRD_ARD:
case IF_RWR_RRD_SRD:
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 ((cond.GetCode() == GenCondition::NE) || (cond.GetCode() == GenCondition::EQ))
{
if (DoesWriteZeroFlag(lastIns) && IsFlagsAlwaysModified(id))
{
return id->idOpSize() == opSize;
}
}
return false;
}
//------------------------------------------------------------------------
// AreFlagsSetToForSignJumpOpt: checks if the previous instruction set the SF if the tree
// node qualifies for a jg/jle to jns/js optimization
//
// Arguments:
// reg - register of interest
// opSize - size of register
// cond - the condition being checked
//
// Return Value:
// true if the tree node qualifies for the jg/jle to jns/js optimization
// false if not, or if we can't safely determine
//
// Notes:
// Currently only looks back one instruction.
bool emitter::AreFlagsSetForSignJumpOpt(regNumber reg, emitAttr opSize, GenCondition cond)
{
assert(reg != REG_NA);
if (!emitComp->opts.OptimizationEnabled())
{
return false;
}
// Only consider if safe
//
if (!emitCanPeepholeLastIns())
{
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;
}
// If we have a GE/LT which generates an jge/jl, and the previous instruction
// sets the SF, we can omit a test instruction and check for jns/js.
if ((cond.GetCode() == GenCondition::SGE) || (cond.GetCode() == GenCondition::SLT))
{
if (DoesWriteSignFlag(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) const
{
return !UseVEXEncoding() && EncodedBySSE38orSSE3A(ins);
}
//------------------------------------------------------------------------
// TakesSimdPrefix: Checks if the instruction should be VEX or EVEX encoded.