-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
lir.cpp
1896 lines (1678 loc) · 59.7 KB
/
lir.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.
#include "jitpch.h"
#include "smallhash.h"
#include "sideeffects.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
LIR::Use::Use()
: m_range(nullptr)
, m_edge(nullptr)
, m_user(nullptr)
{
}
LIR::Use::Use(const Use& other)
{
*this = other;
}
//------------------------------------------------------------------------
// LIR::Use::Use: Constructs a use <-> def edge given the range that
// contains the use and the def, the use -> def edge, and
// the user.
//
// Arguments:
// range - The range that contains the use and the def.
// edge - The use -> def edge.
// user - The node that uses the def.
//
// Return Value:
//
LIR::Use::Use(Range& range, GenTree** edge, GenTree* user)
: m_range(&range)
, m_edge(edge)
, m_user(user)
{
AssertIsValid();
}
LIR::Use& LIR::Use::operator=(const Use& other)
{
m_range = other.m_range;
m_user = other.m_user;
m_edge = other.IsDummyUse() ? &m_user : other.m_edge;
assert(IsDummyUse() == other.IsDummyUse());
return *this;
}
LIR::Use& LIR::Use::operator=(Use&& other)
{
*this = other;
return *this;
}
//------------------------------------------------------------------------
// LIR::Use::MakeDummyUse: Make a use into a dummy use.
//
// This method is provided as a convenience to allow transforms to work
// uniformly over Use values. It allows the creation of a Use given a node
// that is not used.
//
// Arguments:
// range - The range that contains the node.
// node - The node for which to create a dummy use.
// dummyUse - [out] the resulting dummy use
//
void LIR::Use::MakeDummyUse(Range& range, GenTree* node, LIR::Use* dummyUse)
{
assert(node != nullptr);
dummyUse->m_range = ⦥
dummyUse->m_user = node;
dummyUse->m_edge = &dummyUse->m_user;
assert(dummyUse->IsInitialized());
}
//------------------------------------------------------------------------
// LIR::Use::IsDummyUse: Indicates whether or not a use is a dummy use.
//
// This method must be called before attempting to call the User() method
// below: for dummy uses, the user is the same node as the def.
//
// Return Value: true if this use is a dummy use; false otherwise.
//
bool LIR::Use::IsDummyUse() const
{
return m_edge == &m_user;
}
//------------------------------------------------------------------------
// LIR::Use::Def: Returns the node that produces the def for this use.
//
GenTree* LIR::Use::Def() const
{
assert(IsInitialized());
return *m_edge;
}
//------------------------------------------------------------------------
// LIR::Use::User: Returns the node that uses the def for this use.
///
GenTree* LIR::Use::User() const
{
assert(IsInitialized());
assert(!IsDummyUse());
return m_user;
}
//------------------------------------------------------------------------
// LIR::Use::IsInitialized: Returns true if the use is minimally valid; false otherwise.
//
bool LIR::Use::IsInitialized() const
{
return (m_range != nullptr) && (m_user != nullptr) && (m_edge != nullptr);
}
//------------------------------------------------------------------------
// LIR::Use::AssertIsValid: DEBUG function to assert on many validity conditions.
//
void LIR::Use::AssertIsValid() const
{
assert(IsInitialized());
assert(m_range->Contains(m_user));
assert(Def() != nullptr);
GenTree** useEdge = nullptr;
assert(m_user->TryGetUse(Def(), &useEdge));
assert(useEdge == m_edge);
}
//------------------------------------------------------------------------
// LIR::Use::ReplaceWith: Changes the use to point to a new value.
//
// For example, given the following LIR:
//
// t15 = lclVar int arg1
// t16 = lclVar int arg1
//
// /--* t15 int
// +--* t16 int
// t17 = * == int
//
// /--* t17 int
// * jmpTrue void
//
// If we wanted to replace the use of t17 with a use of the constant "1", we
// might do the following (where `opEq` is a `Use` value that represents the
// use of t17):
//
// GenTree* constantOne = compiler->gtNewIconNode(1);
// range.InsertAfter(opEq.Def(), constantOne);
// opEq.ReplaceWith(constantOne);
//
// Which would produce something like the following LIR:
//
// t15 = lclVar int arg1
// t16 = lclVar int arg1
//
// /--* t15 int
// +--* t16 int
// t17 = * == int
//
// t18 = const int 1
//
// /--* t18 int
// * jmpTrue void
//
// Eliminating the now-dead compare and its operands using `LIR::Range::Remove`
// would then give us:
//
// t18 = const int 1
//
// /--* t18 int
// * jmpTrue void
//
// Arguments:
// replacement - The replacement node.
//
void LIR::Use::ReplaceWith(GenTree* replacement)
{
assert(IsInitialized());
assert(replacement != nullptr);
assert(IsDummyUse() || m_range->Contains(m_user));
assert(m_range->Contains(replacement));
if (!IsDummyUse())
{
m_user->ReplaceOperand(m_edge, replacement);
}
else
{
*m_edge = replacement;
}
}
//------------------------------------------------------------------------
// LIR::Use::ReplaceWithLclVar: Assigns the def for this use to a local
// var and points the use to a use of that
// local var. If no local number is provided,
// creates a new local var.
//
// For example, given the following IR:
//
// t15 = lclVar int arg1
// t16 = lclVar int arg1
//
// /--* t15 int
// +--* t16 int
// t17 = * == int
//
// /--* t17 int
// * jmpTrue void
//
// If we wanted to replace the use of t17 with a use of a new local var
// that holds the value represented by t17, we might do the following
// (where `opEq` is a `Use` value that represents the use of t17):
//
// opEq.ReplaceUseWithLclVar(compiler, block->getBBWeight(compiler));
//
// This would produce the following LIR:
//
// t15 = lclVar int arg1
// t16 = lclVar int arg1
//
// /--* t15 int
// +--* t16 int
// t17 = * == int
//
// /--* t17 int
// * st.lclVar int tmp0
//
// t18 = lclVar int tmp0
//
// /--* t18 int
// * jmpTrue void
//
// Arguments:
// compiler - The Compiler context.
// lclNum - The local to use for temporary storage. If BAD_VAR_NUM (the
// default) is provided, this method will create and use a new
// local var.
// pStore - On return, if non null, contains the created store node
//
// Return Value: The number of the local var used for temporary storage.
//
unsigned LIR::Use::ReplaceWithLclVar(Compiler* compiler, unsigned lclNum, GenTree** pStore)
{
assert(IsInitialized());
assert(compiler != nullptr);
assert(m_range->Contains(m_user));
assert(m_range->Contains(*m_edge));
GenTree* const node = *m_edge;
if (lclNum == BAD_VAR_NUM)
{
lclNum = compiler->lvaGrabTemp(true DEBUGARG("ReplaceWithLclVar is creating a new local variable"));
}
GenTreeLclVar* const store = compiler->gtNewTempStore(lclNum, node)->AsLclVar();
assert(store != nullptr);
assert(store->gtOp1 == node);
GenTree* const load =
new (compiler, GT_LCL_VAR) GenTreeLclVar(GT_LCL_VAR, store->TypeGet(), store->AsLclVarCommon()->GetLclNum());
m_range->InsertAfter(node, store, load);
ReplaceWith(load);
JITDUMP("ReplaceWithLclVar created store :\n");
DISPNODE(store);
if (pStore != nullptr)
{
*pStore = store;
}
return lclNum;
}
LIR::ReadOnlyRange::ReadOnlyRange()
: m_firstNode(nullptr)
, m_lastNode(nullptr)
{
}
LIR::ReadOnlyRange::ReadOnlyRange(ReadOnlyRange&& other)
: m_firstNode(other.m_firstNode)
, m_lastNode(other.m_lastNode)
{
#ifdef DEBUG
other.m_firstNode = nullptr;
other.m_lastNode = nullptr;
#endif
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::ReadOnlyRange:
// Creates a `ReadOnlyRange` value given the first and last node in
// the range.
//
// Arguments:
// firstNode - The first node in the range.
// lastNode - The last node in the range.
//
LIR::ReadOnlyRange::ReadOnlyRange(GenTree* firstNode, GenTree* lastNode)
: m_firstNode(firstNode)
, m_lastNode(lastNode)
{
assert((m_firstNode == nullptr) == (m_lastNode == nullptr));
assert((m_firstNode == m_lastNode) || (Contains(m_lastNode)));
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::operator=:
// Move assignment operator= for LIR ranges.
//
// Arguments:
// other - The range to move from.
//
LIR::ReadOnlyRange& LIR::ReadOnlyRange::operator=(ReadOnlyRange&& other)
{
m_firstNode = other.m_firstNode;
m_lastNode = other.m_lastNode;
#ifdef DEBUG
other.m_firstNode = nullptr;
other.m_lastNode = nullptr;
#endif
return *this;
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::FirstNode: Returns the first node in the range.
//
GenTree* LIR::ReadOnlyRange::FirstNode() const
{
return m_firstNode;
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::LastNode: Returns the last node in the range.
//
GenTree* LIR::ReadOnlyRange::LastNode() const
{
return m_lastNode;
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::IsEmpty: Returns true if the range is empty; false
// otherwise.
//
bool LIR::ReadOnlyRange::IsEmpty() const
{
assert((m_firstNode == nullptr) == (m_lastNode == nullptr));
return m_firstNode == nullptr;
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::begin: Returns an iterator positioned at the first
// node in the range.
//
LIR::ReadOnlyRange::Iterator LIR::ReadOnlyRange::begin() const
{
return Iterator(m_firstNode);
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::end: Returns an iterator positioned after the last
// node in the range.
//
LIR::ReadOnlyRange::Iterator LIR::ReadOnlyRange::end() const
{
return Iterator(m_lastNode == nullptr ? nullptr : m_lastNode->gtNext);
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::rbegin: Returns an iterator positioned at the last
// node in the range.
//
LIR::ReadOnlyRange::ReverseIterator LIR::ReadOnlyRange::rbegin() const
{
return ReverseIterator(m_lastNode);
}
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::rend: Returns an iterator positioned before the first
// node in the range.
//
LIR::ReadOnlyRange::ReverseIterator LIR::ReadOnlyRange::rend() const
{
return ReverseIterator(m_firstNode == nullptr ? nullptr : m_firstNode->gtPrev);
}
#ifdef DEBUG
//------------------------------------------------------------------------
// LIR::ReadOnlyRange::Contains: Indicates whether or not this range
// contains a given node.
//
// Arguments:
// node - The node to find.
//
// Return Value: True if this range contains the given node; false
// otherwise.
//
bool LIR::ReadOnlyRange::Contains(GenTree* node) const
{
assert(node != nullptr);
// TODO-LIR: derive this from the # of nodes in the function as well as
// the debug level. Checking small functions is pretty cheap; checking
// large functions is not.
if (JitConfig.JitExpensiveDebugCheckLevel() < 2)
{
return true;
}
for (GenTree* n : *this)
{
if (n == node)
{
return true;
}
}
return false;
}
#endif
LIR::Range::Range()
: ReadOnlyRange()
{
}
LIR::Range::Range(Range&& other)
: ReadOnlyRange(std::move(other))
{
}
//------------------------------------------------------------------------
// LIR::Range::Range: Creates a `Range` value given the first and last
// node in the range.
//
// Arguments:
// firstNode - The first node in the range.
// lastNode - The last node in the range.
//
LIR::Range::Range(GenTree* firstNode, GenTree* lastNode)
: ReadOnlyRange(firstNode, lastNode)
{
}
//------------------------------------------------------------------------
// LIR::Range::FirstNonCatchArgNode: Returns the first node after all catch arg nodes in this range.
//
GenTree* LIR::Range::FirstNonCatchArgNode() const
{
for (GenTree* node : *this)
{
if (node->OperIs(GT_CATCH_ARG))
{
continue;
}
else if ((node->OperIs(GT_STORE_LCL_VAR)) && (node->gtGetOp1()->OperIs(GT_CATCH_ARG)))
{
continue;
}
return node;
}
return nullptr;
}
//------------------------------------------------------------------------
// LIR::Range::InsertBefore: Inserts a node before another node in this range.
//
// Arguments:
// insertionPoint - The node before which `node` will be inserted. If non-null, must be part
// of this range. If null, insert at the end of the range.
// node - The node to insert. Must not be part of any range.
//
void LIR::Range::InsertBefore(GenTree* insertionPoint, GenTree* node)
{
assert(node != nullptr);
assert(node->gtPrev == nullptr);
assert(node->gtNext == nullptr);
FinishInsertBefore(insertionPoint, node, node);
}
//------------------------------------------------------------------------
// LIR::Range::InsertBefore: Inserts 2 nodes before another node in this range.
//
// Arguments:
// insertionPoint - The node before which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the end of the range.
// node1 - The first node to insert. Must not be part of any range.
// node2 - The second node to insert. Must not be part of any range.
//
// Notes:
// Resulting order:
// previous insertionPoint->gtPrev <-> node1 <-> node2 <-> insertionPoint
//
void LIR::Range::InsertBefore(GenTree* insertionPoint, GenTree* node1, GenTree* node2)
{
assert(node1 != nullptr);
assert(node2 != nullptr);
assert(node1->gtNext == nullptr);
assert(node1->gtPrev == nullptr);
assert(node2->gtNext == nullptr);
assert(node2->gtPrev == nullptr);
node1->gtNext = node2;
node2->gtPrev = node1;
FinishInsertBefore(insertionPoint, node1, node2);
}
//------------------------------------------------------------------------
// LIR::Range::InsertBefore: Inserts 3 nodes before another node in this range.
//
// Arguments:
// insertionPoint - The node before which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the end of the range.
// node1 - The first node to insert. Must not be part of any range.
// node2 - The second node to insert. Must not be part of any range.
// node3 - The third node to insert. Must not be part of any range.
//
// Notes:
// Resulting order:
// previous insertionPoint->gtPrev <-> node1 <-> node2 <-> node3 <-> insertionPoint
//
void LIR::Range::InsertBefore(GenTree* insertionPoint, GenTree* node1, GenTree* node2, GenTree* node3)
{
assert(node1 != nullptr);
assert(node2 != nullptr);
assert(node3 != nullptr);
assert(node1->gtNext == nullptr);
assert(node1->gtPrev == nullptr);
assert(node2->gtNext == nullptr);
assert(node2->gtPrev == nullptr);
assert(node3->gtNext == nullptr);
assert(node3->gtPrev == nullptr);
node1->gtNext = node2;
node2->gtPrev = node1;
node2->gtNext = node3;
node3->gtPrev = node2;
FinishInsertBefore(insertionPoint, node1, node3);
}
//------------------------------------------------------------------------
// LIR::Range::InsertBefore: Inserts 4 nodes before another node in this range.
//
// Arguments:
// insertionPoint - The node before which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the end of the range.
// node1 - The first node to insert. Must not be part of any range.
// node2 - The second node to insert. Must not be part of any range.
// node3 - The third node to insert. Must not be part of any range.
// node4 - The fourth node to insert. Must not be part of any range.
//
// Notes:
// Resulting order:
// previous insertionPoint->gtPrev <-> node1 <-> node2 <-> node3 <-> node4 <-> insertionPoint
//
void LIR::Range::InsertBefore(GenTree* insertionPoint, GenTree* node1, GenTree* node2, GenTree* node3, GenTree* node4)
{
assert(node1 != nullptr);
assert(node2 != nullptr);
assert(node3 != nullptr);
assert(node4 != nullptr);
assert(node1->gtNext == nullptr);
assert(node1->gtPrev == nullptr);
assert(node2->gtNext == nullptr);
assert(node2->gtPrev == nullptr);
assert(node3->gtNext == nullptr);
assert(node3->gtPrev == nullptr);
assert(node4->gtNext == nullptr);
assert(node4->gtPrev == nullptr);
node1->gtNext = node2;
node2->gtPrev = node1;
node2->gtNext = node3;
node3->gtPrev = node2;
node3->gtNext = node4;
node4->gtPrev = node3;
FinishInsertBefore(insertionPoint, node1, node4);
}
//------------------------------------------------------------------------
// LIR::Range::FinishInsertBefore: Helper function to finalize InsertBefore processing: link the
// range to insertionPoint. gtNext/gtPrev links between first and last are already set.
//
// Arguments:
// insertionPoint - The node before which the nodes will be inserted. If non-null, must be part
// of this range. If null, indicates to insert at the end of the range.
// first - The first node of the range to insert.
// last - The last node of the range to insert.
//
// Notes:
// Resulting order:
// previous insertionPoint->gtPrev <-> first <-> ... <-> last <-> insertionPoint
//
void LIR::Range::FinishInsertBefore(GenTree* insertionPoint, GenTree* first, GenTree* last)
{
assert(first != nullptr);
assert(last != nullptr);
assert(first->gtPrev == nullptr);
assert(last->gtNext == nullptr);
if (insertionPoint == nullptr)
{
if (m_firstNode == nullptr)
{
m_firstNode = first;
}
else
{
assert(m_lastNode != nullptr);
assert(m_lastNode->gtNext == nullptr);
m_lastNode->gtNext = first;
first->gtPrev = m_lastNode;
}
m_lastNode = last;
}
else
{
assert(Contains(insertionPoint));
first->gtPrev = insertionPoint->gtPrev;
if (first->gtPrev == nullptr)
{
assert(insertionPoint == m_firstNode);
m_firstNode = first;
}
else
{
first->gtPrev->gtNext = first;
}
last->gtNext = insertionPoint;
insertionPoint->gtPrev = last;
}
}
//------------------------------------------------------------------------
// LIR::Range::InsertAfter: Inserts a node after another node in this range.
//
// Arguments:
// insertionPoint - The node after which `node` will be inserted. If non-null, must be part
// of this range. If null, insert at the beginning of the range.
// node - The node to insert. Must not be part of any range.
//
// Notes:
// Resulting order:
// insertionPoint <-> node <-> previous insertionPoint->gtNext
//
void LIR::Range::InsertAfter(GenTree* insertionPoint, GenTree* node)
{
assert(node != nullptr);
assert(node->gtNext == nullptr);
assert(node->gtPrev == nullptr);
FinishInsertAfter(insertionPoint, node, node);
}
//------------------------------------------------------------------------
// LIR::Range::InsertAfter: Inserts 2 nodes after another node in this range.
//
// Arguments:
// insertionPoint - The node after which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the beginning of the range.
// node1 - The first node to insert. Must not be part of any range.
// node2 - The second node to insert. Must not be part of any range. Inserted after node1.
//
// Notes:
// Resulting order:
// insertionPoint <-> node1 <-> node2 <-> previous insertionPoint->gtNext
//
void LIR::Range::InsertAfter(GenTree* insertionPoint, GenTree* node1, GenTree* node2)
{
assert(node1 != nullptr);
assert(node2 != nullptr);
assert(node1->gtNext == nullptr);
assert(node1->gtPrev == nullptr);
assert(node2->gtNext == nullptr);
assert(node2->gtPrev == nullptr);
node1->gtNext = node2;
node2->gtPrev = node1;
FinishInsertAfter(insertionPoint, node1, node2);
}
//------------------------------------------------------------------------
// LIR::Range::InsertAfter: Inserts 3 nodes after another node in this range.
//
// Arguments:
// insertionPoint - The node after which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the beginning of the range.
// node1 - The first node to insert. Must not be part of any range.
// node2 - The second node to insert. Must not be part of any range. Inserted after node1.
// node3 - The third node to insert. Must not be part of any range. Inserted after node2.
//
// Notes:
// Resulting order:
// insertionPoint <-> node1 <-> node2 <-> node3 <-> previous insertionPoint->gtNext
//
void LIR::Range::InsertAfter(GenTree* insertionPoint, GenTree* node1, GenTree* node2, GenTree* node3)
{
assert(node1 != nullptr);
assert(node2 != nullptr);
assert(node3 != nullptr);
assert(node1->gtNext == nullptr);
assert(node1->gtPrev == nullptr);
assert(node2->gtNext == nullptr);
assert(node2->gtPrev == nullptr);
assert(node3->gtNext == nullptr);
assert(node3->gtPrev == nullptr);
node1->gtNext = node2;
node2->gtPrev = node1;
node2->gtNext = node3;
node3->gtPrev = node2;
FinishInsertAfter(insertionPoint, node1, node3);
}
//------------------------------------------------------------------------
// LIR::Range::InsertAfter: Inserts 4 nodes after another node in this range.
//
// Arguments:
// insertionPoint - The node after which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the beginning of the range.
// node1 - The first node to insert. Must not be part of any range.
// node2 - The second node to insert. Must not be part of any range. Inserted after node1.
// node3 - The third node to insert. Must not be part of any range. Inserted after node2.
// node4 - The fourth node to insert. Must not be part of any range. Inserted after node3.
//
// Notes:
// Resulting order:
// insertionPoint <-> node1 <-> node2 <-> node3 <-> node4 <-> previous insertionPoint->gtNext
//
void LIR::Range::InsertAfter(GenTree* insertionPoint, GenTree* node1, GenTree* node2, GenTree* node3, GenTree* node4)
{
assert(node1 != nullptr);
assert(node2 != nullptr);
assert(node3 != nullptr);
assert(node4 != nullptr);
assert(node1->gtNext == nullptr);
assert(node1->gtPrev == nullptr);
assert(node2->gtNext == nullptr);
assert(node2->gtPrev == nullptr);
assert(node3->gtNext == nullptr);
assert(node3->gtPrev == nullptr);
assert(node4->gtNext == nullptr);
assert(node4->gtPrev == nullptr);
node1->gtNext = node2;
node2->gtPrev = node1;
node2->gtNext = node3;
node3->gtPrev = node2;
node3->gtNext = node4;
node4->gtPrev = node3;
FinishInsertAfter(insertionPoint, node1, node4);
}
//------------------------------------------------------------------------
// LIR::Range::FinishInsertAfter: Helper function to finalize InsertAfter processing: link the
// range to insertionPoint. gtNext/gtPrev links between first and last are already set.
//
// Arguments:
// insertionPoint - The node after which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the beginning of the range.
// first - The first node of the range to insert.
// last - The last node of the range to insert.
//
// Notes:
// Resulting order:
// insertionPoint <-> first <-> ... <-> last <-> previous insertionPoint->gtNext
//
void LIR::Range::FinishInsertAfter(GenTree* insertionPoint, GenTree* first, GenTree* last)
{
assert(first != nullptr);
assert(last != nullptr);
assert(first->gtPrev == nullptr);
assert(last->gtNext == nullptr);
if (insertionPoint == nullptr)
{
if (m_lastNode == nullptr)
{
m_lastNode = last;
}
else
{
assert(m_firstNode != nullptr);
assert(m_firstNode->gtPrev == nullptr);
m_firstNode->gtPrev = last;
last->gtNext = m_firstNode;
}
m_firstNode = first;
}
else
{
assert(Contains(insertionPoint));
last->gtNext = insertionPoint->gtNext;
if (last->gtNext == nullptr)
{
assert(insertionPoint == m_lastNode);
m_lastNode = last;
}
else
{
last->gtNext->gtPrev = last;
}
first->gtPrev = insertionPoint;
insertionPoint->gtNext = first;
}
}
//------------------------------------------------------------------------
// LIR::Range::InsertBefore: Inserts a range before another node in `this` range.
//
// Arguments:
// insertionPoint - The node before which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the end of the range.
// range - The range to splice in.
//
void LIR::Range::InsertBefore(GenTree* insertionPoint, Range&& range)
{
assert(!range.IsEmpty());
FinishInsertBefore(insertionPoint, range.m_firstNode, range.m_lastNode);
}
//------------------------------------------------------------------------
// LIR::Range::InsertAfter: Inserts a range after another node in `this` range.
//
// Arguments:
// insertionPoint - The node after which the nodes will be inserted. If non-null, must be part
// of this range. If null, insert at the beginning of the range.
// range - The range to splice in.
//
void LIR::Range::InsertAfter(GenTree* insertionPoint, Range&& range)
{
assert(!range.IsEmpty());
FinishInsertAfter(insertionPoint, range.m_firstNode, range.m_lastNode);
}
//------------------------------------------------------------------------
// LIR::Range::InsertAtBeginning: Inserts a node at the beginning of this range.
//
// Arguments:
// node - The node to insert. Must not be part of any range.
//
void LIR::Range::InsertAtBeginning(GenTree* node)
{
InsertBefore(m_firstNode, node);
}
//------------------------------------------------------------------------
// LIR::Range::InsertAtEnd: Inserts a node at the end of this range.
//
// Arguments:
// node - The node to insert. Must not be part of any range.
//
void LIR::Range::InsertAtEnd(GenTree* node)
{
InsertAfter(m_lastNode, node);
}
//------------------------------------------------------------------------
// LIR::Range::InsertAtBeginning: Inserts a range at the beginning of `this` range.
//
// Arguments:
// range - The range to splice in.
//
void LIR::Range::InsertAtBeginning(Range&& range)
{
InsertBefore(m_firstNode, std::move(range));
}
//------------------------------------------------------------------------
// LIR::Range::InsertAtEnd: Inserts a range at the end of `this` range.
//
// Arguments:
// range - The range to splice in.
//
void LIR::Range::InsertAtEnd(Range&& range)
{
InsertAfter(m_lastNode, std::move(range));
}
//------------------------------------------------------------------------
// LIR::Range::Remove: Removes a node from this range.
//
// Arguments:
// node - The node to remove. Must be part of this range.
// markOperandsUnused - If true, marks the node's operands as unused.
//
void LIR::Range::Remove(GenTree* node, bool markOperandsUnused)
{
assert(node != nullptr);
assert(Contains(node));
if (markOperandsUnused)
{
node->VisitOperands([](GenTree* operand) -> GenTree::VisitResult {
// The operand of JTRUE does not produce a value (just sets the flags).
if (operand->IsValue())
{
operand->SetUnusedValue();
}
return GenTree::VisitResult::Continue;
});
}
GenTree* prev = node->gtPrev;
GenTree* next = node->gtNext;
if (prev != nullptr)
{
prev->gtNext = next;
}
else
{
assert(node == m_firstNode);
m_firstNode = next;
}
if (next != nullptr)
{
next->gtPrev = prev;
}
else
{
assert(node == m_lastNode);
m_lastNode = prev;
}
node->gtPrev = nullptr;
node->gtNext = nullptr;
}
//------------------------------------------------------------------------
// LIR::Range::Remove: Removes a subrange from this range.
//
// Both the start and the end of the subrange must be part of this range.
//
// Arguments:
// firstNode - The first node in the subrange.
// lastNode - The last node in the subrange.
//
// Returns:
// A mutable range containing the removed nodes.
//
LIR::Range LIR::Range::Remove(GenTree* firstNode, GenTree* lastNode)
{
assert(firstNode != nullptr);
assert(lastNode != nullptr);
assert(Contains(firstNode));
assert((firstNode == lastNode) || firstNode->Precedes(lastNode));
GenTree* prev = firstNode->gtPrev;
GenTree* next = lastNode->gtNext;