[llvm] cbc2ac5 - [WebAssembly] Fold TargetGlobalAddress with added offset (#145829)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 3 03:01:40 PDT 2025
Author: jjasmine
Date: 2025-07-03T11:01:36+01:00
New Revision: cbc2ac5db8ead660f1c264b6eb1e374665e4c152
URL: https://github.com/llvm/llvm-project/commit/cbc2ac5db8ead660f1c264b6eb1e374665e4c152
DIFF: https://github.com/llvm/llvm-project/commit/cbc2ac5db8ead660f1c264b6eb1e374665e4c152.diff
LOG: [WebAssembly] Fold TargetGlobalAddress with added offset (#145829)
Previously we only folded TargetGlobalAddresses into the memarg if they
were on their own, so this patch supports folding TargetGlobalAddresses
that are added to some other offset.
Previously we weren't able to do this because we didn't have nuw on the
add, but we can now that getelementptr has nuw and is plumbed through to
the add in 0564d0665b302d1c7861e03d2995612f46613a0f.
Fixes #61930
Added:
Modified:
llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp
llvm/test/CodeGen/WebAssembly/eh-lsda.ll
llvm/test/CodeGen/WebAssembly/exception-legacy.ll
llvm/test/CodeGen/WebAssembly/offset.ll
Removed:
################################################################################
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<ConstantSDNode>(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
;===----------------------------------------------------------------------------
More information about the llvm-commits
mailing list