Index: llvm/trunk/include/llvm/IR/RuntimeLibcalls.def =================================================================== --- llvm/trunk/include/llvm/IR/RuntimeLibcalls.def +++ llvm/trunk/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 Index: llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.h =================================================================== --- llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.h +++ llvm/trunk/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; Index: llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -273,6 +273,11 @@ setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2"); setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2"); + // Define the emscripten name for return address helper. + // TODO: when implementing other WASM backends, make this generic or only do + // this on emscripten depending on what they end up doing. + 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 +924,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 +982,26 @@ 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 Index: llvm/trunk/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp +++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp @@ -325,6 +325,9 @@ // __stack_chk_fail Table[RTLIB::STACKPROTECTOR_CHECK_FAIL] = func; + // Return address handling + Table[RTLIB::RETURN_ADDRESS] = i32_func_i32; + // Element-wise Atomic memory // TODO: Fix these when we implement atomic support Table[RTLIB::MEMCPY_ELEMENT_UNORDERED_ATOMIC_1] = unsupported; @@ -499,6 +502,8 @@ // 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; } }; Index: llvm/trunk/test/CodeGen/WebAssembly/return-address-emscripten.ll =================================================================== --- llvm/trunk/test/CodeGen/WebAssembly/return-address-emscripten.ll +++ llvm/trunk/test/CodeGen/WebAssembly/return-address-emscripten.ll @@ -0,0 +1,19 @@ +; 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" + +; This tests the implementation of __builtin_return_address on emscripten + +; CHECK-LABEL: test_returnaddress: +; CHECK-NEXT: .functype test_returnaddress () -> (i32){{$}} +; CHECK-NEXT: {{^}} i32.const 0{{$}} +; CHECK-NEXT: {{^}} i32.call emscripten_return_address{{$}} +; CHECK-NEXT: {{^}} end_function{{$}} +define i8* @test_returnaddress() { + %r = call i8* @llvm.returnaddress(i32 0) + ret i8* %r +} + +; LLVM represents __builtin_return_address as call to this function in IR. +declare i8* @llvm.returnaddress(i32 immarg) Index: llvm/trunk/test/CodeGen/WebAssembly/return-address-unknown.ll =================================================================== --- llvm/trunk/test/CodeGen/WebAssembly/return-address-unknown.ll +++ llvm/trunk/test/CodeGen/WebAssembly/return-address-unknown.ll @@ -0,0 +1,16 @@ +; RUN: not llc < %s -asm-verbose=false 2>&1 | FileCheck %s + +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-unknown" + +; This tests the implementation of __builtin_return_address on the unknown OS. +; Since this is not implemented, it should fail. + +; CHECK: Non-Emscripten WebAssembly hasn't implemented __builtin_return_address +define i8* @test_returnaddress() { + %r = call i8* @llvm.returnaddress(i32 0) + ret i8* %r +} + +; LLVM represents __builtin_return_address as call to this function in IR. +declare i8* @llvm.returnaddress(i32 immarg)