-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathinstr.cpp
2680 lines (2387 loc) · 78 KB
/
instr.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 Instruction XX
XX XX
XX The interface to generate a machine-instruction. XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "codegen.h"
#include "instr.h"
#include "emit.h"
/*****************************************************************************/
//-----------------------------------------------------------------------------
// genInsName: Returns the string representation of the given CPU instruction, as
// it exists in the instruction table. Note that some architectures don't encode the
// name completely in the table: xarch sometimes prepends a "v", and arm sometimes
// appends a "s". Use `genInsDisplayName()` to get a fully-formed name.
//
const char* CodeGen::genInsName(instruction ins)
{
// clang-format off
static
const char * const insNames[] =
{
#if defined(TARGET_XARCH)
#define INST0(id, nm, um, mr, tt, flags) nm,
#define INST1(id, nm, um, mr, tt, flags) nm,
#define INST2(id, nm, um, mr, mi, tt, flags) nm,
#define INST3(id, nm, um, mr, mi, rm, tt, flags) nm,
#define INST4(id, nm, um, mr, mi, rm, a4, tt, flags) nm,
#define INST5(id, nm, um, mr, mi, rm, a4, rr, tt, flags) nm,
#include "instrs.h"
#elif defined(TARGET_ARM)
#define INST1(id, nm, fp, ldst, fmt, e1 ) nm,
#define INST2(id, nm, fp, ldst, fmt, e1, e2 ) nm,
#define INST3(id, nm, fp, ldst, fmt, e1, e2, e3 ) nm,
#define INST4(id, nm, fp, ldst, fmt, e1, e2, e3, e4 ) nm,
#define INST5(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5 ) nm,
#define INST6(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5, e6 ) nm,
#define INST8(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5, e6, e7, e8 ) nm,
#define INST9(id, nm, fp, ldst, fmt, e1, e2, e3, e4, e5, e6, e7, e8, e9 ) nm,
#include "instrs.h"
#elif defined(TARGET_ARM64)
#define INST1(id, nm, ldst, fmt, e1 ) nm,
#define INST2(id, nm, ldst, fmt, e1, e2 ) nm,
#define INST3(id, nm, ldst, fmt, e1, e2, e3 ) nm,
#define INST4(id, nm, ldst, fmt, e1, e2, e3, e4 ) nm,
#define INST5(id, nm, ldst, fmt, e1, e2, e3, e4, e5 ) nm,
#define INST6(id, nm, ldst, fmt, e1, e2, e3, e4, e5, e6 ) nm,
#define INST9(id, nm, ldst, fmt, e1, e2, e3, e4, e5, e6, e7, e8, e9 ) nm,
#include "instrs.h"
#define INST1(id, nm, info, fmt, e1 ) nm,
#define INST2(id, nm, info, fmt, e1, e2 ) nm,
#define INST3(id, nm, info, fmt, e1, e2, e3 ) nm,
#define INST4(id, nm, info, fmt, e1, e2, e3, e4 ) nm,
#define INST5(id, nm, info, fmt, e1, e2, e3, e4, e5 ) nm,
#define INST6(id, nm, info, fmt, e1, e2, e3, e4, e5, e6 ) nm,
#define INST7(id, nm, info, fmt, e1, e2, e3, e4, e5, e6, e7 ) nm,
#define INST8(id, nm, info, fmt, e1, e2, e3, e4, e5, e6, e7, e8 ) nm,
#define INST9(id, nm, info, fmt, e1, e2, e3, e4, e5, e6, e7, e8, e9 ) nm,
#define INST11(id, nm, info, fmt, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10,e11 ) nm,
#define INST13(id, nm, info, fmt, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13) nm,
#include "instrsarm64sve.h"
#elif defined(TARGET_LOONGARCH64)
#define INST(id, nm, ldst, e1, msk, fmt) nm,
#include "instrs.h"
#elif defined(TARGET_RISCV64)
#define INST(id, nm, ldst, e1) nm,
#include "instrs.h"
#else
#error "Unknown TARGET"
#endif
};
// clang-format on
assert((unsigned)ins < ArrLen(insNames));
assert(insNames[ins] != nullptr);
return insNames[ins];
}
//-----------------------------------------------------------------------------
// genInsDisplayName: Get a fully-formed instruction display name. This only handles
// the xarch case of prepending a "v", not the arm case of appending an "s".
// This can be called up to four times in a single 'printf' before the static buffers
// get reused.
//
// Returns:
// String with instruction name
//
const char* CodeGen::genInsDisplayName(emitter::instrDesc* id)
{
instruction ins = id->idIns();
const char* insName = genInsName(ins);
#ifdef TARGET_XARCH
const int TEMP_BUFFER_LEN = 40;
static unsigned curBuf = 0;
static char buf[4][TEMP_BUFFER_LEN];
const char* retbuf;
const emitter* emit = GetEmitter();
if (emit->IsVexOrEvexEncodableInstruction(ins))
{
if (!emit->IsBMIInstruction(ins) && !emit->IsKInstruction(ins))
{
if (emit->TakesEvexPrefix(id))
{
switch (ins)
{
case INS_movdqa:
{
return "vmovdqa32";
}
case INS_movdqu:
{
return "vmovdqu32";
}
case INS_pand:
{
return "vpandd";
}
case INS_pandn:
{
return "vpandnd";
}
case INS_por:
{
return "vpord";
}
case INS_pxor:
{
return "vpxord";
}
case INS_roundpd:
{
return "vrndscalepd";
}
case INS_roundps:
{
return "vrndscaleps";
}
case INS_roundsd:
{
return "vrndscalesd";
}
case INS_roundss:
{
return "vrndscaless";
}
case INS_vbroadcastf128:
{
return "vbroadcastf32x4";
}
case INS_vextractf128:
{
return "vextractf32x4";
}
case INS_vinsertf128:
{
return "vinsertf32x4";
}
case INS_vbroadcasti128:
{
return "vbroadcasti32x4";
}
case INS_vextracti128:
{
return "vextracti32x4";
}
case INS_vinserti128:
{
return "vinserti32x4";
}
default:
{
break;
}
}
}
sprintf_s(buf[curBuf], TEMP_BUFFER_LEN, "v%s", insName);
retbuf = buf[curBuf];
curBuf = (curBuf + 1) % 4;
return retbuf;
}
}
// Some instructions have different mnemonics depending on the size.
switch (ins)
{
case INS_cdq:
switch (id->idOpSize())
{
case EA_8BYTE:
return "cqo";
case EA_4BYTE:
return "cdq";
case EA_2BYTE:
return "cwd";
default:
unreached();
}
case INS_cwde:
switch (id->idOpSize())
{
case EA_8BYTE:
return "cdqe";
case EA_4BYTE:
return "cwde";
case EA_2BYTE:
return "cbw";
default:
unreached();
}
default:
break;
}
#endif // TARGET_XARCH
return insName;
}
/*****************************************************************************
*
* Return the size string (e.g. "word ptr") appropriate for the given size.
*/
const char* CodeGen::genSizeStr(emitAttr attr)
{
// clang-format off
static
const char * const sizes[] =
{
"byte ptr ",
"word ptr ",
"dword ptr ",
"qword ptr ",
"xmmword ptr ",
"ymmword ptr ",
"zmmword ptr "
};
// clang-format on
unsigned size = EA_SIZE(attr);
assert(genMaxOneBit(size) && (size <= 64));
if (EA_ATTR(size) == attr)
{
return (size > 0) ? sizes[genLog2(size)] : "";
}
else if (attr == EA_GCREF)
{
return "gword ptr ";
}
else if (attr == EA_BYREF)
{
return "bword ptr ";
}
else if (EA_IS_DSP_RELOC(attr))
{
return "rword ptr ";
}
else
{
assert(!"Unexpected");
return "unknw ptr ";
}
}
/*****************************************************************************
*
* Generate an instruction.
*/
void CodeGen::instGen(instruction ins)
{
GetEmitter()->emitIns(ins);
#ifdef TARGET_XARCH
#ifdef PSEUDORANDOM_NOP_INSERTION
// A workaround necessitated by limitations of emitter
// if we are scheduled to insert a nop here, we have to delay it
// hopefully we have not missed any other prefix instructions or places
// they could be inserted
if (ins == INS_lock && GetEmitter()->emitNextNop == 0)
{
GetEmitter()->emitNextNop = 1;
}
#endif // PSEUDORANDOM_NOP_INSERTION
#endif
}
/*****************************************************************************
*
* Returns non-zero if the given CPU instruction is a floating-point ins.
*/
// static inline
bool CodeGenInterface::instIsFP(instruction ins)
{
assert((unsigned)ins < ArrLen(instInfo));
#ifdef TARGET_XARCH
return (instInfo[ins] & INS_FLAGS_x87Instr) != 0;
#else
return (instInfo[ins] & INST_FP) != 0;
#endif
}
#if defined(TARGET_XARCH)
/*****************************************************************************
*
* Returns non-zero if the given CPU instruction is an embedded broadcast
* compatible instruction.
*/
// static inline
bool CodeGenInterface::instIsEmbeddedBroadcastCompatible(instruction ins)
{
assert((unsigned)ins < ArrLen(instInfo));
return (instInfo[ins] & INS_Flags_EmbeddedBroadcastSupported) != 0;
}
/*****************************************************************************
*
* Returns the value of the given instruction's input size attribute, in bytes.
*/
unsigned CodeGenInterface::instInputSize(instruction ins)
{
assert((unsigned)ins < ArrLen(instInfo));
insFlags inputSize = static_cast<insFlags>((instInfo[ins] & Input_Mask));
switch (inputSize)
{
case Input_8Bit:
return 1;
case Input_16Bit:
return 2;
case Input_32Bit:
return 4;
case Input_64Bit:
return 8;
default:
unreached();
}
}
#endif // TARGET_XARCH
/*****************************************************************************
*
* Generate a set instruction.
*/
void CodeGen::inst_SET(emitJumpKind condition, regNumber reg)
{
#ifdef TARGET_XARCH
instruction ins;
/* Convert the condition to an instruction opcode */
switch (condition)
{
case EJ_js:
ins = INS_sets;
break;
case EJ_jns:
ins = INS_setns;
break;
case EJ_je:
ins = INS_sete;
break;
case EJ_jne:
ins = INS_setne;
break;
case EJ_jl:
ins = INS_setl;
break;
case EJ_jle:
ins = INS_setle;
break;
case EJ_jge:
ins = INS_setge;
break;
case EJ_jg:
ins = INS_setg;
break;
case EJ_jb:
ins = INS_setb;
break;
case EJ_jbe:
ins = INS_setbe;
break;
case EJ_jae:
ins = INS_setae;
break;
case EJ_ja:
ins = INS_seta;
break;
case EJ_jp:
ins = INS_setp;
break;
case EJ_jnp:
ins = INS_setnp;
break;
default:
NO_WAY("unexpected condition type");
return;
}
assert(genRegMask(reg) & RBM_BYTE_REGS);
// These instructions only write the low byte of 'reg'
GetEmitter()->emitIns_R(ins, EA_1BYTE, reg);
#elif defined(TARGET_ARM64)
GetEmitter()->emitIns_R_COND(INS_cset, EA_8BYTE, reg, JumpKindToInsCond(condition));
#else
NYI("inst_SET");
#endif
}
/*****************************************************************************
*
* Generate a "op reg" instruction.
*/
void CodeGen::inst_RV(instruction ins, regNumber reg, var_types type, emitAttr size)
{
if (size == EA_UNKNOWN)
{
size = emitActualTypeSize(type);
}
#ifdef TARGET_LOONGARCH64
// inst_RV is not used for LoongArch64, so there is no need to define `emitIns_R`.
NYI_LOONGARCH64("inst_RV-----unused on LOONGARCH64----");
#elif defined(TARGET_RISCV64)
NYI_RISCV64("inst_RV-----unused on RISCV64----");
#else
GetEmitter()->emitIns_R(ins, size, reg);
#endif
}
/*****************************************************************************
*
* Generate a "mov reg1, reg2" instruction.
*/
void CodeGen::inst_Mov(var_types dstType,
regNumber dstReg,
regNumber srcReg,
bool canSkip,
emitAttr size,
insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
#if defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
if (isFloatRegType(dstType) != genIsValidFloatReg(dstReg))
{
if (dstType == TYP_FLOAT)
{
dstType = TYP_INT;
}
else if (dstType == TYP_DOUBLE)
{
dstType = TYP_LONG;
}
else if (dstType == TYP_INT)
{
dstType = TYP_FLOAT;
}
else if (dstType == TYP_LONG)
{
dstType = TYP_DOUBLE;
}
else
{
NYI_LOONGARCH64("CodeGen::inst_Mov dstType");
NYI_RISCV64("CodeGen::inst_Mov dstType");
}
}
#endif
instruction ins = ins_Copy(srcReg, dstType);
if (size == EA_UNKNOWN)
{
size = emitActualTypeSize(dstType);
}
#ifdef TARGET_ARM
GetEmitter()->emitIns_Mov(ins, size, dstReg, srcReg, canSkip, flags);
#else
GetEmitter()->emitIns_Mov(ins, size, dstReg, srcReg, canSkip);
#endif
}
/*****************************************************************************
*
* Generate a "mov reg1, reg2" instruction.
*/
void CodeGen::inst_Mov_Extend(var_types srcType,
bool srcInReg,
regNumber dstReg,
regNumber srcReg,
bool canSkip,
emitAttr size,
insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
instruction ins = ins_Move_Extend(srcType, srcInReg);
if (size == EA_UNKNOWN)
{
size = emitActualTypeSize(srcType);
}
#ifdef TARGET_ARM
GetEmitter()->emitIns_Mov(ins, size, dstReg, srcReg, canSkip, flags);
#else
GetEmitter()->emitIns_Mov(ins, size, dstReg, srcReg, canSkip);
#endif
}
/*****************************************************************************
*
* Generate a "op reg1, reg2" instruction.
*/
//------------------------------------------------------------------------
// inst_RV_RV: Generate a "op reg1, reg2" instruction.
//
// Arguments:
// ins - the instruction to generate;
// reg1 - the first register to use, the dst for most instructions;
// reg2 - the second register to use, the src for most instructions;
// type - the type used to get the size attribute if not given, usually type of the reg2 operand;
// size - the size attribute, the type arg is ignored if this arg is provided with an actual value;
// flags - whether flags are set for arm32.
//
void CodeGen::inst_RV_RV(instruction ins,
regNumber reg1,
regNumber reg2,
var_types type /* = TYP_I_IMPL */,
emitAttr size /* = EA_UNKNOWN */,
insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
if (size == EA_UNKNOWN)
{
size = emitActualTypeSize(type);
}
#ifdef TARGET_ARM
GetEmitter()->emitIns_R_R(ins, size, reg1, reg2, flags);
#else
GetEmitter()->emitIns_R_R(ins, size, reg1, reg2);
#endif
}
/*****************************************************************************
*
* Generate a "op reg1, reg2, reg3" instruction.
*/
void CodeGen::inst_RV_RV_RV(instruction ins,
regNumber reg1,
regNumber reg2,
regNumber reg3,
emitAttr size,
insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
#ifdef TARGET_ARM
GetEmitter()->emitIns_R_R_R(ins, size, reg1, reg2, reg3, flags);
#elif defined(TARGET_XARCH) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
GetEmitter()->emitIns_R_R_R(ins, size, reg1, reg2, reg3);
#else
NYI("inst_RV_RV_RV");
#endif
}
/*****************************************************************************
*
* Generate a "op icon" instruction.
*/
void CodeGen::inst_IV(instruction ins, cnsval_ssize_t val)
{
GetEmitter()->emitIns_I(ins, EA_PTRSIZE, val);
}
/*****************************************************************************
*
* Generate a "op icon" instruction where icon is a handle of type specified
* by 'flags'
*/
void CodeGen::inst_IV_handle(instruction ins, cnsval_ssize_t val)
{
GetEmitter()->emitIns_I(ins, EA_HANDLE_CNS_RELOC, val);
}
/*****************************************************************************
*
* Display a stack frame reference.
*/
void CodeGen::inst_set_SV_var(GenTree* tree)
{
#ifdef DEBUG
assert((tree != nullptr) && (tree->OperIs(GT_LCL_VAR, GT_STORE_LCL_VAR) || tree->IsLclVarAddr()));
assert(tree->AsLclVarCommon()->GetLclNum() < compiler->lvaCount);
GetEmitter()->emitVarRefOffs = tree->AsLclVar()->gtLclILoffs;
#endif // DEBUG
}
/*****************************************************************************
*
* Generate a "op reg, icon" instruction.
*/
void CodeGen::inst_RV_IV(
instruction ins, regNumber reg, target_ssize_t val, emitAttr size, insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
#if !defined(TARGET_64BIT)
assert(size != EA_8BYTE);
#endif
#ifdef TARGET_ARM
if (arm_Valid_Imm_For_Instr(ins, val, flags))
{
GetEmitter()->emitIns_R_I(ins, size, reg, val, flags);
}
else if (ins == INS_mov)
{
instGen_Set_Reg_To_Imm(size, reg, val);
}
else
{
// TODO-Cleanup: Add a comment about why this is unreached() for RyuJIT backend.
unreached();
}
#elif defined(TARGET_ARM64)
// TODO-Arm64-Bug: handle large constants!
// Probably need something like the ARM case above: if (arm_Valid_Imm_For_Instr(ins, val)) ...
assert(ins != INS_cmp);
assert(ins != INS_tst);
assert(ins != INS_mov);
GetEmitter()->emitIns_R_R_I(ins, size, reg, reg, val);
#elif defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
GetEmitter()->emitIns_R_R_I(ins, size, reg, reg, val);
#else // !TARGET_ARM
#ifdef TARGET_AMD64
// Instead of an 8-byte immediate load, a 4-byte immediate will do fine
// as the high 4 bytes will be zero anyway.
if (size == EA_8BYTE && ins == INS_mov && ((val & 0xFFFFFFFF00000000LL) == 0))
{
size = EA_4BYTE;
GetEmitter()->emitIns_R_I(ins, size, reg, val);
}
else if (EA_SIZE(size) == EA_8BYTE && ins != INS_mov && (((int)val != val) || EA_IS_CNS_RELOC(size)))
{
assert(!"Invalid immediate for inst_RV_IV");
}
else
#endif // TARGET_AMD64
{
GetEmitter()->emitIns_R_I(ins, size, reg, val);
}
#endif // !TARGET_ARM
}
//------------------------------------------------------------------------
// inst_TT_RV: Generate a store of a lclVar
//
// Arguments:
// ins - the instruction to generate
// size - the size attributes for the store
// tree - the lclVar node
// reg - the register currently holding the value of the local
//
void CodeGen::inst_TT_RV(instruction ins, emitAttr size, GenTree* tree, regNumber reg)
{
#ifdef DEBUG
// The tree must have a valid register value.
assert(reg != REG_STK);
bool isValidInReg = ((tree->gtFlags & GTF_SPILLED) == 0);
if (!isValidInReg)
{
// Is this the special case of a write-thru lclVar?
// We mark it as SPILLED to denote that its value is valid in memory.
if (((tree->gtFlags & GTF_SPILL) != 0) && tree->gtOper == GT_STORE_LCL_VAR)
{
isValidInReg = true;
}
}
assert(isValidInReg);
assert(size != EA_UNKNOWN);
assert(tree->OperIs(GT_LCL_VAR, GT_STORE_LCL_VAR));
#endif // DEBUG
unsigned varNum = tree->AsLclVarCommon()->GetLclNum();
assert(varNum < compiler->lvaCount);
#if CPU_LOAD_STORE_ARCH
#ifdef TARGET_ARM64
// Workaround until https://github.com/dotnet/runtime/issues/105512 is fixed.
assert(GetEmitter()->emitInsIsStore(ins) || (ins == INS_sve_str));
#else
assert(GetEmitter()->emitInsIsStore(ins));
#endif
#endif
GetEmitter()->emitIns_S_R(ins, size, reg, varNum, 0);
}
/*****************************************************************************
*
* Generate a "shift reg, icon" instruction.
*/
void CodeGen::inst_RV_SH(
instruction ins, emitAttr size, regNumber reg, unsigned val, insFlags flags /* = INS_FLAGS_DONT_CARE */)
{
#if defined(TARGET_ARM)
if (val >= 32)
val &= 0x1f;
GetEmitter()->emitIns_R_I(ins, size, reg, val, flags);
#elif defined(TARGET_XARCH)
#ifdef TARGET_AMD64
// X64 JB BE ensures only encodable values make it here.
// x86 can encode 8 bits, though it masks down to 5 or 6
// depending on 32-bit or 64-bit registers are used.
// Here we will allow anything that is encodable.
assert(val < 256);
#endif
ins = genMapShiftInsToShiftByConstantIns(ins, val);
if (val == 1)
{
GetEmitter()->emitIns_R(ins, size, reg);
}
else
{
GetEmitter()->emitIns_R_I(ins, size, reg, val);
}
#else
NYI("inst_RV_SH - unknown target");
#endif // TARGET*
}
#if defined(TARGET_XARCH)
//------------------------------------------------------------------------
// genOperandDesc: Create an operand descriptor for the given operand node.
//
// The XARCH emitter requires codegen to use different methods for different
// kinds of operands. However, the logic for determining which ones, in
// general, is not simple (due to the fact that "memory" in the emitter can
// be represented in more than one way). This helper method encapsulated the
// logic for determining what "kind" of operand "op" is.
//
// Arguments:
// op - The operand node for which to obtain the descriptor.
//
// Return Value:
// The operand descriptor for "op".
//
// Notes:
// This method is not idempotent - it can only be called once for a
// given node.
//
CodeGen::OperandDesc CodeGen::genOperandDesc(GenTree* op)
{
if (!op->isContained() && !op->isUsedFromSpillTemp())
{
return OperandDesc(op->GetRegNum());
}
emitter* emit = GetEmitter();
TempDsc* tmpDsc = nullptr;
unsigned varNum = BAD_VAR_NUM;
uint16_t offset = UINT16_MAX;
if (op->isUsedFromSpillTemp())
{
assert(op->IsRegOptional());
tmpDsc = getSpillTempDsc(op);
varNum = tmpDsc->tdTempNum();
offset = 0;
regSet.tmpRlsTemp(tmpDsc);
}
else if (op->isIndir() || op->OperIsHWIntrinsic())
{
GenTree* addr;
GenTreeIndir* memIndir = nullptr;
if (op->isIndir())
{
memIndir = op->AsIndir();
addr = memIndir->Addr();
}
else
{
assert(op->OperIsHWIntrinsic());
#if defined(FEATURE_HW_INTRINSICS)
GenTreeHWIntrinsic* hwintrinsic = op->AsHWIntrinsic();
NamedIntrinsic intrinsicId = hwintrinsic->GetHWIntrinsicId();
var_types simdBaseType = hwintrinsic->GetSimdBaseType();
switch (intrinsicId)
{
case NI_SSE3_LoadAndDuplicateToVector128:
case NI_AVX_BroadcastScalarToVector128:
case NI_AVX_BroadcastScalarToVector256:
{
// we have the assumption that these intrinsics
// only take a memory address as the operand.
assert(hwintrinsic->isContained());
assert(hwintrinsic->OperIsMemoryLoad());
assert(hwintrinsic->GetOperandCount() == 1);
assert(varTypeIsFloating(simdBaseType));
GenTree* hwintrinsicChild = hwintrinsic->Op(1);
assert(hwintrinsicChild->isContained());
if (hwintrinsicChild->OperIs(GT_LCL_ADDR, GT_CNS_INT, GT_LEA))
{
addr = hwintrinsic->Op(1);
break;
}
else
{
assert(hwintrinsicChild->OperIs(GT_LCL_VAR));
return OperandDesc(simdBaseType, hwintrinsicChild);
}
}
case NI_SSE3_MoveAndDuplicate:
case NI_AVX2_BroadcastScalarToVector128:
case NI_AVX2_BroadcastScalarToVector256:
case NI_AVX512F_BroadcastScalarToVector512:
{
assert(hwintrinsic->isContained());
if (intrinsicId == NI_SSE3_MoveAndDuplicate)
{
assert(simdBaseType == TYP_DOUBLE);
}
// If broadcast node is contained, should mean that we have some forms like
// Broadcast -> CreateScalarUnsafe -> Scalar.
// If so, directly emit scalar.
// In the code below, we specially handle the `Broadcast -> CNS_INT/CNS_LNG` form and
// handle other cases recursively.
GenTree* hwintrinsicChild = hwintrinsic->Op(1);
assert(hwintrinsicChild->isContained());
if (hwintrinsicChild->OperIs(GT_CNS_INT, GT_CNS_LNG))
{
// a special case is when the operand of CreateScalarUnsafe is an integer type,
// CreateScalarUnsafe node will be folded, so we directly match a pattern of
// broadcast -> LCL_VAR(TYP_(U)INT/LONG)
INT64 scalarValue = hwintrinsicChild->AsIntConCommon()->IntegralValue();
UNATIVE_OFFSET cnum = emit->emitDataConst(&scalarValue, genTypeSize(simdBaseType),
genTypeSize(simdBaseType), simdBaseType);
return OperandDesc(compiler->eeFindJitDataOffs(cnum));
}
else
{
// If the operand of broadcast is not a constant integer,
// we handle all the other cases recursively.
return genOperandDesc(hwintrinsicChild);
}
break;
}
case NI_Vector128_CreateScalarUnsafe:
case NI_Vector256_CreateScalarUnsafe:
case NI_Vector512_CreateScalarUnsafe:
{
// The hwintrinsic should be contained and its
// op1 should be either contained or spilled. This
// allows us to transparently "look through" the
// CreateScalarUnsafe and treat it directly like
// a load from memory.
assert(hwintrinsic->isContained());
op = hwintrinsic->Op(1);
return genOperandDesc(op);
}
default:
{
assert(hwintrinsic->OperIsMemoryLoad());
assert(hwintrinsic->GetOperandCount() == 1);
addr = hwintrinsic->Op(1);
break;
}
}
#else
unreached();
#endif // FEATURE_HW_INTRINSICS
}
if (addr->isContained() && addr->OperIs(GT_LCL_ADDR))
{
varNum = addr->AsLclFld()->GetLclNum();
offset = addr->AsLclFld()->GetLclOffs();
}
else
{
return (memIndir != nullptr) ? OperandDesc(memIndir) : OperandDesc(op->TypeGet(), addr);
}
}
else
{
switch (op->OperGet())
{
case GT_LCL_FLD:
varNum = op->AsLclFld()->GetLclNum();
offset = op->AsLclFld()->GetLclOffs();
break;
case GT_LCL_VAR:
assert(op->IsRegOptional() || !compiler->lvaGetDesc(op->AsLclVar())->lvIsRegCandidate());
varNum = op->AsLclVar()->GetLclNum();
offset = 0;
break;
case GT_CNS_DBL:
return OperandDesc(emit->emitFltOrDblConst(op->AsDblCon()->DconValue(), emitTypeSize(op)));
case GT_CNS_INT:
{
assert(op->isContainedIntOrIImmed());
return OperandDesc(op->AsIntCon()->IconValue(), op->AsIntCon()->ImmedValNeedsReloc(compiler));
}
#if defined(FEATURE_SIMD)
case GT_CNS_VEC:
{
switch (op->TypeGet())
{
case TYP_SIMD8:
{
simd8_t constValue;
memcpy(&constValue, &op->AsVecCon()->gtSimdVal, sizeof(simd8_t));
return OperandDesc(emit->emitSimd8Const(constValue));
}
case TYP_SIMD12:
{
simd16_t constValue = {};