This repository was archived by the owner on Jan 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathblock.cpp
1586 lines (1420 loc) · 48.2 KB
/
block.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.
// See the LICENSE file in the project root for more information.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX BasicBlock XX
XX XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include"jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#if MEASURE_BLOCK_SIZE
/* static */
size_t BasicBlock::s_Size;
/* static */
size_t BasicBlock::s_Count;
#endif// MEASURE_BLOCK_SIZE
#ifdef DEBUG
// The max # of tree nodes in any BB
/* static */
unsigned BasicBlock::s_nMaxTrees;
#endif// DEBUG
#ifdef DEBUG
flowList* ShuffleHelper(unsigned hash, flowList* res)
{
flowList* head = res;
for (flowList *prev = nullptr; res != nullptr; prev = res, res = res->flNext)
{
unsigned blkHash = (hash ^ (res->flBlock->bbNum << 16) ^ res->flBlock->bbNum);
if (((blkHash % 1879) & 1) && prev != nullptr)
{
// Swap res with head.
prev->flNext = head;
jitstd::swap(head->flNext, res->flNext);
jitstd::swap(head, res);
}
}
return head;
}
unsignedSsaStressHashHelper()
{
// hash = 0: turned off, hash = 1: use method hash, hash = *: use custom hash.
unsigned hash = JitConfig.JitSsaStress();
if (hash == 0)
{
return hash;
}
if (hash == 1)
{
returnJitTls::GetCompiler()->info.compMethodHash();
}
return ((hash >> 16) == 0) ? ((hash << 16) | hash) : hash;
}
#endif
EHSuccessorIterPosition::EHSuccessorIterPosition(Compiler* comp, BasicBlock* block)
: m_remainingRegSuccs(block->NumSucc(comp)), m_curRegSucc(nullptr), m_curTry(comp->ehGetBlockExnFlowDsc(block))
{
// If "block" is a "leave helper" block (the empty BBJ_ALWAYS block that pairs with a
// preceding BBJ_CALLFINALLY block to implement a "leave" IL instruction), then no exceptions
// can occur within it, so clear m_curTry if it's non-null.
if (m_curTry != nullptr)
{
BasicBlock* beforeBlock = block->bbPrev;
if (beforeBlock != nullptr && beforeBlock->isBBCallAlwaysPair())
{
m_curTry = nullptr;
}
}
if (m_curTry == nullptr && m_remainingRegSuccs > 0)
{
// Examine the successors to see if any are the start of try blocks.
FindNextRegSuccTry(comp, block);
}
}
voidEHSuccessorIterPosition::FindNextRegSuccTry(Compiler* comp, BasicBlock* block)
{
assert(m_curTry == nullptr);
// Must now consider the next regular successor, if any.
while (m_remainingRegSuccs > 0)
{
m_remainingRegSuccs--;
m_curRegSucc = block->GetSucc(m_remainingRegSuccs, comp);
if (comp->bbIsTryBeg(m_curRegSucc))
{
assert(m_curRegSucc->hasTryIndex()); // Since it is a try begin.
unsigned newTryIndex = m_curRegSucc->getTryIndex();
// If the try region started by "m_curRegSucc" (represented by newTryIndex) contains m_block,
// we've already yielded its handler, as one of the EH handler successors of m_block itself.
if (comp->bbInExnFlowRegions(newTryIndex, block))
{
continue;
}
// Otherwise, consider this try.
m_curTry = comp->ehGetDsc(newTryIndex);
break;
}
}
}
voidEHSuccessorIterPosition::Advance(Compiler* comp, BasicBlock* block)
{
assert(m_curTry != nullptr);
if (m_curTry->ebdEnclosingTryIndex != EHblkDsc::NO_ENCLOSING_INDEX)
{
m_curTry = comp->ehGetDsc(m_curTry->ebdEnclosingTryIndex);
// If we've gone over into considering try's containing successors,
// then the enclosing try must have the successor as its first block.
if (m_curRegSucc == nullptr || m_curTry->ebdTryBeg == m_curRegSucc)
{
return;
}
// Otherwise, give up, try the next regular successor.
m_curTry = nullptr;
}
else
{
m_curTry = nullptr;
}
// We've exhausted all try blocks.
// See if there are any remaining regular successors that start try blocks.
FindNextRegSuccTry(comp, block);
}
BasicBlock* EHSuccessorIterPosition::Current(Compiler* comp, BasicBlock* block)
{
assert(m_curTry != nullptr);
return m_curTry->ExFlowBlock();
}
flowList* Compiler::BlockPredsWithEH(BasicBlock* blk)
{
BlockToFlowListMap* ehPreds = GetBlockToEHPreds();
flowList* res;
if (ehPreds->Lookup(blk, &res))
{
return res;
}
res = blk->bbPreds;
unsigned tryIndex;
if (bbIsExFlowBlock(blk, &tryIndex))
{
// Find the first block of the try.
EHblkDsc* ehblk = ehGetDsc(tryIndex);
BasicBlock* tryStart = ehblk->ebdTryBeg;
for (flowList* tryStartPreds = tryStart->bbPreds; tryStartPreds != nullptr;
tryStartPreds = tryStartPreds->flNext)
{
res = new (this, CMK_FlowList) flowList(tryStartPreds->flBlock, res);
#if MEASURE_BLOCK_SIZE
genFlowNodeCnt += 1;
genFlowNodeSize += sizeof(flowList);
#endif// MEASURE_BLOCK_SIZE
}
// Now add all blocks handled by this handler (except for second blocks of BBJ_CALLFINALLY/BBJ_ALWAYS pairs;
// these cannot cause transfer to the handler...)
BasicBlock* prevBB = nullptr;
// TODO-Throughput: It would be nice if we could iterate just over the blocks in the try, via
// something like:
// for (BasicBlock* bb = ehblk->ebdTryBeg; bb != ehblk->ebdTryLast->bbNext; bb = bb->bbNext)
// (plus adding in any filter blocks outside the try whose exceptions are handled here).
// That doesn't work, however: funclets have caused us to sometimes split the body of a try into
// more than one sequence of contiguous blocks. We need to find a better way to do this.
for (BasicBlock *bb = fgFirstBB; bb != nullptr; prevBB = bb, bb = bb->bbNext)
{
if (bbInExnFlowRegions(tryIndex, bb) && (prevBB == nullptr || !prevBB->isBBCallAlwaysPair()))
{
res = new (this, CMK_FlowList) flowList(bb, res);
#if MEASURE_BLOCK_SIZE
genFlowNodeCnt += 1;
genFlowNodeSize += sizeof(flowList);
#endif// MEASURE_BLOCK_SIZE
}
}
#ifdef DEBUG
unsigned hash = SsaStressHashHelper();
if (hash != 0)
{
res = ShuffleHelper(hash, res);
}
#endif// DEBUG
ehPreds->Set(blk, res);
}
return res;
}
#ifdef DEBUG
//------------------------------------------------------------------------
// dspBlockILRange(): Display the block's IL range as [XXX...YYY), where XXX and YYY might be "???" for BAD_IL_OFFSET.
//
voidBasicBlock::dspBlockILRange() const
{
if (bbCodeOffs != BAD_IL_OFFSET)
{
printf("[%03X..", bbCodeOffs);
}
else
{
printf("[???"
"..");
}
if (bbCodeOffsEnd != BAD_IL_OFFSET)
{
// brace-matching editor workaround for following line: (
printf("%03X)", bbCodeOffsEnd);
}
else
{
// brace-matching editor workaround for following line: (
printf("???"
")");
}
}
//------------------------------------------------------------------------
// dspFlags: Print out the block's flags
//
voidBasicBlock::dspFlags()
{
if (bbFlags & BBF_VISITED)
{
printf("v ");
}
if (bbFlags & BBF_MARKED)
{
printf("m ");
}
if (bbFlags & BBF_CHANGED)
{
printf("! ");
}
if (bbFlags & BBF_REMOVED)
{
printf("del ");
}
if (bbFlags & BBF_DONT_REMOVE)
{
printf("keep ");
}
if (bbFlags & BBF_IMPORTED)
{
printf("i ");
}
if (bbFlags & BBF_INTERNAL)
{
printf("internal ");
}
if (bbFlags & BBF_FAILED_VERIFICATION)
{
printf("failV ");
}
if (bbFlags & BBF_TRY_BEG)
{
printf("try ");
}
if (bbFlags & BBF_NEEDS_GCPOLL)
{
printf("poll ");
}
if (bbFlags & BBF_RUN_RARELY)
{
printf("rare ");
}
if (bbFlags & BBF_LOOP_HEAD)
{
printf("Loop ");
}
if (bbFlags & BBF_LOOP_CALL0)
{
printf("Loop0 ");
}
if (bbFlags & BBF_LOOP_CALL1)
{
printf("Loop1 ");
}
if (bbFlags & BBF_HAS_LABEL)
{
printf("label ");
}
if (bbFlags & BBF_JMP_TARGET)
{
printf("target ");
}
if (bbFlags & BBF_HAS_JMP)
{
printf("jmp ");
}
if (bbFlags & BBF_GC_SAFE_POINT)
{
printf("gcsafe ");
}
if (bbFlags & BBF_FUNCLET_BEG)
{
printf("flet ");
}
if (bbFlags & BBF_HAS_IDX_LEN)
{
printf("idxlen ");
}
if (bbFlags & BBF_HAS_NEWARRAY)
{
printf("new[] ");
}
if (bbFlags & BBF_HAS_NEWOBJ)
{
printf("newobj ");
}
#if defined(FEATURE_EH_FUNCLETS) && defined(_TARGET_ARM_)
if (bbFlags & BBF_FINALLY_TARGET)
{
printf("ftarget ");
}
#endif// defined(FEATURE_EH_FUNCLETS) && defined(_TARGET_ARM_)
if (bbFlags & BBF_BACKWARD_JUMP)
{
printf("bwd ");
}
if (bbFlags & BBF_RETLESS_CALL)
{
printf("retless ");
}
if (bbFlags & BBF_LOOP_PREHEADER)
{
printf("LoopPH ");
}
if (bbFlags & BBF_COLD)
{
printf("cold ");
}
if (bbFlags & BBF_PROF_WEIGHT)
{
printf("IBC ");
}
if (bbFlags & BBF_IS_LIR)
{
printf("LIR ");
}
if (bbFlags & BBF_KEEP_BBJ_ALWAYS)
{
printf("KEEP ");
}
if (bbFlags & BBF_CLONED_FINALLY_BEGIN)
{
printf("cfb ");
}
if (bbFlags & BBF_CLONED_FINALLY_END)
{
printf("cfe ");
}
}
/*****************************************************************************
*
* Display the bbPreds basic block list (the block predecessors).
* Returns the number of characters printed.
*/
unsignedBasicBlock::dspPreds()
{
unsigned count = 0;
for (flowList* pred = bbPreds; pred != nullptr; pred = pred->flNext)
{
if (count != 0)
{
printf(",");
count += 1;
}
printf(FMT_BB, pred->flBlock->bbNum);
count += 4;
// Account for %02u only handling 2 digits, but we can display more than that.
unsigned digits = CountDigits(pred->flBlock->bbNum);
if (digits > 2)
{
count += digits - 2;
}
// Does this predecessor have an interesting dup count? If so, display it.
if (pred->flDupCount > 1)
{
printf("(%u)", pred->flDupCount);
count += 2 + CountDigits(pred->flDupCount);
}
}
return count;
}
/*****************************************************************************
*
* Display the bbCheapPreds basic block list (the block predecessors).
* Returns the number of characters printed.
*/
unsignedBasicBlock::dspCheapPreds()
{
unsigned count = 0;
for (BasicBlockList* pred = bbCheapPreds; pred != nullptr; pred = pred->next)
{
if (count != 0)
{
printf(",");
count += 1;
}
printf(FMT_BB, pred->block->bbNum);
count += 4;
// Account for %02u only handling 2 digits, but we can display more than that.
unsigned digits = CountDigits(pred->block->bbNum);
if (digits > 2)
{
count += digits - 2;
}
}
return count;
}
/*****************************************************************************
*
* Display the basic block successors.
* Returns the count of successors.
*/
unsignedBasicBlock::dspSuccs(Compiler* compiler)
{
unsigned numSuccs = NumSucc(compiler);
unsigned count = 0;
for (unsigned i = 0; i < numSuccs; i++)
{
printf("%s", (count == 0) ? "" : ",");
printf(FMT_BB, GetSucc(i, compiler)->bbNum);
count++;
}
return count;
}
// Display a compact representation of the bbJumpKind, that is, where this block branches.
// This is similar to code in Compiler::fgTableDispBasicBlock(), but doesn't have that code's requirements to align
// things strictly.
voidBasicBlock::dspJumpKind()
{
switch (bbJumpKind)
{
case BBJ_EHFINALLYRET:
printf(" (finret)");
break;
case BBJ_EHFILTERRET:
printf(" (fltret)");
break;
case BBJ_EHCATCHRET:
printf(" -> " FMT_BB " (cret)", bbJumpDest->bbNum);
break;
case BBJ_THROW:
printf(" (throw)");
break;
case BBJ_RETURN:
printf(" (return)");
break;
case BBJ_NONE:
// For fall-through blocks, print nothing.
break;
case BBJ_ALWAYS:
if (bbFlags & BBF_KEEP_BBJ_ALWAYS)
{
printf(" -> " FMT_BB " (ALWAYS)", bbJumpDest->bbNum);
}
else
{
printf(" -> " FMT_BB " (always)", bbJumpDest->bbNum);
}
break;
case BBJ_LEAVE:
printf(" -> " FMT_BB " (leave)", bbJumpDest->bbNum);
break;
case BBJ_CALLFINALLY:
printf(" -> " FMT_BB " (callf)", bbJumpDest->bbNum);
break;
case BBJ_COND:
printf(" -> " FMT_BB " (cond)", bbJumpDest->bbNum);
break;
case BBJ_SWITCH:
printf(" ->");
unsigned jumpCnt;
jumpCnt = bbJumpSwt->bbsCount;
BasicBlock** jumpTab;
jumpTab = bbJumpSwt->bbsDstTab;
do
{
printf("%c" FMT_BB, (jumpTab == bbJumpSwt->bbsDstTab) ? '' : ',', (*jumpTab)->bbNum);
} while (++jumpTab, --jumpCnt);
printf(" (switch)");
break;
default:
unreached();
break;
}
}
voidBasicBlock::dspBlockHeader(Compiler* compiler,
bool showKind /*= true*/,
bool showFlags /*= false*/,
bool showPreds /*= true*/)
{
printf(FMT_BB "", bbNum);
dspBlockILRange();
if (showKind)
{
dspJumpKind();
}
if (showPreds)
{
printf(", preds={");
if (compiler->fgCheapPredsValid)
{
dspCheapPreds();
}
else
{
dspPreds();
}
printf("} succs={");
dspSuccs(compiler);
printf("}");
}
if (showFlags)
{
constunsigned lowFlags = (unsigned)bbFlags;
constunsigned highFlags = (unsigned)(bbFlags >> 32);
printf(" flags=0x%08x.%08x: ", highFlags, lowFlags);
dspFlags();
}
printf("\n");
}
constchar* BasicBlock::dspToString(int blockNumPadding /* = 2*/)
{
staticchar buffers[3][64]; // static array of 3 to allow 3 concurrent calls in one printf()
staticint nextBufferIndex = 0;
auto& buffer = buffers[nextBufferIndex];
nextBufferIndex = (nextBufferIndex + 1) % _countof(buffers);
_snprintf_s(buffer, _countof(buffer), _countof(buffer), FMT_BB "%*s [%04u]", bbNum, blockNumPadding, "", bbID);
return buffer;
}
#endif// DEBUG
// Allocation function for MemoryPhiArg.
void* BasicBlock::MemoryPhiArg::operatornew(size_t sz, Compiler* comp)
{
return comp->getAllocator(CMK_MemoryPhiArg).allocate<char>(sz);
}
//------------------------------------------------------------------------
// CloneBlockState: Try to populate `to` block with a copy of `from` block's statements, replacing
// uses of local `varNum` with IntCns `varVal`.
//
// Arguments:
// compiler - Jit compiler instance
// to - New/empty block to copy statements into
// from - Block to copy statements from
// varNum - lclVar uses with lclNum `varNum` will be replaced; can be ~0 to indicate no replacement.
// varVal - If replacing uses of `varNum`, replace them with int constants with value `varVal`.
//
// Return Value:
// Cloning may fail because this routine uses `gtCloneExpr` for cloning and it can't handle all
// IR nodes. If cloning of any statement fails, `false` will be returned and block `to` may be
// partially populated. If cloning of all statements succeeds, `true` will be returned and
// block `to` will be fully populated.
boolBasicBlock::CloneBlockState(
Compiler* compiler, BasicBlock* to, const BasicBlock* from, unsigned varNum, int varVal)
{
assert(to->bbStmtList == nullptr);
to->bbFlags = from->bbFlags;
to->bbWeight = from->bbWeight;
BlockSetOps::AssignAllowUninitRhs(compiler, to->bbReach, from->bbReach);
to->copyEHRegion(from);
to->bbCatchTyp = from->bbCatchTyp;
to->bbRefs = from->bbRefs;
to->bbStkTempsIn = from->bbStkTempsIn;
to->bbStkTempsOut = from->bbStkTempsOut;
to->bbStkDepth = from->bbStkDepth;
to->bbCodeOffs = from->bbCodeOffs;
to->bbCodeOffsEnd = from->bbCodeOffsEnd;
VarSetOps::AssignAllowUninitRhs(compiler, to->bbScope, from->bbScope);
to->bbNatLoopNum = from->bbNatLoopNum;
#ifdef DEBUG
to->bbLoopNum = from->bbLoopNum;
to->bbTgtStkDepth = from->bbTgtStkDepth;
#endif// DEBUG
for (Statement* fromStmt : from->Statements())
{
auto newExpr = compiler->gtCloneExpr(fromStmt->GetRootNode(), 0, varNum, varVal);
if (!newExpr)
{
// gtCloneExpr doesn't handle all opcodes, so may fail to clone a statement.
// When that happens, it returns nullptr; abandon the rest of this block and
// return `false` to the caller to indicate that cloning was unsuccessful.
returnfalse;
}
compiler->fgInsertStmtAtEnd(to, compiler->fgNewStmtFromTree(newExpr));
}
returntrue;
}
// LIR helpers
voidBasicBlock::MakeLIR(GenTree* firstNode, GenTree* lastNode)
{
assert(!IsLIR());
assert((firstNode == nullptr) == (lastNode == nullptr));
assert((firstNode == lastNode) || firstNode->Precedes(lastNode));
m_firstNode = firstNode;
m_lastNode = lastNode;
bbFlags |= BBF_IS_LIR;
}
boolBasicBlock::IsLIR()
{
assert(isValid());
constbool isLIR = ((bbFlags & BBF_IS_LIR) != 0);
return isLIR;
}
//------------------------------------------------------------------------
// firstStmt: Returns the first statement in the block
//
// Arguments:
// None.
//
// Return Value:
// The first statement in the block's bbStmtList.
//
Statement* BasicBlock::firstStmt() const
{
return bbStmtList;
}
//------------------------------------------------------------------------
// lastStmt: Returns the last statement in the block
//
// Arguments:
// None.
//
// Return Value:
// The last statement in the block's bbStmtList.
//
Statement* BasicBlock::lastStmt() const
{
if (bbStmtList == nullptr)
{
returnnullptr;
}
Statement* result = bbStmtList->GetPrevStmt();
assert(result != nullptr && result->GetNextStmt() == nullptr);
return result;
}
//------------------------------------------------------------------------
// BasicBlock::firstNode: Returns the first node in the block.
//
GenTree* BasicBlock::firstNode()
{
returnIsLIR() ? GetFirstLIRNode() : Compiler::fgGetFirstNode(firstStmt()->GetRootNode());
}
//------------------------------------------------------------------------
// BasicBlock::lastNode: Returns the last node in the block.
//
GenTree* BasicBlock::lastNode()
{
returnIsLIR() ? m_lastNode : lastStmt()->GetRootNode();
}
//------------------------------------------------------------------------
// GetUniquePred: Returns the unique predecessor of a block, if one exists.
// The predecessor lists must be accurate.
//
// Arguments:
// None.
//
// Return Value:
// The unique predecessor of a block, or nullptr if there is no unique predecessor.
//
// Notes:
// If the first block has a predecessor (which it may have, if it is the target of
// a backedge), we never want to consider it "unique" because the prolog is an
// implicit predecessor.
BasicBlock* BasicBlock::GetUniquePred(Compiler* compiler)
{
if ((bbPreds == nullptr) || (bbPreds->flNext != nullptr) || (this == compiler->fgFirstBB))
{
returnnullptr;
}
else
{
return bbPreds->flBlock;
}
}
//------------------------------------------------------------------------
// GetUniqueSucc: Returns the unique successor of a block, if one exists.
// Only considers BBJ_ALWAYS and BBJ_NONE block types.
//
// Arguments:
// None.
//
// Return Value:
// The unique successor of a block, or nullptr if there is no unique successor.
BasicBlock* BasicBlock::GetUniqueSucc()
{
if (bbJumpKind == BBJ_ALWAYS)
{
return bbJumpDest;
}
elseif (bbJumpKind == BBJ_NONE)
{
return bbNext;
}
else
{
returnnullptr;
}
}
// Static vars.
BasicBlock::MemoryPhiArg* BasicBlock::EmptyMemoryPhiDef = (BasicBlock::MemoryPhiArg*)0x1;
unsigned JitPtrKeyFuncs<BasicBlock>::GetHashCode(const BasicBlock* ptr)
{
#ifdef DEBUG
unsigned hash = SsaStressHashHelper();
if (hash != 0)
{
return (hash ^ (ptr->bbNum << 16) ^ ptr->bbNum);
}
#endif
return ptr->bbNum;
}
boolBasicBlock::isEmpty()
{
if (!IsLIR())
{
return (this->FirstNonPhiDef() == nullptr);
}
for (GenTree* node : LIR::AsRange(this).NonPhiNodes())
{
if (node->OperGet() != GT_IL_OFFSET)
{
returnfalse;
}
}
returntrue;
}
//------------------------------------------------------------------------
// isValid: Checks that the basic block doesn't mix statements and LIR lists.
//
// Return Value:
// True if it a valid basic block.
//
boolBasicBlock::isValid()
{
constbool isLIR = ((bbFlags & BBF_IS_LIR) != 0);
if (isLIR)
{
// Should not have statements in LIR.
return (bbStmtList == nullptr);
}
else
{
// Should not have tree list before LIR.
return (GetFirstLIRNode() == nullptr);
}
}
Statement* BasicBlock::FirstNonPhiDef()
{
Statement* stmt = firstStmt();
if (stmt == nullptr)
{
returnnullptr;
}
GenTree* tree = stmt->GetRootNode();
while ((tree->OperGet() == GT_ASG && tree->AsOp()->gtOp2->OperGet() == GT_PHI) ||
(tree->OperGet() == GT_STORE_LCL_VAR && tree->AsOp()->gtOp1->OperGet() == GT_PHI))
{
stmt = stmt->GetNextStmt();
if (stmt == nullptr)
{
returnnullptr;
}
tree = stmt->GetRootNode();
}
return stmt;
}
Statement* BasicBlock::FirstNonPhiDefOrCatchArgAsg()
{
Statement* stmt = FirstNonPhiDef();
if (stmt == nullptr)
{
returnnullptr;
}
GenTree* tree = stmt->GetRootNode();
if ((tree->OperGet() == GT_ASG && tree->AsOp()->gtOp2->OperGet() == GT_CATCH_ARG) ||
(tree->OperGet() == GT_STORE_LCL_VAR && tree->AsOp()->gtOp1->OperGet() == GT_CATCH_ARG))
{
stmt = stmt->GetNextStmt();
}
return stmt;
}
/*****************************************************************************
*
* Mark a block as rarely run, we also don't want to have a loop in a
* rarely run block, and we set it's weight to zero.
*/
voidBasicBlock::bbSetRunRarely()
{
setBBWeight(BB_ZERO_WEIGHT);
if (bbWeight == BB_ZERO_WEIGHT)
{
bbFlags |= BBF_RUN_RARELY; // This block is never/rarely run
}
}
/*****************************************************************************
*
* Can a BasicBlock be inserted after this without altering the flowgraph
*/
boolBasicBlock::bbFallsThrough()
{
switch (bbJumpKind)
{
case BBJ_THROW:
case BBJ_EHFINALLYRET:
case BBJ_EHFILTERRET:
case BBJ_EHCATCHRET:
case BBJ_RETURN:
case BBJ_ALWAYS:
case BBJ_LEAVE:
case BBJ_SWITCH:
returnfalse;
case BBJ_NONE:
case BBJ_COND:
returntrue;
case BBJ_CALLFINALLY:
return ((bbFlags & BBF_RETLESS_CALL) == 0);
default:
assert(!"Unknown bbJumpKind in bbFallsThrough()");
returntrue;
}
}
//------------------------------------------------------------------------
// NumSucc: Returns the count of block successors. See the declaration comment for details.
//
// Arguments:
// None.
//
// Return Value:
// Count of block successors.
//
unsignedBasicBlock::NumSucc()
{
switch (bbJumpKind)
{
case BBJ_THROW:
case BBJ_RETURN:
case BBJ_EHFINALLYRET:
case BBJ_EHFILTERRET:
return0;
case BBJ_CALLFINALLY:
case BBJ_ALWAYS:
case BBJ_EHCATCHRET:
case BBJ_LEAVE:
case BBJ_NONE:
return1;
case BBJ_COND:
if (bbJumpDest == bbNext)
{
return1;
}
else
{
return2;
}
case BBJ_SWITCH:
return bbJumpSwt->bbsCount;
default:
unreached();
}
}
//------------------------------------------------------------------------
// GetSucc: Returns the requested block successor. See the declaration comment for details.
//
// Arguments:
// i - index of successor to return. 0 <= i <= NumSucc().
//
// Return Value:
// Requested successor block
//
BasicBlock* BasicBlock::GetSucc(unsigned i)
{
assert(i < NumSucc()); // Index bounds check.
switch (bbJumpKind)
{
case BBJ_CALLFINALLY:
case BBJ_ALWAYS:
case BBJ_EHCATCHRET:
case BBJ_LEAVE:
return bbJumpDest;
case BBJ_NONE:
return bbNext;
case BBJ_COND:
if (i == 0)
{
return bbNext;
}
else
{
assert(i == 1);
return bbJumpDest;
}
case BBJ_SWITCH:
return bbJumpSwt->bbsDstTab[i];
default:
unreached();
}
}
//------------------------------------------------------------------------
// NumSucc: Returns the count of block successors. See the declaration comment for details.
//
// Arguments: