Summary:
In this patch, `ISD::RETURNADDR` is lowered on the emscripten target
to the new Emscripten runtime function `emscripten_return_address`, which
implements the functionality.
Patch by Guanzhong Chen
Reviewers: tlively, aheejin
Reviewed By: tlively
Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62210
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361454
91177308-0d34-0410-b5e6-
96231b3b80d8
// Deoptimization
HANDLE_LIBCALL(DEOPTIMIZE, "__llvm_deoptimize")
+// Return address
+HANDLE_LIBCALL(RETURN_ADDRESS, nullptr)
+
HANDLE_LIBCALL(UNKNOWN_LIBCALL, nullptr)
#undef HANDLE_LIBCALL
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.
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:
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<ConstantSDNode>(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
// 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;
// __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;
// 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;
}
};
--- /dev/null
+; 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)
--- /dev/null
+; 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)