Skip to content

Extend getMaxbits to handle Block type #7590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 3, 2025
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
4 changes: 4 additions & 0 deletions src/ir/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ Index getMaxBits(Expression* curr,
if (LoadUtils::isSignRelevant(load) && !load->signed_) {
return 8 * load->bytes;
}
} else if (auto* block = curr->dynCast<Block>()) {
if (!block->name.is() && !block->list.empty() && block->type.isConcrete()) {
return getMaxBits(block->list.back(), localInfoProvider);
}
}
switch (curr->type.getBasic()) {
case Type::i32:
Expand Down
9 changes: 7 additions & 2 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2586,8 +2586,13 @@ struct OptimizeInstructions
}

Index getMaxBitsForLocal(LocalGet* get) {
// check what we know about the local
return localInfo[get->index].maxBits;
// check what we know about the local (we may know nothing, if this local
// was added after the pass scanned for locals; in that case, full
// optimization may require another cycle)
if (get->index < localInfo.size()) {
return localInfo[get->index].maxBits;
}
return getBitsForType(get->type);
}

private:
Expand Down
105 changes: 99 additions & 6 deletions test/lit/passes/optimize-instructions-mvp.wast
Original file line number Diff line number Diff line change
Expand Up @@ -8412,36 +8412,129 @@
(i32.const 0)
)
)
;; CHECK: (func $andZero (param $0 i32) (result i32)
;; CHECK: (func $andZero (param $0 i32) (param $1 i64) (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $0
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $1
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call $andZero
;; CHECK-NEXT: (i32.const 1234)
;; CHECK-NEXT: (local.tee $0
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $1
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i64)
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $andZero (param $0 i32) (result i32)
(func $andZero (param $0 i32) (param $1 i64) (result i32)
(drop
(i32.and
(local.get $0)
(i32.const 0)
)
)
(drop
(i64.and
(local.get $1)
(i64.const 0)
)
)
;; side effects. we must keep the tee, but
;; can drop it.
(drop
(i32.and
(call $andZero (i32.const 1234)) ;; side effects, we must keep this, but
;; can drop it.
(local.tee $0
(i32.const 1)
)
(i32.const 0)
)
)
(drop
(i64.and
(local.tee $1
(i64.const 1)
)
(i64.const 0)
)
)
;; We can optimize out the |and| even if the 0 is at the end of a block.
(drop
(i32.and
(local.tee $0
(i32.const 1)
)
(block (result i32)
(local.set $0
(i32.const 1)
)
(i32.const 0)
)
)
)
(drop
(i64.and
(local.tee $1
(i64.const 1)
)
(block (result i64)
(local.set $1
(i64.const 1)
)
(i64.const 0)
)
)
)
(unreachable)
)
;; CHECK: (func $abstract-additions (param $x32 i32) (param $x64 i64) (param $y32 f32) (param $y64 f64)
Expand Down
Loading