Skip to content

Commit 3dfe71a

Browse files
badumbatishbadumbatish
authored andcommitted
[WebAssembly] Fold nuw global add to load offset
Fold nuw global add to a load. Fix extra tests that involves loads in exception-legacy, eh-lsda, and address-offsets.
1 parent fe369c1 commit 3dfe71a

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,16 @@ bool WebAssemblyDAGToDAGISel::SelectAddrAddOperands(MVT OffsetType, SDValue N,
354354
Addr = OtherOp;
355355
return true;
356356
}
357+
358+
// Fold Add of Global Address straight into load
359+
if (Op.getOpcode() == WebAssemblyISD::Wrapper)
360+
Op = Op.getOperand(0);
361+
362+
if (Op.getOpcode() == ISD::TargetGlobalAddress) {
363+
Addr = OtherOp;
364+
Offset = Op;
365+
return true;
366+
}
357367
}
358368
return false;
359369
}

llvm/test/CodeGen/WebAssembly/address-offsets.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ define i32 @load_test0() {
1212
; CHECK-LABEL: load_test0:
1313
; CHECK: .functype load_test0 () -> (i32)
1414
; CHECK-NEXT: # %bb.0:
15-
; CHECK-NEXT: global.get $push0=, g@GOT
16-
; CHECK-NEXT: i32.load $push1=, 40($pop0)
15+
; CHECK-NEXT: i32.const $push0=, 40
16+
; CHECK-NEXT: i32.load $push1=, g@GOT($pop0)
1717
; CHECK-NEXT: return $pop1
1818
%t = load i32, ptr getelementptr inbounds ([0 x i32], ptr @g, i32 0, i32 10), align 4
1919
ret i32 %t
@@ -395,8 +395,8 @@ define void @store_test0(i32 %i) {
395395
; CHECK-LABEL: store_test0:
396396
; CHECK: .functype store_test0 (i32) -> ()
397397
; CHECK-NEXT: # %bb.0:
398-
; CHECK-NEXT: global.get $push0=, g@GOT
399-
; CHECK-NEXT: i32.store 40($pop0), $0
398+
; CHECK-NEXT: i32.const $push0=, 40
399+
; CHECK-NEXT: i32.store g@GOT($pop0), $0
400400
; CHECK-NEXT: return
401401
store i32 %i, ptr getelementptr inbounds ([0 x i32], ptr @g, i32 0, i32 10), align 4
402402
ret void

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
; RUN: llc < %s --mtriple=wasm32-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32
2-
; RUN: llc < %s --mtriple=wasm64-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64
3-
; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32
4-
; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64
5-
; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=32
6-
; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=64
1+
; RUN: llc < %s --mtriple=wasm32-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32 -DALIGNMENT=4
2+
; RUN: llc < %s --mtriple=wasm64-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64 -DALIGNMENT=8
3+
; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32 -DALIGNMENT=4
4+
; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64 -DALIGNMENT=8
5+
; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=32 -DALIGNMENT=4
6+
; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=64 -DALIGNMENT=8
77

88
@_ZTIi = external constant ptr
99
@_ZTIf = external constant ptr
@@ -66,18 +66,17 @@ 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:.*]]=, [[ALIGNMENT]]
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.
7575
; PIC: global.get $push[[CONTEXT:.*]]=, __wasm_lpad_context@GOT
76-
; PIC-NEXT: local.tee $push{{.*}}=, $[[CONTEXT_LOCAL:.*]]=, $pop[[CONTEXT]]
7776
; PIC: global.get $push[[MEMORY_BASE:.*]]=, __memory_base
7877
; PIC-NEXT: i[[PTR]].const $push[[EXCEPT_TABLE_REL:.*]]=, GCC_except_table1@MBREL
7978
; PIC-NEXT: i[[PTR]].add $push[[EXCEPT_TABLE:.*]]=, $pop[[MEMORY_BASE]], $pop[[EXCEPT_TABLE_REL]]
80-
; PIC-NEXT: i[[PTR]].store {{[48]}}($[[CONTEXT_LOCAL]]), $pop[[EXCEPT_TABLE]]
79+
; PIC-NEXT: i[[PTR]].store __wasm_lpad_context@GOT(${{.*}}), $pop[[EXCEPT_TABLE]]
8180

8281
; CHECK: .section .rodata.gcc_except_table,"",@
8382
; CHECK-NEXT: .p2align 2

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|const}} {{.*}} 4
3838
; CHECK: call $drop=, _Unwind_CallPersonality, $[[EXN]]
3939
; CHECK: block
4040
; CHECK: br_if 0

llvm/test/CodeGen/WebAssembly/offset.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ define i32 @load_i32_with_folded_gep_offset_nuw(ptr %p) {
6565
define hidden signext i8 @global_load_i32_with_folded_gep_offset_nonconst_nuw(i32 noundef %idx) local_unnamed_addr {
6666
; CHECK-LABEL: global_load_i32_with_folded_gep_offset_nonconst_nuw:
6767
; CHECK: .functype global_load_i32_with_folded_gep_offset_nonconst_nuw (i32) -> (i32)
68-
; CHECK: i32.const $push0=, global_data
69-
; CHECK: i32.add $push1=, $0, $pop0
70-
; CHECK: i32.load8_s $push2=, 0($pop1)
71-
; CHECK: return $pop2
68+
; CHECK: i32.load8_s $push0=, global_data($0)
69+
; CHECK: return $pop0
7270
entry:
7371
%arrayidx = getelementptr inbounds nuw [12 x i8], ptr @global_data, i32 0, i32 %idx
7472
%0 = load i8, ptr %arrayidx, align 1

0 commit comments

Comments
 (0)