diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.def b/llvm/include/llvm/IR/RuntimeLibcalls.def --- a/llvm/include/llvm/IR/RuntimeLibcalls.def +++ b/llvm/include/llvm/IR/RuntimeLibcalls.def @@ -539,6 +539,9 @@ // Deoptimization HANDLE_LIBCALL(DEOPTIMIZE, "__llvm_deoptimize") +// Return address +HANDLE_LIBCALL(RETURN_ADDRESS, nullptr) + HANDLE_LIBCALL(UNKNOWN_LIBCALL, nullptr) #undef HANDLE_LIBCALL diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h --- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h +++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h @@ -88,6 +88,7 @@ // Custom lowering hooks. SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override; SDValue LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const; + SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const; SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const; SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const; SDValue LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -273,6 +273,9 @@ setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2"); setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2"); + // Define the emscripten name for return address helper. + setLibcallName(RTLIB::RETURN_ADDRESS, "emscripten_return_address"); + // Always convert switches to br_tables unless there is only one case, which // is equivalent to a simple branch. This reduces code size for wasm, and we // defer possible jump table optimizations to the VM. @@ -919,9 +922,8 @@ case ISD::BRIND: fail(DL, DAG, "WebAssembly hasn't implemented computed gotos"); return SDValue(); - case ISD::RETURNADDR: // Probably nothing meaningful can be returned here. - fail(DL, DAG, "WebAssembly hasn't implemented __builtin_return_address"); - return SDValue(); + case ISD::RETURNADDR: + return LowerRETURNADDR(Op, DAG); case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); case ISD::CopyToReg: @@ -978,6 +980,24 @@ return DAG.getTargetFrameIndex(FI, Op.getValueType()); } +SDValue WebAssemblyTargetLowering::LowerRETURNADDR(SDValue Op, + SelectionDAG &DAG) const { + SDLoc DL(Op); + + if (!Subtarget->getTargetTriple().isOSEmscripten()) { + fail(DL, DAG, "Non-Emscripten WebAssembly hasn't implemented " + "__builtin_return_address"); + return SDValue(); + } + + if (verifyReturnAddressArgumentIsConstant(Op, DAG)) + return SDValue(); + + unsigned Depth = cast(Op.getOperand(0))->getZExtValue(); + return makeLibCall(DAG, RTLIB::RETURN_ADDRESS, Op.getValueType(), + {DAG.getConstant(Depth, DL, MVT::i32)}, false, DL).first; +} + SDValue WebAssemblyTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { // Non-zero depths are not supported by WebAssembly currently. Use the diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp @@ -463,6 +463,9 @@ Table[RTLIB::ATOMIC_FETCH_NAND_4] = unsupported; Table[RTLIB::ATOMIC_FETCH_NAND_8] = unsupported; Table[RTLIB::ATOMIC_FETCH_NAND_16] = unsupported; + + // Return address handling + Table[RTLIB::RETURN_ADDRESS] = i32_func_i32; } }; @@ -490,6 +493,7 @@ // consistent with the f64 and f128 names. Map["__extendhfsf2"] = RTLIB::FPEXT_F16_F32; Map["__truncsfhf2"] = RTLIB::FPROUND_F32_F16; + Map["emscripten_return_address"] = RTLIB::RETURN_ADDRESS; } }; diff --git a/llvm/test/CodeGen/WebAssembly/return-address.ll b/llvm/test/CodeGen/WebAssembly/return-address.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/WebAssembly/return-address.ll @@ -0,0 +1,16 @@ +; RUN: llc < %s -asm-verbose=false | FileCheck %s + +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-emscripten" + +; CHECK-LABEL: return_test: +; CHECK-NEXT: .functype return_test () -> (i32){{$}} +; CHECK-NEXT: {{^}} i32.const 0{{$}} +; CHECK-NEXT: {{^}} i32.call emscripten_return_address{{$}} +; CHECK-NEXT: {{^}} end_function{{$}} +define i8* @return_test() { + %r = call i8* @llvm.returnaddress(i32 0) + ret i8* %r +} + +declare i8* @llvm.returnaddress(i32 immarg)