Skip to content

Commit e14540a

Browse files
JIT: Make BasicBlock jump target private (#93152)
1 parent e041633 commit e14540a

38 files changed

+692
-681
lines changed

src/coreclr/jit/assertionprop.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5291,7 +5291,7 @@ class AssertionPropFlowCallback
52915291
{
52925292
ASSERT_TP pAssertionOut;
52935293

5294-
if (predBlock->KindIs(BBJ_COND) && (predBlock->bbJumpDest == block))
5294+
if (predBlock->KindIs(BBJ_COND) && predBlock->HasJumpTo(block))
52955295
{
52965296
pAssertionOut = mJumpDestOut[predBlock->bbNum];
52975297

@@ -5493,7 +5493,7 @@ ASSERT_TP* Compiler::optComputeAssertionGen()
54935493
optPrintAssertionIndices(block->bbAssertionGen);
54945494
if (block->KindIs(BBJ_COND))
54955495
{
5496-
printf(" => " FMT_BB " valueGen = ", block->bbJumpDest->bbNum);
5496+
printf(" => " FMT_BB " valueGen = ", block->GetJumpDest()->bbNum);
54975497
optPrintAssertionIndices(jumpDestGen[block->bbNum]);
54985498
}
54995499
printf("\n");
@@ -6053,7 +6053,7 @@ PhaseStatus Compiler::optAssertionPropMain()
60536053
optDumpAssertionIndices(" out = ", block->bbAssertionOut, "\n");
60546054
if (block->KindIs(BBJ_COND))
60556055
{
6056-
printf(" " FMT_BB " = ", block->bbJumpDest->bbNum);
6056+
printf(" " FMT_BB " = ", block->GetJumpDest()->bbNum);
60576057
optDumpAssertionIndices(bbJtrueAssertionOut[block->bbNum], "\n");
60586058
}
60596059
}

src/coreclr/jit/block.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,7 @@ BasicBlock* Compiler::bbNewBasicBlock(BBjumpKinds jumpKind)
14491449

14501450
/* Record the jump kind in the block */
14511451

1452-
block->SetBBJumpKind(jumpKind DEBUG_ARG(this));
1452+
block->SetJumpKind(jumpKind DEBUG_ARG(this));
14531453

14541454
if (jumpKind == BBJ_THROW)
14551455
{

src/coreclr/jit/block.h

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -514,21 +514,28 @@ struct BasicBlock : private LIR::Range
514514

515515
BBjumpKinds bbJumpKind; // jump (if any) at the end of this block
516516

517+
/* The following union describes the jump target(s) of this block */
518+
union {
519+
unsigned bbJumpOffs; // PC offset (temporary only)
520+
BasicBlock* bbJumpDest; // basic block
521+
BBswtDesc* bbJumpSwt; // switch descriptor
522+
};
523+
517524
public:
518-
BBjumpKinds GetBBJumpKind() const
525+
BBjumpKinds GetJumpKind() const
519526
{
520527
return bbJumpKind;
521528
}
522529

523-
void SetBBJumpKind(BBjumpKinds kind DEBUG_ARG(Compiler* compiler))
530+
void SetJumpKind(BBjumpKinds jumpKind DEBUG_ARG(Compiler* compiler))
524531
{
525532
#ifdef DEBUG
526533
// BBJ_NONE should only be assigned when optimizing jumps in Compiler::optOptimizeLayout
527534
// TODO: Change assert to check if compiler is in appropriate optimization phase to use BBJ_NONE
528535
// (right now, this assertion does the null check to avoid unused variable warnings)
529-
assert((kind != BBJ_NONE) || (compiler != nullptr));
536+
assert((jumpKind != BBJ_NONE) || (compiler != nullptr));
530537
#endif // DEBUG
531-
bbJumpKind = kind;
538+
bbJumpKind = jumpKind;
532539
}
533540

534541
BasicBlock* Prev() const
@@ -569,12 +576,12 @@ struct BasicBlock : private LIR::Range
569576
return (bbNext == nullptr);
570577
}
571578

572-
bool PrevIs(BasicBlock* block) const
579+
bool PrevIs(const BasicBlock* block) const
573580
{
574581
return (bbPrev == block);
575582
}
576583

577-
bool NextIs(BasicBlock* block) const
584+
bool NextIs(const BasicBlock* block) const
578585
{
579586
return (bbNext == block);
580587
}
@@ -583,12 +590,61 @@ struct BasicBlock : private LIR::Range
583590

584591
bool IsFirstColdBlock(Compiler* compiler) const;
585592

586-
/* The following union describes the jump target(s) of this block */
587-
union {
588-
unsigned bbJumpOffs; // PC offset (temporary only)
589-
BasicBlock* bbJumpDest; // basic block
590-
BBswtDesc* bbJumpSwt; // switch descriptor
591-
};
593+
unsigned GetJumpOffs() const
594+
{
595+
return bbJumpOffs;
596+
}
597+
598+
void SetJumpOffs(unsigned jumpOffs)
599+
{
600+
bbJumpOffs = jumpOffs;
601+
}
602+
603+
BasicBlock* GetJumpDest() const
604+
{
605+
return bbJumpDest;
606+
}
607+
608+
void SetJumpDest(BasicBlock* jumpDest)
609+
{
610+
bbJumpDest = jumpDest;
611+
}
612+
613+
void SetJumpKindAndTarget(BBjumpKinds jumpKind, BasicBlock* jumpDest)
614+
{
615+
assert(jumpDest != nullptr);
616+
bbJumpKind = jumpKind;
617+
bbJumpDest = jumpDest;
618+
assert(KindIs(BBJ_ALWAYS, BBJ_CALLFINALLY, BBJ_COND, BBJ_EHCATCHRET, BBJ_LEAVE));
619+
}
620+
621+
bool HasJumpTo(const BasicBlock* jumpDest) const
622+
{
623+
return (bbJumpDest == jumpDest);
624+
}
625+
626+
bool JumpsToNext() const
627+
{
628+
return (bbJumpDest == bbNext);
629+
}
630+
631+
BBswtDesc* GetJumpSwt() const
632+
{
633+
return bbJumpSwt;
634+
}
635+
636+
void SetJumpSwt(BBswtDesc* jumpSwt)
637+
{
638+
bbJumpSwt = jumpSwt;
639+
}
640+
641+
void SetJumpKindAndTarget(BBjumpKinds jumpKind, BBswtDesc* jumpSwt)
642+
{
643+
assert(jumpKind == BBJ_SWITCH);
644+
assert(jumpSwt != nullptr);
645+
bbJumpKind = jumpKind;
646+
bbJumpSwt = jumpSwt;
647+
}
592648

593649
BasicBlockFlags bbFlags;
594650

@@ -1617,7 +1673,7 @@ inline BBArrayIterator BBSwitchTargetList::end() const
16171673
inline BasicBlock::BBSuccList::BBSuccList(const BasicBlock* block)
16181674
{
16191675
assert(block != nullptr);
1620-
switch (block->GetBBJumpKind())
1676+
switch (block->GetJumpKind())
16211677
{
16221678
case BBJ_THROW:
16231679
case BBJ_RETURN:
@@ -1633,7 +1689,7 @@ inline BasicBlock::BBSuccList::BBSuccList(const BasicBlock* block)
16331689
case BBJ_ALWAYS:
16341690
case BBJ_EHCATCHRET:
16351691
case BBJ_LEAVE:
1636-
m_succs[0] = block->bbJumpDest;
1692+
m_succs[0] = block->GetJumpDest();
16371693
m_begin = &m_succs[0];
16381694
m_end = &m_succs[1];
16391695
break;
@@ -1650,23 +1706,23 @@ inline BasicBlock::BBSuccList::BBSuccList(const BasicBlock* block)
16501706

16511707
// If both fall-through and branch successors are identical, then only include
16521708
// them once in the iteration (this is the same behavior as NumSucc()/GetSucc()).
1653-
if (block->NextIs(block->bbJumpDest))
1709+
if (block->JumpsToNext())
16541710
{
16551711
m_end = &m_succs[1];
16561712
}
16571713
else
16581714
{
1659-
m_succs[1] = block->bbJumpDest;
1715+
m_succs[1] = block->GetJumpDest();
16601716
m_end = &m_succs[2];
16611717
}
16621718
break;
16631719

16641720
case BBJ_SWITCH:
16651721
// We don't use the m_succs in-line data for switches; use the existing jump table in the block.
1666-
assert(block->bbJumpSwt != nullptr);
1667-
assert(block->bbJumpSwt->bbsDstTab != nullptr);
1668-
m_begin = block->bbJumpSwt->bbsDstTab;
1669-
m_end = block->bbJumpSwt->bbsDstTab + block->bbJumpSwt->bbsCount;
1722+
assert(block->GetJumpSwt() != nullptr);
1723+
assert(block->GetJumpSwt()->bbsDstTab != nullptr);
1724+
m_begin = block->GetJumpSwt()->bbsDstTab;
1725+
m_end = block->GetJumpSwt()->bbsDstTab + block->GetJumpSwt()->bbsCount;
16701726
break;
16711727

16721728
default:

src/coreclr/jit/codegenarm.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,17 @@ BasicBlock* CodeGen::genCallFinally(BasicBlock* block)
125125

126126
assert(!block->IsLast());
127127
assert(block->Next()->KindIs(BBJ_ALWAYS));
128-
assert(block->Next()->bbJumpDest != NULL);
129-
assert(block->Next()->bbJumpDest->bbFlags & BBF_FINALLY_TARGET);
128+
assert(!block->Next()->HasJumpTo(nullptr));
129+
assert(block->Next()->GetJumpDest()->bbFlags & BBF_FINALLY_TARGET);
130130

131-
bbFinallyRet = block->Next()->bbJumpDest;
131+
bbFinallyRet = block->Next()->GetJumpDest();
132132

133133
// Load the address where the finally funclet should return into LR.
134134
// The funclet prolog/epilog will do "push {lr}" / "pop {pc}" to do the return.
135135
genMov32RelocatableDisplacement(bbFinallyRet, REG_LR);
136136

137137
// Jump to the finally BB
138-
inst_JMP(EJ_jmp, block->bbJumpDest);
138+
inst_JMP(EJ_jmp, block->GetJumpDest());
139139

140140
// The BBJ_ALWAYS is used because the BBJ_CALLFINALLY can't point to the
141141
// jump target using bbJumpDest - that is already used to point
@@ -150,7 +150,7 @@ BasicBlock* CodeGen::genCallFinally(BasicBlock* block)
150150
// genEHCatchRet:
151151
void CodeGen::genEHCatchRet(BasicBlock* block)
152152
{
153-
genMov32RelocatableDisplacement(block->bbJumpDest, REG_INTRET);
153+
genMov32RelocatableDisplacement(block->GetJumpDest(), REG_INTRET);
154154
}
155155

156156
//------------------------------------------------------------------------
@@ -633,8 +633,8 @@ void CodeGen::genJumpTable(GenTree* treeNode)
633633
noway_assert(compiler->compCurBB->KindIs(BBJ_SWITCH));
634634
assert(treeNode->OperGet() == GT_JMPTABLE);
635635

636-
unsigned jumpCount = compiler->compCurBB->bbJumpSwt->bbsCount;
637-
BasicBlock** jumpTable = compiler->compCurBB->bbJumpSwt->bbsDstTab;
636+
unsigned jumpCount = compiler->compCurBB->GetJumpSwt()->bbsCount;
637+
BasicBlock** jumpTable = compiler->compCurBB->GetJumpSwt()->bbsDstTab;
638638
unsigned jmpTabBase;
639639

640640
jmpTabBase = GetEmitter()->emitBBTableDataGenBeg(jumpCount, false);
@@ -1299,7 +1299,7 @@ void CodeGen::genCodeForJTrue(GenTreeOp* jtrue)
12991299
GenTree* op = jtrue->gtGetOp1();
13001300
regNumber reg = genConsumeReg(op);
13011301
inst_RV_RV(INS_tst, reg, reg, genActualType(op));
1302-
inst_JMP(EJ_ne, compiler->compCurBB->bbJumpDest);
1302+
inst_JMP(EJ_ne, compiler->compCurBB->GetJumpDest());
13031303
}
13041304

13051305
//------------------------------------------------------------------------

src/coreclr/jit/codegenarm64.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,7 @@ BasicBlock* CodeGen::genCallFinally(BasicBlock* block)
21582158
{
21592159
GetEmitter()->emitIns_Mov(INS_mov, EA_PTRSIZE, REG_R0, REG_SPBASE, /* canSkip */ false);
21602160
}
2161-
GetEmitter()->emitIns_J(INS_bl_local, block->bbJumpDest);
2161+
GetEmitter()->emitIns_J(INS_bl_local, block->GetJumpDest());
21622162

21632163
BasicBlock* const nextBlock = block->Next();
21642164

@@ -2181,7 +2181,7 @@ BasicBlock* CodeGen::genCallFinally(BasicBlock* block)
21812181
// handler. So turn off GC reporting for this single instruction.
21822182
GetEmitter()->emitDisableGC();
21832183

2184-
BasicBlock* const jumpDest = nextBlock->bbJumpDest;
2184+
BasicBlock* const jumpDest = nextBlock->GetJumpDest();
21852185

21862186
// Now go to where the finally funclet needs to return to.
21872187
if (nextBlock->NextIs(jumpDest) && !compiler->fgInDifferentRegions(nextBlock, jumpDest))
@@ -2216,7 +2216,7 @@ void CodeGen::genEHCatchRet(BasicBlock* block)
22162216
{
22172217
// For long address (default): `adrp + add` will be emitted.
22182218
// For short address (proven later): `adr` will be emitted.
2219-
GetEmitter()->emitIns_R_L(INS_adr, EA_PTRSIZE, block->bbJumpDest, REG_INTRET);
2219+
GetEmitter()->emitIns_R_L(INS_adr, EA_PTRSIZE, block->GetJumpDest(), REG_INTRET);
22202220
}
22212221

22222222
// move an immediate value into an integer register
@@ -3752,8 +3752,8 @@ void CodeGen::genJumpTable(GenTree* treeNode)
37523752
noway_assert(compiler->compCurBB->KindIs(BBJ_SWITCH));
37533753
assert(treeNode->OperGet() == GT_JMPTABLE);
37543754

3755-
unsigned jumpCount = compiler->compCurBB->bbJumpSwt->bbsCount;
3756-
BasicBlock** jumpTable = compiler->compCurBB->bbJumpSwt->bbsDstTab;
3755+
unsigned jumpCount = compiler->compCurBB->GetJumpSwt()->bbsCount;
3756+
BasicBlock** jumpTable = compiler->compCurBB->GetJumpSwt()->bbsDstTab;
37573757
unsigned jmpTabOffs;
37583758
unsigned jmpTabBase;
37593759

@@ -4654,7 +4654,7 @@ void CodeGen::genCodeForJTrue(GenTreeOp* jtrue)
46544654

46554655
GenTree* op = jtrue->gtGetOp1();
46564656
regNumber reg = genConsumeReg(op);
4657-
GetEmitter()->emitIns_J_R(INS_cbnz, emitActualTypeSize(op), compiler->compCurBB->bbJumpDest, reg);
4657+
GetEmitter()->emitIns_J_R(INS_cbnz, emitActualTypeSize(op), compiler->compCurBB->GetJumpDest(), reg);
46584658
}
46594659

46604660
//------------------------------------------------------------------------
@@ -4872,15 +4872,15 @@ void CodeGen::genCodeForJumpCompare(GenTreeOpCC* tree)
48724872
instruction ins = (cc.GetCode() == GenCondition::EQ) ? INS_tbz : INS_tbnz;
48734873
int imm = genLog2((size_t)compareImm);
48744874

4875-
GetEmitter()->emitIns_J_R_I(ins, attr, compiler->compCurBB->bbJumpDest, reg, imm);
4875+
GetEmitter()->emitIns_J_R_I(ins, attr, compiler->compCurBB->GetJumpDest(), reg, imm);
48764876
}
48774877
else
48784878
{
48794879
assert(op2->IsIntegralConst(0));
48804880

48814881
instruction ins = (cc.GetCode() == GenCondition::EQ) ? INS_cbz : INS_cbnz;
48824882

4883-
GetEmitter()->emitIns_J_R(ins, attr, compiler->compCurBB->bbJumpDest, reg);
4883+
GetEmitter()->emitIns_J_R(ins, attr, compiler->compCurBB->GetJumpDest(), reg);
48844884
}
48854885
}
48864886

