Skip to content

Commit cbc2ac5

Browse files
authored
[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 0564d06. Fixes #61930
1 parent a17c598 commit cbc2ac5

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,29 @@ bool WebAssemblyDAGToDAGISel::SelectAddrAddOperands(MVT OffsetType, SDValue N,
343343
if (N.getOpcode() == ISD::ADD && !N.getNode()->getFlags().hasNoUnsignedWrap())
344344
return false;
345345

346-
// Folds constants in an add into the offset.
347346
for (size_t i = 0; i < 2; ++i) {
348347
SDValue Op = N.getOperand(i);
349348
SDValue OtherOp = N.getOperand(i == 0 ? 1 : 0);
350349

350+
// Folds constants in an add into the offset.
351351
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
352352
Offset =
353353
CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(N), OffsetType);
354354
Addr = OtherOp;
355355
return true;
356356
}
357+
358+
// Fold target global addresses into the offset.
359+
if (!TM.isPositionIndependent()) {
360+
if (Op.getOpcode() == WebAssemblyISD::Wrapper)
361+
Op = Op.getOperand(0);
362+
363+
if (Op.getOpcode() == ISD::TargetGlobalAddress) {
364+
Addr = OtherOp;
365+
Offset = Op;
366+
return true;
367+
}
368+
}
357369
}
358370
return false;
359371
}

llvm/test/CodeGen/WebAssembly/eh-lsda.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ try.cont: ; preds = %entry, %catch.start
6666

6767
; CHECK-LABEL: test1:
6868
; In static linking, we load GCC_except_table as a constant directly.
69-
; NOPIC: i[[PTR]].const $push[[CONTEXT:.*]]=, __wasm_lpad_context
69+
; NOPIC: i[[PTR]].const $push[[CONTEXT:.*]]=, {{[48]}}
7070
; NOPIC-NEXT: i[[PTR]].const $push[[EXCEPT_TABLE:.*]]=, GCC_except_table1
71-
; NOPIC-NEXT: i[[PTR]].store {{[48]}}($pop[[CONTEXT]]), $pop[[EXCEPT_TABLE]]
71+
; NOPIC-NEXT: i[[PTR]].store __wasm_lpad_context($pop[[CONTEXT]]), $pop[[EXCEPT_TABLE]]
7272

7373
; In case of PIC, we make GCC_except_table symbols a relative on based on
7474
; __memory_base.

llvm/test/CodeGen/WebAssembly/exception-legacy.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ define void @throw(ptr %p) {
3434
; CHECK: call foo
3535
; CHECK: catch $[[EXN:[0-9]+]]=, __cpp_exception
3636
; CHECK: global.set __stack_pointer
37-
; CHECK: i32.{{store|const}} {{.*}} __wasm_lpad_context
37+
; CHECK: i32.store __wasm_lpad_context
3838
; CHECK: call $drop=, _Unwind_CallPersonality, $[[EXN]]
3939
; CHECK: block
4040
; CHECK: br_if 0

llvm/test/CodeGen/WebAssembly/offset.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ define i32 @load_i32_from_global_address() {
118118
ret i32 %t
119119
}
120120

121+
define i32 @load_i32_global_with_folded_gep_offset_nonconst_nuw(i32 %idx) {
122+
; CHECK-LABEL: load_i32_global_with_folded_gep_offset_nonconst_nuw:
123+
; CHECK: i32.const $push0=, 2
124+
; CHECK: i32.shl $push1=, $0, $pop0
125+
; CHECK: i32.load $push2=, gv($pop1)
126+
%s = getelementptr nuw i32, ptr @gv, i32 %idx
127+
%t = load i32, ptr %s
128+
ret i32 %t
129+
}
130+
121131
;===----------------------------------------------------------------------------
122132
; Loads: 64-bit
123133
;===----------------------------------------------------------------------------

0 commit comments

Comments
 (0)