Skip to content

Commit

Permalink
[RISC-V] Opt emitLoadImmediate (#109062)
Browse files Browse the repository at this point in the history
Skip `slli` and `add` if `low12` is 0 for the iterations.
  • Loading branch information
clamp03 authored Oct 28, 2024
1 parent 6bc04bf commit f1ad214
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/coreclr/jit/emitriscv64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,19 +1258,34 @@ void emitter::emitLoadImmediate(emitAttr size, regNumber reg, ssize_t imm)
INT32 high19 = ((int32_t)(high31 + 0x800)) >> 12;

emitIns_R_I(INS_lui, size, reg, high19);
emitIns_R_R_I(INS_addiw, size, reg, reg, high31 & 0xFFF);
if (high31 & 0xFFF)
{
emitIns_R_R_I(INS_addiw, size, reg, reg, high31 & 0xFFF);
}

// And load remaining part part by batches of 11 bits size.
INT32 remainingShift = msb - 30;

UINT32 shiftAccumulator = 0;
while (remainingShift > 0)
{
UINT32 shift = remainingShift >= 11 ? 11 : remainingShift % 11;
emitIns_R_R_I(INS_slli, size, reg, reg, shift);
UINT32 mask = 0x7ff >> (11 - shift);
remainingShift -= shift;
ssize_t low11 = (imm >> remainingShift) & mask;
shiftAccumulator += shift;

if (low11)
{
emitIns_R_R_I(INS_slli, size, reg, reg, shiftAccumulator);
shiftAccumulator = 0;

UINT32 mask = 0x7ff >> (11 - shift);
ssize_t low11 = (imm >> (remainingShift - shift)) & mask;
emitIns_R_R_I(INS_addi, size, reg, reg, low11);
remainingShift = remainingShift - shift;
emitIns_R_R_I(INS_addi, size, reg, reg, low11);
}
}
if (shiftAccumulator)
{
emitIns_R_R_I(INS_slli, size, reg, reg, shiftAccumulator);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/vm/riscv64/stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,10 @@ void StubLinkerCPU::EmitMovConstant(IntReg reg, UINT64 imm)

EmitLuImm(reg, high19);
int low12 = int(high31) << (32-12) >> (32-12);
EmitAddImm(reg, reg, low12);
if (low12)
{
EmitAddImm(reg, reg, low12);
}

// And load remaining part by batches of 11 bits size.
INT32 remainingShift = msb - 30;
Expand Down

0 comments on commit f1ad214

Please sign in to comment.