Index: lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp +++ lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp @@ -158,6 +158,7 @@ BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) .addImm(StackSize); auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer"); + // TODO: Fold this add into the const offset field of the store. BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32) .addReg(WebAssembly::SP32) .addReg(OffsetReg); Index: lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp +++ lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp @@ -81,15 +81,18 @@ auto &MRI = MF.getRegInfo(); const auto *TII = MF.getSubtarget().getInstrInfo(); - unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); - BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(WebAssembly::CONST_I32), - OffsetReg) - .addImm(FrameOffset); - BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(WebAssembly::ADD_I32), - OffsetReg) - .addReg(WebAssembly::SP32) - .addReg(OffsetReg); - MI.getOperand(FIOperandNum).ChangeToRegister(OffsetReg, /*IsDef=*/false); + unsigned FIRegOperand = WebAssembly::SP32; + if (FrameOffset) { + FIRegOperand = MRI.createVirtualRegister(&WebAssembly::I32RegClass); + BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(WebAssembly::CONST_I32), + FIRegOperand) + .addImm(FrameOffset); + BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(WebAssembly::ADD_I32), + FIRegOperand) + .addReg(WebAssembly::SP32) + .addReg(FIRegOperand); + } + MI.getOperand(FIOperandNum).ChangeToRegister(FIRegOperand, /*IsDef=*/false); } } Index: test/CodeGen/WebAssembly/userstack.ll =================================================================== --- test/CodeGen/WebAssembly/userstack.ll +++ test/CodeGen/WebAssembly/userstack.ll @@ -76,6 +76,24 @@ ret void } +declare void @ext_func(i64* %ptr) +; CHECK-LABEL: non_mem_use +define void @non_mem_use() { + ; CHECK: i32.const [[L2:.+]]=, 16 + ; CHECK-NEXT: i32.sub [[SP:.+]]=, {{.+}}, [[L2]] + %r = alloca i64 + %r2 = alloca i64 + ; %r is at SP+8 + ; CHECK: i32.const [[OFF:.+]]=, 8 + ; CHECK-NEXT: i32.add [[ARG1:.+]]=, [[SP]], [[OFF]] + ; CHECK-NEXT: call ext_func@FUNCTION, [[ARG1]] + call void @ext_func(i64* %r) + ; %r2 is at SP+0, no add needed + ; CHECK-NEXT: call ext_func@FUNCTION, [[SP]] + call void @ext_func(i64* %r2) + ret void +} + ; CHECK-LABEL: allocarray_inbounds: ; CHECK: .local i32, i32, i32, i32{{$}} define void @allocarray_inbounds() {