Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2501,11 +2501,33 @@ struct OptimizeInstructions
// Ignore extraneous things and compare them syntactically. We can also
// look at the full fallthrough for both sides now.
left = Properties::getFallthrough(left, passOptions, *getModule());
auto* originalRight = right;
right = Properties::getFallthrough(right, passOptions, *getModule());
if (!ExpressionAnalyzer::equal(left, right)) {
return false;
}

// We must also not have non-fallthrough effects that invalidate us, such as
// this situation:
//
// (local.get $x)
// (block
// (local.set $x ..)
// (local.get $x)
// )
//
// The fallthroughs are identical, but the set may cause us to read a
// different value.
if (originalRight != right) {
// TODO: We could be more precise here and ignore right itself in
// originalRightEffects.
auto originalRightEffects = effects(originalRight);
auto rightEffects = effects(right);
if (originalRightEffects.invalidates(rightEffects)) {
return false;
}
}

// To be equal, they must also be known to return the same result
// deterministically.
return !Properties::isGenerative(left);
Expand Down
13 changes: 10 additions & 3 deletions test/lit/passes/j2cl-inline.wast
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
(module

;; A once function that has become empty
;; CHECK: (type $0 (func))

;; CHECK: (global $$class-initialized@Zoo (mut i32) (i32.const 0))

;; CHECK: (func $clinit-trivial-1_<once>_@Foo (type $0)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $clinit-trivial-1_<once>_@Foo )

;; A once function that just calls another
;; CHECK: (func $clinit-trivial-2_<once>_@Bar (type $0)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $clinit-trivial-2_<once>_@Bar
(call $clinit-trivial-1_<once>_@Foo)
)

;; CHECK: (type $0 (func))

;; CHECK: (global $$class-initialized@Zoo (mut i32) (i32.const 0))
(global $$class-initialized@Zoo (mut i32) (i32.const 0))

;; Not hoisted but trivial.
Expand Down
Loading