Skip to content

Commit b13bca7

Browse files
authored
[WebAssembly] Unstackify registers with no uses in ExplicitLocals (#149626)
There are cases we end up removing some intructions that use stackified registers after RegStackify. For example, ```wasm bb.0: %0 = ... ;; %0 is stackified br_if %bb.1, %0 bb.1: ``` In this code, br_if will be removed in CFGSort, so we should unstackify %0 so that it can be correctly dropped in ExplicitLocals. Rather than handling this in case-by-case basis, this PR just unstackifies all stackifies register with no uses in the beginning of ExplicitLocals, so that they can be correctly dropped. Fixes #149097.
1 parent 5ecb580 commit b13bca7

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyExplicitLocals.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,17 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
256256

257257
// Precompute the set of registers that are unused, so that we can insert
258258
// drops to their defs.
259+
// And unstackify any stackified registers that don't have any uses, so that
260+
// they can be dropped later. This can happen when transformations after
261+
// RegStackify remove instructions using stackified registers.
259262
BitVector UseEmpty(MRI.getNumVirtRegs());
260-
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I)
261-
UseEmpty[I] = MRI.use_empty(Register::index2VirtReg(I));
263+
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
264+
Register Reg = Register::index2VirtReg(I);
265+
if (MRI.use_empty(Reg)) {
266+
UseEmpty[I] = true;
267+
MFI.unstackifyVReg(Reg);
268+
}
269+
}
262270

263271
// Visit each instruction in the function.
264272
for (MachineBasicBlock &MBB : MF) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -O0 -verify-machineinstrs < %s | FileCheck %s
3+
4+
target triple = "wasm32-unknown-unknown"
5+
6+
define void @test(i1 %x) {
7+
; CHECK-LABEL: test:
8+
; CHECK: .functype test (i32) -> ()
9+
; CHECK-NEXT: # %bb.0:
10+
; CHECK-NEXT: local.get 0
11+
; CHECK-NEXT: i32.const -1
12+
; CHECK-NEXT: i32.xor
13+
; CHECK-NEXT: i32.const 1
14+
; CHECK-NEXT: i32.and
15+
; CHECK-NEXT: drop
16+
; CHECK-NEXT: # %bb.1: # %exit
17+
; CHECK-NEXT: return
18+
%y = xor i1 %x, true
19+
; This br_if's operand (%y) is stackified in RegStackify. But this terminator
20+
; will be removed in CFGSort after that. We need to make sure we unstackify %y
21+
; so that it can be dropped in ExplicitLocals.
22+
br i1 %y, label %exit, label %exit
23+
24+
exit:
25+
ret void
26+
}

0 commit comments

Comments
 (0)