From 0fc78e62bb0b8d824efe7421983702b97a60def6 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Sat, 8 Apr 2023 14:16:19 -0700 Subject: [PATCH] Perform ldr to ldp peephole optimization (#84399) * Perform ldr to ldp peephole optimization * jit format * handle non-gc str cases * Add the comment --- src/coreclr/jit/emitarm64.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/coreclr/jit/emitarm64.h b/src/coreclr/jit/emitarm64.h index 7c8fac3e772c3..ee0e029c84b68 100644 --- a/src/coreclr/jit/emitarm64.h +++ b/src/coreclr/jit/emitarm64.h @@ -146,7 +146,7 @@ inline bool OptimizeLdrStr(instruction ins, { assert(ins == INS_ldr || ins == INS_str); - if (!emitCanPeepholeLastIns()) + if (!emitCanPeepholeLastIns() || (emitLastIns->idIns() != ins)) { return false; } @@ -161,9 +161,21 @@ inline bool OptimizeLdrStr(instruction ins, reg2 = encodingZRtoSP(reg2); // If the previous instruction was a matching load/store, then try to replace it instead of emitting. - // Don't do this if either instruction had a local variable. - if ((emitLastIns->idIns() == ins) && !localVar && !emitLastIns->idIsLclVar() && - ReplaceLdrStrWithPairInstr(ins, reg1Attr, reg1, reg2, imm, size, fmt)) + // + bool canReplaceWithPair = true; + if (ins == INS_str) + { + // For INS_str, don't do this if either instruction had a local GC variable. + // For INS_ldr, it is fine to perform this optimization because the output code already handles the code of + // updating the gc refs. We do not need offset tracking for load cases. + if ((localVar && EA_IS_GCREF_OR_BYREF(reg1Attr)) || + (emitLastIns->idIsLclVar() && (emitLastIns->idGCref() != GCT_NONE))) + { + canReplaceWithPair = false; + } + } + + if (canReplaceWithPair && ReplaceLdrStrWithPairInstr(ins, reg1Attr, reg1, reg2, imm, size, fmt)) { return true; }