-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
lsrabuild.cpp
4423 lines (4068 loc) · 169 KB
/
lsrabuild.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 Interval and RefPosition Building XX
XX XX
XX This contains the logic for constructing Intervals and RefPositions that XX
XX is common across architectures. See lsra{arch}.cpp for the architecture- XX
XX specific methods for building. XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "lsra.h"
//------------------------------------------------------------------------
// RefInfoList
//------------------------------------------------------------------------
// removeListNode - retrieve the RefInfoListNode for the given GenTree node
//
// Notes:
// The BuildNode methods use this helper to retrieve the RefPositions for child nodes
// from the useList being constructed. Note that, if the user knows the order of the operands,
// it is expected that they should just retrieve them directly.
//
RefInfoListNode* RefInfoList::removeListNode(GenTree* node)
{
RefInfoListNode* prevListNode = nullptr;
for (RefInfoListNode *listNode = Begin(), *end = End(); listNode != end; listNode = listNode->Next())
{
if (listNode->treeNode == node)
{
assert(listNode->ref->getMultiRegIdx() == 0);
return removeListNode(listNode, prevListNode);
}
prevListNode = listNode;
}
assert(!"removeListNode didn't find the node");
unreached();
}
//------------------------------------------------------------------------
// removeListNode - retrieve the RefInfoListNode for one reg of the given multireg GenTree node
//
// Notes:
// The BuildNode methods use this helper to retrieve the RefPositions for child nodes
// from the useList being constructed. Note that, if the user knows the order of the operands,
// it is expected that they should just retrieve them directly.
//
RefInfoListNode* RefInfoList::removeListNode(GenTree* node, unsigned multiRegIdx)
{
RefInfoListNode* prevListNode = nullptr;
for (RefInfoListNode *listNode = Begin(), *end = End(); listNode != end; listNode = listNode->Next())
{
if ((listNode->treeNode == node) && (listNode->ref->getMultiRegIdx() == multiRegIdx))
{
return removeListNode(listNode, prevListNode);
}
prevListNode = listNode;
}
assert(!"removeListNode didn't find the node");
unreached();
}
//------------------------------------------------------------------------
// RefInfoListNodePool::RefInfoListNodePool:
// Creates a pool of `RefInfoListNode` values.
//
// Arguments:
// compiler - The compiler context.
// preallocate - The number of nodes to preallocate.
//
RefInfoListNodePool::RefInfoListNodePool(Compiler* compiler, unsigned preallocate)
: m_compiler(compiler)
{
if (preallocate > 0)
{
RefInfoListNode* preallocatedNodes = compiler->getAllocator(CMK_LSRA).allocate<RefInfoListNode>(preallocate);
RefInfoListNode* head = preallocatedNodes;
head->m_next = nullptr;
for (unsigned i = 1; i < preallocate; i++)
{
RefInfoListNode* node = &preallocatedNodes[i];
node->m_next = head;
head = node;
}
m_freeList = head;
}
}
//------------------------------------------------------------------------
// RefInfoListNodePool::GetNode: Fetches an unused node from the
// pool.
//
// Arguments:
// r - The `RefPosition` for the `RefInfo` value.
// t - The IR node for the `RefInfo` value
//
// Returns:
// A pooled or newly-allocated `RefInfoListNode`, depending on the
// contents of the pool.
RefInfoListNode* RefInfoListNodePool::GetNode(RefPosition* r, GenTree* t)
{
RefInfoListNode* head = m_freeList;
if (head == nullptr)
{
head = m_compiler->getAllocator(CMK_LSRA).allocate<RefInfoListNode>(1);
}
else
{
m_freeList = head->m_next;
}
head->ref = r;
head->treeNode = t;
head->m_next = nullptr;
return head;
}
//------------------------------------------------------------------------
// RefInfoListNodePool::ReturnNode: Returns a list of nodes to the node
// pool and clears the given list.
//
// Arguments:
// list - The list to return.
//
void RefInfoListNodePool::ReturnNode(RefInfoListNode* listNode)
{
listNode->m_next = m_freeList;
m_freeList = listNode;
}
//------------------------------------------------------------------------
// newInterval: Create a new Interval of the given RegisterType.
//
// Arguments:
// theRegisterType - The type of Interval to create.
//
// TODO-Cleanup: Consider adding an overload that takes a varDsc, and can appropriately
// set such fields as isStructField
//
Interval* LinearScan::newInterval(RegisterType theRegisterType)
{
intervals.emplace_back(theRegisterType, allRegs(theRegisterType));
Interval* newInt = &intervals.back();
#ifdef DEBUG
newInt->intervalIndex = static_cast<unsigned>(intervals.size() - 1);
#endif // DEBUG
DBEXEC(VERBOSE, newInt->dump(this->compiler));
return newInt;
}
//------------------------------------------------------------------------
// newRefPositionRaw: Create a new RefPosition
//
// Arguments:
// nodeLocation - The location of the reference.
// treeNode - The GenTree of the reference.
// refType - The type of reference
//
// Notes:
// This is used to create RefPositions for both RegRecords and Intervals,
// so it does only the common initialization.
//
RefPosition* LinearScan::newRefPositionRaw(LsraLocation nodeLocation, GenTree* treeNode, RefType refType)
{
refPositions.emplace_back(curBBNum, nodeLocation, treeNode, refType DEBUG_ARG(currBuildNode));
RefPosition* newRP = &refPositions.back();
#ifdef DEBUG
// Reset currBuildNode so we do not set it for subsequent refpositions belonging
// to the same treeNode and hence, avoid printing it for every refposition inside
// the allocation table.
currBuildNode = nullptr;
newRP->rpNum = static_cast<unsigned>(refPositions.size() - 1);
if (!enregisterLocalVars)
{
assert(!((refType == RefTypeParamDef) || (refType == RefTypeZeroInit) || (refType == RefTypeDummyDef) ||
(refType == RefTypeExpUse)));
}
#endif // DEBUG
return newRP;
}
//------------------------------------------------------------------------
// resolveConflictingDefAndUse: Resolve the situation where we have conflicting def and use
// register requirements on a single-def, single-use interval.
//
// Arguments:
// defRefPosition - The interval definition
// useRefPosition - The (sole) interval use
//
// Return Value:
// None.
//
// Assumptions:
// The two RefPositions are for the same interval, which is a tree-temp.
//
// Notes:
// We require some special handling for the case where the use is a "delayRegFree" case of a fixedReg.
// In that case, if we change the registerAssignment on the useRefPosition, we will lose the fact that,
// even if we assign a different register (and rely on codegen to do the copy), that fixedReg also needs
// to remain busy until the Def register has been allocated. In that case, we don't allow Case 1 or Case 4
// below.
// Here are the cases we consider (in this order):
// 1. If The defRefPosition specifies a single register, and there are no conflicting
// FixedReg uses of it between the def and use, we use that register, and the code generator
// will insert the copy. Note that it cannot be in use because there is a FixedRegRef for the def.
// 2. If the useRefPosition specifies a single register, and it is not in use, and there are no
// conflicting FixedReg uses of it between the def and use, we use that register, and the code generator
// will insert the copy.
// 3. If the defRefPosition specifies a single register (but there are conflicts, as determined
// in 1.), and there are no conflicts with the useRefPosition register (if it's a single register),
/// we set the register requirements on the defRefPosition to the use registers, and the
// code generator will insert a copy on the def. We can't rely on the code generator to put a copy
// on the use if it has multiple possible candidates, as it won't know which one has been allocated.
// 4. If the useRefPosition specifies a single register, and there are no conflicts with the register
// on the defRefPosition, we leave the register requirements on the defRefPosition as-is, and set
// the useRefPosition to the def registers, for similar reasons to case #3.
// 5. If both the defRefPosition and the useRefPosition specify single registers, but both have conflicts,
// We set the candidates on defRefPosition to be all regs of the appropriate type, and since they are
// single registers, codegen can insert the copy.
// 6. Finally, if the RefPositions specify disjoint subsets of the registers (or the use is fixed but
// has a conflict), we must insert a copy. The copy will be inserted before the use if the
// use is not fixed (in the fixed case, the code generator will insert the use).
//
// TODO-CQ: We get bad register allocation in case #3 in the situation where no register is
// available for the lifetime. We end up allocating a register that must be spilled, and it probably
// won't be the register that is actually defined by the target instruction. So, we have to copy it
// and THEN spill it. In this case, we should be using the def requirement. But we need to change
// the interface to this method a bit to make that work (e.g. returning a candidate set to use, but
// leaving the registerAssignment as-is on the def, so that if we find that we need to spill anyway
// we can use the fixed-reg on the def.
//
void LinearScan::resolveConflictingDefAndUse(Interval* interval, RefPosition* defRefPosition)
{
assert(!interval->isLocalVar);
RefPosition* useRefPosition = defRefPosition->nextRefPosition;
regMaskTP defRegAssignment = defRefPosition->registerAssignment;
regMaskTP useRegAssignment = useRefPosition->registerAssignment;
RegRecord* defRegRecord = nullptr;
RegRecord* useRegRecord = nullptr;
regNumber defReg = REG_NA;
regNumber useReg = REG_NA;
bool defRegConflict = ((defRegAssignment & useRegAssignment) == RBM_NONE);
bool useRegConflict = defRegConflict;
// If the useRefPosition is a "delayRegFree", we can't change the registerAssignment
// on it, or we will fail to ensure that the fixedReg is busy at the time the target
// (of the node that uses this interval) is allocated.
bool canChangeUseAssignment = !useRefPosition->isFixedRegRef || !useRefPosition->delayRegFree;
INDEBUG(dumpLsraAllocationEvent(LSRA_EVENT_DEFUSE_CONFLICT));
if (!canChangeUseAssignment)
{
INDEBUG(dumpLsraAllocationEvent(LSRA_EVENT_DEFUSE_FIXED_DELAY_USE));
}
if (defRefPosition->isFixedRegRef && !defRegConflict)
{
defReg = defRefPosition->assignedReg();
defRegRecord = getRegisterRecord(defReg);
if (canChangeUseAssignment)
{
RefPosition* currFixedRegRefPosition = defRegRecord->recentRefPosition;
assert(currFixedRegRefPosition != nullptr &&
currFixedRegRefPosition->nodeLocation == defRefPosition->nodeLocation);
if (currFixedRegRefPosition->nextRefPosition == nullptr ||
currFixedRegRefPosition->nextRefPosition->nodeLocation > useRefPosition->getRefEndLocation())
{
// This is case #1. Use the defRegAssignment
INDEBUG(dumpLsraAllocationEvent(LSRA_EVENT_DEFUSE_CASE1));
useRefPosition->registerAssignment = defRegAssignment;
return;
}
else
{
defRegConflict = true;
}
}
}
if (useRefPosition->isFixedRegRef && !useRegConflict)
{
useReg = useRefPosition->assignedReg();
useRegRecord = getRegisterRecord(useReg);
// We know that useRefPosition is a fixed use, so the nextRefPosition must not be null.
RefPosition* nextFixedRegRefPosition = useRegRecord->getNextRefPosition();
assert(nextFixedRegRefPosition != nullptr &&
nextFixedRegRefPosition->nodeLocation <= useRefPosition->nodeLocation);
// First, check to see if there are any conflicting FixedReg references between the def and use.
if (nextFixedRegRefPosition->nodeLocation == useRefPosition->nodeLocation)
{
// OK, no conflicting FixedReg references.
// Now, check to see whether it is currently in use.
if (useRegRecord->assignedInterval != nullptr)
{
RefPosition* possiblyConflictingRef = useRegRecord->assignedInterval->recentRefPosition;
LsraLocation possiblyConflictingRefLocation = possiblyConflictingRef->getRefEndLocation();
if (possiblyConflictingRefLocation >= defRefPosition->nodeLocation)
{
useRegConflict = true;
}
}
if (!useRegConflict)
{
// This is case #2. Use the useRegAssignment
INDEBUG(dumpLsraAllocationEvent(LSRA_EVENT_DEFUSE_CASE2, interval));
defRefPosition->registerAssignment = useRegAssignment;
return;
}
}
else
{
useRegConflict = true;
}
}
if (defRegRecord != nullptr && !useRegConflict)
{
// This is case #3.
INDEBUG(dumpLsraAllocationEvent(LSRA_EVENT_DEFUSE_CASE3, interval));
defRefPosition->registerAssignment = useRegAssignment;
return;
}
if (useRegRecord != nullptr && !defRegConflict && canChangeUseAssignment)
{
// This is case #4.
INDEBUG(dumpLsraAllocationEvent(LSRA_EVENT_DEFUSE_CASE4, interval));
useRefPosition->registerAssignment = defRegAssignment;
return;
}
if (defRegRecord != nullptr && useRegRecord != nullptr)
{
// This is case #5.
INDEBUG(dumpLsraAllocationEvent(LSRA_EVENT_DEFUSE_CASE5, interval));
RegisterType regType = interval->registerType;
assert((getRegisterType(interval, defRefPosition) == regType) &&
(getRegisterType(interval, useRefPosition) == regType));
regMaskTP candidates = allRegs(regType);
defRefPosition->registerAssignment = candidates;
defRefPosition->isFixedRegRef = false;
return;
}
INDEBUG(dumpLsraAllocationEvent(LSRA_EVENT_DEFUSE_CASE6, interval));
return;
}
//------------------------------------------------------------------------
// applyCalleeSaveHeuristics: Set register preferences for an interval based on the given RefPosition
//
// Arguments:
// rp - The RefPosition of interest
//
// Notes:
// This is slightly more general than its name applies, and updates preferences not just
// for callee-save registers.
//
void LinearScan::applyCalleeSaveHeuristics(RefPosition* rp)
{
#ifdef TARGET_AMD64
if (compiler->opts.compDbgEnC)
{
// We only use RSI and RDI for EnC code, so we don't want to favor callee-save regs.
return;
}
#endif // TARGET_AMD64
Interval* theInterval = rp->getInterval();
#ifdef DEBUG
if (!doReverseCallerCallee())
#endif // DEBUG
{
// Set preferences so that this register set will be preferred for earlier refs
theInterval->mergeRegisterPreferences(rp->registerAssignment);
}
}
//------------------------------------------------------------------------
// checkConflictingDefUse: Ensure that we have consistent def/use on SDSU temps.
//
// Arguments:
// useRP - The use RefPosition of a tree temp (SDSU Interval)
//
// Notes:
// There are a couple of cases where this may over-constrain allocation:
// 1. In the case of a non-commutative rmw def (in which the rmw source must be delay-free), or
// 2. In the case where the defining node requires a temp distinct from the target (also a
// delay-free case).
// In those cases, if we propagate a single-register restriction from the consumer to the producer
// the delayed uses will not see a fixed reference in the PhysReg at that position, and may
// incorrectly allocate that register.
// TODO-CQ: This means that we may often require a copy at the use of this node's result.
// This case could be moved to BuildRefPositionsForNode, at the point where the def RefPosition is
// created, causing a RefTypeFixedReg to be added at that location. This, however, results in
// more PhysReg RefPositions (a throughput impact), and a large number of diffs that require
// further analysis to determine benefit.
// See Issue #11274.
//
void LinearScan::checkConflictingDefUse(RefPosition* useRP)
{
assert(useRP->refType == RefTypeUse);
Interval* theInterval = useRP->getInterval();
assert(!theInterval->isLocalVar);
RefPosition* defRP = theInterval->firstRefPosition;
// All defs must have a valid treeNode, but we check it below to be conservative.
assert(defRP->treeNode != nullptr);
regMaskTP prevAssignment = defRP->registerAssignment;
regMaskTP newAssignment = (prevAssignment & useRP->registerAssignment);
if (newAssignment != RBM_NONE)
{
if (!isSingleRegister(newAssignment) || !theInterval->hasInterferingUses)
{
defRP->registerAssignment = newAssignment;
}
}
else
{
theInterval->hasConflictingDefUse = true;
}
}
//------------------------------------------------------------------------
// associateRefPosWithInterval: Update the Interval based on the given RefPosition.
//
// Arguments:
// rp - The RefPosition of interest
//
// Notes:
// This is called at the time when 'rp' has just been created, so it becomes
// the nextRefPosition of the recentRefPosition, and both the recentRefPosition
// and lastRefPosition of its referent.
//
void LinearScan::associateRefPosWithInterval(RefPosition* rp)
{
Referenceable* theReferent = rp->referent;
if (theReferent != nullptr)
{
// All RefPositions except the dummy ones at the beginning of blocks
if (rp->isIntervalRef())
{
Interval* theInterval = rp->getInterval();
applyCalleeSaveHeuristics(rp);
if (theInterval->isLocalVar)
{
if (RefTypeIsUse(rp->refType))
{
RefPosition* const prevRP = theInterval->recentRefPosition;
if ((prevRP != nullptr) && (prevRP->bbNum == rp->bbNum))
{
prevRP->lastUse = false;
}
}
rp->lastUse = (rp->refType != RefTypeExpUse) && (rp->refType != RefTypeParamDef) &&
(rp->refType != RefTypeZeroInit) && !extendLifetimes();
}
else if (rp->refType == RefTypeUse)
{
checkConflictingDefUse(rp);
rp->lastUse = true;
}
}
RefPosition* prevRP = theReferent->recentRefPosition;
if (prevRP != nullptr)
{
prevRP->nextRefPosition = rp;
}
else
{
theReferent->firstRefPosition = rp;
}
theReferent->recentRefPosition = rp;
theReferent->lastRefPosition = rp;
}
else
{
assert((rp->refType == RefTypeBB) || (rp->refType == RefTypeKillGCRefs));
}
}
//---------------------------------------------------------------------------
// newRefPosition: allocate and initialize a new RefPosition.
//
// Arguments:
// reg - reg number that identifies RegRecord to be associated
// with this RefPosition
// theLocation - LSRA location of RefPosition
// theRefType - RefPosition type
// theTreeNode - GenTree node for which this RefPosition is created
// mask - Set of valid registers for this RefPosition
// multiRegIdx - register position if this RefPosition corresponds to a
// multi-reg call node.
//
// Return Value:
// a new RefPosition
//
RefPosition* LinearScan::newRefPosition(
regNumber reg, LsraLocation theLocation, RefType theRefType, GenTree* theTreeNode, regMaskTP mask)
{
RefPosition* newRP = newRefPositionRaw(theLocation, theTreeNode, theRefType);
RegRecord* regRecord = getRegisterRecord(reg);
newRP->setReg(regRecord);
newRP->registerAssignment = mask;
newRP->setMultiRegIdx(0);
newRP->setRegOptional(false);
// We can't have two RefPositions on a RegRecord at the same location, unless they are different types.
assert((regRecord->lastRefPosition == nullptr) || (regRecord->lastRefPosition->nodeLocation < theLocation) ||
(regRecord->lastRefPosition->refType != theRefType));
associateRefPosWithInterval(newRP);
DBEXEC(VERBOSE, newRP->dump(this));
return newRP;
}
//---------------------------------------------------------------------------
// newRefPosition: allocate and initialize a new RefPosition.
//
// Arguments:
// theInterval - interval to which RefPosition is associated with.
// theLocation - LSRA location of RefPosition
// theRefType - RefPosition type
// theTreeNode - GenTree node for which this RefPosition is created
// mask - Set of valid registers for this RefPosition
// multiRegIdx - register position if this RefPosition corresponds to a
// multi-reg call node.
//
// Return Value:
// a new RefPosition
//
RefPosition* LinearScan::newRefPosition(Interval* theInterval,
LsraLocation theLocation,
RefType theRefType,
GenTree* theTreeNode,
regMaskTP mask,
unsigned multiRegIdx /* = 0 */)
{
if (theInterval != nullptr)
{
if (mask == RBM_NONE)
{
mask = allRegs(theInterval->registerType);
}
}
else
{
assert(theRefType == RefTypeBB || theRefType == RefTypeKillGCRefs);
}
#ifdef DEBUG
if (theInterval != nullptr && regType(theInterval->registerType) == FloatRegisterType)
{
// In the case we're using floating point registers we must make sure
// this flag was set previously in the compiler since this will mandate
// whether LSRA will take into consideration FP reg killsets.
assert(compiler->compFloatingPointUsed || ((mask & RBM_FLT_CALLEE_SAVED) == 0));
}
#endif // DEBUG
// If this reference is constrained to a single register (and it's not a dummy
// or Kill reftype already), add a RefTypeFixedReg at this location so that its
// availability can be more accurately determined
bool isFixedRegister = isSingleRegister(mask);
bool insertFixedRef = false;
if (isFixedRegister)
{
// Insert a RefTypeFixedReg for any normal def or use (not ParamDef or BB),
// but not an internal use (it will already have a FixedRef for the def).
if ((theRefType == RefTypeDef) || ((theRefType == RefTypeUse) && !theInterval->isInternal))
{
insertFixedRef = true;
}
}
if (insertFixedRef)
{
regNumber physicalReg = genRegNumFromMask(mask);
RefPosition* pos = newRefPosition(physicalReg, theLocation, RefTypeFixedReg, nullptr, mask);
assert(theInterval != nullptr);
#if defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
// The LoongArch64's ABI which the float args maybe passed by integer register
// when no float register left but free integer register.
assert((regType(theInterval->registerType) == FloatRegisterType) ||
(allRegs(theInterval->registerType) & mask) != 0);
#else
assert((allRegs(theInterval->registerType) & mask) != 0);
#endif
}
RefPosition* newRP = newRefPositionRaw(theLocation, theTreeNode, theRefType);
newRP->setInterval(theInterval);
// Spill info
newRP->isFixedRegRef = isFixedRegister;
#ifndef TARGET_AMD64
// We don't need this for AMD because the PInvoke method epilog code is explicit
// at register allocation time.
if (theInterval != nullptr && theInterval->isLocalVar && compiler->compMethodRequiresPInvokeFrame() &&
theInterval->varNum == compiler->genReturnLocal)
{
mask &= ~(RBM_PINVOKE_TCB | RBM_PINVOKE_FRAME);
noway_assert(mask != RBM_NONE);
}
#endif // !TARGET_AMD64
newRP->registerAssignment = mask;
newRP->setMultiRegIdx(multiRegIdx);
newRP->setRegOptional(false);
#if FEATURE_PARTIAL_SIMD_CALLEE_SAVE
newRP->skipSaveRestore = false;
newRP->liveVarUpperSave = false;
#endif
associateRefPosWithInterval(newRP);
if (RefTypeIsDef(newRP->refType))
{
assert(theInterval != nullptr);
theInterval->isSingleDef = theInterval->firstRefPosition == newRP;
}
DBEXEC(VERBOSE, newRP->dump(this));
return newRP;
}
//------------------------------------------------------------------------
// IsContainableMemoryOp: Checks whether this is a memory op that can be contained.
//
// Arguments:
// node - the node of interest.
//
// Return value:
// True if this will definitely be a memory reference that could be contained.
//
// Notes:
// This differs from the isMemoryOp() method on GenTree because it checks for
// the case of doNotEnregister local. This won't include locals that
// for some other reason do not become register candidates, nor those that get
// spilled.
// Also, because we usually call this before we redo dataflow, any new lclVars
// introduced after the last dataflow analysis will not yet be marked lvTracked,
// so we don't use that.
//
bool LinearScan::isContainableMemoryOp(GenTree* node)
{
if (node->isMemoryOp())
{
return true;
}
if (node->IsLocal())
{
if (!enregisterLocalVars)
{
return true;
}
const LclVarDsc* varDsc = compiler->lvaGetDesc(node->AsLclVar());
return varDsc->lvDoNotEnregister;
}
return false;
}
//------------------------------------------------------------------------
// addRefsForPhysRegMask: Adds RefPositions of the given type for all the registers in 'mask'.
//
// Arguments:
// mask - the mask (set) of registers.
// currentLoc - the location at which they should be added
// refType - the type of refposition
// isLastUse - true IFF this is a last use of the register
//
void LinearScan::addRefsForPhysRegMask(regMaskTP mask, LsraLocation currentLoc, RefType refType, bool isLastUse)
{
assert(refType == RefTypeKill);
// The mask identifies a set of registers that will be used during
// codegen. Mark these as modified here, so when we do final frame
// layout, we'll know about all these registers. This is especially
// important if mask contains callee-saved registers, which affect the
// frame size since we need to save/restore them. In the case where we
// have a copyBlk with GC pointers, can need to call the
// CORINFO_HELP_ASSIGN_BYREF helper, which kills callee-saved RSI and
// RDI, if LSRA doesn't assign RSI/RDI, they wouldn't get marked as
// modified until codegen, which is too late.
compiler->codeGen->regSet.rsSetRegsModified(mask DEBUGARG(true));
for (regMaskTP candidates = mask; candidates != RBM_NONE;)
{
regNumber reg = genFirstRegNumFromMaskAndToggle(candidates);
// This assumes that these are all "special" RefTypes that
// don't need to be recorded on the tree (hence treeNode is nullptr)
RefPosition* pos = newRefPosition(reg, currentLoc, refType, nullptr,
genRegMask(reg)); // This MUST occupy the physical register (obviously)
if (isLastUse)
{
pos->lastUse = true;
}
}
}
//------------------------------------------------------------------------
// getKillSetForStoreInd: Determine the liveness kill set for a GT_STOREIND node.
// If the GT_STOREIND will generate a write barrier, determine the specific kill
// set required by the case-specific, platform-specific write barrier. If no
// write barrier is required, the kill set will be RBM_NONE.
//
// Arguments:
// tree - the GT_STOREIND node
//
// Return Value: a register mask of the registers killed
//
regMaskTP LinearScan::getKillSetForStoreInd(GenTreeStoreInd* tree)
{
assert(tree->OperIs(GT_STOREIND));
regMaskTP killMask = RBM_NONE;
GCInfo::WriteBarrierForm writeBarrierForm = compiler->codeGen->gcInfo.gcIsWriteBarrierCandidate(tree);
if (writeBarrierForm != GCInfo::WBF_NoBarrier)
{
if (compiler->codeGen->genUseOptimizedWriteBarriers(writeBarrierForm))
{
// We can't determine the exact helper to be used at this point, because it depends on
// the allocated register for the `data` operand. However, all the (x86) optimized
// helpers have the same kill set: EDX. And note that currently, only x86 can return
// `true` for genUseOptimizedWriteBarriers().
killMask = RBM_CALLEE_TRASH_NOGC;
}
else
{
// Figure out which helper we're going to use, and then get the kill set for that helper.
CorInfoHelpFunc helper = compiler->codeGen->genWriteBarrierHelperForWriteBarrierForm(writeBarrierForm);
killMask = compiler->compHelperCallKillSet(helper);
}
}
return killMask;
}
//------------------------------------------------------------------------
// getKillSetForShiftRotate: Determine the liveness kill set for a shift or rotate node.
//
// Arguments:
// shiftNode - the shift or rotate node
//
// Return Value: a register mask of the registers killed
//
regMaskTP LinearScan::getKillSetForShiftRotate(GenTreeOp* shiftNode)
{
regMaskTP killMask = RBM_NONE;
#ifdef TARGET_XARCH
assert(shiftNode->OperIsShiftOrRotate());
GenTree* shiftBy = shiftNode->gtGetOp2();
if (!shiftBy->isContained())
{
killMask = RBM_RCX;
}
#endif // TARGET_XARCH
return killMask;
}
//------------------------------------------------------------------------
// getKillSetForMul: Determine the liveness kill set for a multiply node.
//
// Arguments:
// tree - the multiply node
//
// Return Value: a register mask of the registers killed
//
regMaskTP LinearScan::getKillSetForMul(GenTreeOp* mulNode)
{
regMaskTP killMask = RBM_NONE;
#ifdef TARGET_XARCH
assert(mulNode->OperIsMul());
if (!mulNode->OperIs(GT_MUL) || (((mulNode->gtFlags & GTF_UNSIGNED) != 0) && mulNode->gtOverflowEx()))
{
killMask = RBM_RAX | RBM_RDX;
}
#endif // TARGET_XARCH
return killMask;
}
//------------------------------------------------------------------------
// getKillSetForModDiv: Determine the liveness kill set for a mod or div node.
//
// Arguments:
// tree - the mod or div node as a GenTreeOp
//
// Return Value: a register mask of the registers killed
//
regMaskTP LinearScan::getKillSetForModDiv(GenTreeOp* node)
{
regMaskTP killMask = RBM_NONE;
#ifdef TARGET_XARCH
assert(node->OperIs(GT_MOD, GT_DIV, GT_UMOD, GT_UDIV));
if (varTypeUsesIntReg(node->TypeGet()))
{
// Both RAX and RDX are killed by the operation
killMask = RBM_RAX | RBM_RDX;
}
#endif // TARGET_XARCH
return killMask;
}
//------------------------------------------------------------------------
// getKillSetForCall: Determine the liveness kill set for a call node.
//
// Arguments:
// tree - the GenTreeCall node
//
// Return Value: a register mask of the registers killed
//
regMaskTP LinearScan::getKillSetForCall(GenTreeCall* call)
{
regMaskTP killMask = RBM_CALLEE_TRASH;
#ifdef TARGET_X86
if (compiler->compFloatingPointUsed)
{
if (call->TypeGet() == TYP_DOUBLE)
{
needDoubleTmpForFPCall = true;
}
else if (call->TypeGet() == TYP_FLOAT)
{
needFloatTmpForFPCall = true;
}
}
#endif // TARGET_X86
if (call->IsHelperCall())
{
CorInfoHelpFunc helpFunc = compiler->eeGetHelperNum(call->gtCallMethHnd);
killMask = compiler->compHelperCallKillSet(helpFunc);
}
// if there is no FP used, we can ignore the FP kills
if (!compiler->compFloatingPointUsed)
{
#if defined(TARGET_XARCH)
killMask &= ~(RBM_FLT_CALLEE_TRASH | RBM_MSK_CALLEE_TRASH);
#else
killMask &= ~RBM_FLT_CALLEE_TRASH;
#endif // TARGET_XARCH
}
#ifdef TARGET_ARM
if (call->IsVirtualStub())
{
killMask |= compiler->virtualStubParamInfo->GetRegMask();
}
#else // !TARGET_ARM
// Verify that the special virtual stub call registers are in the kill mask.
// We don't just add them unconditionally to the killMask because for most architectures
// they are already in the RBM_CALLEE_TRASH set,
// and we don't want to introduce extra checks and calls in this hot function.
assert(!call->IsVirtualStub() ||
((killMask & compiler->virtualStubParamInfo->GetRegMask()) == compiler->virtualStubParamInfo->GetRegMask()));
#endif // !TARGET_ARM
#ifdef SWIFT_SUPPORT
// Swift calls that throw may trash the callee-saved error register,
// so don't use the register post-call until it is consumed by SwiftError.
if (call->HasSwiftErrorHandling())
{
killMask |= RBM_SWIFT_ERROR;
}
#endif // SWIFT_SUPPORT
return killMask;
}
//------------------------------------------------------------------------
// getKillSetForBlockStore: Determine the liveness kill set for a block store node.
//
// Arguments:
// tree - the block store node as a GenTreeBlk
//
// Return Value: a register mask of the registers killed
//
regMaskTP LinearScan::getKillSetForBlockStore(GenTreeBlk* blkNode)
{
assert(blkNode->OperIsStoreBlk());
regMaskTP killMask = RBM_NONE;
bool isCopyBlk = varTypeIsStruct(blkNode->Data());
switch (blkNode->gtBlkOpKind)
{
case GenTreeBlk::BlkOpKindCpObjUnroll:
#ifdef TARGET_XARCH
case GenTreeBlk::BlkOpKindCpObjRepInstr:
#endif // TARGET_XARCH
assert(isCopyBlk && blkNode->AsBlk()->GetLayout()->HasGCPtr());
killMask = compiler->compHelperCallKillSet(CORINFO_HELP_ASSIGN_BYREF);
break;
#ifdef TARGET_XARCH
case GenTreeBlk::BlkOpKindRepInstr:
if (isCopyBlk)
{
// rep movs kills RCX, RDI and RSI
killMask = RBM_RCX | RBM_RDI | RBM_RSI;
}
else
{
// rep stos kills RCX and RDI.
// (Note that the Data() node, if not constant, will be assigned to
// RCX, but it's find that this kills it, as the value is not available
// after this node in any case.)
killMask = RBM_RDI | RBM_RCX;
}
break;
#endif
case GenTreeBlk::BlkOpKindUnrollMemmove:
case GenTreeBlk::BlkOpKindUnroll:
case GenTreeBlk::BlkOpKindLoop:
case GenTreeBlk::BlkOpKindInvalid:
// for these 'gtBlkOpKind' kinds, we leave 'killMask' = RBM_NONE
break;
}
return killMask;
}
#ifdef FEATURE_HW_INTRINSICS
//------------------------------------------------------------------------
// getKillSetForHWIntrinsic: Determine the liveness kill set for a GT_STOREIND node.
// If the GT_STOREIND will generate a write barrier, determine the specific kill
// set required by the case-specific, platform-specific write barrier. If no
// write barrier is required, the kill set will be RBM_NONE.
//
// Arguments:
// tree - the GT_STOREIND node
//
// Return Value: a register mask of the registers killed
//
regMaskTP LinearScan::getKillSetForHWIntrinsic(GenTreeHWIntrinsic* node)
{
regMaskTP killMask = RBM_NONE;
#ifdef TARGET_XARCH
switch (node->GetHWIntrinsicId())
{
case NI_SSE2_MaskMove:
// maskmovdqu uses edi as the implicit address register.
// Although it is set as the srcCandidate on the address, if there is also a fixed
// assignment for the definition of the address, resolveConflictingDefAndUse() may
// change the register assignment on the def or use of a tree temp (SDSU) when there
// is a conflict, and the FixedRef on edi won't be sufficient to ensure that another
// Interval will not be allocated there.
// Issue #17674 tracks this.
killMask = RBM_EDI;
break;
default:
// Leave killMask as RBM_NONE
break;
}
#endif // TARGET_XARCH
return killMask;
}
#endif // FEATURE_HW_INTRINSICS
//------------------------------------------------------------------------
// getKillSetForReturn: Determine the liveness kill set for a return node.
//
// Arguments:
// NONE (this kill set is independent of the details of the specific return.)
//
// Return Value: a register mask of the registers killed
//
regMaskTP LinearScan::getKillSetForReturn()
{
return compiler->compIsProfilerHookNeeded() ? compiler->compHelperCallKillSet(CORINFO_HELP_PROF_FCN_LEAVE)
: RBM_NONE;
}