src/coreclr/jit/codegencommon.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ void CodeGen::genMarkLabelsForCodegen()
376376

377377
for (BasicBlock* const block : compiler->Blocks())
378378
{
379-
switch (block->GetBBJumpKind())
379+
switch (block->GetJumpKind())
380380
{
381381
case BBJ_ALWAYS: // This will also handle the BBJ_ALWAYS of a BBJ_CALLFINALLY/BBJ_ALWAYS pair.
382382
case BBJ_COND:
383383
case BBJ_EHCATCHRET:
384-
JITDUMP(" " FMT_BB " : branch target\n", block->bbJumpDest->bbNum);
385-
block->bbJumpDest->bbFlags |= BBF_HAS_LABEL;
384+
JITDUMP(" " FMT_BB " : branch target\n", block->GetJumpDest()->bbNum);
385+
block->GetJumpDest()->bbFlags |= BBF_HAS_LABEL;
386386
break;
387387

388388
case BBJ_SWITCH:

src/coreclr/jit/codegenlinear.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ void CodeGen::genCodeForBBlist()
622622
{
623623
// We only need the NOP if we're not going to generate any more code as part of the block end.
624624

625-
switch (block->GetBBJumpKind())
625+
switch (block->GetJumpKind())
626626
{
627627
case BBJ_ALWAYS:
628628
case BBJ_THROW:
@@ -665,7 +665,7 @@ void CodeGen::genCodeForBBlist()
665665

666666
/* Do we need to generate a jump or return? */
667667

668-
switch (block->GetBBJumpKind())
668+
switch (block->GetJumpKind())
669669
{
670670
case BBJ_RETURN:
671671
genExitCode(block);
@@ -749,7 +749,7 @@ void CodeGen::genCodeForBBlist()
749749
// with a jump, do not remove jumps from such blocks.
750750
// Do not remove a jump between hot and cold regions.
751751
bool isRemovableJmpCandidate =
752-
!block->hasAlign() && !compiler->fgInDifferentRegions(block, block->bbJumpDest);
752+
!block->hasAlign() && !compiler->fgInDifferentRegions(block, block->GetJumpDest());
753753

754754
#ifdef TARGET_AMD64
755755
// AMD64 requires an instruction after a call instruction for unwinding
@@ -758,10 +758,10 @@ void CodeGen::genCodeForBBlist()
758758
isRemovableJmpCandidate = isRemovableJmpCandidate && !GetEmitter()->emitIsLastInsCall();
759759
#endif // TARGET_AMD64
760760

761-
inst_JMP(EJ_jmp, block->bbJumpDest, isRemovableJmpCandidate);
761+
inst_JMP(EJ_jmp, block->GetJumpDest(), isRemovableJmpCandidate);
762762
}
763763
#else
764-
inst_JMP(EJ_jmp, block->bbJumpDest);
764+
inst_JMP(EJ_jmp, block->GetJumpDest());
765765
#endif // TARGET_XARCH
766766

767767
FALLTHROUGH;
@@ -782,9 +782,9 @@ void CodeGen::genCodeForBBlist()
782782
// block, even if one is not otherwise needed, to be able to calculate the size of this
783783
// loop (loop size is calculated by walking the instruction groups; see emitter::getLoopSize()).
784784

785-
if (block->bbJumpDest->isLoopAlign())
785+
if (block->GetJumpDest()->isLoopAlign())
786786
{
787-
GetEmitter()->emitSetLoopBackEdge(block->bbJumpDest);
787+
GetEmitter()->emitSetLoopBackEdge(block->GetJumpDest());
788788

789789
if (!block->IsLast())
790790
{
@@ -2621,7 +2621,7 @@ void CodeGen::genCodeForJcc(GenTreeCC* jcc)
26212621
assert(compiler->compCurBB->KindIs(BBJ_COND));
26222622
assert(jcc->OperIs(GT_JCC));
26232623

2624-
inst_JCC(jcc->gtCondition, compiler->compCurBB->bbJumpDest);
2624+
inst_JCC(jcc->gtCondition, compiler->compCurBB->GetJumpDest());
26252625
}
26262626

26272627
//------------------------------------------------------------------------

0 commit comments

Comments
 (0)