Skip to content

Commit 6c2df8e

Browse files
committed
Fix returning stackalloc'ed address
1 parent 13bc555 commit 6c2df8e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/coreclr/jit/codegenriscv64.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,7 @@ void CodeGen::genLclHeap(GenTree* tree)
13641364
regNumber targetReg = tree->GetRegNum();
13651365
regNumber regCnt = REG_NA;
13661366
regNumber tempReg = REG_NA;
1367+
regNumber spSourceReg = REG_SPBASE;
13671368
var_types type = genActualType(size->gtType);
13681369
emitAttr easz = emitTypeSize(type);
13691370
BasicBlock* endLabel = nullptr; // can optimize for riscv64.
@@ -1567,11 +1568,10 @@ void CodeGen::genLclHeap(GenTree* tree)
15671568
tempReg = internalRegisters.Extract(tree);
15681569

15691570
assert(regCnt != tempReg);
1570-
// regCnt now holds the number of bytes to localloc, after the sequence it will be set to the ultimate SP
15711571
if (compiler->compOpportunisticallyDependsOn(InstructionSet_Zbb))
15721572
{
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);
15751575
}
15761576
else
15771577
{
@@ -1581,8 +1581,9 @@ void CodeGen::genLclHeap(GenTree* tree)
15811581
emit->emitIns_R_R_I(INS_addi, EA_PTRSIZE, tempReg, tempReg, -1); // temp = overflow ? 0 : full_mask;
15821582
emit->emitIns_R_R_R(INS_and, EA_PTRSIZE, regCnt, regCnt, tempReg); // cnt = overflow ? 0 : cnt;
15831583
}
1584-
regNumber rPageSize = internalRegisters.GetSingle(tree);
1584+
// At this point 'regCnt' is set to the ultimate SP.
15851585

1586+
regNumber rPageSize = internalRegisters.GetSingle(tree);
15861587
noway_assert(rPageSize != tempReg);
15871588

15881589
emit->emitIns_R_I(INS_lui, EA_PTRSIZE, rPageSize, pageSize >> 12);
@@ -1599,6 +1600,7 @@ void CodeGen::genLclHeap(GenTree* tree)
15991600
// we're going to assume the worst and probe.
16001601
// Move the final value to SP
16011602
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
16021604
}
16031605

16041606
ALLOC_DONE:
@@ -1621,13 +1623,12 @@ void CodeGen::genLclHeap(GenTree* tree)
16211623
// Return the stackalloc'ed address in result register.
16221624
// TargetReg = SP + stackAdjustment.
16231625
//
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);
16251627
}
16261628
else // stackAdjustment == 0
16271629
{
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);
16311632
}
16321633

16331634
BAILOUT:

0 commit comments

Comments
 (0)