diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp index 48c0b7e50f080..ac819cf5c1801 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp @@ -343,17 +343,29 @@ bool WebAssemblyDAGToDAGISel::SelectAddrAddOperands(MVT OffsetType, SDValue N, if (N.getOpcode() == ISD::ADD && !N.getNode()->getFlags().hasNoUnsignedWrap()) return false; - // Folds constants in an add into the offset. for (size_t i = 0; i < 2; ++i) { SDValue Op = N.getOperand(i); SDValue OtherOp = N.getOperand(i == 0 ? 1 : 0); + // Folds constants in an add into the offset. if (ConstantSDNode *CN = dyn_cast(Op)) { Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(N), OffsetType); Addr = OtherOp; return true; } + + // Fold target global addresses into the offset. + if (!TM.isPositionIndependent()) { + if (Op.getOpcode() == WebAssemblyISD::Wrapper) + Op = Op.getOperand(0); + + if (Op.getOpcode() == ISD::TargetGlobalAddress) { + Addr = OtherOp; + Offset = Op; + return true; + } + } } return false; } diff --git a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll index ce55f7620e14a..d5b28b279d419 100644 --- a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll +++ b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll @@ -66,9 +66,9 @@ try.cont: ; preds = %entry, %catch.start ; CHECK-LABEL: test1: ; In static linking, we load GCC_except_table as a constant directly. -; NOPIC: i[[PTR]].const $push[[CONTEXT:.*]]=, __wasm_lpad_context +; NOPIC: i[[PTR]].const $push[[CONTEXT:.*]]=, {{[48]}} ; NOPIC-NEXT: i[[PTR]].const $push[[EXCEPT_TABLE:.*]]=, GCC_except_table1 -; NOPIC-NEXT: i[[PTR]].store {{[48]}}($pop[[CONTEXT]]), $pop[[EXCEPT_TABLE]] +; NOPIC-NEXT: i[[PTR]].store __wasm_lpad_context($pop[[CONTEXT]]), $pop[[EXCEPT_TABLE]] ; In case of PIC, we make GCC_except_table symbols a relative on based on ; __memory_base. diff --git a/llvm/test/CodeGen/WebAssembly/exception-legacy.ll b/llvm/test/CodeGen/WebAssembly/exception-legacy.ll index b4ffd185e3ca8..149e443903a88 100644 --- a/llvm/test/CodeGen/WebAssembly/exception-legacy.ll +++ b/llvm/test/CodeGen/WebAssembly/exception-legacy.ll @@ -34,7 +34,7 @@ define void @throw(ptr %p) { ; CHECK: call foo ; CHECK: catch $[[EXN:[0-9]+]]=, __cpp_exception ; CHECK: global.set __stack_pointer -; CHECK: i32.{{store|const}} {{.*}} __wasm_lpad_context +; CHECK: i32.store __wasm_lpad_context ; CHECK: call $drop=, _Unwind_CallPersonality, $[[EXN]] ; CHECK: block ; CHECK: br_if 0 diff --git a/llvm/test/CodeGen/WebAssembly/offset.ll b/llvm/test/CodeGen/WebAssembly/offset.ll index 130508424f630..109607c914271 100644 --- a/llvm/test/CodeGen/WebAssembly/offset.ll +++ b/llvm/test/CodeGen/WebAssembly/offset.ll @@ -118,6 +118,16 @@ define i32 @load_i32_from_global_address() { ret i32 %t } +define i32 @load_i32_global_with_folded_gep_offset_nonconst_nuw(i32 %idx) { +; CHECK-LABEL: load_i32_global_with_folded_gep_offset_nonconst_nuw: +; CHECK: i32.const $push0=, 2 +; CHECK: i32.shl $push1=, $0, $pop0 +; CHECK: i32.load $push2=, gv($pop1) + %s = getelementptr nuw i32, ptr @gv, i32 %idx + %t = load i32, ptr %s + ret i32 %t +} + ;===---------------------------------------------------------------------------- ; Loads: 64-bit ;===----------------------------------------------------------------------------