forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegencommon.cpp
11821 lines (9895 loc) · 415 KB
/
codegencommon.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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Code Generator Common: XX
XX Methods common to all architectures and register allocation strategies XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
// TODO-Cleanup: There are additional methods in CodeGen*.cpp that are almost
// identical, and which should probably be moved here.
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "codegen.h"
#include "gcinfo.h"
#include "emit.h"
#ifndef JIT32_GCENCODER
#include "gcinfoencoder.h"
#endif
/*****************************************************************************/
const BYTE genTypeSizes[] =
{
#define DEF_TP(tn,nm,jitType,verType,sz,sze,asze,st,al,tf,howUsed) sz,
#include "typelist.h"
#undef DEF_TP
};
const BYTE genTypeAlignments[] =
{
#define DEF_TP(tn,nm,jitType,verType,sz,sze,asze,st,al,tf,howUsed) al,
#include "typelist.h"
#undef DEF_TP
};
const BYTE genTypeStSzs[] =
{
#define DEF_TP(tn,nm,jitType,verType,sz,sze,asze,st,al,tf,howUsed) st,
#include "typelist.h"
#undef DEF_TP
};
const BYTE genActualTypes[] =
{
#define DEF_TP(tn,nm,jitType,verType,sz,sze,asze,st,al,tf,howUsed) jitType,
#include "typelist.h"
#undef DEF_TP
};
void CodeGenInterface::setFramePointerRequiredEH(bool value)
{
m_cgFramePointerRequired = value;
#ifndef JIT32_GCENCODER
if (value)
{
// EnumGcRefs will only enumerate slots in aborted frames
// if they are fully-interruptible. So if we have a catch
// or finally that will keep frame-vars alive, we need to
// force fully-interruptible.
#ifdef DEBUG
if (verbose)
printf("Method has EH, marking method as fully interruptible\n");
#endif
m_cgInterruptible = true;
}
#endif // JIT32_GCENCODER
}
/*****************************************************************************/
CodeGenInterface *getCodeGenerator(Compiler *comp)
{
return new (comp, CMK_Codegen) CodeGen(comp);
}
// CodeGen constructor
CodeGenInterface::CodeGenInterface(Compiler* theCompiler) :
gcInfo(theCompiler),
regSet(theCompiler, gcInfo),
compiler(theCompiler)
{
}
/*****************************************************************************/
CodeGen::CodeGen(Compiler * theCompiler) :
CodeGenInterface(theCompiler)
{
#if defined(_TARGET_XARCH_) && !FEATURE_STACK_FP_X87
negBitmaskFlt = nullptr;
negBitmaskDbl = nullptr;
absBitmaskFlt = nullptr;
absBitmaskDbl = nullptr;
u8ToDblBitmask = nullptr;
#endif // defined(_TARGET_XARCH_) && !FEATURE_STACK_FP_X87
regTracker.rsTrackInit(compiler, ®Set);
gcInfo.regSet = ®Set;
m_cgEmitter = new (compiler->getAllocator()) emitter();
m_cgEmitter->codeGen = this;
m_cgEmitter->gcInfo = &gcInfo;
#ifdef DEBUG
setVerbose(compiler->verbose);
#endif // DEBUG
compiler->tmpInit ();
#ifdef DEBUG
#if defined(_TARGET_X86_) && defined(LEGACY_BACKEND)
// This appears to be x86-specific. It's attempting to make sure all offsets to temps
// are large. For ARM, this doesn't interact well with our decision about whether to use
// R10 or not as a reserved register.
if (regSet.rsStressRegs())
compiler->tmpIntSpillMax = (SCHAR_MAX/sizeof(int));
#endif // defined(_TARGET_X86_) && defined(LEGACY_BACKEND)
#endif // DEBUG
instInit();
// TODO-Cleanup: These used to be set in rsInit() - should they be moved to RegSet??
// They are also accessed by the register allocators and fgMorphLclVar().
intRegState.rsCurRegArgNum = 0;
floatRegState.rsCurRegArgNum = 0;
#ifdef LATE_DISASM
getDisAssembler().disInit(compiler);
#endif
#ifdef DEBUG
genTempLiveChg = true;
genTrnslLocalVarCount = 0;
// Shouldn't be used before it is set in genFnProlog()
compiler->compCalleeRegsPushed = UninitializedWord<unsigned>();
#ifdef _TARGET_AMD64_
// Shouldn't be used before it is set in genFnProlog()
compiler->compCalleeFPRegsSavedMask = (regMaskTP)-1;
#endif // _TARGET_AMD64_
#endif // DEBUG
#ifdef _TARGET_AMD64_
// This will be set before final frame layout.
compiler->compVSQuirkStackPaddingNeeded = 0;
#endif // _TARGET_AMD64_
#ifdef LEGACY_BACKEND
genFlagsEqualToNone();
#endif // LEGACY_BACKEND
#ifdef DEBUGGING_SUPPORT
// Initialize the IP-mapping logic.
compiler->genIPmappingList = nullptr;
compiler->genIPmappingLast = nullptr;
compiler->genCallSite2ILOffsetMap = nullptr;
#endif
/* Assume that we not fully interruptible */
genInterruptible = false;
#ifdef DEBUG
genInterruptibleUsed = false;
genCurDispOffset = (unsigned) -1;
#endif
}
void CodeGenInterface::genMarkTreeInReg(GenTreePtr tree, regNumber reg)
{
tree->gtRegNum = reg;
tree->gtFlags |= GTF_REG_VAL;
}
void CodeGenInterface::genMarkTreeInRegPair(GenTreePtr tree, regPairNo regPair)
{
tree->gtRegPair = regPair;
tree->gtFlags |= GTF_REG_VAL;
}
#if defined(_TARGET_X86_) || defined(_TARGET_ARM_)
//---------------------------------------------------------------------
// genTotalFrameSize - return the "total" size of the stack frame, including local size
// and callee-saved register size. There are a few things "missing" depending on the
// platform. The function genCallerSPtoInitialSPdelta() includes those things.
//
// For ARM, this doesn't include the prespilled registers.
//
// For x86, this doesn't include the frame pointer if codeGen->isFramePointerUsed() is true.
// It also doesn't include the pushed return address.
//
// Return value:
// Frame size
int CodeGenInterface::genTotalFrameSize()
{
assert(!IsUninitialized(compiler->compCalleeRegsPushed));
int totalFrameSize = compiler->compCalleeRegsPushed * REGSIZE_BYTES +
compiler->compLclFrameSize;
assert(totalFrameSize >= 0);
return totalFrameSize;
}
//---------------------------------------------------------------------
// genSPtoFPdelta - return the offset from SP to the frame pointer.
// This number is going to be positive, since SP must be at the lowest
// address.
//
// There must be a frame pointer to call this function!
int CodeGenInterface::genSPtoFPdelta()
{
assert(isFramePointerUsed());
int delta;
delta = -genCallerSPtoInitialSPdelta() + genCallerSPtoFPdelta();
return delta;
}
//---------------------------------------------------------------------
// genCallerSPtoFPdelta - return the offset from Caller-SP to the frame pointer.
// This number is going to be negative, since the Caller-SP is at a higher
// address than the frame pointer.
//
// There must be a frame pointer to call this function!
int CodeGenInterface::genCallerSPtoFPdelta()
{
assert(isFramePointerUsed());
int callerSPtoFPdelta = 0;
#if defined(_TARGET_ARM_)
// On ARM, we first push the prespill registers, then store LR, then R11 (FP), and point R11 at the saved R11.
callerSPtoFPdelta -= genCountBits(regSet.rsMaskPreSpillRegs(true)) * REGSIZE_BYTES;
callerSPtoFPdelta -= 2 * REGSIZE_BYTES;
#elif defined(_TARGET_X86_)
// Thanks to ebp chaining, the difference between ebp-based addresses
// and caller-SP-relative addresses is just the 2 pointers:
// return address
// pushed ebp
callerSPtoFPdelta -= 2 * REGSIZE_BYTES;
#else
#error "Unknown _TARGET_"
#endif // _TARGET_*
assert(callerSPtoFPdelta <= 0);
return callerSPtoFPdelta;
}
//---------------------------------------------------------------------
// genCallerSPtoInitialSPdelta - return the offset from Caller-SP to Initial SP.
//
// This number will be negative.
int CodeGenInterface::genCallerSPtoInitialSPdelta()
{
int callerSPtoSPdelta = 0;
#if defined(_TARGET_ARM_)
callerSPtoSPdelta -= genCountBits(regSet.rsMaskPreSpillRegs(true)) * REGSIZE_BYTES;
callerSPtoSPdelta -= genTotalFrameSize();
#elif defined(_TARGET_X86_)
callerSPtoSPdelta -= genTotalFrameSize();
callerSPtoSPdelta -= REGSIZE_BYTES; // caller-pushed return address
// compCalleeRegsPushed does not account for the frame pointer
// TODO-Cleanup: shouldn't this be part of genTotalFrameSize?
if (isFramePointerUsed())
{
callerSPtoSPdelta -= REGSIZE_BYTES;
}
#else
#error "Unknown _TARGET_"
#endif // _TARGET_*
assert(callerSPtoSPdelta <= 0);
return callerSPtoSPdelta;
}
#endif // defined(_TARGET_X86_) || defined(_TARGET_ARM_)
/*****************************************************************************
* Should we round simple operations (assignments, arithmetic operations, etc.)
*/
// inline
// static
bool CodeGen::genShouldRoundFP()
{
RoundLevel roundLevel = getRoundFloatLevel();
switch (roundLevel)
{
case ROUND_NEVER:
case ROUND_CMP_CONST:
case ROUND_CMP:
return false;
default:
assert(roundLevel == ROUND_ALWAYS);
return true;
}
}
/*****************************************************************************
*
* Initialize some global variables.
*/
void CodeGen::genPrepForCompiler()
{
unsigned varNum;
LclVarDsc * varDsc;
/* Figure out which non-register variables hold pointers */
VarSetOps::AssignNoCopy(compiler, gcInfo.gcTrkStkPtrLcls, VarSetOps::MakeEmpty(compiler));
// Figure out which variables live in registers.
// Also, initialize gcTrkStkPtrLcls to include all tracked variables that do not fully live
// in a register (i.e. they live on the stack for all or part of their lifetime).
// Note that lvRegister indicates that a lclVar is in a register for its entire lifetime.
VarSetOps::AssignNoCopy(compiler, compiler->raRegVarsMask, VarSetOps::MakeEmpty(compiler));
for (varNum = 0, varDsc = compiler->lvaTable;
varNum < compiler->lvaCount;
varNum++ , varDsc++)
{
if (varDsc->lvTracked
#ifndef LEGACY_BACKEND
|| varDsc->lvIsRegCandidate()
#endif // !LEGACY_BACKEND
)
{
if (varDsc->lvRegister
#if FEATURE_STACK_FP_X87
&& !varDsc->IsFloatRegType()
#endif
)
{
VarSetOps::AddElemD(compiler, compiler->raRegVarsMask, varDsc->lvVarIndex);
}
else if (compiler->lvaIsGCTracked(varDsc) &&
(!varDsc->lvIsParam || varDsc->lvIsRegArg) )
{
VarSetOps::AddElemD(compiler, gcInfo.gcTrkStkPtrLcls, varDsc->lvVarIndex);
}
}
}
VarSetOps::AssignNoCopy(compiler, genLastLiveSet, VarSetOps::MakeEmpty(compiler));
genLastLiveMask = RBM_NONE;
#ifdef DEBUG
compiler->fgBBcountAtCodegen = compiler->fgBBcount;
#endif
}
/*****************************************************************************
* To report exception handling information to the VM, we need the size of the exception
* handling regions. To compute that, we need to emit labels for the beginning block of
* an EH region, and the block that immediately follows a region. Go through the EH
* table and mark all these blocks with BBF_HAS_LABEL to make this happen.
*
* The beginning blocks of the EH regions already should have this flag set.
*
* No blocks should be added or removed after this.
*
* This code is closely couple with genReportEH() in the sense that any block
* that this procedure has determined it needs to have a label has to be selected
* using the same logic both here and in genReportEH(), so basically any time there is
* a change in the way we handle EH reporting, we have to keep the logic of these two
* methods 'in sync'.
*/
void CodeGen::genPrepForEHCodegen()
{
assert(!compiler->fgSafeBasicBlockCreation);
EHblkDsc* HBtab;
EHblkDsc* HBtabEnd;
bool anyFinallys = false;
for (HBtab = compiler->compHndBBtab, HBtabEnd = compiler->compHndBBtab + compiler->compHndBBtabCount;
HBtab < HBtabEnd;
HBtab++)
{
assert(HBtab->ebdTryBeg->bbFlags & BBF_HAS_LABEL);
assert(HBtab->ebdHndBeg->bbFlags & BBF_HAS_LABEL);
if (HBtab->ebdTryLast->bbNext != nullptr)
{
HBtab->ebdTryLast->bbNext->bbFlags |= BBF_HAS_LABEL;
}
if (HBtab->ebdHndLast->bbNext != nullptr)
{
HBtab->ebdHndLast->bbNext->bbFlags |= BBF_HAS_LABEL;
}
if (HBtab->HasFilter())
{
assert(HBtab->ebdFilter->bbFlags & BBF_HAS_LABEL);
// The block after the last block of the filter is
// the handler begin block, which we already asserted
// has BBF_HAS_LABEL set.
}
#ifdef _TARGET_AMD64_
if (HBtab->HasFinallyHandler())
{
anyFinallys = true;
}
#endif // _TARGET_AMD64_
}
#ifdef _TARGET_AMD64_
if (anyFinallys)
{
for (BasicBlock* block = compiler->fgFirstBB; block != nullptr; block = block->bbNext)
{
if (block->bbJumpKind == BBJ_CALLFINALLY)
{
BasicBlock* bbToLabel = block->bbNext;
if (block->isBBCallAlwaysPair())
{
bbToLabel = bbToLabel->bbNext; // skip the BBJ_ALWAYS
}
if (bbToLabel != nullptr)
{
bbToLabel->bbFlags |= BBF_HAS_LABEL;
}
} // block is BBJ_CALLFINALLY
} // for each block
} // if (anyFinallys)
#endif // _TARGET_AMD64_
}
void
CodeGenInterface::genUpdateLife (GenTreePtr tree)
{
compiler->compUpdateLife</*ForCodeGen*/true>(tree);
}
void
CodeGenInterface::genUpdateLife (VARSET_VALARG_TP newLife)
{
compiler->compUpdateLife</*ForCodeGen*/true>(newLife);
}
// Returns the liveSet after tree has executed.
// "tree" MUST occur in the current statement, AFTER the most recent
// update of compiler->compCurLifeTree and compiler->compCurLife.
//
VARSET_VALRET_TP CodeGen::genUpdateLiveSetForward(GenTreePtr tree)
{
VARSET_TP VARSET_INIT(compiler, startLiveSet, compiler->compCurLife);
GenTreePtr startNode;
assert(tree != compiler->compCurLifeTree);
if (compiler->compCurLifeTree == NULL)
{
assert(compiler->compCurStmt != NULL);
startNode = compiler->compCurStmt->gtStmt.gtStmtList;
}
else
{
startNode = compiler->compCurLifeTree->gtNext;
}
return compiler->fgUpdateLiveSet(startLiveSet, startNode, tree);
}
// Determine the registers that are live after "second" has been evaluated,
// but which are not live after "first".
// PRECONDITIONS:
// 1. "first" must occur after compiler->compCurLifeTree in execution order for the current statement
// 2. "second" must occur after "first" in the current statement
//
regMaskTP
CodeGen::genNewLiveRegMask(GenTreePtr first, GenTreePtr second)
{
// First, compute the liveset after "first"
VARSET_TP firstLiveSet = genUpdateLiveSetForward(first);
// Now, update the set forward from "first" to "second"
VARSET_TP secondLiveSet = compiler->fgUpdateLiveSet(firstLiveSet, first->gtNext, second);
regMaskTP newLiveMask = genLiveMask(VarSetOps::Diff(compiler, secondLiveSet, firstLiveSet));
return newLiveMask;
}
// Return the register mask for the given register variable
// inline
regMaskTP CodeGenInterface::genGetRegMask(const LclVarDsc * varDsc)
{
regMaskTP regMask = RBM_NONE;
assert(varDsc->lvIsInReg());
if (varTypeIsFloating(varDsc->TypeGet()))
{
regMask = genRegMaskFloat(varDsc->lvRegNum, varDsc->TypeGet());
}
else
{
regMask = genRegMask(varDsc->lvRegNum);
if (isRegPairType(varDsc->lvType))
{
regMask |= genRegMask(varDsc->lvOtherReg);
}
}
return regMask;
}
// Return the register mask for the given lclVar or regVar tree node
// inline
regMaskTP CodeGenInterface::genGetRegMask(GenTreePtr tree)
{
assert (tree->gtOper == GT_LCL_VAR || tree->gtOper == GT_REG_VAR);
regMaskTP regMask = RBM_NONE;
const LclVarDsc * varDsc = compiler->lvaTable + tree->gtLclVarCommon.gtLclNum;
if (varDsc->lvPromoted)
{
for (unsigned i = varDsc->lvFieldLclStart;
i < varDsc->lvFieldLclStart + varDsc->lvFieldCnt;
++i)
{
noway_assert(compiler->lvaTable[i].lvIsStructField);
if (compiler->lvaTable[i].lvIsInReg())
{
regMask |= genGetRegMask(&compiler->lvaTable[i]);
}
}
}
else if (varDsc->lvIsInReg())
{
regMask = genGetRegMask(varDsc);
}
return regMask;
}
/*****************************************************************************
* TRACKING OF FLAGS
*****************************************************************************/
#ifdef LEGACY_BACKEND
void CodeGen::genFlagsEqualToNone()
{
genFlagsEqReg = REG_NA;
genFlagsEqVar = (unsigned)-1;
genFlagsEqLoc.Init();
}
/*****************************************************************************
*
* Record the fact that the flags register has a value that reflects the
* contents of the given register.
*/
void CodeGen::genFlagsEqualToReg(GenTreePtr tree,
regNumber reg)
{
genFlagsEqLoc.CaptureLocation(getEmitter());
genFlagsEqReg = reg;
/* previous setting of flags by a var becomes invalid */
genFlagsEqVar = 0xFFFFFFFF;
/* Set appropriate flags on the tree */
if (tree)
{
tree->gtFlags |= GTF_ZSF_SET;
assert(tree->gtSetFlags());
}
}
/*****************************************************************************
*
* Record the fact that the flags register has a value that reflects the
* contents of the given local variable.
*/
void CodeGen::genFlagsEqualToVar(GenTreePtr tree,
unsigned var)
{
genFlagsEqLoc.CaptureLocation(getEmitter());
genFlagsEqVar = var;
/* previous setting of flags by a register becomes invalid */
genFlagsEqReg = REG_NA;
/* Set appropriate flags on the tree */
if (tree)
{
tree->gtFlags |= GTF_ZSF_SET;
assert(tree->gtSetFlags());
}
}
/*****************************************************************************
*
* Return an indication of whether the flags register is set to the current
* value of the given register/variable. The return value is as follows:
*
* false .. nothing
* true .. the zero flag (ZF) and sign flag (SF) is set
*/
bool CodeGen::genFlagsAreReg(regNumber reg)
{
if ((genFlagsEqReg == reg) && genFlagsEqLoc.IsCurrentLocation(getEmitter()))
{
return true;
}
return false;
}
bool CodeGen::genFlagsAreVar(unsigned var)
{
if ((genFlagsEqVar == var) && genFlagsEqLoc.IsCurrentLocation(getEmitter()))
{
return true;
}
return false;
}
// TODO-Cleanup: Move this out of CodeGenCommon.cpp - we shouldn't need to use this
// in the new backend
/*****************************************************************************
* This utility function returns true iff the execution path from "from"
* (inclusive) to "to" (exclusive) contains a death of the given var
*/
bool
CodeGen::genContainsVarDeath(GenTreePtr from, GenTreePtr to, unsigned varNum)
{
GenTreePtr tree;
for (tree = from; tree != NULL && tree != to; tree = tree->gtNext)
{
if (tree->IsLocal() && (tree->gtFlags & GTF_VAR_DEATH))
{
unsigned dyingVarNum = tree->gtLclVarCommon.gtLclNum;
if (dyingVarNum == varNum) return true;
LclVarDsc * varDsc = &(compiler->lvaTable[varNum]);
if (varDsc->lvPromoted)
{
assert(varDsc->lvType == TYP_STRUCT);
unsigned firstFieldNum = varDsc->lvFieldLclStart;
if (varNum >= firstFieldNum && varNum < firstFieldNum + varDsc->lvFieldCnt)
{
return true;
}
}
}
}
assert(tree != NULL);
return false;
}
#endif // LEGACY_BACKEND
// The given lclVar is either going live (being born) or dying.
// It might be both going live and dying (that is, it is a dead store) under MinOpts.
// Update regSet.rsMaskVars accordingly.
// inline
void CodeGenInterface::genUpdateRegLife(const LclVarDsc * varDsc, bool isBorn, bool isDying
DEBUGARG(GenTreePtr tree))
{
#if FEATURE_STACK_FP_X87
// The stack fp reg vars are handled elsewhere
if (varTypeIsFloating(varDsc->TypeGet())) return;
#endif
regMaskTP regMask = genGetRegMask(varDsc);
#ifdef DEBUG
if (compiler->verbose)
{
printf("\t\t\t\t\t\t\tV%02u in reg ",
(varDsc - compiler->lvaTable));
varDsc->PrintVarReg();
printf(" is becoming %s ", (isDying) ? "dead" : "live");
Compiler::printTreeID(tree);
printf("\n");
}
#endif // DEBUG
if (isDying)
{
// We'd like to be able to assert the following, however if we are walking
// through a qmark/colon tree, we may encounter multiple last-use nodes.
// assert((regSet.rsMaskVars & regMask) == regMask);
regSet.rsMaskVars &= ~(regMask);
}
else
{
assert((regSet.rsMaskVars & regMask) == 0);
regSet.rsMaskVars |= regMask;
}
}
// Gets a register mask that represent the kill set for a helper call since
// not all JIT Helper calls follow the standard ABI on the target architecture.
//
// TODO-CQ: Currently this list is incomplete (not all helpers calls are
// enumerated) and not 100% accurate (some killsets are bigger than
// what they really are).
// There's some work to be done in several places in the JIT to
// accurately track the registers that are getting killed by
// helper calls:
// a) LSRA needs several changes to accomodate more precise killsets
// for every helper call it sees (both explicitly [easy] and
// implicitly [hard])
// b) Currently for AMD64, when we generate code for a helper call
// we're independently over-pessimizing the killsets of the call
// (independently from LSRA) and this needs changes
// both in CodeGenAmd64.cpp and emitx86.cpp.
//
// The best solution for this problem would be to try to centralize
// the killset information in a single place but then make the
// corresponding changes so every code generation phase is in sync
// about this.
//
// The interim solution is to only add known helper calls that don't
// follow the AMD64 ABI and actually trash registers that are supposed to be non-volatile.
regMaskTP Compiler::compHelperCallKillSet(CorInfoHelpFunc helper)
{
switch(helper)
{
case CORINFO_HELP_ASSIGN_BYREF:
#if defined(_TARGET_AMD64_)
return RBM_RSI|RBM_RDI|RBM_CALLEE_TRASH;
#elif defined(_TARGET_ARM64_)
return RBM_CALLEE_TRASH_NOGC;
#else
NYI("Model kill set for CORINFO_HELP_ASSIGN_BYREF on target arch");
return RBM_CALLEE_TRASH;
#endif
case CORINFO_HELP_PROF_FCN_ENTER:
#ifdef _TARGET_AMD64_
return RBM_PROFILER_ENTER_TRASH;
#else
unreached();
#endif
case CORINFO_HELP_PROF_FCN_LEAVE:
case CORINFO_HELP_PROF_FCN_TAILCALL:
#ifdef _TARGET_AMD64_
return RBM_PROFILER_LEAVE_TRASH;
#else
unreached();
#endif
case CORINFO_HELP_STOP_FOR_GC:
return RBM_STOP_FOR_GC_TRASH;
default:
return RBM_CALLEE_TRASH;
}
}
//
// Gets a register mask that represents the kill set for "NO GC" helper calls since
// not all JIT Helper calls follow the standard ABI on the target architecture.
//
// Note: This list may not be complete and defaults to the default NOGC registers.
//
regMaskTP Compiler::compNoGCHelperCallKillSet(CorInfoHelpFunc helper)
{
assert(emitter::emitNoGChelper(helper));
#ifdef _TARGET_AMD64_
switch (helper)
{
case CORINFO_HELP_PROF_FCN_ENTER:
return RBM_PROFILER_ENTER_TRASH;
case CORINFO_HELP_PROF_FCN_LEAVE:
case CORINFO_HELP_PROF_FCN_TAILCALL:
return RBM_PROFILER_LEAVE_TRASH;
default:
return RBM_CALLEE_TRASH_NOGC;
}
#else
return RBM_CALLEE_TRASH_NOGC;
#endif
}
// Update liveness (always var liveness, i.e., compCurLife, and also, if "ForCodeGen" is true, reg liveness, i.e., regSet.rsMaskVars as well)
// if the given lclVar (or indir(addr(local)))/regVar node is going live (being born) or dying.
template<bool ForCodeGen>
void Compiler::compUpdateLifeVar(GenTreePtr tree, VARSET_TP* pLastUseVars)
{
GenTreePtr indirAddrLocal = fgIsIndirOfAddrOfLocal(tree);
assert(tree->OperIsNonPhiLocal() || indirAddrLocal != NULL);
// Get the local var tree -- if "tree" is "Ldobj(addr(x))", or "ind(addr(x))" this is "x", else it's "tree".
GenTreePtr lclVarTree = indirAddrLocal;
if (lclVarTree == NULL) lclVarTree = tree;
unsigned int lclNum = lclVarTree->gtLclVarCommon.gtLclNum;
LclVarDsc * varDsc = lvaTable + lclNum;
// Struct fields are not traversed in a consistent order, so ignore them when
// verifying that we see the var nodes in execution order
#ifdef DEBUG
#if !defined(_TARGET_AMD64_) // no addr nodes on AMD and experimenting with with encountering vars in 'random' order
if (ForCodeGen)
{
if (tree->gtOper == GT_LDOBJ)
{
// The tree must have the particular form LDOBJ(ADDR(LCL)); no need to do the check below.
assert(indirAddrLocal != NULL);
}
else if (tree->OperIsIndir())
{
assert(indirAddrLocal != NULL);
}
else if (tree->gtNext != NULL
&& tree->gtNext->gtOper == GT_ADDR
&& ((tree->gtNext->gtNext == NULL || !tree->gtNext->gtNext->OperIsIndir())))
{
assert(tree->IsLocal()); // Can only take the address of a local.
// The ADDR might occur in a context where the address it contributes is eventually
// dereferenced, so we can't say that this is not a use or def.
}
#if 0
// TODO-ARM64-Bug?: These asserts don't seem right for ARM64: I don't understand why we have to assert
// two consecutive lclvars (in execution order) can only be observed if the first one is a struct field.
// It seems to me this is code only applicable to the legacy JIT and not RyuJIT (and therefore why it was
// ifdef'ed out for AMD64).
else if (!varDsc->lvIsStructField)
{
GenTreePtr prevTree;
for (prevTree = tree->gtPrev;
prevTree != NULL && prevTree != compCurLifeTree;
prevTree = prevTree->gtPrev)
{
if ((prevTree->gtOper == GT_LCL_VAR) || (prevTree->gtOper == GT_REG_VAR))
{
LclVarDsc * prevVarDsc = lvaTable + prevTree->gtLclVarCommon.gtLclNum;
// These are the only things for which this method MUST be called
assert(prevVarDsc->lvIsStructField);
}
}
assert(prevTree == compCurLifeTree);
}
#endif // 0
}
#endif // !_TARGET_AMD64_
#endif // DEBUG
compCurLifeTree = tree;
VARSET_TP VARSET_INIT(this, newLife, compCurLife);
// By codegen, a struct may not be TYP_STRUCT, so we have to
// check lvPromoted, for the case where the fields are being
// tracked.
if (!varDsc->lvTracked && !varDsc->lvPromoted)
return;
bool isBorn = ((tree->gtFlags & GTF_VAR_DEF) != 0 &&
(tree->gtFlags & GTF_VAR_USEASG) == 0); // if it's "x <op>= ..." then variable "x" must have had a previous, original, site to be born.
bool isDying = ((tree->gtFlags & GTF_VAR_DEATH) != 0);
#ifndef LEGACY_BACKEND
bool spill = ((tree->gtFlags & GTF_SPILL) != 0);
#endif // !LEGACY_BACKEND
#ifndef LEGACY_BACKEND
// For RyuJIT backend, since all tracked vars are register candidates, but not all are in registers at all times,
// we maintain two separate sets of variables - the total set of variables that are either
// born or dying here, and the subset of those that are on the stack
VARSET_TP VARSET_INIT_NOCOPY(stackVarDeltaSet, VarSetOps::MakeEmpty(this));
#endif // !LEGACY_BACKEND
if (isBorn || isDying)
{
bool hasDeadTrackedFieldVars = false; // If this is true, then, for a LDOBJ(ADDR(<promoted struct local>)),
VARSET_TP* deadTrackedFieldVars = NULL; // *deadTrackedFieldVars indicates which tracked field vars are dying.
VARSET_TP VARSET_INIT_NOCOPY(varDeltaSet, VarSetOps::MakeEmpty(this));
if (varDsc->lvTracked)
{
VarSetOps::AddElemD(this, varDeltaSet, varDsc->lvVarIndex);
if (ForCodeGen)
{
#ifndef LEGACY_BACKEND
if (isBorn && varDsc->lvIsRegCandidate() && tree->gtHasReg())
{
codeGen->genUpdateVarReg(varDsc, tree);
}
#endif // !LEGACY_BACKEND
if (varDsc->lvIsInReg()
#ifndef LEGACY_BACKEND
&& tree->gtRegNum != REG_NA
#endif // !LEGACY_BACKEND
)
{
codeGen->genUpdateRegLife(varDsc, isBorn, isDying DEBUGARG(tree));
}
#ifndef LEGACY_BACKEND
else
{
VarSetOps::AddElemD(this, stackVarDeltaSet, varDsc->lvVarIndex);
}
#endif // !LEGACY_BACKEND
}
}
else if (varDsc->lvPromoted)
{
if (indirAddrLocal != NULL && isDying)
{
assert(!isBorn); // GTF_VAR_DEATH only set for LDOBJ last use.
hasDeadTrackedFieldVars = GetPromotedStructDeathVars()->Lookup(indirAddrLocal, &deadTrackedFieldVars);
if (hasDeadTrackedFieldVars)
{
VarSetOps::Assign(this, varDeltaSet, *deadTrackedFieldVars);
}
}
for (unsigned i = varDsc->lvFieldLclStart;
i < varDsc->lvFieldLclStart + varDsc->lvFieldCnt;
++i)
{
LclVarDsc * fldVarDsc = &(lvaTable[i]);
noway_assert(fldVarDsc->lvIsStructField);
if (fldVarDsc->lvTracked)
{
unsigned fldVarIndex = fldVarDsc->lvVarIndex;
noway_assert(fldVarIndex < lvaTrackedCount);
if (!hasDeadTrackedFieldVars)
{
VarSetOps::AddElemD(this, varDeltaSet, fldVarIndex);
if (ForCodeGen)
{
// We repeat this call here and below to avoid the VarSetOps::IsMember
// test in this, the common case, where we have no deadTrackedFieldVars.
if (fldVarDsc->lvIsInReg())
{
#ifndef LEGACY_BACKEND
if (isBorn) codeGen->genUpdateVarReg(fldVarDsc, tree);
#endif // !LEGACY_BACKEND
codeGen->genUpdateRegLife(fldVarDsc, isBorn, isDying DEBUGARG(tree));
}
#ifndef LEGACY_BACKEND
else
{
VarSetOps::AddElemD(this, stackVarDeltaSet, fldVarIndex);
}
#endif // !LEGACY_BACKEND
}
}
else if (ForCodeGen && VarSetOps::IsMember(this, varDeltaSet, fldVarIndex))
{
if (lvaTable[i].lvIsInReg())
{
#ifndef LEGACY_BACKEND
if (isBorn) codeGen->genUpdateVarReg(fldVarDsc, tree);
#endif // !LEGACY_BACKEND
codeGen->genUpdateRegLife(fldVarDsc, isBorn, isDying DEBUGARG(tree));
}
#ifndef LEGACY_BACKEND
else
{
VarSetOps::AddElemD(this, stackVarDeltaSet, fldVarIndex);
}
#endif // !LEGACY_BACKEND
}
}
}
}
// First, update the live set