@@ -1364,6 +1364,7 @@ void CodeGen::genLclHeap(GenTree* tree)
1364
1364
regNumber targetReg = tree->GetRegNum ();
1365
1365
regNumber regCnt = REG_NA;
1366
1366
regNumber tempReg = REG_NA;
1367
+ regNumber spSourceReg = REG_SPBASE;
1367
1368
var_types type = genActualType (size->gtType );
1368
1369
emitAttr easz = emitTypeSize (type);
1369
1370
BasicBlock* endLabel = nullptr ; // can optimize for riscv64.
@@ -1567,11 +1568,10 @@ void CodeGen::genLclHeap(GenTree* tree)
1567
1568
tempReg = internalRegisters.Extract (tree);
1568
1569
1569
1570
assert (regCnt != tempReg);
1570
- // regCnt now holds the number of bytes to localloc, after the sequence it will be set to the ultimate SP
1571
1571
if (compiler->compOpportunisticallyDependsOn (InstructionSet_Zbb))
1572
1572
{
1573
- emit->emitIns_R_R_R (INS_maxu, EA_PTRSIZE, tempReg, REG_SPBASE, regCnt); // temp = max(sp, cnt);
1574
- emit->emitIns_R_R_R (INS_sub, EA_PTRSIZE, regCnt, tempReg, regCnt); // cnt = temp - cnt;
1573
+ emit->emitIns_R_R_R (INS_maxu, EA_PTRSIZE, tempReg, REG_SPBASE, regCnt);
1574
+ emit->emitIns_R_R_R (INS_sub, EA_PTRSIZE, regCnt, tempReg, regCnt);
1575
1575
}
1576
1576
else
1577
1577
{
@@ -1581,8 +1581,9 @@ void CodeGen::genLclHeap(GenTree* tree)
1581
1581
emit->emitIns_R_R_I (INS_addi, EA_PTRSIZE, tempReg, tempReg, -1 ); // temp = overflow ? 0 : full_mask;
1582
1582
emit->emitIns_R_R_R (INS_and, EA_PTRSIZE, regCnt, regCnt, tempReg); // cnt = overflow ? 0 : cnt;
1583
1583
}
1584
- regNumber rPageSize = internalRegisters. GetSingle (tree);
1584
+ // At this point 'regCnt' is set to the ultimate SP.
1585
1585
1586
+ regNumber rPageSize = internalRegisters.GetSingle (tree);
1586
1587
noway_assert (rPageSize != tempReg);
1587
1588
1588
1589
emit->emitIns_R_I (INS_lui, EA_PTRSIZE, rPageSize, pageSize >> 12 );
@@ -1599,6 +1600,7 @@ void CodeGen::genLclHeap(GenTree* tree)
1599
1600
// we're going to assume the worst and probe.
1600
1601
// Move the final value to SP
1601
1602
emit->emitIns_R_R (INS_mov, EA_PTRSIZE, REG_SPBASE, regCnt);
1603
+ spSourceReg = regCnt; // regCnt may be same as targetReg which gives advantage in returning the address below
1602
1604
}
1603
1605
1604
1606
ALLOC_DONE:
@@ -1621,13 +1623,12 @@ void CodeGen::genLclHeap(GenTree* tree)
1621
1623
// Return the stackalloc'ed address in result register.
1622
1624
// TargetReg = SP + stackAdjustment.
1623
1625
//
1624
- genInstrWithConstant (INS_addi, EA_PTRSIZE, targetReg, REG_SPBASE , (ssize_t )stackAdjustment, tempReg);
1626
+ genInstrWithConstant (INS_addi, EA_PTRSIZE, targetReg, spSourceReg , (ssize_t )stackAdjustment, tempReg);
1625
1627
}
1626
1628
else // stackAdjustment == 0
1627
1629
{
1628
- // Move the final value of SP to targetReg (if necessary; targetReg may be same as regCnt which also holds SP)
1629
- regNumber spSrc = (regCnt == REG_NA) ? REG_SPBASE : regCnt;
1630
- emit->emitIns_Mov (EA_PTRSIZE, targetReg, spSrc, true );
1630
+ // Move the final value of SP to targetReg
1631
+ emit->emitIns_Mov (EA_PTRSIZE, targetReg, spSourceReg, true );
1631
1632
}
1632
1633
1633
1634
BAILOUT:
0 commit comments