@@ -2715,7 +2715,11 @@ void CodeGen::genCodeForLclAddr(GenTreeLclFld* lclAddrNode)
27152715 unsigned lclOffset = lclAddrNode->GetLclOffs ();
27162716
27172717 GetEmitter ()->emitIns_I (INS_local_get, EA_PTRSIZE , GetFramePointerRegIndex ());
2718- if ((lclOffset != 0 ) || (m_compiler->lvaFrameAddress (lclNum, &FPBased) != 0 ))
2718+
2719+ // A folded address contributes its frame offset to the user's memarg instead.
2720+ //
2721+ if (((lclAddrNode->gtLIRFlags & LIR ::Flags::FoldedAddr) == LIR ::Flags::None) &&
2722+ ((lclOffset != 0 ) || (m_compiler->lvaFrameAddress (lclNum, &FPBased) != 0 )))
27192723 {
27202724 GetEmitter ()->emitIns_S (INS_I_const, EA_PTRSIZE , lclNum, lclOffset);
27212725 GetEmitter ()->emitIns (INS_I_add);
@@ -2935,6 +2939,35 @@ void CodeGen::genStoreIndTypeSimd12(GenTreeStoreInd* tree)
29352939 }
29362940}
29372941
2942+ // ------------------------------------------------------------------------
2943+ // genWasmMemargOffset: Get the offset an indirection folds into its memarg.
2944+ //
2945+ // Arguments:
2946+ // addr - the address node of the indirection
2947+ //
2948+ // Return Value:
2949+ // The offset to encode in the memarg, or zero if the address carries no folded offset.
2950+ //
2951+ cnsval_ssize_t CodeGen::genWasmMemargOffset (GenTree* addr)
2952+ {
2953+ if (addr->isContained () && addr->OperIs (GT_LEA ))
2954+ {
2955+ return addr->AsAddrMode ()->Offset ();
2956+ }
2957+
2958+ if (addr->OperIs (GT_LCL_ADDR ) && ((addr->gtLIRFlags & LIR ::Flags::FoldedAddr) != LIR ::Flags::None))
2959+ {
2960+ bool FPBased;
2961+ const int offset = m_compiler->lvaFrameAddress (addr->AsLclFld ()->GetLclNum (), &FPBased) +
2962+ static_cast <int >(addr->AsLclFld ()->GetLclOffs ());
2963+ noway_assert (offset >= 0 ); // WASM address modes are unsigned.
2964+ assert (FPBased);
2965+ return offset;
2966+ }
2967+
2968+ return 0 ;
2969+ }
2970+
29382971// ------------------------------------------------------------------------
29392972// genCodeForIndir: Produce code for a GT_IND node.
29402973//
@@ -2959,13 +2992,7 @@ void CodeGen::genCodeForIndir(GenTreeIndir* tree)
29592992
29602993 // TODO-WASM: Memory barriers
29612994
2962- if (addr->isContained () && addr->OperIs (GT_LEA ))
2963- {
2964- // The contained address mode's offset folds into the memarg; its base is already pushed.
2965- //
2966- GetEmitter ()->emitIns_I (ins_Load (type), emitActualTypeSize (type), addr->AsAddrMode ()->Offset ());
2967- }
2968- else if (addr->isContained ())
2995+ if (addr->isContained () && !addr->OperIs (GT_LEA ))
29692996 {
29702997 // A contained address constant folds into the memarg offset, so emit just the image base here.
29712998 //
@@ -2981,7 +3008,9 @@ void CodeGen::genCodeForIndir(GenTreeIndir* tree)
29813008 }
29823009 else
29833010 {
2984- GetEmitter ()->emitIns_I (ins_Load (type), emitActualTypeSize (type), 0 );
3011+ // A contained address mode's offset or a folded local's frame offset supplies the memarg.
3012+ //
3013+ GetEmitter ()->emitIns_I (ins_Load (type), emitActualTypeSize (type), genWasmMemargOffset (addr));
29853014 }
29863015
29873016 WasmProduceReg (tree);
@@ -2998,10 +3027,10 @@ void CodeGen::genCodeForStoreInd(GenTreeStoreInd* tree)
29983027 GenTree* data = tree->Data ();
29993028 GenTree* addr = tree->Addr ();
30003029
3001- // Only a contained address mode is expected; its offset folds into the memarg below .
3030+ // A contained address mode's offset or a folded local's frame offset supplies the memarg.
30023031 //
30033032 assert (!addr->isContained () || addr->OperIs (GT_LEA ));
3004- const cnsval_ssize_t offset = addr-> isContained () ? addr-> AsAddrMode ()-> Offset () : 0 ;
3033+ const cnsval_ssize_t offset = genWasmMemargOffset ( addr) ;
30053034
30063035 // We must consume the operands in the proper execution order,
30073036 // so that liveness is updated appropriately.
0 commit comments